Re: [PATCH 0/2] mm/memfd: add F_SEAL_PUNCH_HOLE
From: Andy Lutomirski
Date: Fri Oct 30 2026 - 10:54:55 EST
On Fri, Oct 30, 2026 at 5:13 AM Jason Gunthorpe <jgg@xxxxxxxx> wrote:
>
> On Thu, Oct 29, 2026 at 07:11:25PM -0700, Andy Lutomirski wrote:
>
> > I can't shake the feeling that the problem here isn't so much that KHO
> > + iommofd doesn't like this but that the behavior in question is
> > nonsensical.
>
> It has been this way for ages, like David says if userspace shoot
> itself in the foot it should not be surprised by this illogical
> behavior..
>
I dunno, a couple weeks ago I tracked down a 20+-year-old bug, and now
it's fixed :)
> You can get the same nonsenscial thing with just mmap too, you don't
> even need a memfd.
I feel like there are multiple levels of footgun here.
mmap anonymous (or MAP_PRIVATE), pin the pages, munmap: okay, mmap
creates unnamed private memory, and munmapping is maybe like unlinking
a file -- if someone else is still accessing it, it's still there. In
fact, you can mmap, fork, unmap, then mmap in the same location, and
your child process still sees the original memory. It's not clear to
me that anything is wrong.
mmap anonymous, pin, MADV_DONTNEED: okay, this one is starting to be a
small footgun. But at least it's pointed at *your own feet* (unless
you've forked, which in which case maybe it's pointed at a related
process' feet). I would still argue that the kernel could notice that
it's false that I "DON'T NEED" the memory and fail or ignore the
request. And the kernel's pages are still anonymous.
mmap a named file or memfd, pin, truncate or punch hole: who says the
footgun is even pointed at my own feet? If I mmap a named file,
SCM_RIGHTS it to you, and you mmap it too, and then I punch a hole,
the rmap machinery had better invalidate your mapping along with mine
(and get the TLB flush right!), and it would be an unambiguous and
severe bug if this didn't happen correctly. After all, what you have
done is to assign a *logical range* within the file an address in your
address space, and nothing I do should change that fact (except,
sadly, truncate, which can give you a SIGBUS, but your vma still
logically refers to the file).
So why is the kernel exempt from keeping its own mappings coherent the
way it keeps user mappings coherent?
What if that memfd (or tmpfs file or whatever) is shared between
multiple processes that don't trust each other? One of them can cause
it to become incoherent from the perspective of the other one's use of
kernel APIs.
P.S. You can access the offending APIs from a VM guest using balloon
in typical implementations. If the host userspace does not strictly
enforce that the guest doesn't balloon memory that is not ordinary
host memory *and that is absolutely not in use for any sort of
kernel-side pinned-folio IO*, then the guest may be able to cause the
host kernel's or hardware's view of its memory to become incoherent.
Heck, host userspace could literally use a memfd to share memory
between the guest and a separate host process that implements a
device.
P.P.S. It's possible for there to be a legitimate security boundary
between user or guest code and a future version of *the same*
userspace or guest, via DRTM. Someone should build a guest that sets
up some form of host-kernel-pinned-folio IO (IOMMU or simply a
host-side emulated device that works this way), then
DONTNEED/REMOVE/hole-punch the memory, then do the DRTM dance and
attest to its state.
--Andy
--
Andy Lutomirski
AMA Capital Management, LLC