add patch improve-code-readability-in-ext4_iget
[ext4-patch-queue.git] / fix-spectre-gadget-in-ext4_mb_regular_allocator
blob08913885b9c2b6fd16b00744e93f9060c7a82502
1 ext4: fix spectre gadget in ext4_mb_regular_allocator()
3 From: Jeremy Cline <jcline@redhat.com>
5 'ac->ac_g_ex.fe_len' is a user-controlled value which is used in the
6 derivation of 'ac->ac_2order'. 'ac->ac_2order', in turn, is used to
7 index arrays which makes it a potential spectre gadget. Fix this by
8 sanitizing the value assigned to 'ac->ac2_order'.  This covers the
9 following accesses found with the help of smatch:
11 * fs/ext4/mballoc.c:1896 ext4_mb_simple_scan_group() warn: potential
12   spectre issue 'grp->bb_counters' [w] (local cap)
14 * fs/ext4/mballoc.c:445 mb_find_buddy() warn: potential spectre issue
15   'EXT4_SB(e4b->bd_sb)->s_mb_offsets' [r] (local cap)
17 * fs/ext4/mballoc.c:446 mb_find_buddy() warn: potential spectre issue
18   'EXT4_SB(e4b->bd_sb)->s_mb_maxs' [r] (local cap)
20 Suggested-by: Josh Poimboeuf <jpoimboe@redhat.com>
21 Signed-off-by: Jeremy Cline <jcline@redhat.com>
22 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
23 Cc: stable@vger.kernel.org
24 ---
26 I broke this out of the "ext4: fix spectre v1 gadgets" patch set since
27 the other patches in that series could, as Josh noted, be replaced with
28 one fix in do_quotactl. I'll send that fix to the disk quota folks
29 separately.
31 Changes from v1:
32   - Sanitize ac_2order on assignment, rather than down the call chain in
33     ext4_mb_simple_scan_group.
35  fs/ext4/mballoc.c | 4 +++-
36  1 file changed, 3 insertions(+), 1 deletion(-)
38 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
39 index f7ab34088162..8b24d3d42cb3 100644
40 --- a/fs/ext4/mballoc.c
41 +++ b/fs/ext4/mballoc.c
42 @@ -14,6 +14,7 @@
43  #include <linux/log2.h>
44  #include <linux/module.h>
45  #include <linux/slab.h>
46 +#include <linux/nospec.h>
47  #include <linux/backing-dev.h>
48  #include <trace/events/ext4.h>
50 @@ -2140,7 +2141,8 @@ ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
51                  * This should tell if fe_len is exactly power of 2
52                  */
53                 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
54 -                       ac->ac_2order = i - 1;
55 +                       ac->ac_2order = array_index_nospec(i - 1,
56 +                                                          sb->s_blocksize_bits + 2);
57         }
59         /* if stream allocation is enabled, use global goal */
60 -- 
61 2.17.1