[PATCH] fs: sync: fix overflow check in sync_file_range()
From: mdshahid03
Date: Tue Nov 17 2026 - 21:35:24 EST
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.
Signed-off-by: Mohammad Shahid <mdshahid03@xxxxxxxxx>
---
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