* [PATCH] fs: sync: fix overflow check in sync_file_range()
@ 2026-11-18 2:34 mdshahid03
2026-11-18 6:20 ` Al Viro
0 siblings, 1 reply; 2+ messages in thread
From: mdshahid03 @ 2026-11-18 2:34 UTC (permalink / raw)
To: viro, brauner; +Cc: jack, linux-fsdevel, linux-kernel, Mohammad Shahid
From: Mohammad Shahid <mdshahid03@gmail.com>
The addition of offset and nbytes can overflow before the result
is checked.
Use check_add_overflow() to detect the overflow safely during
addition.
Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com>
---
fs/sync.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/fs/sync.c b/fs/sync.c
index 4a84dd837b86..9497604d7b17 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -10,6 +10,7 @@
#include <linux/slab.h>
#include <linux/export.h>
#include <linux/namei.h>
+#include <linux/overflow.h>
#include <linux/sched.h>
#include <linux/writeback.h>
#include <linux/syscalls.h>
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] fs: sync: fix overflow check in sync_file_range()
2026-11-18 2:34 [PATCH] fs: sync: fix overflow check in sync_file_range() mdshahid03
@ 2026-11-18 6:20 ` Al Viro
0 siblings, 0 replies; 2+ messages in thread
From: Al Viro @ 2026-11-18 6:20 UTC (permalink / raw)
To: mdshahid03; +Cc: brauner, jack, linux-fsdevel, linux-kernel
On Wed, Nov 18, 2026 at 08:04:52AM +0530, mdshahid03@gmail.com wrote:
> From: Mohammad Shahid <mdshahid03@gmail.com>
>
> 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...
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-18 6:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 2:34 [PATCH] fs: sync: fix overflow check in sync_file_range() mdshahid03
2026-11-18 6:20 ` Al Viro
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.