Skip to contentSkip to conversation

ext4: clear in-inode xattr space before adding the first xattr

PATCH Applied Jiaming Zhang 4 participants 6 messages Oct 21 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ext4: clear in-inode xattr space before adding the first xattr

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
	...
	void *val = s->base + min_offs - new_size;

A crafted image can leave non-zero bytes in that space without the magic where ext4 looks for it, for example by shrinking i_extra_isize. Those bytes are parsed as entries, and an entry that claims an in-inode value at offset 0 sets min_offs to 0. Because free is a size_t, the subtraction then wraps around to a huge value, and the free space check passes although the value does not fit. The value is copied new_size bytes before the start of the space, over the inode's own fields and, for a large value, the memory in front of the inode.

On a kernel without KASAN, the copy corrupts memory: it overwrites the inode's own fields and, for a large value, also the memory in front of the inode. The bytes and their length are decided by the setxattr() caller. Nothing is freed and reused here, so the use-after-free in the report only reflects where the write happened to land.

Triggering this issue requires mounting a crafted image, which needs CAP_SYS_ADMIN or automounting of removable media; after that, any setxattr() on the file reaches this path. The issue was found by fuzzing and has not been seen in production; a reproducer is available in the linked report.

When EXT4_STATE_XATTR is not set, zero the first 32 bits of the space in ext4_xattr_ibody_set() before calling ext4_xattr_set_entry(). These are the bits IS_LAST_ENTRY() reads, so the space is treated as empty and the walk stops before reading any stale entry.

Fixes ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Assisted-by Claude Code:claude-opus-5-5

---
Changes in v2:
- Zero only the first 32 bits space instead of the whole space.
- Add a comment above the check.
- v1: https://lore.kernel.org/lkml/[email protected]/

 fs/ext4/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ext4/xattr.c +8 -0
index 5c310747b965..8e1fd26329d3 100644
@@ -2274,6 +2274,14 @@ int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
 		if (IS_ERR(ea_inode))
 			return PTR_ERR(ea_inode);
 	}
+
+	/*
+	 * Make sure xattr space is properly initialized so that
+	 * IS_LAST_ENTRY() returns true for its first entry
+	 */
+	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
+		*(__u32 *)s->first = 0;
+
 	error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
 				     false /* is_block */);
 	if (error) {
Signature · 1 line
2.43.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
	...
	void *val = s->base + min_offs - new_size;
Quoted 6 lines
[ ... ]
Fixes ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Assisted-by Claude Code:claude-opus-5-5

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Jiaming Zhang
Quoted 41 lines · Jiaming Zhang

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

	free = min_offs - ((void *)last - s->base) - sizeof(__u32);
	...
	void *val = s->base + min_offs - new_size;

A crafted image can leave non-zero bytes in that space without the magic where ext4 looks for it, for example by shrinking i_extra_isize. Those bytes are parsed as entries, and an entry that claims an in-inode value at offset 0 sets min_offs to 0. Because free is a size_t, the subtraction then wraps around to a huge value, and the free space check passes although the value does not fit. The value is copied new_size bytes before the start of the space, over the inode's own fields and, for a large value, the memory in front of the inode.

On a kernel without KASAN, the copy corrupts memory: it overwrites the inode's own fields and, for a large value, also the memory in front of the inode. The bytes and their length are decided by the setxattr() caller. Nothing is freed and reused here, so the use-after-free in the report only reflects where the write happened to land.

Triggering this issue requires mounting a crafted image, which needs CAP_SYS_ADMIN or automounting of removable media; after that, any setxattr() on the file reaches this path. The issue was found by fuzzing and has not been seen in production; a reproducer is available in the linked report.

When EXT4_STATE_XATTR is not set, zero the first 32 bits of the space in ext4_xattr_ibody_set() before calling ext4_xattr_set_entry(). These are the bits IS_LAST_ENTRY() reads, so the space is treated as empty and the walk stops before reading any stale entry.

Quoted 5 lines
Fixes ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Assisted-by Claude Code:claude-opus-5-5

Looks good to me. Feel free to add:

Honza

Quoted 31 lines

---
Changes in v2:
- Zero only the first 32 bits space instead of the whole space.
- Add a comment above the check.
- v1: https://lore.kernel.org/lkml/[email protected]/

 fs/ext4/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ext4/xattr.c +8 -0
index 5c310747b965..8e1fd26329d3 100644
@@ -2274,6 +2274,14 @@ int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
 		if (IS_ERR(ea_inode))
 			return PTR_ERR(ea_inode);
 	}
+
+	/*
+	 * Make sure xattr space is properly initialized so that
+	 * IS_LAST_ENTRY() returns true for its first entry
+	 */
+	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
+		*(__u32 *)s->first = 0;
+
 	error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
 				     false /* is_block */);
 	if (error) {
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
In reply to Jiaming Zhang
Quoted 76 lines · Jiaming Zhang <[email protected]> 于2026年10月21日周三 16:56写道:

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

        free = min_offs - ((void *)last - s->base) - sizeof(__u32);
        ...
        void *val = s->base + min_offs - new_size;

A crafted image can leave non-zero bytes in that space without the magic where ext4 looks for it, for example by shrinking i_extra_isize. Those bytes are parsed as entries, and an entry that claims an in-inode value at offset 0 sets min_offs to 0. Because free is a size_t, the subtraction then wraps around to a huge value, and the free space check passes although the value does not fit. The value is copied new_size bytes before the start of the space, over the inode's own fields and, for a large value, the memory in front of the inode.

On a kernel without KASAN, the copy corrupts memory: it overwrites the inode's own fields and, for a large value, also the memory in front of the inode. The bytes and their length are decided by the setxattr() caller. Nothing is freed and reused here, so the use-after-free in the report only reflects where the write happened to land.

Triggering this issue requires mounting a crafted image, which needs CAP_SYS_ADMIN or automounting of removable media; after that, any setxattr() on the file reaches this path. The issue was found by fuzzing and has not been seen in production; a reproducer is available in the linked report.

When EXT4_STATE_XATTR is not set, zero the first 32 bits of the space in ext4_xattr_ibody_set() before calling ext4_xattr_set_entry(). These are the bits IS_LAST_ENTRY() reads, so the space is treated as empty and the walk stops before reading any stale entry.

Fixes ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Assisted-by Claude Code:claude-opus-5-5

---
Changes in v2:
- Zero only the first 32 bits space instead of the whole space.
- Add a comment above the check.
- v1: https://lore.kernel.org/lkml/[email protected]/

 fs/ext4/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ext4/xattr.c +8 -1
index 5c310747b965..8e1fd26329d3 100644
@@ -2274,6 +2274,14 @@ int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
                if (IS_ERR(ea_inode))
                        return PTR_ERR(ea_inode);
        }
+
+       /*
+        * Make sure xattr space is properly initialized so that
+        * IS_LAST_ENTRY() returns true for its first entry
+        */
+       if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
+               *(__u32 *)s->first = 0;
+
        error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
                                     false /* is_block */);
        if (error) {
--
Quoted 2 lines

2.43.0

Friendly ping, please tell me if any further change is needed.

#4 lore raw
In reply to Jiaming Zhang
Quoted 76 lines · Jiaming Zhang <[email protected]> 于2026年10月21日周三 16:56写道:

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

        free = min_offs - ((void *)last - s->base) - sizeof(__u32);
        ...
        void *val = s->base + min_offs - new_size;

A crafted image can leave non-zero bytes in that space without the magic where ext4 looks for it, for example by shrinking i_extra_isize. Those bytes are parsed as entries, and an entry that claims an in-inode value at offset 0 sets min_offs to 0. Because free is a size_t, the subtraction then wraps around to a huge value, and the free space check passes although the value does not fit. The value is copied new_size bytes before the start of the space, over the inode's own fields and, for a large value, the memory in front of the inode.

On a kernel without KASAN, the copy corrupts memory: it overwrites the inode's own fields and, for a large value, also the memory in front of the inode. The bytes and their length are decided by the setxattr() caller. Nothing is freed and reused here, so the use-after-free in the report only reflects where the write happened to land.

Triggering this issue requires mounting a crafted image, which needs CAP_SYS_ADMIN or automounting of removable media; after that, any setxattr() on the file reaches this path. The issue was found by fuzzing and has not been seen in production; a reproducer is available in the linked report.

When EXT4_STATE_XATTR is not set, zero the first 32 bits of the space in ext4_xattr_ibody_set() before calling ext4_xattr_set_entry(). These are the bits IS_LAST_ENTRY() reads, so the space is treated as empty and the walk stops before reading any stale entry.

Fixes ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Assisted-by Claude Code:claude-opus-5-5

---
Changes in v2:
- Zero only the first 32 bits space instead of the whole space.
- Add a comment above the check.
- v1: https://lore.kernel.org/lkml/[email protected]/

 fs/ext4/xattr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ext4/xattr.c +8 -1
index 5c310747b965..8e1fd26329d3 100644
@@ -2274,6 +2274,14 @@ int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
                if (IS_ERR(ea_inode))
                        return PTR_ERR(ea_inode);
        }
+
+       /*
+        * Make sure xattr space is properly initialized so that
+        * IS_LAST_ENTRY() returns true for its first entry
+        */
+       if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
+               *(__u32 *)s->first = 0;
+
        error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
                                     false /* is_block */);
        if (error) {
--
Quoted 2 lines

2.43.0

Friendly ping, please tell me if any further change is needed.

maintainer Applied
#5 lore raw
In reply to Jiaming Zhang
Quoted 6 lines · Jiaming Zhang

An inode can hold extended attributes in the space that follows its extra fields, whose size is given by i_extra_isize. When ext4 loads an inode, ext4_iget_extra_inode() validates that space only if it starts with the xattr magic, and only then sets EXT4_STATE_XATTR. Without the magic, the space is treated as empty and is not checked.

Quoted 6 lines

ext4_xattr_ibody_set() passes the space to ext4_xattr_set_entry() whether or not EXT4_STATE_XATTR is set. ext4_xattr_set_entry() walks the entries there to find min_offs, the lowest value offset. That offset decides whether the new value fits and where the value is copied:

[...]

Applied, thanks!

[1/1] ext4: clear in-inode xattr space before adding the first xattr

      commit: ab7d0fdc084fa3812613378af181fcc047c81beb

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>