Skip to contentSkip to conversation

backing-file: enforce write seals and writable mapping in mmap

PATCH In review Dominik Maier 4 participants 5 messages Dec 3

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 backing-file: enforce write seals and writable mapping in mmap

do_mmap() and mmap_region() check memfd write seals, FMODE_WRITE, and mapping_map_writable() on the outer file before calling ->mmap(). When backing_file_mmap() switches vma->vm_file onto a backing file, those checks are never repeated on the underlying file or address_space.

Mapping a FUSE passthrough file backed by a write-sealed memfd therefore bypasses F_SEAL_WRITE and F_SEAL_FUTURE_WRITE, granting writable shared mappings over a write-sealed memfd or racing past concurrent F_SEAL_WRITE additions.

Check memfd seals and FMODE_WRITE in backing_file_mmap() before vma_set_file(), downgrade read-only shared backing mappings to match do_mmap(), and re-validate seals while holding mapping_map_writable() across vma_link_file() whenever ->mmap() swaps vma->vm_file.

Fixes f567377e406c ("fs: factor out backing_file_mmap() helper")

---
Found with an AI-assisted code review tool (ARTIST) and verified in QEMU/KVM across shared writable, mprotect(), and concurrent F_SEAL_WRITE passthrough mmap tests.

 fs/backing-file.c                 | 10 ++++++++++
 mm/internal.h                     |  5 +++++
 mm/vma.c                          | 20 ++++++++++++++++++--
 mm/vma_internal.h                 |  1 +
 tools/testing/vma/include/stubs.h | 11 +++++++++++
 5 files changed, 45 insertions(+), 2 deletions(-)
fs/backing-file.c +10 -0
index c601b27..f0547d1 100644
@@ -10,6 +10,7 @@
 
 #include <linux/fs.h>
 #include <linux/backing-file.h>
+#include <linux/memfd.h>
 #include <linux/splice.h>
 #include <linux/mm.h>
 #include <linux/security.h>
@@ -373,6 +374,7 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
 		      struct backing_file_ctx *ctx)
 {
 	struct file *user_file = vma->vm_file;
+	vma_flags_t vma_flags = vma->flags;
 	int ret;
 
 	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
@@ -385,6 +387,14 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
 	if ((vma->vm_flags & VM_SHARED) && IS_APPEND(file_inode(file)))
 		return -EACCES;
 
+	if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) == (VM_SHARED | VM_WRITE) &&
+	    !(file->f_mode & FMODE_WRITE))
+		return -EACCES;
+
+	ret = memfd_check_seals_mmap(file, &vma_flags);
+	if (ret)
+		return ret;
+
 	vma_set_file(vma, file);
 
 	scoped_with_creds(ctx->cred) {
mm/internal.h +5 -0
index 96cb55d..3a8bea6 100644
@@ -279,6 +279,11 @@ static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
 	return err;
 }
 
+static inline bool backing_file_is_readonly(const struct file *file)
+{
+	return (file->f_mode & (FMODE_BACKING | FMODE_WRITE)) == FMODE_BACKING;
+}
+
 /* unmap_vmas is in mm/memory.c */
 void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap);
 
mm/vma.c +18 -2
index c82e930..7a240d0 100644
@@ -2668,7 +2668,8 @@ static bool map_same_file(struct mmap_state *map)
 }
 
 static int __mmap_new_file_vma(struct mmap_state *map,
-			       struct vm_area_struct *vma)
+			       struct vm_area_struct *vma,
+			       bool *swapped_writable)
 {
 	struct vma_iterator *vmi = map->vmi;
 	int error;
@@ -2687,6 +2688,18 @@ static int __mmap_new_file_vma(struct mmap_state *map,
 	vma_clear_flags_mask(vma, VMA_LOCKED_MASK);
 	error = mmap_file(vma->vm_file, vma);
 	map->vm_file = vma->vm_file;
+	if (!error && vma->vm_file && !map_same_file(map)) {
+		if (backing_file_is_readonly(vma->vm_file) &&
+		    vma_is_shared_maywrite(vma))
+			vma_clear_flags(vma, VMA_MAYWRITE_BIT, VMA_SHARED_BIT);
+
+		error = memfd_check_seals_mmap(vma->vm_file, &vma->flags);
+		if (!error && vma_is_shared_maywrite(vma)) {
+			error = mapping_map_writable(vma->vm_file->f_mapping);
+			if (!error)
+				*swapped_writable = true;
+		}
+	}
 
 	if (error) {
 		UNMAP_STATE(unmap, vmi, vma, vma->vm_start, vma->vm_end,
@@ -2754,6 +2767,7 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 {
 	const bool is_anon = map_is_anon(map);
 	struct vma_iterator *vmi = map->vmi;
+	bool swapped_writable = false;
 	int error = 0;
 	struct vm_area_struct *vma;
 
@@ -2782,7 +2796,7 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 
 	/* Invoke callbacks. */
 	if (map->vm_file)
-		error = __mmap_new_file_vma(map, vma);
+		error = __mmap_new_file_vma(map, vma, &swapped_writable);
 	else if (!is_anon)
 		error = shmem_zero_setup(vma);
 
@@ -2799,6 +2813,8 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap,
 	vma_iter_store_new(vmi, vma);
 	map->mm->map_count++;
 	vma_link_file(vma, action->hide_from_rmap_until_complete);
+	if (swapped_writable)
+		mapping_unmap_writable(vma->vm_file->f_mapping);
 
 	/*
 	 * vma_merge_new_range() calls khugepaged_enter_vma() too, the below
mm/vma_internal.h +1 -0
index 4f73f0a..71b552b 100644
@@ -23,6 +23,7 @@
 #include <linux/khugepaged.h>
 #include <linux/list.h>
 #include <linux/maple_tree.h>
+#include <linux/memfd.h>
 #include <linux/mempolicy.h>
 #include <linux/mm.h>
 #include <linux/mm_inline.h>
tools/testing/vma/include/stubs.h +11 -0
index 9f5b8e9..e15d903 100644
@@ -441,6 +441,17 @@ static inline void mapping_unmap_writable(struct address_space *mapping)
 {
 }
 
+static inline bool backing_file_is_readonly(const struct file *file)
+{
+	return false;
+}
+
+static inline int memfd_check_seals_mmap(struct file *file,
+					 vma_flags_t *vma_flags_ptr)
+{
+	return 0;
+}
+
 static inline void flush_dcache_mmap_lock(struct address_space *mapping)
 {
 }

base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790

Signature · 1 line
2.56.1.911.g883d8bcb20-goog
#1 lore raw
Quoted 3 lines · Dominik Maier

do_mmap() and mmap_region() check memfd write seals, FMODE_WRITE, and mapping_map_writable() on the outer file before calling ->mmap(). When backing_file_mmap() switches vma->vm_file onto a backing file,

Ehm, what?

Lorenzo am I daydreaming or is that completely shaky?

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 3 lines · Dominik Maier

do_mmap() and mmap_region() check memfd write seals, FMODE_WRITE, and mapping_map_writable() on the outer file before calling ->mmap(). When backing_file_mmap() switches vma->vm_file onto a backing file,

Ehm, what?

Lorenzo am I daydreaming or is that completely shaky?

Yeah sorry Dominik, this is making changes to core incorrectly, mixing and matching a bunch of things.

The problem does seem genuine and local claude confirms that, but it needs a significant rework.

Given workload the simplest thing is for me to take this over and address all that, I will obviously give you proper Reported-by/Closes credit.

Thanks for reporting it!

--
Cheers, Lorenzo

maintainer
#3 lore raw
Quoted 11 lines · Lorenzo Stoakes (ARM)
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 3 lines · Dominik Maier

do_mmap() and mmap_region() check memfd write seals, FMODE_WRITE, and mapping_map_writable() on the outer file before calling ->mmap(). When backing_file_mmap() switches vma->vm_file onto a backing file,

Ehm, what?

Lorenzo am I daydreaming or is that completely shaky?

Quoted 7 lines

Yeah sorry Dominik, this is making changes to core incorrectly, mixing and matching a bunch of things.

The problem does seem genuine and local claude confirms that, but it needs a significant rework.

Given workload the simplest thing is for me to take this over and address all

Lorenzo,

Do feel free to address anything you feel like in mm, but fwiw, I believe the simplest thing is to not allow this from the start. Dominic, please verify the attached patch.

Note that Christian has already queued a similar patch for banning pseudo fs passthrough files [1].

Miklos, do you concur?

Thanks,
Amir.

P.S. I intentionally placed the gate outside the S_IFMT switch to future-proof non-regular passthrough file types.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

From: Amir Goldstein <[email protected]>
Subject: [PATCH] fuse: refuse kernel-internal files as passthrough backing files

The fuse server can register a regular file on a filesystem that only the kernel mounts, such as a memfd, as a backing file. Such files were never meant to be passthrough backing files, and some come with semantics of their own, such as memfd seals, which passthrough does not respect.

Refuse files on filesystems mounted with SB_KERNMOUNT, as is done for files on pseudo filesystems.

A fuse server that wants to keep the data of its backing files in memory can create them with O_TMPFILE on a tmpfs that it mounts. Such files cannot be sealed.

Fixes 44350256ab94 ("fuse: implement ioctls to manage backing files")
---
 fs/fuse/backing.c | 3 +++
 1 file changed, 3 insertions(+)
fs/fuse/backing.c +3 -0
index 4e4fb810ddaa8..287a4b6e18526 100644
@@ -138,6 +138,9 @@ static int fuse_backing_open_file(struct fuse_conn *fc, struct fuse_backing *fb,
 	struct dax_device *daxdev;
 	int err;
 
+	if (inode->i_sb->s_flags & SB_KERNMOUNT)
+		return -EINVAL;
+
 	switch (inode->i_mode & S_IFMT) {
 	case S_IFREG:
 		/* TODO: relax CAP_SYS_ADMIN once backing files are visible to lsof */
#4 lore raw
Quoted 18 lines · Amir Goldstein
Quoted 17 lines · Lorenzo Stoakes (ARM)
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 3 lines · Dominik Maier

do_mmap() and mmap_region() check memfd write seals, FMODE_WRITE, and mapping_map_writable() on the outer file before calling ->mmap(). When backing_file_mmap() switches vma->vm_file onto a backing file,

Ehm, what?

Lorenzo am I daydreaming or is that completely shaky?

Yeah sorry Dominik, this is making changes to core incorrectly, mixing and matching a bunch of things.

The problem does seem genuine and local claude confirms that, but it needs a significant rework.

Quoted 7 lines
Quoted 1 line

Given workload the simplest thing is for me to take this over and address all

Lorenzo,

Do feel free to address anything you feel like in mm, but fwiw, I believe the simplest thing is to not allow this from the start. Dominic, please verify the attached patch.

Oh perfect, this would be far more preferable thanks!

This patch did strike me as fixing the issue in the wrong place.

So if we could just get fuse to not even possibly do the wrong thing then this resolves things beautifully :)

Quoted 3 lines

Note that Christian has already queued a similar patch for banning pseudo fs passthrough files [1].

Lovely!

Quoted 10 lines

Miklos, do you concur?

Thanks,
Amir.

P.S. I intentionally placed the gate outside the S_IFMT switch to future-proof non-regular passthrough file types.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/

--
Cheers, Lorenzo