* [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
* Re: [PATCH] backing-file: enforce write seals and writable mapping in mmap
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)
0 siblings, 1 reply; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-03 8:33 UTC (permalink / raw)
To: Dominik Maier, Miklos Szeredi, Amir Goldstein, Alexander Viro,
Christian Brauner, Andrew Morton, Liam R . Howlett,
Lorenzo Stoakes
Cc: linux-fsdevel, linux-unionfs, linux-mm, dmnk+artist
On 12/3/26 03:04, Dominik Maier wrote:
> 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?
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] backing-file: enforce write seals and writable mapping in mmap
2026-12-03 8:33 ` David Hildenbrand (Arm)
@ 2026-12-03 9:16 ` Lorenzo Stoakes (ARM)
2026-12-03 10:16 ` Amir Goldstein
0 siblings, 1 reply; 5+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-03 9:16 UTC (permalink / raw)
To: Dominik Maier
Cc: David Hildenbrand (Arm), Miklos Szeredi, Amir Goldstein,
Alexander Viro, Christian Brauner, Andrew Morton,
Liam R . Howlett, linux-fsdevel, linux-unionfs, linux-mm,
dmnk+artist
On Thu, Dec 03, 2026 at 09:33:34AM +0100, David Hildenbrand (Arm) wrote:
> On 12/3/26 03:04, Dominik Maier wrote:
> > 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] backing-file: enforce write seals and writable mapping in mmap
2026-12-03 9:16 ` Lorenzo Stoakes (ARM)
@ 2026-12-03 10:16 ` Amir Goldstein
2026-12-03 12:14 ` Lorenzo Stoakes (ARM)
0 siblings, 1 reply; 5+ messages in thread
From: Amir Goldstein @ 2026-12-03 10:16 UTC (permalink / raw)
To: Lorenzo Stoakes (ARM), Miklos Szeredi
Cc: Dominik Maier, David Hildenbrand (Arm), Alexander Viro,
Christian Brauner, Andrew Morton, Liam R . Howlett, linux-fsdevel,
linux-unionfs, linux-mm, dmnk+artist
[-- Attachment #1: Type: text/plain, Size: 1375 bytes --]
On Thu, Dec 3, 2026 at 10:16 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
>
> On Thu, Dec 03, 2026 at 09:33:34AM +0100, David Hildenbrand (Arm) wrote:
> > On 12/3/26 03:04, Dominik Maier wrote:
> > > 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
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/20261110-work-loop-pseudo-v1-38-4c998976c6c7@kernel.org/
[-- Attachment #2: 0001-fuse-refuse-kernel-internal-files-as-passthrough-backing.patch --]
[-- Type: text/x-patch, Size: 1309 bytes --]
From: Amir Goldstein <amir73il@gmail.com>
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")
Cc: stable@vger.kernel.org
---
fs/fuse/backing.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/fuse/backing.c b/fs/fuse/backing.c
index 4e4fb810ddaa8..287a4b6e18526 100644
--- a/fs/fuse/backing.c
+++ b/fs/fuse/backing.c
@@ -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 */
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] backing-file: enforce write seals and writable mapping in mmap
2026-12-03 10:16 ` Amir Goldstein
@ 2026-12-03 12:14 ` Lorenzo Stoakes (ARM)
0 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-03 12:14 UTC (permalink / raw)
To: Amir Goldstein
Cc: Miklos Szeredi, Dominik Maier, David Hildenbrand (Arm),
Alexander Viro, Christian Brauner, Andrew Morton,
Liam R . Howlett, linux-fsdevel, linux-unionfs, linux-mm,
dmnk+artist
On Thu, Dec 03, 2026 at 11:16:45AM +0100, Amir Goldstein wrote:
> On Thu, Dec 3, 2026 at 10:16 AM Lorenzo Stoakes (ARM) <ljs@kernel.org> wrote:
> >
> > On Thu, Dec 03, 2026 at 09:33:34AM +0100, David Hildenbrand (Arm) wrote:
> > > On 12/3/26 03:04, Dominik Maier wrote:
> > > > 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
>
> 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 :)
>
> Note that Christian has already queued a similar patch for banning
> pseudo fs passthrough files [1].
Lovely!
>
> 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/20261110-work-loop-pseudo-v1-38-4c998976c6c7@kernel.org/
--
Cheers, Lorenzo
^ permalink raw reply [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