Skip to contentSkip to conversation

fs: sync: fix overflow check in sync_file_range()

PATCH Superseded [email protected] 2 participants 3 messages Nov 18 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: sync: fix overflow check in sync_file_range()

From: Mohammad Shahid <[email protected]>

The addition of offset and nbytes can overflow before the result is checked.

Use check_add_overflow() to detect the overflow safely during addition.

---
 fs/sync.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/sync.c +6 -3
index 4a84dd837b86..9497604d7b17 100644
@@ -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;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 7 lines · On Wed, Nov 18, 2026 at 08:04:52AM +0530, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

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"?

Quoted 7 lines
fs/sync.c +2 -1
@@ -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;

fs/sync.c int sync_file_range(struct file *file, loff_t offset, loff_t nbytes, · line 246

Quoted 10 lines
fs/sync.c +3 -2
 
-	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...

#2 lore raw

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.

Quoted 38 lines · Al Viro
Quoted 7 lines · On Wed, Nov 18, 2026 at 08:04:52AM +0530, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

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"?

Quoted 7 lines
fs/sync.c +2 -1
@@ -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;

fs/sync.c int sync_file_range(struct file *file, loff_t offset, loff_t nbytes, · line 246

Quoted 10 lines
fs/sync.c +3 -2
 
-	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...