From 9e7549aa73a4240c61dc838b82feba138b5e70c9 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Mon, 15 Aug 2022 13:29:03 +0200 Subject: [PATCH] 4.9-stable patches added patches: ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch ext4-correct-max_inline_xattr_value_size-computing.patch ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch ext4-make-sure-ext4_append-always-allocates-new-block.patch ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch --- ...ode_has_xattr_space-macro-in-xattr.h.patch | 45 +++++++ ...ax_inline_xattr_value_size-computing.patch | 36 +++++ ...ace-in-writeback-error-recovery-path.patch | 52 ++++++++ ...e-after-free-in-ext4_xattr_set_entry.patch | 123 ++++++++++++++++++ ...t4_append-always-allocates-new-block.patch | 58 +++++++++ ...-superblock-during-an-on-line-resize.patch | 45 +++++++ queue-4.9/series | 6 + 7 files changed, 365 insertions(+) create mode 100644 queue-4.9/ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch create mode 100644 queue-4.9/ext4-correct-max_inline_xattr_value_size-computing.patch create mode 100644 queue-4.9/ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch create mode 100644 queue-4.9/ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch create mode 100644 queue-4.9/ext4-make-sure-ext4_append-always-allocates-new-block.patch create mode 100644 queue-4.9/ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch diff --git a/queue-4.9/ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch b/queue-4.9/ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch new file mode 100644 index 00000000000..691712a0345 --- /dev/null +++ b/queue-4.9/ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch @@ -0,0 +1,45 @@ +From 179b14152dcb6a24c3415200603aebca70ff13af Mon Sep 17 00:00:00 2001 +From: Baokun Li +Date: Thu, 16 Jun 2022 10:13:55 +0800 +Subject: ext4: add EXT4_INODE_HAS_XATTR_SPACE macro in xattr.h + +From: Baokun Li + +commit 179b14152dcb6a24c3415200603aebca70ff13af upstream. + +When adding an xattr to an inode, we must ensure that the inode_size is +not less than EXT4_GOOD_OLD_INODE_SIZE + extra_isize + pad. Otherwise, +the end position may be greater than the start position, resulting in UAF. + +Signed-off-by: Baokun Li +Reviewed-by: Jan Kara +Reviewed-by: Ritesh Harjani (IBM) +Link: https://lore.kernel.org/r/20220616021358.2504451-2-libaokun1@huawei.com +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/xattr.h | 13 +++++++++++++ + 1 file changed, 13 insertions(+) + +--- a/fs/ext4/xattr.h ++++ b/fs/ext4/xattr.h +@@ -76,6 +76,19 @@ struct ext4_xattr_entry { + + #define EXT4_ZERO_XATTR_VALUE ((void *)-1) + ++/* ++ * If we want to add an xattr to the inode, we should make sure that ++ * i_extra_isize is not 0 and that the inode size is not less than ++ * EXT4_GOOD_OLD_INODE_SIZE + extra_isize + pad. ++ * EXT4_GOOD_OLD_INODE_SIZE extra_isize header entry pad data ++ * |--------------------------|------------|------|---------|---|-------| ++ */ ++#define EXT4_INODE_HAS_XATTR_SPACE(inode) \ ++ ((EXT4_I(inode)->i_extra_isize != 0) && \ ++ (EXT4_GOOD_OLD_INODE_SIZE + EXT4_I(inode)->i_extra_isize + \ ++ sizeof(struct ext4_xattr_ibody_header) + EXT4_XATTR_PAD <= \ ++ EXT4_INODE_SIZE((inode)->i_sb))) ++ + struct ext4_xattr_info { + int name_index; + const char *name; diff --git a/queue-4.9/ext4-correct-max_inline_xattr_value_size-computing.patch b/queue-4.9/ext4-correct-max_inline_xattr_value_size-computing.patch new file mode 100644 index 00000000000..9ff706eef5e --- /dev/null +++ b/queue-4.9/ext4-correct-max_inline_xattr_value_size-computing.patch @@ -0,0 +1,36 @@ +From c9fd167d57133c5b748d16913c4eabc55e531c73 Mon Sep 17 00:00:00 2001 +From: Baokun Li +Date: Thu, 16 Jun 2022 10:13:57 +0800 +Subject: ext4: correct max_inline_xattr_value_size computing + +From: Baokun Li + +commit c9fd167d57133c5b748d16913c4eabc55e531c73 upstream. + +If the ext4 inode does not have xattr space, 0 is returned in the +get_max_inline_xattr_value_size function. Otherwise, the function returns +a negative value when the inode does not contain EXT4_STATE_XATTR. + +Cc: stable@kernel.org +Signed-off-by: Baokun Li +Reviewed-by: Ritesh Harjani (IBM) +Reviewed-by: Jan Kara +Link: https://lore.kernel.org/r/20220616021358.2504451-4-libaokun1@huawei.com +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/inline.c | 3 +++ + 1 file changed, 3 insertions(+) + +--- a/fs/ext4/inline.c ++++ b/fs/ext4/inline.c +@@ -40,6 +40,9 @@ static int get_max_inline_xattr_value_si + struct ext4_inode *raw_inode; + int free, min_offs; + ++ if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) ++ return 0; ++ + min_offs = EXT4_SB(inode->i_sb)->s_inode_size - + EXT4_GOOD_OLD_INODE_SIZE - + EXT4_I(inode)->i_extra_isize - diff --git a/queue-4.9/ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch b/queue-4.9/ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch new file mode 100644 index 00000000000..47c404d0e6f --- /dev/null +++ b/queue-4.9/ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch @@ -0,0 +1,52 @@ +From 7f0d8e1d607c1a4fa9a27362a108921d82230874 Mon Sep 17 00:00:00 2001 +From: Eric Whitney +Date: Wed, 15 Jun 2022 12:05:30 -0400 +Subject: ext4: fix extent status tree race in writeback error recovery path + +From: Eric Whitney + +commit 7f0d8e1d607c1a4fa9a27362a108921d82230874 upstream. + +A race can occur in the unlikely event ext4 is unable to allocate a +physical cluster for a delayed allocation in a bigalloc file system +during writeback. Failure to allocate a cluster forces error recovery +that includes a call to mpage_release_unused_pages(). That function +removes any corresponding delayed allocated blocks from the extent +status tree. If a new delayed write is in progress on the same cluster +simultaneously, resulting in the addition of an new extent containing +one or more blocks in that cluster to the extent status tree, delayed +block accounting can be thrown off if that delayed write then encounters +a similar cluster allocation failure during future writeback. + +Write lock the i_data_sem in mpage_release_unused_pages() to fix this +problem. Ext4's block/cluster accounting code for bigalloc relies on +i_data_sem for mutual exclusion, as is found in the delayed write path, +and the locking in mpage_release_unused_pages() is missing. + +Cc: stable@kernel.org +Reported-by: Ye Bin +Signed-off-by: Eric Whitney +Link: https://lore.kernel.org/r/20220615160530.1928801-1-enwlinux@gmail.com +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/inode.c | 7 +++++++ + 1 file changed, 7 insertions(+) + +--- a/fs/ext4/inode.c ++++ b/fs/ext4/inode.c +@@ -1659,7 +1659,14 @@ static void mpage_release_unused_pages(s + ext4_lblk_t start, last; + start = index << (PAGE_SHIFT - inode->i_blkbits); + last = end << (PAGE_SHIFT - inode->i_blkbits); ++ ++ /* ++ * avoid racing with extent status tree scans made by ++ * ext4_insert_delayed_block() ++ */ ++ down_write(&EXT4_I(inode)->i_data_sem); + ext4_es_remove_extent(inode, start, last - start + 1); ++ up_write(&EXT4_I(inode)->i_data_sem); + } + + pagevec_init(&pvec, 0); diff --git a/queue-4.9/ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch b/queue-4.9/ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch new file mode 100644 index 00000000000..c79f4d16b8a --- /dev/null +++ b/queue-4.9/ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch @@ -0,0 +1,123 @@ +From 67d7d8ad99beccd9fe92d585b87f1760dc9018e3 Mon Sep 17 00:00:00 2001 +From: Baokun Li +Date: Thu, 16 Jun 2022 10:13:56 +0800 +Subject: ext4: fix use-after-free in ext4_xattr_set_entry + +From: Baokun Li + +commit 67d7d8ad99beccd9fe92d585b87f1760dc9018e3 upstream. + +Hulk Robot reported a issue: +================================================================== +BUG: KASAN: use-after-free in ext4_xattr_set_entry+0x18ab/0x3500 +Write of size 4105 at addr ffff8881675ef5f4 by task syz-executor.0/7092 + +CPU: 1 PID: 7092 Comm: syz-executor.0 Not tainted 4.19.90-dirty #17 +Call Trace: +[...] + memcpy+0x34/0x50 mm/kasan/kasan.c:303 + ext4_xattr_set_entry+0x18ab/0x3500 fs/ext4/xattr.c:1747 + ext4_xattr_ibody_inline_set+0x86/0x2a0 fs/ext4/xattr.c:2205 + ext4_xattr_set_handle+0x940/0x1300 fs/ext4/xattr.c:2386 + ext4_xattr_set+0x1da/0x300 fs/ext4/xattr.c:2498 + __vfs_setxattr+0x112/0x170 fs/xattr.c:149 + __vfs_setxattr_noperm+0x11b/0x2a0 fs/xattr.c:180 + __vfs_setxattr_locked+0x17b/0x250 fs/xattr.c:238 + vfs_setxattr+0xed/0x270 fs/xattr.c:255 + setxattr+0x235/0x330 fs/xattr.c:520 + path_setxattr+0x176/0x190 fs/xattr.c:539 + __do_sys_lsetxattr fs/xattr.c:561 [inline] + __se_sys_lsetxattr fs/xattr.c:557 [inline] + __x64_sys_lsetxattr+0xc2/0x160 fs/xattr.c:557 + do_syscall_64+0xdf/0x530 arch/x86/entry/common.c:298 + entry_SYSCALL_64_after_hwframe+0x44/0xa9 +RIP: 0033:0x459fe9 +RSP: 002b:00007fa5e54b4c08 EFLAGS: 00000246 ORIG_RAX: 00000000000000bd +RAX: ffffffffffffffda RBX: 000000000051bf60 RCX: 0000000000459fe9 +RDX: 00000000200003c0 RSI: 0000000020000180 RDI: 0000000020000140 +RBP: 000000000051bf60 R08: 0000000000000001 R09: 0000000000000000 +R10: 0000000000001009 R11: 0000000000000246 R12: 0000000000000000 +R13: 00007ffc73c93fc0 R14: 000000000051bf60 R15: 00007fa5e54b4d80 +[...] +================================================================== + +Above issue may happen as follows: +------------------------------------- +ext4_xattr_set + ext4_xattr_set_handle + ext4_xattr_ibody_find + >> s->end < s->base + >> no EXT4_STATE_XATTR + >> xattr_check_inode is not executed + ext4_xattr_ibody_set + ext4_xattr_set_entry + >> size_t min_offs = s->end - s->base + >> UAF in memcpy + +we can easily reproduce this problem with the following commands: + mkfs.ext4 -F /dev/sda + mount -o debug_want_extra_isize=128 /dev/sda /mnt + touch /mnt/file + setfattr -n user.cat -v `seq -s z 4096|tr -d '[:digit:]'` /mnt/file + +In ext4_xattr_ibody_find, we have the following assignment logic: + header = IHDR(inode, raw_inode) + = raw_inode + EXT4_GOOD_OLD_INODE_SIZE + i_extra_isize + is->s.base = IFIRST(header) + = header + sizeof(struct ext4_xattr_ibody_header) + is->s.end = raw_inode + s_inode_size + +In ext4_xattr_set_entry + min_offs = s->end - s->base + = s_inode_size - EXT4_GOOD_OLD_INODE_SIZE - i_extra_isize - + sizeof(struct ext4_xattr_ibody_header) + last = s->first + free = min_offs - ((void *)last - s->base) - sizeof(__u32) + = s_inode_size - EXT4_GOOD_OLD_INODE_SIZE - i_extra_isize - + sizeof(struct ext4_xattr_ibody_header) - sizeof(__u32) + +In the calculation formula, all values except s_inode_size and +i_extra_size are fixed values. When i_extra_size is the maximum value +s_inode_size - EXT4_GOOD_OLD_INODE_SIZE, min_offs is -4 and free is -8. +The value overflows. As a result, the preceding issue is triggered when +memcpy is executed. + +Therefore, when finding xattr or setting xattr, check whether +there is space for storing xattr in the inode to resolve this issue. + +Cc: stable@kernel.org +Reported-by: Hulk Robot +Signed-off-by: Baokun Li +Reviewed-by: Ritesh Harjani (IBM) +Reviewed-by: Jan Kara +Link: https://lore.kernel.org/r/20220616021358.2504451-3-libaokun1@huawei.com +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/xattr.c | 6 ++++-- + 1 file changed, 4 insertions(+), 2 deletions(-) + +--- a/fs/ext4/xattr.c ++++ b/fs/ext4/xattr.c +@@ -1053,8 +1053,9 @@ int ext4_xattr_ibody_find(struct inode * + struct ext4_inode *raw_inode; + int error; + +- if (EXT4_I(inode)->i_extra_isize == 0) ++ if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) + return 0; ++ + raw_inode = ext4_raw_inode(&is->iloc); + header = IHDR(inode, raw_inode); + is->s.base = is->s.first = IFIRST(header); +@@ -1107,8 +1108,9 @@ static int ext4_xattr_ibody_set(handle_t + struct ext4_xattr_search *s = &is->s; + int error; + +- if (EXT4_I(inode)->i_extra_isize == 0) ++ if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) + return -ENOSPC; ++ + error = ext4_xattr_set_entry(i, s, inode); + if (error) + return error; diff --git a/queue-4.9/ext4-make-sure-ext4_append-always-allocates-new-block.patch b/queue-4.9/ext4-make-sure-ext4_append-always-allocates-new-block.patch new file mode 100644 index 00000000000..1b67cfa51ff --- /dev/null +++ b/queue-4.9/ext4-make-sure-ext4_append-always-allocates-new-block.patch @@ -0,0 +1,58 @@ +From b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b Mon Sep 17 00:00:00 2001 +From: Lukas Czerner +Date: Mon, 4 Jul 2022 16:27:21 +0200 +Subject: ext4: make sure ext4_append() always allocates new block + +From: Lukas Czerner + +commit b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b upstream. + +ext4_append() must always allocate a new block, otherwise we run the +risk of overwriting existing directory block corrupting the directory +tree in the process resulting in all manner of problems later on. + +Add a sanity check to see if the logical block is already allocated and +error out if it is. + +Cc: stable@kernel.org +Signed-off-by: Lukas Czerner +Reviewed-by: Andreas Dilger +Link: https://lore.kernel.org/r/20220704142721.157985-2-lczerner@redhat.com +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/namei.c | 16 ++++++++++++++++ + 1 file changed, 16 insertions(+) + +--- a/fs/ext4/namei.c ++++ b/fs/ext4/namei.c +@@ -51,6 +51,7 @@ static struct buffer_head *ext4_append(h + struct inode *inode, + ext4_lblk_t *block) + { ++ struct ext4_map_blocks map; + struct buffer_head *bh; + int err; + +@@ -60,6 +61,21 @@ static struct buffer_head *ext4_append(h + return ERR_PTR(-ENOSPC); + + *block = inode->i_size >> inode->i_sb->s_blocksize_bits; ++ map.m_lblk = *block; ++ map.m_len = 1; ++ ++ /* ++ * We're appending new directory block. Make sure the block is not ++ * allocated yet, otherwise we will end up corrupting the ++ * directory. ++ */ ++ err = ext4_map_blocks(NULL, inode, &map, 0); ++ if (err < 0) ++ return ERR_PTR(err); ++ if (err) { ++ EXT4_ERROR_INODE(inode, "Logical block already allocated"); ++ return ERR_PTR(-EFSCORRUPTED); ++ } + + bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE); + if (IS_ERR(bh)) diff --git a/queue-4.9/ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch b/queue-4.9/ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch new file mode 100644 index 00000000000..970cf0bf55d --- /dev/null +++ b/queue-4.9/ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch @@ -0,0 +1,45 @@ +From de394a86658ffe4e89e5328fd4993abfe41b7435 Mon Sep 17 00:00:00 2001 +From: Theodore Ts'o +Date: Wed, 29 Jun 2022 00:00:25 -0400 +Subject: ext4: update s_overhead_clusters in the superblock during an on-line resize + +From: Theodore Ts'o + +commit de394a86658ffe4e89e5328fd4993abfe41b7435 upstream. + +When doing an online resize, the on-disk superblock on-disk wasn't +updated. This means that when the file system is unmounted and +remounted, and the on-disk overhead value is non-zero, this would +result in the results of statfs(2) to be incorrect. + +This was partially fixed by Commits 10b01ee92df5 ("ext4: fix overhead +calculation to account for the reserved gdt blocks"), 85d825dbf489 +("ext4: force overhead calculation if the s_overhead_cluster makes no +sense"), and eb7054212eac ("ext4: update the cached overhead value in +the superblock"). + +However, since it was too expensive to forcibly recalculate the +overhead for bigalloc file systems at every mount, this didn't fix the +problem for bigalloc file systems. This commit should address the +problem when resizing file systems with the bigalloc feature enabled. + +Signed-off-by: Theodore Ts'o +Cc: stable@kernel.org +Reviewed-by: Andreas Dilger +Link: https://lore.kernel.org/r/20220629040026.112371-1-tytso@mit.edu +Signed-off-by: Theodore Ts'o +Signed-off-by: Greg Kroah-Hartman +--- + fs/ext4/resize.c | 1 + + 1 file changed, 1 insertion(+) + +--- a/fs/ext4/resize.c ++++ b/fs/ext4/resize.c +@@ -1446,6 +1446,7 @@ static void ext4_update_super(struct sup + * Update the fs overhead information + */ + ext4_calculate_overhead(sb); ++ es->s_overhead_clusters = cpu_to_le32(sbi->s_overhead); + + if (test_opt(sb, DEBUG)) + printk(KERN_DEBUG "EXT4-fs: added group %u:" diff --git a/queue-4.9/series b/queue-4.9/series index 62a6c4c853c..7eb89ab3bf5 100644 --- a/queue-4.9/series +++ b/queue-4.9/series @@ -48,3 +48,9 @@ netfilter-nf_tables-fix-null-deref-due-to-zeroed-list-head.patch scsi-zfcp-fix-missing-auto-port-scan-and-thus-missing-target-ports.patch x86-olpc-fix-logical-not-is-only-applied-to-the-left-hand-side.patch spmi-trace-fix-stack-out-of-bound-access-in-spmi-tracing-functions.patch +ext4-add-ext4_inode_has_xattr_space-macro-in-xattr.h.patch +ext4-make-sure-ext4_append-always-allocates-new-block.patch +ext4-fix-use-after-free-in-ext4_xattr_set_entry.patch +ext4-update-s_overhead_clusters-in-the-superblock-during-an-on-line-resize.patch +ext4-fix-extent-status-tree-race-in-writeback-error-recovery-path.patch +ext4-correct-max_inline_xattr_value_size-computing.patch -- 2.47.3