2024-05-09 15:54:13

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH] fs: remove accidental overflow during wraparound check

On Tue 07-05-24 23:17:57, Justin Stitt wrote:
> Running syzkaller with the newly enabled signed integer overflow
> sanitizer produces this report:
>
> [ 195.401651] ------------[ cut here ]------------
> [ 195.404808] UBSAN: signed-integer-overflow in ../fs/open.c:321:15
> [ 195.408739] 9223372036854775807 + 562984447377399 cannot be represented in type 'loff_t' (aka 'long long')
> [ 195.414683] CPU: 1 PID: 703 Comm: syz-executor.0 Not tainted 6.8.0-rc2-00039-g14de58dbe653-dirty #11
> [ 195.420138] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
> [ 195.425804] Call Trace:
> [ 195.427360] <TASK>
> [ 195.428791] dump_stack_lvl+0x93/0xd0
> [ 195.431150] handle_overflow+0x171/0x1b0
> [ 195.433640] vfs_fallocate+0x459/0x4f0

Well, we compile the kernel with -fno-strict-overflow for a reason so I
wouldn't consider this a bug. But check_add_overflow() is easier to digest
since we don't have to worry about type details so I'm for this change.

> @@ -319,8 +320,12 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
> if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
> return -ENODEV;
>
> - /* Check for wrap through zero too */
> - if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
> + /* Check for wraparound */
> + if (check_add_overflow(offset, len, &sum))
> + return -EFBIG;
> +
> + /* Now, check bounds */
> + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> return -EFBIG;

But why do you check for sum < 0? We know from previous checks offset >= 0
&& len > 0 so unless we overflow, sum is guaranteed to be > 0.

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR


2024-05-09 22:10:31

by Justin Stitt

[permalink] [raw]
Subject: Re: [PATCH] fs: remove accidental overflow during wraparound check

On Thu, May 9, 2024 at 8:53 AM Jan Kara <[email protected]> wrote:
> > @@ -319,8 +320,12 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
> > if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
> > return -ENODEV;
> >
> > - /* Check for wrap through zero too */
> > - if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
> > + /* Check for wraparound */
> > + if (check_add_overflow(offset, len, &sum))
> > + return -EFBIG;
> > +
> > + /* Now, check bounds */
> > + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> > return -EFBIG;
>
> But why do you check for sum < 0? We know from previous checks offset >= 0
> && len > 0 so unless we overflow, sum is guaranteed to be > 0.

Fair enough. I suppose with the overflow check in place we can no
longer have a sum less than zero there. If nothing else, it tells
readers of this code what the domain of (offset+len) is. I don't mind
sending a new version, though.

>
> Honza
> --
> Jan Kara <[email protected]>
> SUSE Labs, CR

2024-05-13 13:45:08

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH] fs: remove accidental overflow during wraparound check

On Thu 09-05-24 15:10:07, Justin Stitt wrote:
> On Thu, May 9, 2024 at 8:53 AM Jan Kara <[email protected]> wrote:
> > > @@ -319,8 +320,12 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
> > > if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
> > > return -ENODEV;
> > >
> > > - /* Check for wrap through zero too */
> > > - if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
> > > + /* Check for wraparound */
> > > + if (check_add_overflow(offset, len, &sum))
> > > + return -EFBIG;
> > > +
> > > + /* Now, check bounds */
> > > + if (sum > inode->i_sb->s_maxbytes || sum < 0)
> > > return -EFBIG;
> >
> > But why do you check for sum < 0? We know from previous checks offset >= 0
> > && len > 0 so unless we overflow, sum is guaranteed to be > 0.
>
> Fair enough. I suppose with the overflow check in place we can no
> longer have a sum less than zero there. If nothing else, it tells
> readers of this code what the domain of (offset+len) is. I don't mind
> sending a new version, though.

Well, for normal readers offset+len is always a positive number. That's
what you expect. If you see a check for offset+len < 0, you start wondering
what are you missing... only to find you miss nothing and the check is
pointless. So yes, please send a version without the pointless check.

Honza

--
Jan Kara <[email protected]>
SUSE Labs, CR