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

From: Al Viro

Date: Wed Nov 18 2026 - 01:22:12 EST


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...