Skip to contentSkip to conversation

f2fs: file: add checks to f2fs_ioc_flush_device()

PATCH In review Roman Smirnov 2 participants 2 messages Aug 1, 2024 – Aug 13, 2024 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: file: add checks to f2fs_ioc_flush_device()

If invalid data is copied from user space and if GET_SEGNO() returns NULL_SEGNO an overflow is possible.

Add checks for invalid values.

Found by Linux Verification Center (linuxtesting.org) with Svace.

---
 fs/f2fs/file.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/file.c +3 -0
index 168f08507004..1b9c4fee9db1 100644
@@ -3093,6 +3093,9 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
 		start_segno = dev_start_segno;
 	end_segno = min(start_segno + range.segments, dev_end_segno);
 
+	if (start_segno > F2FS_MAX_SEGMENT - range.segments || end_segno == NULL_SEGNO)
+		return -EINVAL;
+
 	while (start_segno < end_segno) {
 		if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
 			ret = -EBUSY;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 2 lines · Roman Smirnov

If invalid data is copied from user space and if GET_SEGNO() returns NULL_SEGNO an overflow is possible.

Can you explain in which condition that FDEV(range.dev_num).start_blk or FDEV(range.dev_num).end_blk will be invalid?

Quoted 14 lines
Quoted 1 line

Add checks for invalid values.

Found by Linux Verification Center (linuxtesting.org) with Svace.

---
  fs/f2fs/file.c | 3 +++
  1 file changed, 3 insertions(+)
fs/f2fs/file.c +0 -0
index 168f08507004..1b9c4fee9db1 100644

fs/f2fs/file.c static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg) · line 3097

Quoted 5 lines
fs/f2fs/file.c +2 -0
@@ -3093,6 +3093,9 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
  		start_segno = dev_start_segno;
  	end_segno = min(start_segno + range.segments, dev_end_segno);
 
+	if (start_segno > F2FS_MAX_SEGMENT - range.segments || end_segno == NULL_SEGNO)
+		return -EINVAL;

It missed to call mnt_drop_write_file() before return.

Thanks,

Quoted 4 lines
fs/f2fs/file.c +1 -0
+
  	while (start_segno < end_segno) {
  		if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
  			ret = -EBUSY;