reenable fix-races-between-changing-journal-mode-and-ext4_writepages
[ext4-patch-queue.git] / silence-ubsan-in-mb_find_order_for_block
blob314415246a5002272ad3856b5a775d860efbd6ca
1 ext4: address UBSAN warning in mb_find_order_for_block()
3 From: Nicolai Stange <nicstange@gmail.com>
5 Currently, in mb_find_order_for_block(), there's a loop like the following:
7   while (order <= e4b->bd_blkbits + 1) {
8     ...
9     bb += 1 << (e4b->bd_blkbits - order);
10   }
12 Note that the updated bb is used in the loop's next iteration only.
14 However, at the last iteration, that is at order == e4b->bd_blkbits + 1,
15 the shift count becomes negative (c.f. C99 6.5.7(3)) and UBSAN reports
17   UBSAN: Undefined behaviour in fs/ext4/mballoc.c:1281:11
18   shift exponent -1 is negative
19   [...]
20   Call Trace:
21    [<ffffffff818c4d35>] dump_stack+0xbc/0x117
22    [<ffffffff818c4c79>] ? _atomic_dec_and_lock+0x169/0x169
23    [<ffffffff819411bb>] ubsan_epilogue+0xd/0x4e
24    [<ffffffff81941cbc>] __ubsan_handle_shift_out_of_bounds+0x1fb/0x254
25    [<ffffffff81941ac1>] ? __ubsan_handle_load_invalid_value+0x158/0x158
26    [<ffffffff816e93a0>] ? ext4_mb_generate_from_pa+0x590/0x590
27    [<ffffffff816502c8>] ? ext4_read_block_bitmap_nowait+0x598/0xe80
28    [<ffffffff816e7b7e>] mb_find_order_for_block+0x1ce/0x240
29    [...]
31 Unless compilers start to do some fancy transformations (which at least
32 GCC 6.0.0 doesn't currently do), the issue is of cosmetic nature only: the
33 such calculated value of bb is never used again.
35 Silence UBSAN by introducing another variable, bb_incr, holding the next
36 increment to apply to bb and adjust that one by right shifting it by one
37 position per loop iteration.
39 Signed-off-by: Nicolai Stange <nicstange@gmail.com>
40 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
41 ---
42  fs/ext4/mballoc.c | 4 +++-
43  1 file changed, 3 insertions(+), 1 deletion(-)
45 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
46 index 50e05df..4bc89fe 100644
47 --- a/fs/ext4/mballoc.c
48 +++ b/fs/ext4/mballoc.c
49 @@ -1266,6 +1266,7 @@ static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
50  static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
51  {
52         int order = 1;
53 +       int bb_incr = 1 << (e4b->bd_blkbits - 1);
54         void *bb;
56         BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
57 @@ -1278,7 +1279,8 @@ static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
58                         /* this block is part of buddy of order 'order' */
59                         return order;
60                 }
61 -               bb += 1 << (e4b->bd_blkbits - order);
62 +               bb += bb_incr;
63 +               bb_incr >>= 1;
64                 order++;
65         }
66         return 0;
67 -- 
68 2.7.3