mm: introduce memfd_secret system call to create "secret" memory areas
Subsystems (16)
Mike Rapoport
maintainerv20 cover mm: introduce memfd_secret system call to create "secret" memory areas
From: Mike Rapoport <[email protected]>
Hi,
@Andrew, this is based on v5.13-rc1, I can rebase whatever way you prefer.
This is an implementation of "secret" mappings backed by a file descriptor.
The file descriptor backing secret memory mappings is created using a dedicated memfd_secret system call The desired protection mode for the memory is configured using flags parameter of the system call. The mmap() of the file descriptor created with memfd_secret() will create a "secret" memory mapping. The pages in that mapping will be marked as not present in the direct map and will be present only in the page table of the owning mm.
Although normally Linux userspace mappings are protected from other users, such secret mappings are useful for environments where a hostile tenant is trying to trick the kernel into giving them access to other tenants mappings.
It's designed to provide the following protections:
* Enhanced protection (in conjunction with all the other in-kernel attack prevention systems) against ROP attacks. Seceretmem makes "simple" ROP insufficient to perform exfiltration, which increases the required complexity of the attack. Along with other protections like the kernel stack size limit and address space layout randomization which make finding gadgets is really hard, absence of any in-kernel primitive for accessing secret memory means the one gadget ROP attack can't work. Since the only way to access secret memory is to reconstruct the missing mapping entry, the attacker has to recover the physical page and insert a PTE pointing to it in the kernel and then retrieve the contents. That takes at least three gadgets which is a level of difficulty beyond most standard attacks.
* Prevent cross-process secret userspace memory exposures. Once the secret memory is allocated, the user can't accidentally pass it into the kernel to be transmitted somewhere. The secreremem pages cannot be accessed via the direct map and they are disallowed in GUP.
* Harden against exploited kernel flaws. In order to access secretmem, a kernel-side attack would need to either walk the page tables and create new ones, or spawn a new privileged uiserspace process to perform secrets exfiltration using ptrace.
In the future the secret mappings may be used as a mean to protect guest memory in a virtual machine host.
For demonstration of secret memory usage we've created a userspace library
https://git.kernel.org/pub/scm/linux/kernel/git/jejb/secret-memory-preloader.git
that does two things: the first is act as a preloader for openssl to redirect all the OPENSSL_malloc calls to secret memory meaning any secret keys get automatically protected this way and the other thing it does is expose the API to the user who needs it. We anticipate that a lot of the use cases would be like the openssl one: many toolkits that deal with secret keys already have special handling for the memory to try to give them greater protection, so this would simply be pluggable into the toolkits without any need for user application modification.
Hiding secret memory mappings behind an anonymous file allows usage of the page cache for tracking pages allocated for the "secret" mappings as well as using address_space_operations for e.g. page migration callbacks.
The anonymous file may be also used implicitly, like hugetlb files, to implement mmap(MAP_SECRET) and use the secret memory areas with "native" mm ABIs in the future.
Removing of the pages from the direct map may cause its fragmentation on architectures that use large pages to map the physical memory which affects the system performance. However, the original Kconfig text for CONFIG_DIRECT_GBPAGES said that gigabyte pages in the direct map "... can improve the kernel's performance a tiny bit ..." (commit 00d1c5e05736 ("x86: add gbpages switches")) and the recent report [1] showed that "... although 1G mappings are a good default choice, there is no compelling evidence that it must be the only choice". Hence, it is sufficient to have secretmem disabled by default with the ability of a system administrator to enable it at boot time.
In addition, there is also a long term goal to improve management of the direct map.
[1] https://lore.kernel.org/linux-mm/[email protected]/
v20:
* Drop the patch that enable multi-page updates to the direct map, per David
* Drop the changes to /dev/mem, they anyway have no effect when CONFIG_STRICT_DEVMEM=y
* Add Acked-by and Reviewed-by tags
v19: https://lore.kernel.org/lkml/[email protected]
* block /dev/mem mmap access, per David
* disallow mmap/mprotect with PROT_EXEC, per Kees
* simplify return in page_is_secretmem(), per Matthew
* use unsigned int for syscall falgs, per Yury
v18: https://lore.kernel.org/lkml/[email protected]
* rebase on v5.12-rc1
* merge kfence fix into the original patch
* massage commit message of the patch introducing the memfd_secret syscall
v17: https://lore.kernel.org/lkml/[email protected]
* Remove pool of large pages backing secretmem allocations, per Michal Hocko
* Add secretmem pages to unevictable LRU, per Michal Hocko
* Use GFP_HIGHUSER as secretmem mapping mask, per Michal Hocko
* Make secretmem an opt-in feature that is disabled by default
v16: https://lore.kernel.org/lkml/[email protected]
* Fix memory leak intorduced in v15
* Clean the data left from previous page user before handing the page to the userspace
Older history:
v15: https://lore.kernel.org/lkml/[email protected]
v14: https://lore.kernel.org/lkml/[email protected]
v13: https://lore.kernel.org/lkml/[email protected]
v12: https://lore.kernel.org/lkml/[email protected]
v11: https://lore.kernel.org/lkml/[email protected]
v10: https://lore.kernel.org/lkml/[email protected]
v9: https://lore.kernel.org/lkml/[email protected]
v8: https://lore.kernel.org/lkml/[email protected]
v7: https://lore.kernel.org/lkml/[email protected]
v6: https://lore.kernel.org/lkml/[email protected]
v5: https://lore.kernel.org/lkml/[email protected]
v4: https://lore.kernel.org/lkml/[email protected]
v3: https://lore.kernel.org/lkml/[email protected]
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]
rfc-v2: https://lore.kernel.org/lkml/[email protected]/
rfc-v1: https://lore.kernel.org/lkml/20200130162340.GA14232@rapoport-lnx/
rfc-v0: https://lore.kernel.org/lkml/[email protected]/
Mike Rapoport (7):
mmap: make mlock_future_check() global
riscv/Kconfig: make direct map manipulation options depend on MMU
set_memory: allow querying whether set_direct_map_*() is actually
enabled
mm: introduce memfd_secret system call to create "secret" memory areas
PM: hibernate: disable when there are active secretmem users arch, mm: wire up memfd_secret system call where relevant
secretmem: test: add basic selftest for memfd_secret(2)
arch/arm64/include/asm/Kbuild | 1 - arch/arm64/include/asm/cacheflush.h | 6 - arch/arm64/include/asm/kfence.h | 2 +- arch/arm64/include/asm/set_memory.h | 17 ++ arch/arm64/include/uapi/asm/unistd.h | 1 + arch/arm64/kernel/machine_kexec.c | 1 + arch/arm64/mm/mmu.c | 6 +- arch/arm64/mm/pageattr.c | 13 +- arch/riscv/Kconfig | 4 +- arch/riscv/include/asm/unistd.h | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/secretmem.h | 54 ++++ include/linux/set_memory.h | 12 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 7 +- include/uapi/linux/magic.h | 1 + kernel/power/hibernate.c | 5 +- kernel/sys_ni.c | 2 + mm/Kconfig | 5 + mm/Makefile | 1 + mm/gup.c | 12 + mm/internal.h | 3 + mm/mlock.c | 3 +- mm/mmap.c | 5 +- mm/secretmem.c | 254 +++++++++++++++++++ scripts/checksyscalls.sh | 4 + tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 3 +- tools/testing/selftests/vm/memfd_secret.c | 296 ++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests.sh | 17 ++ 31 files changed, 716 insertions(+), 24 deletions(-) create mode 100644 arch/arm64/include/asm/set_memory.h create mode 100644 include/linux/secretmem.h create mode 100644 mm/secretmem.c create mode 100644 tools/testing/selftests/vm/memfd_secret.c
base-commit: 6efb943b8616ec53a5e444193dccf1af9ad627b5
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 1/7 mmap: make mlock_future_check() global
From: Mike Rapoport <[email protected]>
It will be used by the upcoming secret memory implementation.
--- mm/internal.h | 3 +++ mm/mmap.c | 5 ++--- 2 files changed, 5 insertions(+), 3 deletions(-)
mm/internal.h +3 -0
@@ -373,6 +373,9 @@ static inline void munlock_vma_pages_all(struct vm_area_struct *vma)extern void mlock_vma_page (struct page *page);extern unsigned int munlock_vma_page (struct page *page); +extern int mlock_future_check (struct mm_struct *mm,unsigned long flags, +unsigned long len); +/* * Clear the page's PageMlocked(). This can be useful in a situation where * we want to unconditionally remove a page from the pagecache -- e.g.,
mm/mmap.c +2 -3
@@ -1352,9 +1352,8 @@ static inline unsigned long round_hint_to_min(unsigned long hint)return hint; } -static inline int mlock_future_check (struct mm_struct *mm, -unsigned long flags, -unsigned long len) +int mlock_future_check (struct mm_struct *mm,unsigned long flags, +unsigned long len) {unsigned long locked, lock_limit;
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 2/7 riscv/Kconfig: make direct map manipulation options depend on MMU
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
ARCH_HAS_SET_DIRECT_MAP and ARCH_HAS_SET_MEMORY configuration options have no meaning when CONFIG_MMU is disabled and there is no point to enable them for the nommu case.
Add an explicit dependency on MMU for these options.
--- arch/riscv/Kconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/Kconfig +2 -2
@@ -26,8 +26,8 @@ config RISCV select ARCH_HAS_KCOV select ARCH_HAS_MMIOWB select ARCH_HAS_PTE_SPECIAL - select ARCH_HAS_SET_DIRECT_MAP - select ARCH_HAS_SET_MEMORY + select ARCH_HAS_SET_DIRECT_MAP if MMU + select ARCH_HAS_SET_MEMORY if MMU select ARCH_HAS_STRICT_KERNEL_RWX if MMU && !XIP_KERNEL select ARCH_HAS_STRICT_MODULE_RWX if MMU && !XIP_KERNEL select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 3/7 set_memory: allow querying whether set_direct_map_*() is actually enabled
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
On arm64, set_direct_map_*() functions may return 0 without actually changing the linear map. This behaviour can be controlled using kernel parameters, so we need a way to determine at runtime whether calls to set_direct_map_invalid_noflush() and set_direct_map_default_noflush() have any effect.
Extend set_memory API with can_set_direct_map() function that allows checking if calling set_direct_map_*() will actually change the page table, replace several occurrences of open coded checks in arm64 with the new function and provide a generic stub for architectures that always modify page tables upon calls to set_direct_map APIs.
[[email protected]: arm64: kfence: fix header inclusion ]
--- arch/arm64/include/asm/Kbuild | 1 - arch/arm64/include/asm/cacheflush.h | 6 ------ arch/arm64/include/asm/kfence.h | 2 +- arch/arm64/include/asm/set_memory.h | 17 +++++++++++++++++ arch/arm64/kernel/machine_kexec.c | 1 + arch/arm64/mm/mmu.c | 6 +++--- arch/arm64/mm/pageattr.c | 13 +++++++++---- include/linux/set_memory.h | 12 ++++++++++++ 8 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 arch/arm64/include/asm/set_memory.h
arch/arm64/include/asm/Kbuild +0 -1
@@ -3,5 +3,4 @@ generic-y += early_ioremap.h generic-y += mcs_spinlock.h generic-y += qrwlock.h generic-y += qspinlock.h -generic-y += set_memory.h generic-y += user.h
arch/arm64/include/asm/cacheflush.h +0 -6
@@ -131,12 +131,6 @@ static __always_inline void __flush_icache_all(void) dsb(ish); } -int set_memory_valid (unsigned long addr,int numpages,int enable); - -int set_direct_map_invalid_noflush (struct page *page); -int set_direct_map_default_noflush (struct page *page); -bool kernel_page_present (struct page *page); -#include <asm-generic/cacheflush.h> #endif /* __ASM_CACHEFLUSH_H */
arch/arm64/include/asm/kfence.h +1 -1
@@ -8,7 +8,7 @@#ifndef __ASM_KFENCE_H#define __ASM_KFENCE_H -#include <asm/cacheflush.h> +#include <asm/set_memory.h> static inline bool arch_kfence_init_pool (void ) {return true; }
arch/arm64/include/asm/set_memory.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_ARM64_SET_MEMORY_H +#define _ASM_ARM64_SET_MEMORY_H + +#include <asm-generic/set_memory.h> + +bool can_set_direct_map (void ); +#define can_set_direct_map can_set_direct_map + +int set_memory_valid (unsigned long addr,int numpages,int enable); + +int set_direct_map_invalid_noflush (struct page *page); +int set_direct_map_default_noflush (struct page *page); +bool kernel_page_present (struct page *page); + +#endif /* _ASM_ARM64_SET_MEMORY_H */
arch/arm64/kernel/machine_kexec.c +1 -0
@@ -11,6 +11,7 @@#include <linux/kernel.h> #include <linux/kexec.h> #include <linux/page-flags.h> +#include <linux/set_memory.h> #include <linux/smp.h> #include <asm/cacheflush.h>
arch/arm64/mm/mmu.c +3 -3
@@ -22,6 +22,7 @@#include <linux/io.h> #include <linux/mm.h> #include <linux/vmalloc.h> +#include <linux/set_memory.h> #include <asm/barrier.h> #include <asm/cputype.h>
@@ -515,7 +516,7 @@ static void __init map_mem(pgd_t *pgdp)*/ BUILD_BUG_ON (pgd_index (direct_map_end -1 ) ==pgd_index (direct_map_end)); -if (rodata_full || crash_mem_map ||debug_pagealloc_enabled ()) +if (can_set_direct_map () || crash_mem_map) flags |=NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ;/*
@@ -1483,8 +1484,7 @@ int arch_add_memory(int nid, u64 start, u64 size,* KFENCE requires linear map to be mapped at page granularity, so that * it is possible to protect/unprotect single pages in the KFENCE pool. */ -if (rodata_full ||debug_pagealloc_enabled () || -IS_ENABLED (CONFIG_KFENCE )) +if (can_set_direct_map () ||IS_ENABLED (CONFIG_KFENCE )) flags |=NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ; __create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
arch/arm64/mm/pageattr.c +9 -4
@@ -19,6 +19,11 @@ struct page_change_data {bool rodata_full__ro_after_init =IS_ENABLED (CONFIG_RODATA_FULL_DEFAULT_ENABLED ); +bool can_set_direct_map (void ) +{ +return rodata_full ||debug_pagealloc_enabled (); +} +static int change_page_range (pte_t *ptep,unsigned long addr,void *data) {struct page_change_data *cdata = data;
@@ -155,7 +160,7 @@ int set_direct_map_invalid_noflush(struct page *page) .clear_mask = __pgprot(PTE_VALID), }; -if (!debug_pagealloc_enabled () && !rodata_full) +if (!can_set_direct_map ())return 0 ; return apply_to_page_range(&init_mm,
@@ -170,7 +175,7 @@ int set_direct_map_default_noflush(struct page *page) .clear_mask = __pgprot(PTE_RDONLY), }; -if (!debug_pagealloc_enabled () && !rodata_full) +if (!can_set_direct_map ())return 0 ; return apply_to_page_range(&init_mm,
@@ -181,7 +186,7 @@ int set_direct_map_default_noflush(struct page *page)#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) { -if (!debug_pagealloc_enabled () && !rodata_full) +if (!can_set_direct_map ())return ;set_memory_valid ((unsigned long )page_address (page), numpages, enable);
@@ -206,7 +211,7 @@ bool kernel_page_present(struct page *page)pte_t *ptep;unsigned long addr = (unsigned long )page_address (page); -if (!debug_pagealloc_enabled () && !rodata_full) +if (!can_set_direct_map ())return true; pgdp =pgd_offset_k (addr);
include/linux/set_memory.h +12 -0
@@ -28,7 +28,19 @@ static inline bool kernel_page_present(struct page *page) {return true; } +#else /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +/* +* Some architectures, e.g. ARM64 can disable direct map modifications at +* boot time. Let them overrive this query. +*/ +#ifndef can_set_direct_map +static inline bool can_set_direct_map (void ) +{ +return true; +} +#define can_set_direct_map can_set_direct_map#endif +#endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ #ifndef set_mce_nospecstatic inline int set_mce_nospec (unsigned long pfn,bool unmap)
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 4/7 mm: introduce memfd_secret system call to create "secret" memory areas
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
Introduce "memfd_secret" system call with the ability to create memory areas visible only in the context of the owning process and not mapped not only to other processes but in the kernel page tables as well.
The secretmem feature is off by default and the user must explicitly enable it at the boot time.
Once secretmem is enabled, the user will be able to create a file descriptor using the memfd_secret() system call. The memory areas created by mmap() calls from this file descriptor will be unmapped from the kernel direct map and they will be only mapped in the page table of the processes that have access to the file descriptor.
Secretmem is designed to provide the following protections:
* Enhanced protection (in conjunction with all the other in-kernel attack prevention systems) against ROP attacks. Seceretmem makes "simple" ROP insufficient to perform exfiltration, which increases the required complexity of the attack. Along with other protections like the kernel stack size limit and address space layout randomization which make finding gadgets is really hard, absence of any in-kernel primitive for accessing secret memory means the one gadget ROP attack can't work. Since the only way to access secret memory is to reconstruct the missing mapping entry, the attacker has to recover the physical page and insert a PTE pointing to it in the kernel and then retrieve the contents. That takes at least three gadgets which is a level of difficulty beyond most standard attacks.
* Prevent cross-process secret userspace memory exposures. Once the secret memory is allocated, the user can't accidentally pass it into the kernel to be transmitted somewhere. The secreremem pages cannot be accessed via the direct map and they are disallowed in GUP.
* Harden against exploited kernel flaws. In order to access secretmem, a kernel-side attack would need to either walk the page tables and create new ones, or spawn a new privileged uiserspace process to perform secrets exfiltration using ptrace.
The file descriptor based memory has several advantages over the "traditional" mm interfaces, such as mlock(), mprotect(), madvise(). File descriptor approach allows explicit and controlled sharing of the memory areas, it allows to seal the operations. Besides, file descriptor based memory paves the way for VMMs to remove the secret memory range from the userspace hipervisor process, for instance QEMU. Andy Lutomirski says:
"Getting fd-backed memory into a guest will take some possibly major work
in the kernel, but getting vma-backed memory into a guest without mapping it in the host user address space seems much, much worse."
memfd_secret() is made a dedicated system call rather than an extension to memfd_create() because it's purpose is to allow the user to create more secure memory mappings rather than to simply allow file based access to the memory. Nowadays a new system call cost is negligible while it is way simpler for userspace to deal with a clear-cut system calls than with a multiplexer or an overloaded syscall. Moreover, the initial implementation of memfd_secret() is completely distinct from memfd_create() so there is no much sense in overloading memfd_create() to begin with. If there will be a need for code sharing between these implementation it can be easily achieved without a need to adjust user visible APIs.
The secret memory remains accessible in the process context using uaccess primitives, but it is not exposed to the kernel otherwise; secret memory areas are removed from the direct map and functions in the follow_page()/get_user_page() family will refuse to return a page that belongs to the secret memory area.
Once there will be a use case that will require exposing secretmem to the kernel it will be an opt-in request in the system call flags so that user would have to decide what data can be exposed to the kernel.
Removing of the pages from the direct map may cause its fragmentation on architectures that use large pages to map the physical memory which affects the system performance. However, the original Kconfig text for CONFIG_DIRECT_GBPAGES said that gigabyte pages in the direct map "... can improve the kernel's performance a tiny bit ..." (commit 00d1c5e05736 ("x86: add gbpages switches")) and the recent report [1] showed that "... although 1G mappings are a good default choice, there is no compelling evidence that it must be the only choice". Hence, it is sufficient to have secretmem disabled by default with the ability of a system administrator to enable it at boot time.
Pages in the secretmem regions are unevictable and unmovable to avoid accidental exposure of the sensitive data via swap or during page migration.
Since the secretmem mappings are locked in memory they cannot exceed RLIMIT_MEMLOCK. Since these mappings are already locked independently from mlock(), an attempt to mlock()/munlock() secretmem range would fail and mlockall()/munlockall() will ignore secretmem mappings.
However, unlike mlock()ed memory, secretmem currently behaves more like long-term GUP: secretmem mappings are unmovable mappings directly consumed by user space. With default limits, there is no excessive use of secretmem and it poses no real problem in combination with ZONE_MOVABLE/CMA, but in the future this should be addressed to allow balanced use of large amounts of secretmem along with ZONE_MOVABLE/CMA.
A page that was a part of the secret memory area is cleared when it is freed to ensure the data is not exposed to the next user of that page.
The following example demonstrates creation of a secret mapping (error handling is omitted):
fd = memfd_secret(0); ftruncate(fd, MAP_SIZE); ptr = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
[1] https://lore.kernel.org/linux-mm/[email protected]/
--- include/linux/secretmem.h | 48 ++++++++ include/uapi/linux/magic.h | 1 + kernel/sys_ni.c | 2 + mm/Kconfig | 5 + mm/Makefile | 1 + mm/gup.c | 12 ++ mm/mlock.c | 3 +- mm/secretmem.c | 239 +++++++++++++++++++++++++++++++++++++ 8 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 include/linux/secretmem.h create mode 100644 mm/secretmem.c
include/linux/secretmem.h new file +48 -0
@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _LINUX_SECRETMEM_H +#define _LINUX_SECRETMEM_H + +#ifdef CONFIG_SECRETMEM + +extern const struct address_space_operations secretmem_aops; + +static inline bool page_is_secretmem (struct page *page) +{ +struct address_space *mapping; + +/* +* Using page_mapping() is quite slow because of the actual call +* instruction and repeated compound_head(page) inside the +* page_mapping() function. +* We know that secretmem pages are not compound and LRU so we can +* save a couple of cycles here. +*/ +if (PageCompound (page) || !PageLRU (page)) +return false; + + mapping = (struct address_space *) + ((unsigned long )page->mapping & ~PAGE_MAPPING_FLAGS ); + +if (mapping != page->mapping) +return false; + +return mapping->a_ops == &secretmem_aops; +} + +bool vma_is_secretmem (struct vm_area_struct *vma); + +#else + +static inline bool vma_is_secretmem (struct vm_area_struct *vma) +{ +return false; +} + +static inline bool page_is_secretmem (struct page *page) +{ +return false; +} + +#endif /* CONFIG_SECRETMEM */ + +#endif /* _LINUX_SECRETMEM_H */
include/uapi/linux/magic.h +1 -0
@@ -97,5 +97,6 @@#define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define Z3FOLD_MAGIC 0x33#define PPC_CMM_MAGIC 0xc7571590 +#define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ #endif /* __LINUX_MAGIC_H__ */
kernel/sys_ni.c +2 -0
@@ -358,6 +358,8 @@ COND_SYSCALL(pkey_mprotect);COND_SYSCALL (pkey_alloc);COND_SYSCALL (pkey_free); +/* memfd_secret */ +COND_SYSCALL (memfd_secret);/* * Architecture specific weak syscall entries.
mm/Kconfig +5 -0
@@ -901,4 +901,9 @@ config KMAP_LOCAL # struct io_mapping based helper. Selected by drivers that need them config IO_MAPPING bool + +config SECRETMEM + def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED + select STRICT_DEVMEM + endmenu
mm/Makefile +1 -0
@@ -125,3 +125,4 @@ obj-$(CONFIG_MAPPING_DIRTY_HELPERS) += mapping_dirty_helpers.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o obj-$(CONFIG_IO_MAPPING) += io-mapping.o +obj-$(CONFIG_SECRETMEM) += secretmem.o
mm/gup.c +12 -0
@@ -10,6 +10,7 @@#include <linux/rmap.h> #include <linux/swap.h> #include <linux/swapops.h> +#include <linux/secretmem.h> #include <linux/sched/signal.h> #include <linux/rwsem.h>
@@ -816,6 +817,9 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address,struct follow_page_context ctx = {NULL };struct page *page; +if (vma_is_secretmem (vma)) +return NULL ; + page =follow_page_mask (vma, address, foll_flags, &ctx);if (ctx.pgmap)put_dev_pagemap (ctx.pgmap);
@@ -949,6 +953,9 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)if ((gup_flags &FOLL_LONGTERM ) &&vma_is_fsdax (vma))return -EOPNOTSUPP ; +if (vma_is_secretmem (vma)) +return -EFAULT ; +if (write) {if (!(vm_flags &VM_WRITE )) {if (!(gup_flags &FOLL_FORCE ))
@@ -2077,6 +2084,11 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,if (!head) goto pte_unmap; +if (unlikely (page_is_secretmem (page))) { +put_compound_head (head,1 , flags); + goto pte_unmap; + } +if (unlikely (pte_val (pte) !=pte_val (*ptep))) {put_compound_head (head,1 , flags); goto pte_unmap;
mm/mlock.c +2 -1
@@ -23,6 +23,7 @@#include <linux/hugetlb.h> #include <linux/memcontrol.h> #include <linux/mm_inline.h> +#include <linux/secretmem.h> #include "internal.h"
@@ -503,7 +504,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev,if (newflags == vma->vm_flags || (vma->vm_flags &VM_SPECIAL ) ||is_vm_hugetlb_page (vma) || vma ==get_gate_vma (current->mm) || -vma_is_dax (vma)) +vma_is_dax (vma) ||vma_is_secretmem (vma))/* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out;
mm/secretmem.c new file +239 -0
239 lines not on this page · raw message
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 5/7 PM: hibernate: disable when there are active secretmem users
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
It is unsafe to allow saving of secretmem areas to the hibernation snapshot as they would be visible after the resume and this essentially will defeat the purpose of secret memory mappings.
Prevent hibernation whenever there are active secret memory users.
--- include/linux/secretmem.h | 6 ++++++ kernel/power/hibernate.c | 5 ++++- mm/secretmem.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-)
include/linux/secretmem.h +6 -0
@@ -30,6 +30,7 @@ static inline bool page_is_secretmem(struct page *page) }bool vma_is_secretmem (struct vm_area_struct *vma); +bool secretmem_active (void );#else
@@ -43,6 +44,11 @@ static inline bool page_is_secretmem(struct page *page)return false; } +static inline bool secretmem_active (void ) +{ +return false; +} +#endif /* CONFIG_SECRETMEM */ #endif /* _LINUX_SECRETMEM_H */
kernel/power/hibernate.c +4 -1
@@ -31,6 +31,7 @@#include <linux/genhd.h> #include <linux/ktime.h> #include <linux/security.h> +#include <linux/secretmem.h> #include <trace/events/power.h> #include "power.h"
@@ -81,7 +82,9 @@ void hibernate_release(void)bool hibernation_available (void ) { -return nohibernate ==0 && !security_locked_down (LOCKDOWN_HIBERNATION ); +return nohibernate ==0 && + !security_locked_down (LOCKDOWN_HIBERNATION ) && + !secretmem_active (); }/**
mm/secretmem.c +15 -0
@@ -40,6 +40,13 @@ module_param_named(enable, secretmem_enable, bool, 0400);MODULE_PARM_DESC (secretmem_enable,"Enable secretmem and memfd_secret(2) system call" ); +static atomic_t secretmem_users; + +bool secretmem_active (void ) +{ +return !!atomic_read (&secretmem_users); +} +static vm_fault_t secretmem_fault (struct vm_fault *vmf) {struct address_space *mapping = vmf->vma->vm_file->f_mapping;
@@ -94,6 +101,12 @@ static const struct vm_operations_struct secretmem_vm_ops = { .fault = secretmem_fault, }; +static int secretmem_release (struct inode *inode,struct file *file) +{ +atomic_dec (&secretmem_users); +return 0 ; +} +static int secretmem_mmap (struct file *file,struct vm_area_struct *vma) {unsigned long len = vma->vm_end - vma->vm_start;
@@ -116,6 +129,7 @@ bool vma_is_secretmem(struct vm_area_struct *vma) }static const struct file_operations secretmem_fops = { + .release = secretmem_release, .mmap = secretmem_mmap, };
@@ -202,6 +216,7 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags) file->f_flags |=O_LARGEFILE ;fd_install (fd, file); +atomic_inc (&secretmem_users);return fd; err_put_fd:
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 6/7 arch, mm: wire up memfd_secret system call where relevant
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
Wire up memfd_secret system call on architectures that define ARCH_HAS_SET_DIRECT_MAP, namely arm64, risc-v and x86.
--- arch/arm64/include/uapi/asm/unistd.h | 1 + arch/riscv/include/asm/unistd.h | 1 + arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 1 + include/uapi/asm-generic/unistd.h | 7 ++++++- scripts/checksyscalls.sh | 4 ++++ 7 files changed, 15 insertions(+), 1 deletion(-)
arch/arm64/include/uapi/asm/unistd.h +1 -0
@@ -20,5 +20,6 @@#define __ARCH_WANT_SET_GET_RLIMIT#define __ARCH_WANT_TIME32_SYSCALLS#define __ARCH_WANT_SYS_CLONE3 +#define __ARCH_WANT_MEMFD_SECRET#include <asm-generic/unistd.h>
arch/riscv/include/asm/unistd.h +1 -0
@@ -9,6 +9,7 @@*/ #define __ARCH_WANT_SYS_CLONE +#define __ARCH_WANT_MEMFD_SECRET#include <uapi/asm/unistd.h>
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
@@ -451,3 +451,4 @@ 444 i386 landlock_create_ruleset sys_landlock_create_ruleset 445 i386 landlock_add_rule sys_landlock_add_rule 446 i386 landlock_restrict_self sys_landlock_restrict_self +447 i386 memfd_secret sys_memfd_secret
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -368,6 +368,7 @@ 444 common landlock_create_ruleset sys_landlock_create_ruleset 445 common landlock_add_rule sys_landlock_add_rule 446 common landlock_restrict_self sys_landlock_restrict_self +447 common memfd_secret sys_memfd_secret # # Due to a historical design error, certain syscalls are numbered differently
include/linux/syscalls.h +1 -0
@@ -1050,6 +1050,7 @@ asmlinkage long sys_landlock_create_ruleset(const struct landlock_ruleset_attr _asmlinkage long sys_landlock_add_rule (int ruleset_fd,enum landlock_rule_type rule_type,const void __user *rule_attr,__u32 flags);asmlinkage long sys_landlock_restrict_self (int ruleset_fd,__u32 flags); +asmlinkage long sys_memfd_secret (unsigned int flags);/* * Architecture-specific system calls
include/uapi/asm-generic/unistd.h +6 -1
@@ -873,8 +873,13 @@ __SYSCALL(__NR_landlock_add_rule, sys_landlock_add_rule)#define __NR_landlock_restrict_self 446 __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self) +#ifdef __ARCH_WANT_MEMFD_SECRET +#define __NR_memfd_secret 447 +__SYSCALL(__NR_memfd_secret, sys_memfd_secret) +#endif +#undef __NR_syscalls -#define __NR_syscalls 447 +#define __NR_syscalls 448/* * 32 bit systems traditionally used different
scripts/checksyscalls.sh +4 -0
@@ -40,6 +40,10 @@ cat << EOF #define __IGNORE_setrlimit /* setrlimit */ #endif +#ifndef __ARCH_WANT_MEMFD_SECRET +#define __IGNORE_memfd_secret +#endif + /* Missing flags argument */ #define __IGNORE_renameat /* renameat2 */
Signature · 1 line
2.28.0
Mike Rapoport
maintainerPATCH v20 7/7 secretmem: test: add basic selftest for memfd_secret(2)
In reply to Mike RapoportFrom: Mike Rapoport <[email protected]>
The test verifies that file descriptor created with memfd_secret does not allow read/write operations, that secret memory mappings respect RLIMIT_MEMLOCK and that remote accesses with process_vm_read() and ptrace() to the secret memory fail.
--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 3 +- tools/testing/selftests/vm/memfd_secret.c | 296 ++++++++++++++++++++++ tools/testing/selftests/vm/run_vmtests.sh | 17 ++ 4 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/vm/memfd_secret.c
tools/testing/selftests/vm/.gitignore +1 -0
@@ -21,5 +21,6 @@ va_128TBswitch map_fixed_noreplace write_to_hugetlbfs hmm-tests +memfd_secret local_config.* split_huge_page_test
tools/testing/selftests/vm/Makefile +2 -1
@@ -34,6 +34,7 @@ TEST_GEN_FILES += khugepaged TEST_GEN_FILES += map_fixed_noreplace TEST_GEN_FILES += map_hugetlb TEST_GEN_FILES += map_populate +TEST_GEN_FILES += memfd_secret TEST_GEN_FILES += mlock-random-test TEST_GEN_FILES += mlock2-tests TEST_GEN_FILES += mremap_dontunmap
@@ -134,7 +135,7 @@ warn_32bit_failure: endif endif -$(OUTPUT)/mlock-random-test: LDLIBS += -lcap +$(OUTPUT)/mlock-random-test $(OUTPUT)/memfd_secret: LDLIBS += -lcap $(OUTPUT)/gup_test: ../../../../mm/gup_test.h
tools/testing/selftests/vm/memfd_secret.c new file +296 -0
296 lines not on this page · raw message
tools/testing/selftests/vm/run_vmtests.sh +17 -0
@@ -346,4 +346,21 @@ else exitcode=1 fi +echo "running memfd_secret test" +echo "------------------------------------" +./memfd_secret +ret_val=$? + +if [ $ret_val -eq 0 ]; then + echo "[PASS]" +elif [ $ret_val -eq $ksft_skip ]; then + echo "[SKIP]" + exitcode=$ksft_skip +else + echo "[FAIL]" + exitcode=1 +fi + +exit $exitcode + exit $exitcode
Signature · 1 line
2.28.0
Andrew Morton
maintainerQuoted 11 lines · On Tue, 18 May 2021 10:20:31 +0300 Mike Rapoport <[email protected]> wrote:
From: Mike Rapoport <[email protected]>
Introduce "
memfd_secret" system call with the ability to create memory areas visible only in the context of the owning process and not mapped not only to other processes but in the kernel page tables as well....
mm/Kconfig config KMAP_LOCAL · line 909
Quoted 9 lines
mm/Kconfig +5
-0@@ -901,4 +901,9 @@ config KMAP_LOCAL # struct io_mapping based helper. Selected by drivers that need them config IO_MAPPING bool + +config SECRETMEM + def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED + select STRICT_DEVMEM + endmenu
WARNING: unmet direct dependencies detected for STRICT_DEVMEM
Depends on [n]: MMU [=y] && DEVMEM [=n] && (ARCH_HAS_DEVMEM_IS_ALLOWED [=y] || GENERIC_LIB_DEVMEM_IS_ALLOWED [=n]) Selected by [y]:
- SECRETMEM [=y]
so I went back to the v19 version, with
mm/Kconfig renamed from mm/Kconfig~mm-introduce-memfd_secret-system-call-to-create-secret-memory-areas-fix +0 -1
@@ -907,6 +907,5 @@ config IO_MAPPING config SECRETMEM def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED - select STRICT_DEVMEM endmenu
_
Mike Rapoport
maintainerQuoted 28 lines · Andrew Morton
Quoted 11 lines · On Tue, 18 May 2021 10:20:31 +0300 Mike Rapoport <[email protected]> wrote:
From: Mike Rapoport <[email protected]>
Introduce "
memfd_secret" system call with the ability to create memory areas visible only in the context of the owning process and not mapped not only to other processes but in the kernel page tables as well....
mm/Kconfig config KMAP_LOCAL · line 909
Quoted 9 lines
mm/Kconfig +5
-0@@ -901,4 +901,9 @@ config KMAP_LOCAL # struct io_mapping based helper. Selected by drivers that need them config IO_MAPPING bool + +config SECRETMEM + def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED + select STRICT_DEVMEM + endmenuWARNING: unmet direct dependencies detected for
STRICT_DEVMEM
Depends on [n]: MMU [=y] && DEVMEM [=n] && (ARCH_HAS_DEVMEM_IS_ALLOWED[=y] ||GENERIC_LIB_DEVMEM_IS_ALLOWED[=n]) Selected by [y]:
- SECRETMEM [=y]
Quoted 1 line
so I went back to the v19 version, with
Ouch, sorry, I forgot to remove that hunk, v19 is the correct version.
Quoted 11 lines
mm/Kconfig renamed from mm/Kconfig~mm-introduce-memfd_secret-system-call-to-create-secret-memory-areas-fix +0
-1@@ -907,6 +907,5 @@ config IO_MAPPING config SECRETMEM def_bool ARCH_HAS_SET_DIRECT_MAP && !EMBEDDED - select STRICT_DEVMEM endmenu_
Signature · 2 lines
Sincerely yours, Mike.