add support for mm-local memory allocations
Subsystems (7)
cover add support for mm-local memory allocations
In a series posted a few years ago [1], a proposal was put forward to allow the kernel to allocate memory local to a mm and thus push it out of reach for current and future speculation-based cross-process attacks. We still believe this is a nice thing to have.
However, in the time passed since that post Linux mm has grown quite a few new goodies, so we'd like to explore possibilities to implement this functionality with less effort and churn leveraging the now available facilities.
Specifically, this is a proof-of-concept attempt to implement mm-local allocations piggy-backing on memfd_secret(), using regular user addressess but pinning the pages and flipping the user/supervisor flag on the respective PTEs to make them directly accessible from kernel, and sealing the VMA to prevent userland from taking over the address range. The approach allowed to delegate all the heavy lifting -- address management, interactions with the direct map, cleanup on mm teardown -- to the existing infrastructure, and required zero architecture-specific code.
Compared to the approach used in the orignal series, where a dedicated kernel address range and thus a dedicated PGD was used for mm-local allocations, the one proposed here may have certain drawbacks, in particular
- using user addresses for kernel memory may violate assumptions in various parts of kernel code which we may not have identified with smoke tests we did
- the allocated addresses are guessable by the userland (ATM they are even visible in /proc/PID/maps but that's fixable) which may weaken the security posture
Also included is a simple test driver and selftest to smoke test and showcase the feature.
The code is PoC RFC and lacks a lot of checks and special case handling, but demonstrates the idea. We'd appreciate any feedback on whether it's a viable approach or it should better be abandoned in favor of the one with dedicated PGD / kernel address range or yet something else.
[1] https://lore.kernel.org/lkml/[email protected]/
Fares Mehanna (2):
mseal: expose interface to seal / unseal user memory ranges mm/secretmem: implement mm-local kernel allocations
Roman Kagan (1):
drivers/misc: add test driver and selftest for proclocal allocator
drivers/misc/Makefile | 1 + tools/testing/selftests/proclocal/Makefile | 6 + include/linux/secretmem.h | 8 + mm/internal.h | 7 + drivers/misc/proclocal-test.c | 200 +++++++++++++++++ mm/gup.c | 4 +- mm/mseal.c | 81 ++++--- mm/secretmem.c | 208 ++++++++++++++++++ .../selftests/proclocal/proclocal-test.c | 150 +++++++++++++ drivers/misc/Kconfig | 15 ++ tools/testing/selftests/proclocal/.gitignore | 1 + 11 files changed, 649 insertions(+), 32 deletions(-) create mode 100644 tools/testing/selftests/proclocal/Makefile create mode 100644 drivers/misc/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/.gitignore
Signature · 12 lines
2.34.1 Amazon Web Services Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 1/3 mseal: expose interface to seal / unseal user memory ranges
From: Fares Mehanna <[email protected]>
To make sure the kernel mm-local mapping is untouched by the user, we will seal the VMA before changing the protection to be used by the kernel.
This will guarantee that userspace can't unmap or alter this VMA while it is being used by the kernel.
After the kernel is done with the secret memory, it will unseal the VMA to be able to unmap and free it.
Unseal operation is not exposed to userspace.
--- mm/internal.h | 7 +++++ mm/mseal.c | 81 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 30 deletions(-)
mm/internal.h +7 -0
@@ -1453,6 +1453,8 @@ bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end);bool can_modify_mm_madv (struct mm_struct *mm,unsigned long start,unsigned long end,int behavior); +/* mm's mmap write lock must be taken before seal/unseal operation */ +int do_mseal (unsigned long start,unsigned long end,bool seal);#else static inline int can_do_mseal (unsigned long flags) {
@@ -1470,6 +1472,11 @@ static inline bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, {return true; } + +static inline int do_mseal (unsigned long start,unsigned long end,bool seal) +{ +return -EINVAL ; +}#endif #ifdef CONFIG_SHRINKER_DEBUG
mm/mseal.c +51 -30
142 lines not on this page · raw message
Signature · 12 lines
2.34.1 Amazon Web Services Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
RFC PATCH 2/3 mm/secretmem: implement mm-local kernel allocations
In reply to Roman KaganFrom: Fares Mehanna <[email protected]>
In order to be resilient against cross-process speculation-based attacks, it makes sense to store certain (secret) items in kernel memory local to mm.
Implement such allocations on top of secretmem infrastructure.
Specifically, on allocate
1. Create secretmem file.
2. To distinguish it from the conventional memfd_secret()-created one and to maintain associated mm-local allocation context, put the latter on ->private_data of the file.
3. Create virtual mapping in user virtual address space using mmap().
4. Seal the virtual mapping to disallow the user from affecting it in any way.
5. Fault the pages in, effectively calling secretmem fault handler to remove the pages from kernel linear address and make them local to process mm.
6. Change the PTE from user mode to kernel mode, any access from userspace will result in segmentation fault. Kernel can access this virtual address now.
7. Return the secure area as a struct containing the pointer to the actual memory and providing the context for the release function later.
On release,
- if called while mm is still in use, remove the mapping
- otherwise, if performed at mm teardown, no unmapping is necessary
The rest is taken care of by secretmem file cleanup, including returning the pages to the kernel direct map.
--- include/linux/secretmem.h | 8 ++ mm/gup.c | 4 +- mm/secretmem.c | 208 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+), 2 deletions(-)
include/linux/secretmem.h +8 -0
@@ -4,6 +4,10 @@#ifdef CONFIG_SECRETMEM +struct secretmem_area { +void *ptr; +}; +extern const struct address_space_operations secretmem_aops;static inline bool secretmem_mapping (struct address_space *mapping)
@@ -12,8 +16,12 @@ static inline bool secretmem_mapping(struct address_space *mapping) }bool vma_is_secretmem (struct vm_area_struct *vma); +bool can_access_secretmem_vma (struct vm_area_struct *vma);bool secretmem_active (void ); +struct secretmem_area *secretmem_allocate_pages (unsigned int order); +void secretmem_release_pages (struct secretmem_area *data); +#else static inline bool vma_is_secretmem (struct vm_area_struct *vma)
mm/gup.c +2 -2
@@ -1172,7 +1172,7 @@ 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)) +if (!can_access_secretmem_vma (vma))return NULL ;if (WARN_ON_ONCE (foll_flags &FOLL_PIN ))
@@ -1377,7 +1377,7 @@ 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)) +if (!can_access_secretmem_vma (vma))return -EFAULT ;if (write) {
mm/secretmem.c +208 -0
258 lines not on this page · raw message
Signature · 12 lines
2.34.1 Amazon Web Services Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
kernel test robot
build botRe: [PATCH RFC 2/3] mm/secretmem: implement mm-local kernel allocations
kernel test robot noticed the following build errors:
Hi Roman,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on char-misc/char-misc-testing char-misc/char-misc-next char-misc/char-misc-linus shuah-kselftest/next shuah-kselftest/fixes linus/master v6.10-rc5 next-20240626] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Roman-Kagan/mseal-expose-interface-to-seal-unseal-user-memory-ranges/20240625-174709 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20240621201501.1059948-3-rkagan%40amazon.de patch subject: [PATCH RFC 2/3] mm/secretmem: implement mm-local kernel allocations config: parisc-allnoconfig (https://download.01.org/0day-ci/archive/20240627/[email protected]/config) compiler: hppa-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
mm/gup.c: In function 'follow_page':
>> mm/gup.c:1175:14: error: implicit declaration of function 'can_access_secretmem_vma' [-Werror=implicit-function-declaration]
1175 | if (!can_access_secretmem_vma(vma))
| ^~~~~~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/can_access_secretmem_vma +1175 mm/gup.c
1168
1169 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1170 unsigned int foll_flags)
1171 {
1172 struct follow_page_context ctx = { NULL };
1173 struct page *page;
1174
> 1175 if (!can_access_secretmem_vma(vma))
1176 return NULL;
1177
1178 if (WARN_ON_ONCE(foll_flags & FOLL_PIN))
1179 return NULL;
1180
1181 /*
1182 * We never set FOLL_HONOR_NUMA_FAULT because callers don't expect
1183 * to fail on PROT_NONE-mapped pages.
1184 */
1185 page = follow_page_mask(vma, address, foll_flags, &ctx);
1186 if (ctx.pgmap)
1187 put_dev_pagemap(ctx.pgmap);
1188 return page;
1189 }
1190
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH RFC 2/3] mm/secretmem: implement mm-local kernel allocations
In reply to Roman Kagankernel test robot noticed the following build errors:
Hi Roman,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on char-misc/char-misc-testing char-misc/char-misc-next char-misc/char-misc-linus shuah-kselftest/next shuah-kselftest/fixes linus/master v6.10-rc5 next-20240626] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Roman-Kagan/mseal-expose-interface-to-seal-unseal-user-memory-ranges/20240625-174709 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20240621201501.1059948-3-rkagan%40amazon.de patch subject: [PATCH RFC 2/3] mm/secretmem: implement mm-local kernel allocations config: um-allnoconfig (https://download.01.org/0day-ci/archive/20240627/[email protected]/config) compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from mm/gup.c:9:
In file included from include/linux/pagemap.h:11:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from arch/um/include/asm/hardirq.h:5:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:14:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:548:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
548 | val = __raw_readb(PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:561:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
561 | val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
| ^
In file included from mm/gup.c:9:
In file included from include/linux/pagemap.h:11:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from arch/um/include/asm/hardirq.h:5:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:14:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:574:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
574 | val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
| ^
In file included from mm/gup.c:9:
In file included from include/linux/pagemap.h:11:
In file included from include/linux/highmem.h:12:
In file included from include/linux/hardirq.h:11:
In file included from arch/um/include/asm/hardirq.h:5:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:14:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:585:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
585 | __raw_writeb(value, PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:595:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
595 | __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:605:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
605 | __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:693:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
693 | readsb(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:701:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
701 | readsw(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:709:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
709 | readsl(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:718:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
718 | writesb(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:727:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
727 | writesw(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:736:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
736 | writesl(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
>> mm/gup.c:1175:7: error: call to undeclared function 'can_access_secretmem_vma'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1175 | if (!can_access_secretmem_vma(vma))
| ^
mm/gup.c:1380:7: error: call to undeclared function 'can_access_secretmem_vma'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1380 | if (!can_access_secretmem_vma(vma))
| ^12 warnings and 2 errors generated.
vim +/can_access_secretmem_vma +1175 mm/gup.c
1168
1169 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1170 unsigned int foll_flags)
1171 {
1172 struct follow_page_context ctx = { NULL };
1173 struct page *page;
1174
> 1175 if (!can_access_secretmem_vma(vma))
1176 return NULL;
1177
1178 if (WARN_ON_ONCE(foll_flags & FOLL_PIN))
1179 return NULL;
1180
1181 /*
1182 * We never set FOLL_HONOR_NUMA_FAULT because callers don't expect
1183 * to fail on PROT_NONE-mapped pages.
1184 */
1185 page = follow_page_mask(vma, address, foll_flags, &ctx);
1186 if (ctx.pgmap)
1187 put_dev_pagemap(ctx.pgmap);
1188 return page;
1189 }
1190
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH 3/3 drivers/misc: add test driver and selftest for proclocal allocator
In reply to Roman KaganIntroduce a simple driver for functional and stress testing of proclocal kernel allocator. The driver exposes a device node /dev/proclocal-test, which allows userland programs to request creation of proclocal areas and to obtain their addresses as seen by the kernel, and in addition to read and write kernel memory at arbitrary address content (simplified /dev/kmem good enough to access proclocal allocations under selftest responsibility).
The driver is not meant for use with production kernels, as it exposes internal kernel pointers and data.
Also add a basic selftest that uses this driver.
--- drivers/misc/Makefile | 1 + tools/testing/selftests/proclocal/Makefile | 6 + drivers/misc/proclocal-test.c | 200 ++++++++++++++++++ .../selftests/proclocal/proclocal-test.c | 150 +++++++++++++ drivers/misc/Kconfig | 15 ++ tools/testing/selftests/proclocal/.gitignore | 1 + 6 files changed, 373 insertions(+) create mode 100644 tools/testing/selftests/proclocal/Makefile create mode 100644 drivers/misc/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/.gitignore
drivers/misc/Makefile +1 -0
@@ -69,3 +69,4 @@ obj-$(CONFIG_TMR_INJECT) += xilinx_tmr_inject.o obj-$(CONFIG_TPS6594_ESM) += tps6594-esm.o obj-$(CONFIG_TPS6594_PFSM) += tps6594-pfsm.o obj-$(CONFIG_NSM) += nsm.o +obj-$(CONFIG_PROCLOCAL_TEST) += proclocal-test.o
tools/testing/selftests/proclocal/Makefile new file +6 -0
@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := proclocal-test +CFLAGS += -O2 -g -Wall $(KHDR_INCLUDES) + +include ../lib.mk
drivers/misc/proclocal-test.c new file +200 -0
200 lines not on this page · raw message
tools/testing/selftests/proclocal/proclocal-test.c new file +150 -0
150 lines not on this page · raw message
drivers/misc/Kconfig +15 -0
@@ -585,6 +585,21 @@ config NSM To compile this driver as a module, choose M here. The module will be called nsm. +config PROCLOCAL_TEST + tristate "Proclocal allocator test driver" + depends on SECRETMEM + help + This driver allows to perform functional and stress tests for + proclocal memory allocator. It exposes /dev/proclocal-test that + userland test programs can use to create and manipulate proclocal + kernel allocations. + + To compile this driver as a module, choose M here: the module will be + called proclocal-test. + + If unsure, say N. + This driver is not meant to be used on production systems. + source "drivers/misc/c2port/Kconfig" source "drivers/misc/eeprom/Kconfig" source "drivers/misc/cb710/Kconfig"
tools/testing/selftests/proclocal/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/proclocal-test
Signature · 12 lines
2.34.1 Amazon Web Services Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B Sitz: Berlin Ust-ID: DE 365 538 597
Greg Kroah-Hartman
maintainerRe: [PATCH RFC 3/3] drivers/misc: add test driver and selftest for proclocal allocator
Quoted 10 lines · Roman Kagan
Introduce a simple driver for functional and stress testing of proclocal kernel allocator. The driver exposes a device node
/dev/proclocal-test, which allows userland programs to request creation of proclocal areas and to obtain their addresses as seen by the kernel, and in addition to read and write kernel memory at arbitrary address content (simplified/dev/kmemgood enough to access proclocal allocations under selftest responsibility).The driver is not meant for use with production kernels, as it exposes internal kernel pointers and data.
Then you MUST taint the kernel and throw up huge warnings whenever it is loaded, otherwise distros will build this in and end up running it.
Quoted 51 lines
Also add a basic selftest that uses this driver.
Signed-off-by Roman Kagan <[email protected]>--- drivers/misc/Makefile | 1 + tools/testing/selftests/proclocal/Makefile | 6 + drivers/misc/proclocal-test.c | 200 ++++++++++++++++++ .../selftests/proclocal/proclocal-test.c | 150 +++++++++++++ drivers/misc/Kconfig | 15 ++ tools/testing/selftests/proclocal/.gitignore | 1 + 6 files changed, 373 insertions(+) create mode 100644 tools/testing/selftests/proclocal/Makefile create mode 100644 drivers/misc/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/proclocal-test.c create mode 100644 tools/testing/selftests/proclocal/.gitignoredrivers/misc/Makefile +1
-0@@ -69,3 +69,4 @@ obj-$(CONFIG_TMR_INJECT) += xilinx_tmr_inject.o obj-$(CONFIG_TPS6594_ESM) += tps6594-esm.o obj-$(CONFIG_TPS6594_PFSM) += tps6594-pfsm.o obj-$(CONFIG_NSM) += nsm.o +obj-$(CONFIG_PROCLOCAL_TEST) += proclocal-test.otools/testing/selftests/proclocal/Makefile new file +6
-0@@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 + +TEST_GEN_PROGS := proclocal-test +CFLAGS += -O2 -g -Wall $(KHDR_INCLUDES) + +include ../lib.mkdrivers/misc/proclocal-test.c new file +7
-0@@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All rights reserved. +* Author: Roman Kagan <[email protected]> +* +* test driver for proclocal memory allocator +*/ +
drivers/misc/proclocal-test.c line 17
Quoted 10 lines
drivers/misc/proclocal-test.c new file +10
-0+#include <linux/compat.h> +#include <linux/miscdevice.h> +#include <linux/module.h> +#include <linux/workqueue.h> +#include <linux/file.h> +#include <linux/secretmem.h> + +struct proclocal_test_alloc { +u64 size; +u64 ptr;
This structure is not defined properly to cross the user/kernel boundry :(
drivers/misc/proclocal-test.c line 20
Quoted 3 lines
drivers/misc/proclocal-test.c new file +3
-0+}; + +#define PROCLOCAL_TEST_ALLOC _IOWR('A', 0x10, struct proclocal_test_alloc)
ioctl definitions belong in a .h file for userspace to be able to see them.
drivers/misc/proclocal-test.c line 22
Quoted 2 lines
drivers/misc/proclocal-test.c new file +2
-0+ +#define BOUNCE_BUF_SIZE PAGE_SIZE
Then why not just use PAGE_SIZE everywhere? Less characters and we all know what that is.
Stopped reviewing here, sorry.
greg k-h
Quoted 11 lines · Roman Kagan
Compared to the approach used in the orignal series, where a dedicated kernel address range and thus a dedicated PGD was used for mm-local allocations, the one proposed here may have certain drawbacks, in particular
- using user addresses for kernel memory may violate assumptions in various parts of kernel code which we may not have identified with smoke tests we did
- the allocated addresses are guessable by the userland (ATM they are even visible in
/proc/PID/mapsbut that's fixable) which may weaken the security posture
I think this approach makes sense as it's generic and applies immediately to all architectures. I'm slightly uncomfortable about using userspace addresses though, and the special cases that it introduces.
I'd like to see a per-arch ARCH_HAS_PROCLOCAL_PGD so that it *can* be put back into a dedicated address range where possible.
Looking forward to the x86 KVM code from before being dusted off and put on top of this, and also the Arm version of same. A test driver and test case is all very well, but it's less exciting than the real use case :)
Hey Roman,
Quoted 32 lines · Roman Kagan
In a series posted a few years ago [1], a proposal was put forward to allow the kernel to allocate memory local to a mm and thus push it out of reach for current and future speculation-based cross-process attacks. We still believe this is a nice thing to have.
However, in the time passed since that post Linux mm has grown quite a few new goodies, so we'd like to explore possibilities to implement this functionality with less effort and churn leveraging the now available facilities.
Specifically, this is a proof-of-concept attempt to implement mm-local allocations piggy-backing on
memfd_secret(), using regular user addressess but pinning the pages and flipping the user/supervisor flag on the respective PTEs to make them directly accessible from kernel, and sealing the VMA to prevent userland from taking over the address range. The approach allowed to delegate all the heavy lifting -- address management, interactions with the direct map, cleanup on mm teardown -- to the existing infrastructure, and required zero architecture-specific code.Compared to the approach used in the orignal series, where a dedicated kernel address range and thus a dedicated PGD was used for mm-local allocations, the one proposed here may have certain drawbacks, in particular
- using user addresses for kernel memory may violate assumptions in various parts of kernel code which we may not have identified with smoke tests we did
- the allocated addresses are guessable by the userland (ATM they are even visible in
/proc/PID/mapsbut that's fixable) which may weaken the security postureAlso included is a simple test driver and selftest to smoke test and showcase the feature.
Quoted 6 lines
The code is PoC RFC and lacks a lot of checks and special case handling, but demonstrates the idea. We'd appreciate any feedback on whether it's a viable approach or it should better be abandoned in favor of the one with dedicated PGD / kernel address range or yet something else.
I haven't seen any negative feedback on the RFC, so when can I expect a v1 of this patch set that addresses the non-production-readyness of it that you call out above? :)
Alex
Amazon Web Services Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597