From: Dominik Maier <dmnk@google.com>
To: Miklos Szeredi <miklos@szeredi.hu>,
Amir Goldstein <amir73il@gmail.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
"Liam R . Howlett" <liam@infradead.org>,
Lorenzo Stoakes <ljs@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org,
linux-mm@kvack.org, dmnk+artist@google.com
Subject: [PATCH] backing-file: enforce write seals and writable mapping in mmap
Date: Thu, 3 Dec 2026 02:04:16 +0000 [thread overview]
Message-ID: <20261203020416.3569911-1-dmnk@google.com> (raw)
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
next reply other threads:[~2026-12-03 2:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 2:04 Dominik Maier [this message]
2026-12-03 8:33 ` [PATCH] backing-file: enforce write seals and writable mapping in mmap David Hildenbrand (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203020416.3569911-1-dmnk@google.com \
--to=dmnk@google.com \
--cc=akpm@linux-foundation.org \
--cc=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=david@kernel.org \
--cc=dmnk+artist@google.com \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-unionfs@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=miklos@szeredi.hu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox