add patch silence-UBSAN-in-ext4_mb_init
[ext4-patch-queue.git] / silence-UBSAN-in-ext4_mb_init
blob42416ad68fb164d1f37d136d6b35eeca19a207ad
1 ext4: silence UBSAN in ext4_mb_init()
3 From: Nicolai Stange <nicstange@gmail.com>
5 Currently, in ext4_mb_init(), there's a loop like the following:
7   do {
8     ...
9     offset += 1 << (sb->s_blocksize_bits - i);
10     i++;
11   } while (i <= sb->s_blocksize_bits + 1);
13 Note that the updated offset is used in the loop's next iteration only.
15 However, at the last iteration, that is at i == sb->s_blocksize_bits + 1,
16 the shift count becomes equal to (unsigned)-1 > 31 (c.f. C99 6.5.7(3))
17 and UBSAN reports
19   UBSAN: Undefined behaviour in fs/ext4/mballoc.c:2621:15
20   shift exponent 4294967295 is too large for 32-bit type 'int'
21   [...]
22   Call Trace:
23    [<ffffffff818c4d25>] dump_stack+0xbc/0x117
24    [<ffffffff818c4c69>] ? _atomic_dec_and_lock+0x169/0x169
25    [<ffffffff819411ab>] ubsan_epilogue+0xd/0x4e
26    [<ffffffff81941cac>] __ubsan_handle_shift_out_of_bounds+0x1fb/0x254
27    [<ffffffff81941ab1>] ? __ubsan_handle_load_invalid_value+0x158/0x158
28    [<ffffffff814b6dc1>] ? kmem_cache_alloc+0x101/0x390
29    [<ffffffff816fc13b>] ? ext4_mb_init+0x13b/0xfd0
30    [<ffffffff814293c7>] ? create_cache+0x57/0x1f0
31    [<ffffffff8142948a>] ? create_cache+0x11a/0x1f0
32    [<ffffffff821c2168>] ? mutex_lock+0x38/0x60
33    [<ffffffff821c23ab>] ? mutex_unlock+0x1b/0x50
34    [<ffffffff814c26ab>] ? put_online_mems+0x5b/0xc0
35    [<ffffffff81429677>] ? kmem_cache_create+0x117/0x2c0
36    [<ffffffff816fcc49>] ext4_mb_init+0xc49/0xfd0
37    [...]
39 Observe that the mentioned shift exponent, 4294967295, equals (unsigned)-1.
41 Unless compilers start to do some fancy transformations (which at least
42 GCC 6.0.0 doesn't currently do), the issue is of cosmetic nature only: the
43 such calculated value of offset is never used again.
45 Silence UBSAN by introducing another variable, offset_incr, holding the
46 next increment to apply to offset and adjust that one by right shifting it
47 by one position per loop iteration.
49 Signed-off-by: Nicolai Stange <nicstange@gmail.com>
50 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
51 ---
52  fs/ext4/mballoc.c | 6 ++++--
53  1 file changed, 4 insertions(+), 2 deletions(-)
55 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
56 index 4bc89fe..8dc0d9b 100644
57 --- a/fs/ext4/mballoc.c
58 +++ b/fs/ext4/mballoc.c
59 @@ -2585,7 +2585,7 @@ int ext4_mb_init(struct super_block *sb)
60  {
61         struct ext4_sb_info *sbi = EXT4_SB(sb);
62         unsigned i, j;
63 -       unsigned offset;
64 +       unsigned offset, offset_incr;
65         unsigned max;
66         int ret;
68 @@ -2614,11 +2614,13 @@ int ext4_mb_init(struct super_block *sb)
70         i = 1;
71         offset = 0;
72 +       offset_incr = 1 << (sb->s_blocksize_bits - 1);
73         max = sb->s_blocksize << 2;
74         do {
75                 sbi->s_mb_offsets[i] = offset;
76                 sbi->s_mb_maxs[i] = max;
77 -               offset += 1 << (sb->s_blocksize_bits - i);
78 +               offset += offset_incr;
79 +               offset_incr = offset_incr >> 1;
80                 max = max >> 1;
81                 i++;
82         } while (i <= sb->s_blocksize_bits + 1);
83 -- 
84 2.7.3