This is a note to let you know that I've just added the patch titled ext4: fix invalid free tracking in ext4_xattr_move_to_block() to the 6.2-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: ext4-fix-invalid-free-tracking-in-ext4_xattr_move_to_block.patch and it can be found in the queue-6.2 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From b87c7cdf2bed4928b899e1ce91ef0d147017ba45 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o <tytso@xxxxxxx> Date: Sun, 30 Apr 2023 03:04:13 -0400 Subject: ext4: fix invalid free tracking in ext4_xattr_move_to_block() From: Theodore Ts'o <tytso@xxxxxxx> commit b87c7cdf2bed4928b899e1ce91ef0d147017ba45 upstream. In ext4_xattr_move_to_block(), the value of the extended attribute which we need to move to an external block may be allocated by kvmalloc() if the value is stored in an external inode. So at the end of the function the code tried to check if this was the case by testing entry->e_value_inum. However, at this point, the pointer to the xattr entry is no longer valid, because it was removed from the original location where it had been stored. So we could end up calling kvfree() on a pointer which was not allocated by kvmalloc(); or we could also potentially leak memory by not freeing the buffer when it should be freed. Fix this by storing whether it should be freed in a separate variable. Cc: stable@xxxxxxxxxx Link: https://lore.kernel.org/r/20230430160426.581366-1-tytso@xxxxxxx Link: https://syzkaller.appspot.com/bug?id=5c2aee8256e30b55ccf57312c16d88417adbd5e1 Link: https://syzkaller.appspot.com/bug?id=41a6b5d4917c0412eb3b3c3c604965bed7d7420b Reported-by: syzbot+64b645917ce07d89bde5@xxxxxxxxxxxxxxxxxxxxxxxxx Reported-by: syzbot+0d042627c4f2ad332195@xxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Theodore Ts'o <tytso@xxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- fs/ext4/xattr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/fs/ext4/xattr.c +++ b/fs/ext4/xattr.c @@ -2581,6 +2581,7 @@ static int ext4_xattr_move_to_block(hand .in_inode = !!entry->e_value_inum, }; struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); + int needs_kvfree = 0; int error; is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); @@ -2603,7 +2604,7 @@ static int ext4_xattr_move_to_block(hand error = -ENOMEM; goto out; } - + needs_kvfree = 1; error = ext4_xattr_inode_get(inode, entry, buffer, value_size); if (error) goto out; @@ -2642,7 +2643,7 @@ static int ext4_xattr_move_to_block(hand out: kfree(b_entry_name); - if (entry->e_value_inum && buffer) + if (needs_kvfree && buffer) kvfree(buffer); if (is) brelse(is->iloc.bh); Patches currently in stable-queue which might be from tytso@xxxxxxx are queue-6.2/ext4-improve-error-recovery-code-paths-in-__ext4_remount.patch queue-6.2/ext4-fix-warning-in-mb_find_extent.patch queue-6.2/ext4-avoid-deadlock-in-fs-reclaim-with-page-writeback.patch queue-6.2/ext4-fix-deadlock-when-converting-an-inline-directory-in-nojournal-mode.patch queue-6.2/ext4-bail-out-of-ext4_xattr_ibody_get-fails-for-any-reason.patch queue-6.2/ext4-improve-error-handling-from-ext4_dirhash.patch queue-6.2/ext4-add-bounds-checking-in-get_max_inline_xattr_value_size.patch queue-6.2/ext4-avoid-a-potential-slab-out-of-bounds-in-ext4_group_desc_csum.patch queue-6.2/ext4-check-iomap-type-only-if-ext4_iomap_begin-does-not-fail.patch queue-6.2/ext4-fix-lockdep-warning-when-enabling-mmp.patch queue-6.2/ext4-fix-invalid-free-tracking-in-ext4_xattr_move_to_block.patch queue-6.2/ext4-remove-a-bug_on-in-ext4_mb_release_group_pa.patch queue-6.2/ext4-fix-data-races-when-using-cached-status-extents.patch