2021-01-25 18:44:07

by Matthew Wilcox (Oracle)

[permalink] [raw]
Subject: Re: UBSAN: shift-out-of-bounds in exfat_fill_super

On Mon, Jan 25, 2021 at 09:33:14AM -0800, syzbot wrote:
> UBSAN: shift-out-of-bounds in fs/exfat/super.c:471:28
> shift exponent 4294967294 is too large for 32-bit type 'int'

This is an integer underflow:

sbi->dentries_per_clu = 1 <<
(sbi->cluster_size_bits - DENTRY_SIZE_BITS);

I think the problem is that there is no validation of sect_per_clus_bits.
We should check it is at least DENTRY_SIZE_BITS and probably that it's
less than ... 16? 64? I don't know what legitimate values are in this
field, but I would imagine that 255 is completely unacceptable.


2021-01-26 12:23:14

by Randy Dunlap

[permalink] [raw]
Subject: Re: UBSAN: shift-out-of-bounds in exfat_fill_super

On 1/25/21 10:39 AM, Matthew Wilcox wrote:
> On Mon, Jan 25, 2021 at 09:33:14AM -0800, syzbot wrote:
>> UBSAN: shift-out-of-bounds in fs/exfat/super.c:471:28
>> shift exponent 4294967294 is too large for 32-bit type 'int'
>
> This is an integer underflow:
>
> sbi->dentries_per_clu = 1 <<
> (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
>
> I think the problem is that there is no validation of sect_per_clus_bits.
> We should check it is at least DENTRY_SIZE_BITS and probably that it's
> less than ... 16? 64? I don't know what legitimate values are in this
> field, but I would imagine that 255 is completely unacceptable.

Ack all of that. The syzbot boot_sector has sect_per_clus_bits == 3
and sect_size_bits == 0, so sbi->cluster_size_bits is 3, then
UBSAN goes bang on:

sbi->dentries_per_clu = 1 <<
(sbi->cluster_size_bits - DENTRY_SIZE_BITS); // 3 - 5


There is also an unprotected shift at line 480:

if (sbi->num_FAT_sectors << p_boot->sect_size_bits <
sbi->num_clusters * 4) {

that should be protected IMO.


--
~Randy

2021-01-26 23:31:41

by Namjae Jeon

[permalink] [raw]
Subject: RE: UBSAN: shift-out-of-bounds in exfat_fill_super

> On 1/25/21 10:39 AM, Matthew Wilcox wrote:
> > On Mon, Jan 25, 2021 at 09:33:14AM -0800, syzbot wrote:
> >> UBSAN: shift-out-of-bounds in fs/exfat/super.c:471:28 shift exponent
> >> 4294967294 is too large for 32-bit type 'int'
> >
> > This is an integer underflow:
> >
> > sbi->dentries_per_clu = 1 <<
> > (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
> >
> > I think the problem is that there is no validation of sect_per_clus_bits.
> > We should check it is at least DENTRY_SIZE_BITS and probably that it's
> > less than ... 16? 64? I don't know what legitimate values are in
> > this field, but I would imagine that 255 is completely unacceptable.
>
> Ack all of that. The syzbot boot_sector has sect_per_clus_bits == 3 and sect_size_bits == 0, so sbi-
> >cluster_size_bits is 3, then UBSAN goes bang on:
>
> sbi->dentries_per_clu = 1 <<
> (sbi->cluster_size_bits - DENTRY_SIZE_BITS); // 3 - 5
>
>
> There is also an unprotected shift at line 480:
>
> if (sbi->num_FAT_sectors << p_boot->sect_size_bits <
> sbi->num_clusters * 4) {
>
> that should be protected IMO.
Right. I will also add validation for fat_length as well as sect_size_bits before this.

Thanks!
>
>
> --
> ~Randy


2021-01-27 19:43:41

by Namjae Jeon

[permalink] [raw]
Subject: RE: UBSAN: shift-out-of-bounds in exfat_fill_super

> On Mon, Jan 25, 2021 at 09:33:14AM -0800, syzbot wrote:
> > UBSAN: shift-out-of-bounds in fs/exfat/super.c:471:28 shift exponent
> > 4294967294 is too large for 32-bit type 'int'
>
> This is an integer underflow:
>
> sbi->dentries_per_clu = 1 <<
> (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
>
> I think the problem is that there is no validation of sect_per_clus_bits.
> We should check it is at least DENTRY_SIZE_BITS and probably that it's less than ... 16? 64? I don't
> know what legitimate values are in this field, but I would imagine that 255 is completely unacceptable.
exfat specification describe sect_per_clus_bits field of boot sector could be at most 32 and
at least 0. And sect_size_bits can also affect this calculation, It also needs validation.
I will fix it.
Thanks!