Skip to contentSkip to conversation

fs: sync: fix overflow check in sync_file_range()

PATCH In review [email protected] 2 participants 2 messages Nov 19 VFS

lore
Share & tools
lore
#0 lore raw

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

From: Mohammad Shahid <[email protected]>

Use check_add_overflow() to detect overflow when calculating the end byte of the requested range.

Reject negative offsets and lengths before performing the addition.

---
 fs/sync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/sync.c +4 -6
index 4a84dd837b86..a1ef2b619cfc 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,13 +234,10 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
 	if (flags & ~VALID_FLAGS)
 		goto out;
 
-	endbyte = offset + nbytes;
-
-	if ((s64)offset < 0)
-		goto out;
-	if ((s64)endbyte < 0)
+	if (offset < 0 || nbytes < 0)
 		goto out;
-	if (endbyte < offset)
+
+	if (check_add_overflow(offset, nbytes, &endbyte))
 		goto out;
 
 	if (sizeof(pgoff_t) == 4) {
Signature · 1 line
2.43.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · On Thu 19-11-26 11:08:27, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

Use check_add_overflow() to detect overflow when calculating the end byte of the requested range.

Reject negative offsets and lengths before performing the addition.

Looks good. Feel free to add:

Honza

Quoted 36 lines
---
 fs/sync.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/sync.c +4 -6
index 4a84dd837b86..a1ef2b619cfc 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,13 +234,10 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
 	if (flags & ~VALID_FLAGS)
 		goto out;
 
-	endbyte = offset + nbytes;
-
-	if ((s64)offset < 0)
-		goto out;
-	if ((s64)endbyte < 0)
+	if (offset < 0 || nbytes < 0)
 		goto out;
-	if (endbyte < offset)
+
+	if (check_add_overflow(offset, nbytes, &endbyte))
 		goto out;
 
 	if (sizeof(pgoff_t) == 4) {
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR