From nobody Thu Dec 18 05:17:31 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C8B617C8B; Thu, 22 Aug 2024 02:40:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724294439; cv=none; b=d0ett2KftczyZ1i4zOlKRwp+i5NGfMsppildnYA+megt2icN8cN6mqAism1XxRGW2W8T+aBQtLTqzjHcnw087of6FfC5o7JO3ECh++okOW26DyL20rwV41X9YjpkaqpbVJqkcT7RxaB7+jouCf48hOJcL2R/GxktSkbsQLYjpdM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724294439; c=relaxed/simple; bh=QbOoxjd5GPwq8IPtveAxaZiHJCegMzmesE7WzUw8150=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U7X6SGDfJYLz+OqtGGo9A+JhrDFOY1eDhegO5Fc5LpLkhIFAlK66qZMHWSWLE5IIrBh3TTPH46EA9VjswHIgVu6oIJvDwGDa7ldUr703q2jZGWFn66rezN/hUz7X4qVXhCy/4Z7A3XDmtzlIzd5ye4nUwxd7MhukWUnxOYj61U4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Wq6r86CP1z4f3jtR; Thu, 22 Aug 2024 10:40:24 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 895151A06DA; Thu, 22 Aug 2024 10:40:34 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgCHr4UapcZmqbd1CQ--.38129S11; Thu, 22 Aug 2024 10:40:34 +0800 (CST) From: libaokun@huaweicloud.com To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, ojaswin@linux.ibm.com, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, libaokun@huaweicloud.com, Baokun Li , stable@kernel.org Subject: [PATCH v2 07/25] ext4: drop ppath from ext4_ext_replay_update_ex() to avoid double-free Date: Thu, 22 Aug 2024 10:35:27 +0800 Message-Id: <20240822023545.1994557-8-libaokun@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240822023545.1994557-1-libaokun@huaweicloud.com> References: <20240822023545.1994557-1-libaokun@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgCHr4UapcZmqbd1CQ--.38129S11 X-Coremail-Antispam: 1UD129KBjvJXoWxGry3Jr1UuFW7ur48GF45ZFb_yoW5CF1xpF 9rCF10gr1rXw1kWrZ7Xa17ZF1Ska18Gw4UGr97Ca9FkFyUXw1F9FyxtF4rtFy5XrW8GFWa vrW0yr15W3WUK37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQY14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwAKzVCY07xG64k0F24lc7CjxVAaw2AFwI0_Jw0_GFyl42xK82IYc2Ij64vIr41l 4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67 AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8I cVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AK xVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUbT7KDUUUUU== X-CM-SenderInfo: 5olet0hnxqqx5xdzvxpfor3voofrz/1tbiAQADBWbFpP9DdAAAsI Content-Type: text/plain; charset="utf-8" From: Baokun Li When calling ext4_force_split_extent_at() in ext4_ext_replay_update_ex(), the 'ppath' is updated but it is the 'path' that is freed, thus potentially triggering a double-free in the following process: ext4_ext_replay_update_ex ppath =3D path ext4_force_split_extent_at(&ppath) ext4_split_extent_at ext4_ext_insert_extent ext4_ext_create_new_leaf ext4_ext_grow_indepth ext4_find_extent if (depth > path[0].p_maxdepth) kfree(path) ---> path First freed *orig_path =3D path =3D NULL ---> null ppath kfree(path) ---> path double-free !!! So drop the unnecessary ppath and use path directly to avoid this problem. And use ext4_find_extent() directly to update path, avoiding unnecessary memory allocation and freeing. Also, propagate the error returned by ext4_find_extent() instead of using strange error codes. Fixes: 8016e29f4362 ("ext4: fast commit recovery path") Cc: stable@kernel.org Signed-off-by: Baokun Li Reviewed-by: Jan Kara Reviewed-by: Ojaswin Mujoo Tested-by: Ojaswin Mujoo --- fs/ext4/extents.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index 91c6586afcca..cd2e71123a25 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c @@ -5921,7 +5921,7 @@ int ext4_clu_mapped(struct inode *inode, ext4_lblk_t = lclu) int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start, int len, int unwritten, ext4_fsblk_t pblk) { - struct ext4_ext_path *path =3D NULL, *ppath; + struct ext4_ext_path *path; struct ext4_extent *ex; int ret; =20 @@ -5937,30 +5937,29 @@ int ext4_ext_replay_update_ex(struct inode *inode, = ext4_lblk_t start, if (le32_to_cpu(ex->ee_block) !=3D start || ext4_ext_get_actual_len(ex) !=3D len) { /* We need to split this extent to match our extent first */ - ppath =3D path; down_write(&EXT4_I(inode)->i_data_sem); - ret =3D ext4_force_split_extent_at(NULL, inode, &ppath, start, 1); + ret =3D ext4_force_split_extent_at(NULL, inode, &path, start, 1); up_write(&EXT4_I(inode)->i_data_sem); if (ret) goto out; - kfree(path); - path =3D ext4_find_extent(inode, start, NULL, 0); + + path =3D ext4_find_extent(inode, start, &path, 0); if (IS_ERR(path)) - return -1; - ppath =3D path; + return PTR_ERR(path); ex =3D path[path->p_depth].p_ext; WARN_ON(le32_to_cpu(ex->ee_block) !=3D start); + if (ext4_ext_get_actual_len(ex) !=3D len) { down_write(&EXT4_I(inode)->i_data_sem); - ret =3D ext4_force_split_extent_at(NULL, inode, &ppath, + ret =3D ext4_force_split_extent_at(NULL, inode, &path, start + len, 1); up_write(&EXT4_I(inode)->i_data_sem); if (ret) goto out; - kfree(path); - path =3D ext4_find_extent(inode, start, NULL, 0); + + path =3D ext4_find_extent(inode, start, &path, 0); if (IS_ERR(path)) - return -EINVAL; + return PTR_ERR(path); ex =3D path[path->p_depth].p_ext; } } --=20 2.39.2