Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ext4: fix invalid free tracking in ext4_xattr_move_to_block()

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@kernel.org
Link: https://lore.kernel.org/r/20230430160426.581366-1-tytso@mit.edu
Link: https://syzkaller.appspot.com/bug?id=5c2aee8256e30b55ccf57312c16d88417adbd5e1
Link: https://syzkaller.appspot.com/bug?id=41a6b5d4917c0412eb3b3c3c604965bed7d7420b
Reported-by: syzbot+64b645917ce07d89bde5@syzkaller.appspotmail.com
Reported-by: syzbot+0d042627c4f2ad332195@syzkaller.appspotmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>

+3 -2
+3 -2
fs/ext4/xattr.c
··· 2614 2614 .in_inode = !!entry->e_value_inum, 2615 2615 }; 2616 2616 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); 2617 + int needs_kvfree = 0; 2617 2618 int error; 2618 2619 2619 2620 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); ··· 2637 2636 error = -ENOMEM; 2638 2637 goto out; 2639 2638 } 2640 - 2639 + needs_kvfree = 1; 2641 2640 error = ext4_xattr_inode_get(inode, entry, buffer, value_size); 2642 2641 if (error) 2643 2642 goto out; ··· 2676 2675 2677 2676 out: 2678 2677 kfree(b_entry_name); 2679 - if (entry->e_value_inum && buffer) 2678 + if (needs_kvfree && buffer) 2680 2679 kvfree(buffer); 2681 2680 if (is) 2682 2681 brelse(is->iloc.bh);