Re: [PATCH] fs: sync: fix overflow check in sync_file_range()

From: mdshahid03

Date: Wed Nov 18 2026 - 12:04:00 EST


Hi Al.

Thank you for review and feedback.
You are right that endbyte < offset checks become redundant for
non-negative operands when check_add_overflow() used.

However,I have also missed one aspect : It also rejects negative nbytes.

I will make that validation explicit by checking nbytes < 0 along with
offset < 0, and remove redundant endbyte < 0 and endbyte < offset checks.

Thanks
Shahid.

On 2026-11-18 06:20 +0000, Al Viro wrote:
> On Wed, Nov 18, 2026 at 08:04:52AM +0530, mdshahid03@xxxxxxxxx wrote:
> > From: Mohammad Shahid <mdshahid03@xxxxxxxxx>
> >
> > The addition of offset and nbytes can overflow before the result
> > is checked.
> >
> > Use check_add_overflow() to detect the overflow safely during
> > addition.
>
> "Safely" as in "doesn't rely upon the -fno-strict-overflow we
> have in CFLAGS"?
>
> > @@ -233,11 +234,13 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
> > if (flags & ~VALID_FLAGS)
> > goto out;
> >
> > - endbyte = offset + nbytes;
> > + if (offset < 0)
> > + goto out;
> >
> > - if ((s64)offset < 0)
> > + if (check_add_overflow(offset, nbytes, &endbyte))
> > goto out;
> > - if ((s64)endbyte < 0)
> > +
> > + if (endbyte < 0)
> > goto out;
> > if (endbyte < offset)
> > goto out;
>
> Why do you keep the old overflow check after having added that thing?
> If sum of two non-negative long long values does *not* wrap around,
> it will never be below either of summands; that's what the last check
> had always been about. I'm not saying that it's worse off with
> the use of check_add_overflow(), but mixing it with the old overflow
> check is very confusing...
>
>