`memfd_tripwire()` creates a file descriptor referring to a memory region that generates poll notifications when it is written. This works by installing read-only mappings. Write accesses then trigger a fault and invoke the `page_mkwrite()` handler, which queues a POLLIN event and allows the PTE to be made writable. After observing the POLLIN, the consumer is expected to invoke the `MEMFD_TRIPWIRE_ACK` ioctl, which write-protects the mappings again to re-arm the mechanism. It also guarantees that previous writes are visible to the consumer. Signed-off-by: Mattias Nissler Assisted-by: Claude:claude-opus-4-6 --- arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/memfd_tripwire.h | 19 ++ kernel/sys_ni.c | 3 + mm/Kconfig | 9 + mm/Makefile | 1 + mm/memfd_tripwire.c | 246 +++++++++++++++++++++++++ scripts/syscall.tbl | 1 + 9 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/memfd_tripwire.h create mode 100644 mm/memfd_tripwire.c diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 524155d655da1..d93ace9385fd0 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -396,6 +396,7 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common memfd_tripwire sys_memfd_tripwire # # Due to a historical design error, certain syscalls are numbered differently diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 4fb7291f54b62..52ea6d808f96d 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -940,6 +940,7 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags, asmlinkage long sys_getrandom(char __user *buf, size_t count, unsigned int flags); asmlinkage long sys_memfd_create(const char __user *uname_ptr, unsigned int flags); +asmlinkage long sys_memfd_tripwire(unsigned int flags); asmlinkage long sys_bpf(int cmd, union bpf_attr __user *attr, unsigned int size); asmlinkage long sys_execveat(int dfd, const char __user *filename, const char __user *const __user *argv, diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index a627acc8fb5fe..38825b1c59cef 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -863,8 +863,11 @@ __SYSCALL(__NR_listns, sys_listns) #define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#define __NR_memfd_tripwire 472 +__SYSCALL(__NR_memfd_tripwire, sys_memfd_tripwire) + #undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 473 /* * 32 bit systems traditionally used different diff --git a/include/uapi/linux/memfd_tripwire.h b/include/uapi/linux/memfd_tripwire.h new file mode 100644 index 0000000000000..478599ba2f813 --- /dev/null +++ b/include/uapi/linux/memfd_tripwire.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_MEMFD_TRIPWIRE_H +#define _UAPI_LINUX_MEMFD_TRIPWIRE_H + +#include + +#define MEMFD_TRIPWIRE_IOC 0xDA + +/* + * MEMFD_TRIPWIRE_ACK serves two purpose: First, it re-arms the mechanism to + * make sure future write activity triggers a POLLIN notification. Second, it + * makes sure that all writes up to the ACK are visible to the calling process. + * It is also guaranteed that no writes can sneak through unnoticed, i.e. after + * ACK concurrent writes have either taken place and are visible to the + * consumer, or will generate subsequent POLLIN events. + */ +#define MEMFD_TRIPWIRE_ACK _IO(MEMFD_TRIPWIRE_IOC, 0x00) + +#endif /* _UAPI_LINUX_MEMFD_TRIPWIRE_H */ diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index add3032da16f5..6bcf8658b4ff5 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -270,6 +270,9 @@ COND_SYSCALL(pkey_free); /* memfd_secret */ COND_SYSCALL(memfd_secret); +/* memfd_tripwire */ +COND_SYSCALL(memfd_tripwire); + /* * Architecture specific weak syscall entries. */ diff --git a/mm/Kconfig b/mm/Kconfig index e8bf1e9e6ad90..c7c1c2e69d37b 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1323,6 +1323,15 @@ config SECRETMEM memory areas visible only in the context of the owning process and not mapped to other processes and other kernel page tables. +config MEMFD_TRIPWIRE + bool "Enable memfd_tripwire() system call" + depends on MMU + help + Enable the memfd_tripwire() system call, which creates an anonymous + shared memory region with built-in write notification support. The + returned file descriptor supports poll() for detecting writes to the + mapped memory and an ioctl for re-arming notifications. + config ANON_VMA_NAME bool "Anonymous VMA name support" depends on PROC_FS && ADVISE_SYSCALLS && MMU diff --git a/mm/Makefile b/mm/Makefile index 8ad2ab08244eb..658443dbcb28d 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -141,6 +141,7 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_MEMFD_TRIPWIRE) += memfd_tripwire.o obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o diff --git a/mm/memfd_tripwire.c b/mm/memfd_tripwire.c new file mode 100644 index 0000000000000..52a27507b2d39 --- /dev/null +++ b/mm/memfd_tripwire.c @@ -0,0 +1,246 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * memfd_tripwire - Memory file descriptor with write notification support. + * + * Creates an anonymous memory-backed file descriptor that supports polling for + * detecting writes to its mappings. + * + * Theory of operation: + * 1. memfd_tripwire() to create a new instance + * 2. Pass the fd / mapping to the (possibly out-of-process) producer + * 3a. The producer uses the memory region as normal memory. Writes stick, but + * generate a notification as a side effect. + * 3b. The consumer polls the file descriptor: + * a. POLLIN is observed when the memory region gets written + * b. Call ioctl(MEMFD_TRIPWIRE_ACK) to restore write protection + * c. Inspect memory contents and react as appropriate + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +struct tripwire_info { + atomic_t dirty; + wait_queue_head_t wqh; +}; + +static struct tripwire_info *to_tripwire_info(struct file *file) +{ + return file->private_data; +} + +static vm_fault_t tripwire_fault(struct vm_fault *vmf) +{ + struct address_space *mapping = vmf->vma->vm_file->f_mapping; + struct inode *inode = file_inode(vmf->vma->vm_file); + pgoff_t offset = vmf->pgoff; + gfp_t gfp = vmf->gfp_mask; + struct folio *folio; + int err; + + if (((loff_t)offset << PAGE_SHIFT) >= i_size_read(inode)) + return vmf_error(-EINVAL); + +retry: + folio = filemap_lock_folio(mapping, offset); + if (IS_ERR(folio)) { + folio = folio_alloc(gfp | __GFP_ZERO, 0); + if (!folio) + return VM_FAULT_OOM; + + __folio_mark_uptodate(folio); + err = filemap_add_folio(mapping, folio, offset, gfp); + if (unlikely(err)) { + folio_put(folio); + if (err == -EEXIST) + goto retry; + return vmf_error(err); + } + } + + vmf->page = folio_file_page(folio, offset); + return VM_FAULT_LOCKED; +} + +static vm_fault_t tripwire_page_mkwrite(struct vm_fault *vmf) +{ + struct tripwire_info *info = to_tripwire_info(vmf->vma->vm_file); + struct folio *folio = page_folio(vmf->page); + + /* + * Note that this might be racing with a concurrent ACK. We need to + * guarantee that the dirty flag and page protection state remains + * consistent and that no notifications are lost. + * + * The actual update to mark the PTE writable only happens after this + * function completes, so the window between here and PTE update must + * be protected against concurrent modifications by the ACK code path. + * Taking the folio lock before notifying consumers conveniently + * makes sure that ACKs can only complete after our PTE updates go + * through, preventing a situation where an interleaved ACK clears the + * dirty flag but we're still going ahead to mark the PTE writable. + */ + folio_lock(folio); + + if (atomic_cmpxchg(&info->dirty, 0, 1) == 0) + wake_up_poll(&info->wqh, EPOLLIN); + + if (folio->mapping != vmf->vma->vm_file->f_mapping) { + folio_unlock(folio); + return VM_FAULT_NOPAGE; + } + + return VM_FAULT_LOCKED; +} + +static const struct vm_operations_struct tripwire_vm_ops = { + .fault = tripwire_fault, + .page_mkwrite = tripwire_page_mkwrite, +}; + +static int tripwire_mmap_prepare(struct vm_area_desc *desc) +{ + file_accessed(desc->file); + desc->vm_ops = &tripwire_vm_ops; + return 0; +} + +static __poll_t tripwire_poll(struct file *file, poll_table *wait) +{ + struct tripwire_info *info = to_tripwire_info(file); + __poll_t mask = 0; + + poll_wait(file, &info->wqh, wait); + + if (atomic_read(&info->dirty)) + mask |= EPOLLIN | EPOLLRDNORM; + + return mask; +} + +static void tripwire_ack(struct file *file) +{ + struct tripwire_info *info = to_tripwire_info(file); + struct address_space *mapping = file->f_mapping; + struct folio_batch fbatch; + pgoff_t index = 0; + int i; + + /* + * Note that this flag update is not protected by the folio locks taken + * below. Hence a concurrent writer might sneak in and switch back to + * dirty state. That's OK though, since it merely results in a spurious + * notification. + */ + atomic_set(&info->dirty, 0); + + folio_batch_init(&fbatch); + while (filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { + for (i = 0; i < folio_batch_count(&fbatch); i++) { + folio_lock(fbatch.folios[i]); + folio_mkclean(fbatch.folios[i]); + folio_unlock(fbatch.folios[i]); + } + folio_batch_release(&fbatch); + cond_resched(); + } +} + +static long tripwire_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + switch (cmd) { + case MEMFD_TRIPWIRE_ACK: + if (arg != 0) + return -EINVAL; + tripwire_ack(file); + return 0; + default: + return -ENOTTY; + } +} + +static int tripwire_release(struct inode *inode, struct file *file) +{ + kfree(to_tripwire_info(file)); + return 0; +} + +static const struct file_operations tripwire_fops = { + .release = tripwire_release, + .mmap_prepare = tripwire_mmap_prepare, + .poll = tripwire_poll, + .unlocked_ioctl = tripwire_ioctl, +}; + +static const struct address_space_operations tripwire_aops = { + .dirty_folio = noop_dirty_folio, +}; + +static int tripwire_setattr(struct mnt_idmap *idmap, struct dentry *dentry, + struct iattr *iattr) +{ + struct inode *inode = d_inode(dentry); + unsigned int ia_valid = iattr->ia_valid; + int ret; + + filemap_invalidate_lock(inode->i_mapping); + + /* Allowing size to change only once avoids the need to unmap here. */ + if ((ia_valid & ATTR_SIZE) && inode->i_size) + ret = -EINVAL; + else + ret = simple_setattr(idmap, dentry, iattr); + + filemap_invalidate_unlock(inode->i_mapping); + + return ret; +} + +static const struct inode_operations tripwire_iops = { + .setattr = tripwire_setattr, +}; + +SYSCALL_DEFINE1(memfd_tripwire, unsigned int, flags) +{ + struct tripwire_info *info; + struct inode *inode; + struct file *file; + + if (flags != 0) + return -EINVAL; + + info = kzalloc_obj(struct tripwire_info); + if (!info) + return -ENOMEM; + + init_waitqueue_head(&info->wqh); + + file = anon_inode_create_getfile("memfd_tripwire", &tripwire_fops, info, + O_RDWR, NULL); + if (IS_ERR(file)) { + kfree(info); + return PTR_ERR(file); + } + + inode = file_inode(file); + inode->i_op = &tripwire_iops; + inode->i_mapping->a_ops = &tripwire_aops; + inode->i_mode |= S_IFREG; + inode->i_size = 0; + + return FD_ADD(O_CLOEXEC, file); +} diff --git a/scripts/syscall.tbl b/scripts/syscall.tbl index 7a42b32b65776..a2bd8cd809ee8 100644 --- a/scripts/syscall.tbl +++ b/scripts/syscall.tbl @@ -412,3 +412,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common memfd_tripwire sys_memfd_tripwire -- 2.52.0