Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] backing-file: enforce write seals and writable mapping in mmap
@ 2026-12-03  2:04 Dominik Maier
  2026-12-03  8:33 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 5+ messages in thread
From: Dominik Maier @ 2026-12-03  2:04 UTC (permalink / raw)
  To: Miklos Szeredi, Amir Goldstein, Alexander Viro, Christian Brauner,
	Andrew Morton, David Hildenbrand, Liam R . Howlett,
	Lorenzo Stoakes
  Cc: linux-fsdevel, linux-unionfs, linux-mm, dmnk+artist

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")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
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(-)

diff --git a/fs/backing-file.c b/fs/backing-file.c
index c601b27..f0547d1 100644
--- a/fs/backing-file.c
+++ b/fs/backing-file.c
@@ -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) {
diff --git a/mm/internal.h b/mm/internal.h
index 96cb55d..3a8bea6 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -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);
 
diff --git a/mm/vma.c b/mm/vma.c
index c82e930..7a240d0 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -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
diff --git a/mm/vma_internal.h b/mm/vma_internal.h
index 4f73f0a..71b552b 100644
--- a/mm/vma_internal.h
+++ b/mm/vma_internal.h
@@ -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>
diff --git a/tools/testing/vma/include/stubs.h b/tools/testing/vma/include/stubs.h
index 9f5b8e9..e15d903 100644
--- a/tools/testing/vma/include/stubs.h
+++ b/tools/testing/vma/include/stubs.h
@@ -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
-- 
2.56.1.911.g883d8bcb20-goog



^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-03 12:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  2:04 [PATCH] backing-file: enforce write seals and writable mapping in mmap Dominik Maier
2026-12-03  8:33 ` David Hildenbrand (Arm)
2026-12-03  9:16   ` Lorenzo Stoakes (ARM)
2026-12-03 10:16     ` Amir Goldstein
2026-12-03 12:14       ` Lorenzo Stoakes (ARM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox