Skip to contentSkip to conversation

f2fs: disallow mmap write and data-modifying fallocate on atomic files

PATCH Superseded Daeho Jeong 2 participants 4 messages Oct 2 – Oct 10 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: disallow mmap write and data-modifying fallocate on atomic files

From: Daeho Jeong <[email protected]>

Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.

fallocate without these flags only preallocates blocks and stays allowed.

Fixes 3db1de0e582c ("f2fs: change the current atomic write way")
---
 fs/f2fs/file.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/file.c +8 -2
index ef4d218e694..56c686b8580 100644
@@ -140,6 +140,10 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 		return VM_FAULT_SIGBUS;
 	}
 
+	/* mmap stores bypass the COW inode and would break atomicity */
+	if (f2fs_is_atomic_file(inode))
+		return VM_FAULT_SIGBUS;
+
 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
 		err = -EIO;
 		goto out;
@@ -2113,9 +2117,11 @@ static long f2fs_fallocate(struct file *file, int mode,
 
 	/*
 	 * Pinned file should not support partial truncation since the block
-	 * can be used by applications.
+	 * can be used by applications. Atomic files should not either, since
+	 * these modify the original inode directly and break atomicity.
 	 */
-	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode) ||
+	     f2fs_is_atomic_file(inode)) &&
 		(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
 			FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
 		ret = -EOPNOTSUPP;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
maintainer
#1 lore raw
Quoted 24 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.

fallocate without these flags only preallocates blocks and stays allowed.

Fixes 3db1de0e582c ("f2fs: change the current atomic write way")
---
 fs/f2fs/file.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/file.c +0 -0
index ef4d218e694..56c686b8580 100644

fs/f2fs/file.c static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) · line 145

Quoted 6 lines
fs/f2fs/file.c +3 -0
@@ -140,6 +140,10 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 		return VM_FAULT_SIGBUS;
 	}
 
+	/* mmap stores bypass the COW inode and would break atomicity */
+	if (f2fs_is_atomic_file(inode))
+		return VM_FAULT_SIGBUS;

Will we suffer race issue due to the check w/o lock?

Thanks,

Quoted 18 lines
fs/f2fs/file.c +5 -2
+
 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
 		err = -EIO;
 		goto out;
@@ -2113,9 +2117,11 @@ static long f2fs_fallocate(struct file *file, int mode,
 
 	/*
 	 * Pinned file should not support partial truncation since the block
-	 * can be used by applications.
+	 * can be used by applications. Atomic files should not either, since
+	 * these modify the original inode directly and break atomicity.
 	 */
-	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+	if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode) ||
+	     f2fs_is_atomic_file(inode)) &&
 		(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
 			FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
 		ret = -EOPNOTSUPP;
#2 lore raw
Quoted 29 lines · Chao Yu
Quoted 27 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.

fallocate without these flags only preallocates blocks and stays allowed.

Fixes 3db1de0e582c ("f2fs: change the current atomic write way")
---
 fs/f2fs/file.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/file.c +0 -0
index ef4d218e694..56c686b8580 100644
@@ -140,6 +140,10 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
              return VM_FAULT_SIGBUS;
      }
 
Quoted 7 lines

fs/f2fs/file.c static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) · line 145

Quoted 3 lines
fs/f2fs/file.c +3 -0
+     /* mmap stores bypass the COW inode and would break atomicity */
+     if (f2fs_is_atomic_file(inode))
+             return VM_FAULT_SIGBUS;

Will we suffer race issue due to the check w/o lock?

Thanks,

Hi Chao,

Yes, there is a race with f2fs_ioc_start_atomic_write().

page_mkwrite cannot take inode_lock, because the lock order is i_rwsem -> mmap_lock (e.g. fiemap copies to user under inode_lock). So v2 will use invalidate_lock instead:

f2fs_ioc_start_atomic_write() holds filemap_invalidate_lock() from the flush until FI_ATOMIC_FILE is set.
f2fs_vm_page_mkwrite() rechecks the flag after filemap_invalidate_lock_shared(), which it holds until the folio is dirtied.

If you are okay with this, I'll send v2.

Thanks,

Quoted 20 lines
Quoted 19 lines
fs/f2fs/file.c +5 -2
+
      if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
              err = -EIO;
              goto out;
@@ -2113,9 +2117,11 @@ static long f2fs_fallocate(struct file *file, int mode,
 
      /*
       * Pinned file should not support partial truncation since the block
-      * can be used by applications.
+      * can be used by applications. Atomic files should not either, since
+      * these modify the original inode directly and break atomicity.
       */
-     if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+     if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode) ||
+          f2fs_is_atomic_file(inode)) &&
              (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
                      FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
              ret = -EOPNOTSUPP;
maintainer
#3 lore raw
Quoted 46 lines · Daeho Jeong
Quoted 29 lines · Chao Yu
Quoted 27 lines · Daeho Jeong

From: Daeho Jeong <[email protected]>

Stores through a shared writable mapping and fallocate with FALLOC_FL_PUNCH_HOLE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE do not go through ->write_begin, so they bypass the COW inode and modify the original inode directly. Reject them while the file is in atomic write mode, as fallocate already does for pinned and compressed files. A write fault gets SIGBUS and fallocate gets -EOPNOTSUPP.

fallocate without these flags only preallocates blocks and stays allowed.

Fixes 3db1de0e582c ("f2fs: change the current atomic write way")
---
 fs/f2fs/file.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/file.c +0 -0
index ef4d218e694..56c686b8580 100644
@@ -140,6 +140,10 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
              return VM_FAULT_SIGBUS;
      }
 
Quoted 7 lines

fs/f2fs/file.c static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) · line 145

Quoted 3 lines
fs/f2fs/file.c +3 -0
+     /* mmap stores bypass the COW inode and would break atomicity */
+     if (f2fs_is_atomic_file(inode))
+             return VM_FAULT_SIGBUS;

Will we suffer race issue due to the check w/o lock?

Thanks,

Hi Chao,

Yes, there is a race with f2fs_ioc_start_atomic_write().

page_mkwrite cannot take inode_lock, because the lock order is i_rwsem -> mmap_lock (e.g. fiemap copies to user under inode_lock). So v2 will use invalidate_lock instead:

Quoted 7 lines

f2fs_ioc_start_atomic_write() holds filemap_invalidate_lock() from the flush until FI_ATOMIC_FILE is set.
f2fs_vm_page_mkwrite() rechecks the flag after filemap_invalidate_lock_shared(), which it holds until the folio is dirtied.

If you are okay with this, I'll send v2.

Daeho, looks fine to me, please go ahead. :)

Thanks,

Quoted 23 lines

Thanks,

Quoted 20 lines
Quoted 19 lines
fs/f2fs/file.c +5 -2
+
      if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
              err = -EIO;
              goto out;
@@ -2113,9 +2117,11 @@ static long f2fs_fallocate(struct file *file, int mode,
 
      /*
       * Pinned file should not support partial truncation since the block
-      * can be used by applications.
+      * can be used by applications. Atomic files should not either, since
+      * these modify the original inode directly and break atomicity.
       */
-     if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
+     if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode) ||
+          f2fs_is_atomic_file(inode)) &&
              (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
                      FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
              ret = -EOPNOTSUPP;