Skip to contentSkip to conversation

CVE-2024-49884

PATCH In review Shaoying Xu 2 participants 5 messages Jan 25, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

cover CVE-2024-49884

Backport CVE-2024-49884 fixes to stable 5.4.

Baokun Li (1):
ext4: fix slab-use-after-free in ext4_split_extent_at()

Theodore Ts'o (1):
ext4: avoid ext4_error()'s caused by ENOMEM in the truncate path

 fs/ext4/ext4.h    |  1 +
 fs/ext4/extents.c | 64 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 11 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/2 ext4: avoid ext4_error()'s caused by ENOMEM in the truncate path

From: Theodore Ts'o <[email protected]>

[ Upstream commit 73c384c0cdaa8ea9ca9ef2d0cff6a25930f1648e ]

We can't fail in the truncate path without requiring an fsck. Add work around for this by using a combination of retry loops and the __GFP_NOFAIL flag.

From: Theodore Ts'o <[email protected]>

Stable-dep-of: c26ab35702f8 ("ext4: fix slab-use-after-free in ext4_split_extent_at()")

[v5.4: resolved contextual conflict in __read_extent_tree_block]
---
 fs/ext4/ext4.h    |  1 +
 fs/ext4/extents.c | 43 +++++++++++++++++++++++++++++++++----------
 2 files changed, 34 insertions(+), 10 deletions(-)
fs/ext4/ext4.h +1 -0
index 4d02116193de..44bfa589ed36 100644
@@ -628,6 +628,7 @@ enum {
  */
 #define EXT4_EX_NOCACHE				0x40000000
 #define EXT4_EX_FORCE_CACHE			0x20000000
+#define EXT4_EX_NOFAIL				0x10000000
 
 /*
  * Flags used by ext4_free_blocks
fs/ext4/extents.c +33 -10

117 lines not on this page · raw message

Signature · 1 line
2.40.1
maintainer Reviewed-by Harshad Shirwadkar on v1 1/2
#2 lore raw

Re: [PATCH 5.4 1/2] ext4: avoid ext4_error()'s caused by ENOMEM in the truncate path

[ Sasha's backport helper bot ]

Hi,

The upstream commit SHA1 provided is correct: 73c384c0cdaa8ea9ca9ef2d0cff6a25930f1648e

WARNING: Author mismatch between patch and upstream commit:
Backport author: Shaoying Xu<[email protected]> Commit author: Theodore Ts'o<[email protected]>

Status in newer kernel trees:
6.12.y | Present (exact SHA1)
6.6.y | Present (exact SHA1)
6.1.y | Present (exact SHA1)
5.15.y | Present (exact SHA1)
5.10.y | Present (exact SHA1)
5.4.y | Not found

Note: The patch differs from the upstream commit:
---
1: 73c384c0cdaa8 ! 1: ad990516172ca ext4: avoid ext4_error()'s caused by ENOMEM in the truncate path
@@ Metadata
## Commit message ##
ext4: avoid ext4_error()'s caused by ENOMEM in the truncate path

+ [ Upstream commit 73c384c0cdaa8ea9ca9ef2d0cff6a25930f1648e ]
+
We can't fail in the truncate path without requiring an fsck. Add work around for this by using a combination of retry loops and the __GFP_NOFAIL flag.
@@ Commit message

+ Stable-dep-of: c26ab35702f8 ("ext4: fix slab-use-after-free in ext4_split_extent_at()")
+ [v5.4: resolved contextual conflict in __read_extent_tree_block]
+ Signed-off-by: Shaoying Xu <[email protected]>

## fs/ext4/ext4.h ##
@@ fs/ext4/ext4.h: enum {
@@ fs/ext4/extents.c: ext4_force_split_extent_at(handle_t *handle, struct inode *in
+ flags);
}

- static int
+ /*
@@ fs/ext4/extents.c: __read_extent_tree_block(const char *function, unsigned int line,
- {
struct buffer_head *bh;
int err;
-+ gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
+ ext4_fsblk_t pblk;
++ gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS;
+
+ if (flags & EXT4_EX_NOFAIL)
-+ gfp_flags |= __GFP_NOFAIL;
++ gfp_flags |= __GFP_NOFAIL;

+ pblk = ext4_idx_pblock(idx);
- bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
+ bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags);
if (unlikely(!bh))
---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-5.4.y        |  Success    |  Success   |
#3 lore raw

PATCH 2/2 ext4: fix slab-use-after-free in ext4_split_extent_at()

In reply to Shaoying Xu

From: Baokun Li <[email protected]>

[ Upstream commit c26ab35702f8cd0cdc78f96aa5856bfb77be798f ]

We hit the following use-after-free:

==================================================================

BUG: KASAN: slab-use-after-free in ext4_split_extent_at+0xba8/0xcc0
Read of size 2 at addr ffff88810548ed08 by task kworker/u20:0/40
CPU: 0 PID: 40 Comm: kworker/u20:0 Not tainted 6.9.0-dirty #724
Call Trace:
 <TASK>
 kasan_report+0x93/0xc0
 ext4_split_extent_at+0xba8/0xcc0
 ext4_split_extent.isra.0+0x18f/0x500
 ext4_split_convert_extents+0x275/0x750
 ext4_ext_handle_unwritten_extents+0x73e/0x1580
 ext4_ext_map_blocks+0xe20/0x2dc0
 ext4_map_blocks+0x724/0x1700
 ext4_do_writepages+0x12d6/0x2a70

[...]

Allocated by task 40:
 __kmalloc_noprof+0x1ac/0x480
 ext4_find_extent+0xf3b/0x1e70
 ext4_ext_map_blocks+0x188/0x2dc0
 ext4_map_blocks+0x724/0x1700
 ext4_do_writepages+0x12d6/0x2a70

[...]

Freed by task 40:
 kfree+0xf1/0x2b0
 ext4_find_extent+0xa71/0x1e70
 ext4_ext_insert_extent+0xa22/0x3260
 ext4_split_extent_at+0x3ef/0xcc0
 ext4_split_extent.isra.0+0x18f/0x500
 ext4_split_convert_extents+0x275/0x750
 ext4_ext_handle_unwritten_extents+0x73e/0x1580
 ext4_ext_map_blocks+0xe20/0x2dc0
 ext4_map_blocks+0x724/0x1700
 ext4_do_writepages+0x12d6/0x2a70

[...]
==================================================================

The flow of issue triggering is as follows:

ext4_split_extent_at
path = *ppath ext4_ext_insert_extent(ppath)
ext4_ext_create_new_leaf(ppath)
ext4_find_extent(orig_path)
path = *orig_path read_extent_tree_block
// return -ENOMEM or -EIO
ext4_free_ext_path(path)
kfree(path)
*orig_path = NULL
a. If err is -ENOMEM:
ext4_ext_dirty(path + path->p_depth)
// path use-after-free !!!
b. If err is -EIO and we have EXT_DEBUG defined:
ext4_ext_show_leaf(path)
eh = path[depth].p_hdr
// path also use-after-free !!!

So when trying to zeroout or fix the extent length, call ext4_find_extent() to update the path.

In addition we use *ppath directly as an ext4_ext_show_leaf() input to avoid possible use-after-free when EXT_DEBUG is defined, and to avoid unnecessary path updates.

---
 fs/ext4/extents.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
fs/ext4/extents.c +20 -1
index 0e16e7c08a42..5c0ef7a04169 100644
@@ -3440,6 +3440,25 @@ static int ext4_split_extent_at(handle_t *handle,
 	if (err != -ENOSPC && err != -EDQUOT && err != -ENOMEM)
 		goto out;
 
+	/*
+	 * Update path is required because previous ext4_ext_insert_extent()
+	 * may have freed or reallocated the path. Using EXT4_EX_NOFAIL
+	 * guarantees that ext4_find_extent() will not return -ENOMEM,
+	 * otherwise -ENOMEM will cause a retry in do_writepages(), and a
+	 * WARN_ON may be triggered in ext4_da_update_reserve_space() due to
+	 * an incorrect ee_len causing the i_reserved_data_blocks exception.
+	 */
+	path = ext4_find_extent(inode, ee_block, ppath,
+				flags | EXT4_EX_NOFAIL);
+	if (IS_ERR(path)) {
+		EXT4_ERROR_INODE(inode, "Failed split extent on %u, err %ld",
+				 split, PTR_ERR(path));
+		return PTR_ERR(path);
+	}
+	depth = ext_depth(inode);
+	ex = path[depth].p_ext;
+	*ppath = path;
+
 	if (EXT4_EXT_MAY_ZEROOUT & split_flag) {
 		if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
 			if (split_flag & EXT4_EXT_DATA_VALID1) {
@@ -3488,7 +3507,7 @@ static int ext4_split_extent_at(handle_t *handle,
 	ext4_ext_dirty(handle, inode, path + path->p_depth);
 	return err;
 out:
-	ext4_ext_show_leaf(inode, path);
+	ext4_ext_show_leaf(inode, *ppath);
 	return err;
 }
 
Signature · 1 line
2.40.1
maintainer Tested-by Ojaswin Mujoo on v1 2/2
#4 lore raw

Re: [PATCH 5.4 2/2] ext4: fix slab-use-after-free in ext4_split_extent_at()

[ Sasha's backport helper bot ]

Hi,

The upstream commit SHA1 provided is correct: c26ab35702f8cd0cdc78f96aa5856bfb77be798f

WARNING: Author mismatch between patch and upstream commit:
Backport author: Shaoying Xu<[email protected]> Commit author: Baokun Li<[email protected]>

Status in newer kernel trees:
6.12.y | Present (exact SHA1)
6.6.y | Present (different SHA1: 8fe117790b37) 6.1.y | Present (different SHA1: a5401d4c3e2a) 5.15.y | Present (different SHA1: cafcc1bd6293) 5.10.y | Present (different SHA1: e52f933598b7) 5.4.y | Not found

Note: The patch differs from the upstream commit:
---
1: c26ab35702f8c ! 1: 2df1566f67dbb ext4: fix slab-use-after-free in ext4_split_extent_at()
@@ Metadata
## Commit message ##
ext4: fix slab-use-after-free in ext4_split_extent_at()

+ [ Upstream commit c26ab35702f8cd0cdc78f96aa5856bfb77be798f ]
+
We hit the following use-after-free:

==================================================================
@@ Commit message

+ Signed-off-by: Shaoying Xu <[email protected]>

## fs/ext4/extents.c ##
@@ fs/ext4/extents.c: static int ext4_split_extent_at(handle_t *handle,
---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-5.4.y        |  Success    |  Success   |