Skip to contentSkip to conversation

f2fs: fix to relocate check condition in f2fs_fallocate()

PATCH Applied Chao Yu 3 participants 6 messages Apr 3, 2024 – Apr 14, 2024 F2FS file system

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH 1/2 f2fs: fix to relocate check condition in f2fs_fallocate()

compress and pinfile flag should be checked after inode lock held to avoid race condition, fix it.

Fixes 4c8ff7095bef ("f2fs: support data compression")
Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")
---
 fs/f2fs/file.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
fs/f2fs/file.c +11 -9
index 148bfe3effdf..83a807e25e31 100644
@@ -1820,15 +1820,6 @@ static long f2fs_fallocate(struct file *file, int mode,
 		(mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
 		return -EOPNOTSUPP;
 
-	/*
-	 * Pinned file should not support partial truncation since the block
-	 * can be used by applications.
-	 */
-	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
-		(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
-			FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
-		return -EOPNOTSUPP;
-
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
 			FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
 			FALLOC_FL_INSERT_RANGE))
@@ -1836,6 +1827,17 @@ static long f2fs_fallocate(struct file *file, int mode,
 
 	inode_lock(inode);
 
+	/*
+	 * Pinned file should not support partial truncation since the block
+	 * can be used by applications.
+	 */
+	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+		(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
+			FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+
 	ret = file_modified(file);
 	if (ret)
 		goto out;
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 2/2 f2fs: fix to check pinfile flag in f2fs_move_file_range()

ioctl(F2FS_IOC_MOVE_RANGE) can truncate or punch hole on pinned file, fix to disallow it.

Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")
---
 fs/f2fs/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/f2fs/file.c +2 -1
index 83a807e25e31..0d1bcdf61a09 100644
@@ -2858,7 +2858,8 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
 			goto out;
 	}
 
-	if (f2fs_compressed_file(src) || f2fs_compressed_file(dst)) {
+	if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
+		f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
 		ret = -EOPNOTSUPP;
 		goto out_unlock;
 	}
Signature · 1 line
2.40.1
#2 lore raw
In reply to Chao Yu
Quoted 36 lines · Chao Yu

compress and pinfile flag should be checked after inode lock held to avoid race condition, fix it.

Fixes 4c8ff7095bef ("f2fs: support data compression")
Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")
---
 fs/f2fs/file.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
fs/f2fs/file.c +1 -9
index 148bfe3effdf..83a807e25e31 100644
@@ -1820,15 +1820,6 @@ static long f2fs_fallocate(struct file *file, int mode,
                (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
                return -EOPNOTSUPP;
 
-       /*
-        * Pinned file should not support partial truncation since the block
-        * can be used by applications.
-        */
-       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
-               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
-                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
-               return -EOPNOTSUPP;
-
        if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
                        FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
                        FALLOC_FL_INSERT_RANGE))
@@ -1836,6 +1827,17 @@ static long f2fs_fallocate(struct file *file, int mode,
 
        inode_lock(inode);
 
+       /*

fs/f2fs/file.c static long f2fs_fallocate(struct file *file, int mode, · line 1840

Quoted 10 lines
fs/f2fs/file.c +10 -0
+        * Pinned file should not support partial truncation since the block
+        * can be used by applications.
+        */
+       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
+                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+

Dear Chao,
I see the judgment code "if(!f2fs_compressed_file(inode))" also is
before inode_lock in functions f2fs_ioc_decompress_file/f2fs_ioc_compress_file/f2fs_reserve_compress_blocks/f2fs_release_compress_blocks. should they are changed together?
thanks!

Quoted 12 lines
fs/f2fs/file.c +0 -1
        ret = file_modified(file);
        if (ret)
                goto out;
--

2.40.1

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#3 lore raw
Quoted 36 lines · Zhiguo Niu
Quoted 35 lines · Chao Yu

compress and pinfile flag should be checked after inode lock held to avoid race condition, fix it.

Fixes 4c8ff7095bef ("f2fs: support data compression")
Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")
---
  fs/f2fs/file.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)
fs/f2fs/file.c +0 -9
index 148bfe3effdf..83a807e25e31 100644
@@ -1820,15 +1820,6 @@ static long f2fs_fallocate(struct file *file, int mode,
                 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
                 return -EOPNOTSUPP;
 
-       /*
-        * Pinned file should not support partial truncation since the block
-        * can be used by applications.
-        */
-       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
-               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
-                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
-               return -EOPNOTSUPP;
-
         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
                         FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
                         FALLOC_FL_INSERT_RANGE))
@@ -1836,6 +1827,17 @@ static long f2fs_fallocate(struct file *file, int mode,
 
         inode_lock(inode);
 
Quoted 16 lines

fs/f2fs/file.c static long f2fs_fallocate(struct file *file, int mode, · line 1840

Quoted 11 lines
fs/f2fs/file.c +11 -0
+       /*
+        * Pinned file should not support partial truncation since the block
+        * can be used by applications.
+        */
+       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
+                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+

Dear Chao,
I see the judgment code "if(!f2fs_compressed_file(inode))" also is
before inode_lock in functions f2fs_ioc_decompress_file/f2fs_ioc_compress_file/f2fs_reserve_compress_blocks/f2fs_release_compress_blocks. should they are changed together?

Zhiguo,

Thanks for noticing that, I've submitted separated patches for fixing because those bugs were introduced by separated commits.

Thanks,

Quoted 13 lines

thanks!

Quoted 12 lines
fs/f2fs/file.c +0 -1
         ret = file_modified(file);
         if (ret)
                 goto out;
--

2.40.1

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw
Quoted 55 lines · Chao Yu
Quoted 36 lines · Zhiguo Niu
Quoted 35 lines · Chao Yu

compress and pinfile flag should be checked after inode lock held to avoid race condition, fix it.

Fixes 4c8ff7095bef ("f2fs: support data compression")
Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")
---
  fs/f2fs/file.c | 20 +++++++++++---------
  1 file changed, 11 insertions(+), 9 deletions(-)
fs/f2fs/file.c +0 -9
index 148bfe3effdf..83a807e25e31 100644
@@ -1820,15 +1820,6 @@ static long f2fs_fallocate(struct file *file, int mode,
                 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
                 return -EOPNOTSUPP;
 
-       /*
-        * Pinned file should not support partial truncation since the block
-        * can be used by applications.
-        */
-       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
-               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
-                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE)))
-               return -EOPNOTSUPP;
-
         if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
                         FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
                         FALLOC_FL_INSERT_RANGE))
@@ -1836,6 +1827,17 @@ static long f2fs_fallocate(struct file *file, int mode,
 
         inode_lock(inode);
 
Quoted 16 lines

fs/f2fs/file.c static long f2fs_fallocate(struct file *file, int mode, · line 1840

Quoted 11 lines
fs/f2fs/file.c +11 -0
+       /*
+        * Pinned file should not support partial truncation since the block
+        * can be used by applications.
+        */
+       if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+               (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
+                       FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
+               ret = -EOPNOTSUPP;
+               goto out;
+       }
+

Dear Chao,
I see the judgment code "if(!f2fs_compressed_file(inode))" also is
before inode_lock in functions f2fs_ioc_decompress_file/f2fs_ioc_compress_file/f2fs_reserve_compress_blocks/f2fs_release_compress_blocks. should they are changed together?

Quoted 6 lines

Zhiguo,

Thanks for noticing that, I've submitted separated patches for fixing because those bugs were introduced by separated commits.

Thanks,

Dear Chao,
You're welcome^^

Quoted 14 lines
Quoted 14 lines

thanks!

Quoted 12 lines
fs/f2fs/file.c +0 -1
         ret = file_modified(file);
         if (ret)
                 goto out;
--

2.40.1

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw
In reply to Chao Yu
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 8 lines · On Wed, 3 Apr 2024 22:24:19 +0800 you wrote:

compress and pinfile flag should be checked after inode lock held to avoid race condition, fix it.

Fixes 4c8ff7095bef ("f2fs: support data compression")
Fixes 5fed0be8583f ("f2fs: do not allow partial truncation on pinned file")

[...]

Here is the summary with links:
- [f2fs-dev,1/2] f2fs: fix to relocate check condition in f2fs_fallocate()

    https://git.kernel.org/jaegeuk/f2fs/c/278a6253a673
  - [f2fs-dev,2/2] f2fs: fix to check pinfile flag in f2fs_move_file_range()
    https://git.kernel.org/jaegeuk/f2fs/c/e07230da0500

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html