fs/ext4/xattr: Fix issues seen while deleting xattr inode(s)
v2 cover fs/ext4/xattr: Fix issues seen while deleting xattr inode(s)
Changes since v1:
----------------
- v1 can be seen here: https://lore.kernel.org/lkml/[email protected]/T/
- As suggested by Cascardo while reviewing v1, there are two
patches in v2:
[PATCH 1/2] Ignores xattr entries past the end entry.
[PATCH 2/2] Hold 'EXT4_I(inode)->xattr_sem' semaphore while deleting the inode.
Bhupesh (2):
fs/ext4/xattr: Ignore xattrs past end
fs/ext4/xattr: Check for 'xattr_sem' inside 'ext4_xattr_delete_inode'
fs/ext4/xattr.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
Signature · 1 line
2.38.1
PATCH v2 1/2 fs/ext4/xattr: Ignore xattrs past end
Once inside 'ext4_xattr_inode_dec_ref_all' we should ignore xattrs entries past the 'end' entry.
This fixes the following KASAN reported issue:
==================================================================
BUG: KASAN: slab-use-after-free in ext4_xattr_inode_dec_ref_all+0xb8c/0xe90 Read of size 4 at addr ffff888012c120c4 by task repro/2065 CPU: 1 UID: 0 PID: 2065 Comm: repro Not tainted 6.13.0-rc2+ #11 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x1fd/0x300 ? tcp_gro_dev_warn+0x260/0x260 ? _printk+0xc0/0x100 ? read_lock_is_recursive+0x10/0x10 ? irq_work_queue+0x72/0xf0 ? __virt_addr_valid+0x17b/0x4b0 print_address_description+0x78/0x390 print_report+0x107/0x1f0 ? __virt_addr_valid+0x17b/0x4b0 ? __virt_addr_valid+0x3ff/0x4b0 ? __phys_addr+0xb5/0x160 ? ext4_xattr_inode_dec_ref_all+0xb8c/0xe90 kasan_report+0xcc/0x100 ? ext4_xattr_inode_dec_ref_all+0xb8c/0xe90 ext4_xattr_inode_dec_ref_all+0xb8c/0xe90 ? ext4_xattr_delete_inode+0xd30/0xd30 ? __ext4_journal_ensure_credits+0x5f0/0x5f0 ? __ext4_journal_ensure_credits+0x2b/0x5f0 ? inode_update_timestamps+0x410/0x410 ext4_xattr_delete_inode+0xb64/0xd30 ? ext4_truncate+0xb70/0xdc0 ? ext4_expand_extra_isize_ea+0x1d20/0x1d20 ? __ext4_mark_inode_dirty+0x670/0x670 ? ext4_journal_check_start+0x16f/0x240 ? ext4_inode_is_fast_symlink+0x2f2/0x3a0 ext4_evict_inode+0xc8c/0xff0 ? ext4_inode_is_fast_symlink+0x3a0/0x3a0 ? do_raw_spin_unlock+0x53/0x8a0 ? ext4_inode_is_fast_symlink+0x3a0/0x3a0 evict+0x4ac/0x950 ? proc_nr_inodes+0x310/0x310 ? trace_ext4_drop_inode+0xa2/0x220 ? _raw_spin_unlock+0x1a/0x30 ? iput+0x4cb/0x7e0 do_unlinkat+0x495/0x7c0 ? try_break_deleg+0x120/0x120 ? 0xffffffff81000000 ? __check_object_size+0x15a/0x210 ? strncpy_from_user+0x13e/0x250 ? getname_flags+0x1dc/0x530 __x64_sys_unlinkat+0xc8/0xf0 do_syscall_64+0x65/0x110 entry_SYSCALL_64_after_hwframe+0x67/0x6f RIP: 0033:0x434ffd Code: 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 8 RSP: 002b:00007ffc50fa7b28 EFLAGS: 00000246 ORIG_RAX: 0000000000000107 RAX: ffffffffffffffda RBX: 00007ffc50fa7e18 RCX: 0000000000434ffd RDX: 0000000000000000 RSI: 0000000020000240 RDI: 0000000000000005 RBP: 00007ffc50fa7be0 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000001 R13: 00007ffc50fa7e08 R14: 00000000004bbf30 R15: 0000000000000001 </TASK> The buggy address belongs to the object at ffff888012c12000 which belongs to the cache filp of size 360
The buggy address is located 196 bytes inside of
freed 360-byte region [ffff888012c12000, ffff888012c12168)
The buggy address belongs to the physical page: page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x12c12 head: order:1 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0 flags: 0x40(head|node=0|zone=0) page_type: f5(slab) raw: 0000000000000040 ffff888000ad7640 ffffea0000497a00 dead000000000004 raw: 0000000000000000 0000000000100010 00000001f5000000 0000000000000000
head: 0000000000000040 ffff888000ad7640 ffffea0000497a00 dead000000000004
head: 0000000000000000 0000000000100010 00000001f5000000 0000000000000000
head: 0000000000000001 ffffea00004b0481 ffffffffffffffff 0000000000000000
head: 0000000000000002 0000000000000000 00000000ffffffff 0000000000000000
page dumped because: kasan: bad access detected Memory state around the buggy address: ffff888012c11f80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ffff888012c12000: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
Quoted 1 line
ffff888012c12080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
^
ffff888012c12100: fb fb fb fb fb fb fb fb fb fb fb fb fb fc fc fc
ffff888012c12180: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
==================================================================
--- fs/ext4/xattr.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
fs/ext4/xattr.c +10 -1
@@ -1176,15 +1176,24 @@ ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent, {struct inode *ea_inode;struct ext4_xattr_entry *entry; +struct ext4_iloc iloc;bool dirty = false;unsigned int ea_ino;int err;int credits; +void *end; + +if (block_csum) + end = (void *)bh->b_data + bh->b_size; +else { +ext4_get_inode_loc (parent, &iloc); + end = (void *)ext4_raw_inode (&iloc) +EXT4_SB (parent->i_sb)->s_inode_size; + }/* One credit for dec ref on ea_inode, one for orphan list addition, */ credits =2 + extra_credits; -for (entry = first; !IS_LAST_ENTRY (entry); +for (entry = first; (void *)entry < end && !IS_LAST_ENTRY (entry); entry =EXT4_XATTR_NEXT (entry)) {if (!entry->e_value_inum)continue ;
Signature · 1 line
2.38.1
PATCH v2 2/2 fs/ext4/xattr: Check for 'xattr_sem' inside 'ext4_xattr_delete_inode'
In reply to BhupeshOnce we are inside the 'ext4_xattr_delete_inode' function and trying to delete the inode, the 'xattr_sem' should be unlocked.
We need trylock here to avoid false-positive warning from lockdep about reclaim circular dependency.
This makes the 'ext4_xattr_delete_inode' implementation mimic the existing 'ext2_xattr_delete_inode' implementation and thus avoid similar lockdep issues while deleting inodes.
--- fs/ext4/xattr.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
fs/ext4/xattr.c +15 -1
@@ -2935,7 +2935,20 @@ int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,struct ext4_iloc iloc = { .bh =NULL };struct ext4_xattr_entry *entry;struct inode *ea_inode; -int error; +int error =0 ; + +/* +* We are the only ones holding inode reference. The xattr_sem should +* better be unlocked! We could as well just not acquire xattr_sem at +* all but this makes the code more futureproof. OTOH we need trylock +* here to avoid false-positive warning from lockdep about reclaim +* circular dependency. +*/ +if (WARN_ON_ONCE (!down_write_trylock (&EXT4_I (inode)->xattr_sem))) +return error; + +if (!EXT4_I (inode)->i_file_acl) + goto cleanup; error =ext4_journal_ensure_credits (handle, extra_credits,ext4_free_metadata_revoke_credits (inode->i_sb,1 ));
@@ -3024,6 +3037,7 @@ int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, cleanup:brelse (iloc.bh);brelse (bh); +up_write (&EXT4_I (inode)->xattr_sem);return error; }
Signature · 1 line
2.38.1
Theodore Ts'o
maintainerRe: [PATCH v2 2/2] fs/ext4/xattr: Check for 'xattr_sem' inside 'ext4_xattr_delete_inode'
Quoted 11 lines · Bhupesh
Once we are inside the '
ext4_xattr_delete_inode' function and trying to delete the inode, the 'xattr_sem' should be unlocked.We need trylock here to avoid false-positive warning from lockdep about reclaim circular dependency.
This makes the '
ext4_xattr_delete_inode' implementation mimic the existing 'ext2_xattr_delete_inode' implementation and thus avoid similar lockdep issues while deleting inodes.Signed-off-by Bhupesh <[email protected]>
This patch is causing a failure of test ext4/026, and also exposed a bug in e2fsprogs[1]. With the e2fsprogs bug fixed, the file system corruption which is induced by ext4/026 is (when running e2fsck -fn on SCRATCH_DEV):
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Regular filesystem inode 14 has EA_INODE flag set. Clear? no
Unattached inode 14
Connect to /lost+found? no
Pass 5: Checking group summary information
/tmp/kvm-xfstests-tytso/vdc.img: ********** WARNING: Filesystem still has errors **********
[1] https://lore.kernel.org/[email protected]
So what appears to be happening is this patch is resulting in ext4/026 failing to clean up a no-longer-used EA inode, which is unfortunate.
Without the e2fsorigs bug fix, ext4/026 will fail but the error message will be much less edifying:
e2fsck 1.47.2 (1-Jan-2025)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference countsext2fs_write_inode: Attempt to write to filesystem opened read-only while writing inode 14 in pass4
e2fsck: aborted
So I'm going to drop this patch (2/2) from the ext4 tree, but I'm going to keep patch 1/2 from this series, since it is fixing a real bug. I presume that without this patch, the syzbot reproducer will trigger a false lockdep warning, but we can fix that later.
Thanks,
- Ted
Re: [PATCH v2 2/2] fs/ext4/xattr: Check for 'xattr_sem' inside 'ext4_xattr_delete_inode'
Hi Ted,
Quoted 46 lines · Theodore Ts'o
Quoted 11 lines · Bhupesh
Once we are inside the '
ext4_xattr_delete_inode' function and trying to delete the inode, the 'xattr_sem' should be unlocked.We need trylock here to avoid false-positive warning from lockdep about reclaim circular dependency.
This makes the '
ext4_xattr_delete_inode' implementation mimic the existing 'ext2_xattr_delete_inode' implementation and thus avoid similar lockdep issues while deleting inodes.Signed-off-by Bhupesh <[email protected]>This patch is causing a failure of test ext4/026, and also exposed a bug in e2fsprogs[1]. With the e2fsprogs bug fixed, the file system corruption which is induced by ext4/026 is (when running e2fsck -fn on
SCRATCH_DEV):Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Regular filesystem inode 14 hasEA_INODEflag set. Clear? noUnattached inode 14
Connect to /lost+found? noPass 5: Checking group summary information
/tmp/kvm-xfstests-tytso/vdc.img: ********** WARNING: Filesystem still has errors **********[1] https://lore.kernel.org/[email protected]
So what appears to be happening is this patch is resulting in ext4/026 failing to clean up a no-longer-used EA inode, which is unfortunate.
Without the e2fsorigs bug fix, ext4/026 will fail but the error message will be much less edifying:
e2fsck 1.47.2 (1-Jan-2025)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference countsext2fs_write_inode: Attempt to write to filesystem opened read-only while writing inode 14 in pass4
e2fsck: aborted
Quoted 4 lines
So I'm going to drop this patch (2/2) from the ext4 tree, but I'm going to keep patch 1/2 from this series, since it is fixing a real bug. I presume that without this patch, the syzbot reproducer will trigger a false lockdep warning, but we can fix that later.
Sure, many thanks for your help.
Regards,
Bhupesh
Quoted 9 lines · Bhupesh
Changes since v1:
----------------
- v1 can be seen here: https://lore.kernel.org/lkml/[email protected]/T/
- As suggested by Cascardo while reviewing v1, there are two
patches in v2:
[PATCH 1/2] Ignores xattr entries past the end entry.
[PATCH 2/2] Hold 'EXT4_I(inode)->xattr_sem' semaphore while deleting the inode.[...]
Applied, thanks!
[1/2] fs/ext4/xattr: Ignore xattrs past end
commit: c8e008b60492cf6fd31ef127aea6d02fd3d314cd
[2/2] fs/ext4/xattr: Check for 'xattr_sem' inside 'ext4_xattr_delete_inode'
(dropped because it breaks ext4/026)
Best regards,
Signature · 1 line
Theodore Ts'o <[email protected]>