prmem: protected memory
Subsystems (14)
cover prmem: protected memory
-- Summary --
Preliminary version of memory protection patchset, including a sample use case, turning into write-rare the IMA measurement list.
The core idea is to introduce two new types of memory protection, beside const and __ro_after_init, which will support:
- statically allocated "write rare" memory
- dynamically allocated "read only" and "write rare" memory
On top of that, follows a set of patches which create a "write rare" counterpart of the kernel infrastructure used in the example chose for
hardening: the IMA measurement list.
-- Mechanism --
Statically allocated protected memory is identified by the __wr_after_init tag, which will cause the linker to place it in a special section.
Dynamically allocated memory is obtained through vmalloc, but compacting each allocation, where possible, in the latest obtained vmap_area.
The write rare mechanism is implemented by creating a temporary alternate writable mapping, applying the change through this mapping and then removing it.
All of this is possible thanks to the system MMU, which must be able to provide write protection.
-- Brief history --
I sent out various versions of memory protection over the last year or so, however this patchset is significantly expanded, including several helper data structures and a use case, so I decided to reset the numbering to v1.
As reference, the latest "old" version is here [1].
The current version is not yet ready for merge, however it is sufficiently complete for supporting an end-to-end discussion, I think.
Eventually, I plan to write a white paper, once the code is in better shape. In the meanwhile, an overview can be had from these slides [2], which are the support material for my presentation at the Linux Security Summit 2018 Europe.
-- Validation --
Most of the testing is done on a Fedora image, with QEMU x86_64, however the code has been also tested on a real x86_64 PC, yielding similar positive results.
For ARM64, I use a custom Debian installation, still with QEMU, but I have obtained similar failures when testing with a real device, using a Kirin970.
I have written some test cases for the most basic parts and the behaviour of IMA and the Fedora image in general do not seem to be negatively affected, when used in conjunction with this patchset. However, it's far from being exhaustive testing and the torture test for rcu is completely missing.
-- Known Issues --
As said, this version is preliminary and certain parts need rework. This is a short and incomplete list of known issues:
* arm64 support is broken for __wr_after_init
I must create a separate section with proper mappings, similar to the ones used for vmalloc()
* alignment of data structures has not been throughly checked There are probably several redundant forced alignments
* there is no fallback for platforms missing MMU write protection
* some additional care might be needed when dealing with double mapping vs data cache coherency, on multicore systems
* lots of additional (stress) tests are needed
* memory reuse (object caches) are probably needed, to support converting more use cases, and so also other data structures.
* credits for original code: I have reimplemented various data structures, I am not sure if I have given credit correctly to the original authors.
* documentation for the re-implemented data structures is missing
* confirm that the hardened usercopy logic is correct
-- Q&As --
During reviews of the older patchset, several objections and questions were formulated.
They are collected here in Q&A format, with both some old and new answers:
1 - "The protection can still be undone"
Yes, it is true. Using a hypervisor, like it is done in certain Huawei and Samsung phones, provides a better level of protection.
However, even without that, it still gives a significantly better level of protection than not protecting the memory at all.
The main advantage of this patchset is that now the attack has to focus on the page table, which is a significantly smaller area, than the whole kernel data.
It is my intention, eventually, to provide also support for interaction with a FOSS hypervisor (ex: KVM), but this patchset should support also those cases where it's not even possible to have an hypervisor. So it seems simpler to start from there. The hypervisor is not mandatory.
2 - "Do not provide a chain of trust, but protect some memory and refer to it with a writable pointer."
This might be ok for protecting against bugs, but in the case of an attacker trying to compromise the system, the unprotected pointer has become the new target. It doesn't change much.
Samsung does use a similar implementation, for protecting LSM hooks, however that solution also add a pointer, from the protected memory back to the writable memory, as validation loop. And the price to pay is that every time the unprotected pointer must be used, it first has to be validated, to point to a certain memory range and to have a specific alignment. It's an alternative solution to the full chain of trust and each has its specific advantages, depending on the data structures that one wants to protect.
3 - "Do not use a secondary mapping, unprotect the current one" The purpose of the secondary mapping is to create a hard-to-spot window of writability at a random address, which cannot be easily exploited. Unprotecting the primary mapping would allow an attack where a core is busy looping trying to figure out if a specific location becomes writable and race against the legitimate writer. For the same reason, interrupts are disabled on the core that is performing the write-rare operation.
4 - "Do not create another allocator over vmalloc(), use it plain" This is not good for various reasons:
a) vmalloc() allocates at least one page for every request it receives, leaving most of the page typically unused. While it might not be a big deal on large systems, on IoT class devices it is possible to find relatively powerful cores paired to relatively little memory. Taking as example a system using SELinux, a relatively small set of rules can genarate a few thousands of allocations (SELinux is deny-by-default). Modeling each allocation to be about 64bytes, on a system with 4kB pages, assuming that the grand total of allocation is 100k, that means
100k * 4kB = 390MB
while, using each 64bytes slot in a page yields:
100k * 64B = 6MBThe first case would not be very compatible with a system having only 512MB or 1GB.
b) even worse, the amount of thrashing of the TLB would be terrible, with each allocation having its own translation.
--
-- References --
[1]: https://lkml.org/lkml/2018/4/23/508
[2]: https://events.linuxfoundation.org/wp-content/uploads/2017/12/Kernel-Hardening-Protecting-the-Protection-Mechanisms-Igor-Stoppa-Huawei.pdf
-- List of patches --
[PATCH 01/17] prmem: linker section for static write rare [PATCH 02/17] prmem: write rare for static allocation [PATCH 03/17] prmem: vmalloc support for dynamic allocation [PATCH 04/17] prmem: dynamic allocation
[PATCH 05/17] prmem: shorthands for write rare on common types [PATCH 06/17] prmem: test cases for memory protection [PATCH 07/17] prmem: lkdtm tests for memory protection [PATCH 08/17] prmem: struct page: track vmap_area [PATCH 09/17] prmem: hardened usercopy
[PATCH 10/17] prmem: documentation
[PATCH 11/17] prmem: llist: use designated initializer [PATCH 12/17] prmem: linked list: set alignment [PATCH 13/17] prmem: linked list: disable layout randomization [PATCH 14/17] prmem: llist, hlist, both plain and rcu [PATCH 15/17] prmem: test cases for prlist and prhlist [PATCH 16/17] prmem: pratomic-long
[PATCH 17/17] prmem: ima: turn the measurements list write rare
-- Diffstat --
Documentation/core-api/index.rst | 1 + Documentation/core-api/prmem.rst | 172 +++++ MAINTAINERS | 14 + drivers/misc/lkdtm/core.c | 13 + drivers/misc/lkdtm/lkdtm.h | 13 + drivers/misc/lkdtm/perms.c | 248 +++++++ include/asm-generic/vmlinux.lds.h | 20 + include/linux/cache.h | 17 + include/linux/list.h | 5 +- include/linux/mm_types.h | 25 +- include/linux/pratomic-long.h | 73 ++ include/linux/prlist.h | 934 ++++++++++++++++++++++++ include/linux/prmem.h | 446 +++++++++++ include/linux/prmemextra.h | 133 ++++ include/linux/types.h | 20 +- include/linux/vmalloc.h | 11 +- lib/Kconfig.debug | 9 + lib/Makefile | 1 + lib/test_prlist.c | 252 +++++++ mm/Kconfig | 6 + mm/Kconfig.debug | 9 + mm/Makefile | 2 + mm/prmem.c | 273 +++++++ mm/test_pmalloc.c | 629 ++++++++++++++++ mm/test_write_rare.c | 236 ++++++ mm/usercopy.c | 5 + mm/vmalloc.c | 7 + security/integrity/ima/ima.h | 18 +- security/integrity/ima/ima_api.c | 29 +- security/integrity/ima/ima_fs.c | 12 +- security/integrity/ima/ima_main.c | 6 + security/integrity/ima/ima_queue.c | 28 +- security/integrity/ima/ima_template.c | 14 +- security/integrity/ima/ima_template_lib.c | 14 +- 34 files changed, 3635 insertions(+), 60 deletions(-)
PATCH 1/17 prmem: linker section for static write rare
Introduce a section and a label for statically allocated write rare data.
The label is named "__write_rare_after_init".
As the name implies, after the init phase is completed, this section will be modifiable only by invoking write rare functions.
NOTE:
this needs rework, because the current write-rare mechanism works only on x86_64 and not arm64, due to arm64 mappings.
--- include/asm-generic/vmlinux.lds.h | 20 ++++++++++++++++++++ include/linux/cache.h | 17 +++++++++++++++++ 2 files changed, 37 insertions(+)
include/asm-generic/vmlinux.lds.h +20 -0
@@ -300,6 +300,25 @@ . = __start_init_task + THREAD_SIZE; \ __end_init_task = .; +/* +* Allow architectures to handle wr_after_init data on their +* own by defining an empty WR_AFTER_INIT_DATA. +* However, it's important that pages containing WR_RARE data do not +* hold anything else, to avoid both accidentally unprotecting something +* that is supposed to stay read-only all the time and also to protect +* something else that is supposed to be writeable all the time. +*/ +#ifndef WR_AFTER_INIT_DATA +#define WR_AFTER_INIT_DATA (align) \ + . = ALIGN(PAGE_SIZE); \ + __start_wr_after_init = .; \ + . = ALIGN(align); \ + *(.data..wr_after_init) \ + . = ALIGN(PAGE_SIZE); \ + __end_wr_after_init = .; \ + . = ALIGN(align); +#endif +/* * Allow architectures to handle ro_after_init data on their * own by defining an empty RO_AFTER_INIT_DATA.
@@ -320,6 +339,7 @@ __start_rodata = .; \ *(.rodata) *(.rodata.*) \ RO_AFTER_INIT_DATA /* Read only after init */ \ + WR_AFTER_INIT_DATA(align) /* wr after init */ \KEEP (*(__vermagic))/* Kernel version magic */ \ . = ALIGN(8); \ __start___tracepoints_ptrs = .; \
include/linux/cache.h +17 -0
@@ -31,6 +31,23 @@#define __ro_after_init __attribute__((__section__(".data..ro_after_init")))#endif +/* +* __wr_after_init is used to mark objects that cannot be modified +* directly after init (i.e. after mark_rodata_ro() has been called). +* These objects become effectively read-only, from the perspective of +* performing a direct write, like a variable assignment. +* However, they can be altered through a dedicated function. +* It is intended for those objects which are occasionally modified after +* init, however they are modified so seldomly, that the extra cost from +* the indirect modification is either negligible or worth paying, for the +* sake of the protection gained. +*/ +#ifndef __wr_after_init +#define __wr_after_init \ + __attribute__((__section__(".data..wr_after_init"))) +#endif + +#ifndef ____cacheline_aligned#define ____cacheline_aligned __attribute__((__aligned__(SMP_CACHE_BYTES)))#endif
Signature · 1 line
2.17.1
PATCH 2/17 prmem: write rare for static allocation
In reply to Igor StoppaImplementation of write rare for statically allocated data, located in a specific memory section through the use of the __write_rare label.
The basic functions are wr_memcpy() and wr_memset(): the write rare counterparts of memcpy() and memset() respectively.
To minimize chances of attacks, this implementation does not unprotect existing memory pages.
Instead, it remaps them, one by one, at random free locations, as writable. Each page is mapped as writable strictly for the time needed to perform changes in said page.
While a page is remapped, interrupts are disabled on the core performing the write rare operation, to avoid being frozen mid-air by an attack using interrupts for stretching the duration of the alternate mapping. OTOH, to avoid introducing unpredictable delays, the interrupts are re-enabled inbetween page remapping, when write operations are either completed or not yet started, and there is not alternate, writable mapping to exploit.
--- MAINTAINERS | 7 ++ include/linux/prmem.h | 213 ++++++++++++++++++++++++++++++++++++++++++ mm/Makefile | 1 + mm/prmem.c | 10 ++ 4 files changed, 231 insertions(+) create mode 100644 include/linux/prmem.h create mode 100644 mm/prmem.c
MAINTAINERS +7 -0
@@ -9454,6 +9454,13 @@ F: kernel/sched/membarrier.c F: include/uapi/linux/membarrier.h F: arch/powerpc/include/asm/membarrier.h +MEMORY HARDENING +M: Igor Stoppa <[email protected]> +L: [email protected] +S: Maintained +F: include/linux/prmem.h +F: mm/prmem.c + MEMORY MANAGEMENT L: [email protected] W: http://www.linux-mm.org
include/linux/prmem.h new file +213 -0
213 lines not on this page · raw message
mm/Makefile +1 -0
@@ -64,6 +64,7 @@ obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o +obj-$(CONFIG_PRMEM) += prmem.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/prmem.c new file +10 -0
@@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* prmem.c: Memory Protection Library +* +* (C) Copyright 2017-2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + +const char WR_ERR_RANGE_MSG [] ="Write rare on invalid memory range." ; +const char WR_ERR_PAGE_MSG [] ="Failed to remap write rare page." ;
Signature · 1 line
2.17.1
Re: [PATCH 02/17] prmem: write rare for static allocation
include/linux/prmem.h line 46
Quoted 9 lines
include/linux/prmem.h new file +9
-0+static __always_inline bool __is_wr_after_init(const void *ptr, size_t size) +{ +size_t start = (size_t )&__start_wr_after_init; +size_t end = (size_t )&__end_wr_after_init; +size_t low = (size_t )ptr; +size_t high = (size_t )ptr + size; + +return likely (start <= low && low < high && high <= end); +}
size_t is an odd type choice for doing address arithmetic.
Quoted 12 lines
include/linux/prmem.h new file +12
-0+/** +* wr_memset() - sets n bytes of the destination to the c value +* @dst: beginning of the memory to write to +* @c: byte to replicate +* @size: amount of bytes to copy +* +* Returns true on success, false otherwise. +*/ +static __always_inline +bool wr_memset(const void *dst, const int c, size_t n_bytes) +{ +size_t size;
include/linux/prmem.h line 69
Quoted 10 lines
include/linux/prmem.h new file +10
-0+unsigned long flags; +uintptr_t d = (uintptr_t )dst; + +if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +return false; +while (n_bytes) { +struct page *page; +uintptr_t base; +uintptr_t offset; +uintptr_t offset_complement;
Again, these are really odd choices for types. vmap() returns a void* pointer, on which you can do arithmetic. Why bother keeping another type to which you have to cast to and from?
BTW, our usual "pointer stored in an integer type" is 'unsigned long', if a pointer needs to be manipulated.
Review
Quoted 1 line
(quoted diff) +1
-0+ local_irq_save(flags);
Why are you doing the local_irq_save()?
include/linux/prmem.h line 76
Quoted 5 lines
include/linux/prmem.h new file +5
-0+ page =virt_to_page (d); + offset = d & ~PAGE_MASK ; + offset_complement =PAGE_SIZE - offset; + size =min (n_bytes, offset_complement); + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL );
Can you even call vmap() (which sleeps) with interrupts off?
include/linux/prmem.h line 80
Quoted 4 lines
include/linux/prmem.h new file +4
-0+if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false; + }
You really need some kmap_atomic()-style accessors to wrap this stuff for you. This little pattern is repeated over and over.
...
mm/prmem.c line 10
Quoted 2 lines
mm/prmem.c new file +2
-0+const char WR_ERR_RANGE_MSG [] ="Write rare on invalid memory range." ; +const char WR_ERR_PAGE_MSG [] ="Failed to remap write rare page." ;
Doesn't the compiler de-duplicate duplicated strings for you? Is there any reason to declare these like this?
Re: [PATCH 02/17] prmem: write rare for static allocation
Quoted 11 lines · Dave Hansen
include/linux/prmem.h line 46
Quoted 9 lines
include/linux/prmem.h new file +9
-0+static __always_inline bool __is_wr_after_init(const void *ptr, size_t size) +{ +size_t start = (size_t )&__start_wr_after_init; +size_t end = (size_t )&__end_wr_after_init; +size_t low = (size_t )ptr; +size_t high = (size_t )ptr + size; + +return likely (start <= low && low < high && high <= end); +}
size_tis an odd type choice for doing address arithmetic.
it seemed more portable than unsigned long
Quoted 17 lines
Quoted 17 lines
include/linux/prmem.h new file +17
-0+/** +* wr_memset() - sets n bytes of the destination to the c value +* @dst: beginning of the memory to write to +* @c: byte to replicate +* @size: amount of bytes to copy +* +* Returns true on success, false otherwise. +*/ +static __always_inline +bool wr_memset(const void *dst, const int c, size_t n_bytes) +{ +size_t size; +unsigned long flags; +uintptr_t d = (uintptr_t )dst; + +if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +return false;
Quoted 8 lines
include/linux/prmem.h line 69
Quoted 5 lines
include/linux/prmem.h new file +5
-0+while (n_bytes) { +struct page *page; +uintptr_t base; +uintptr_t offset; +uintptr_t offset_complement;Again, these are really odd choices for types.
vmap()returns a void* pointer, on which you can do arithmetic.
I wasn't sure of how much I could rely on the compiler not doing some unwanted optimizations.
Quoted 2 lines
Why bother keeping another
type to which you have to cast to and from?
For the above reason. If I'm worrying unnecessarily, I can switch back to void * It certainly is easier to use.
Quoted 2 lines
BTW, our usual "pointer stored in an integer type" is 'unsigned long', if a pointer needs to be manipulated.
yes, I noticed that, but it seemed strange ...size_t corresponds to unsigned long, afaik
but it seems that I have not fully understood where to use it
anyway, I can stick to the convention with unsigned long
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ local_irq_save(flags);Why are you doing the
local_irq_save()?
The idea was to avoid the case where an attack would somehow freeze the core doing the write-rare operation, while the temporary mapping is accessible.
I have seen comments about using mappings that are private to the current core (and I will reply to those comments as well), but this approach seems architecture-dependent, while I was looking for a solution that, albeit not 100% reliable, would work on any system with an MMU. This would not prevent each arch to come up with own custom implementation that provides better coverage, performance, etc.
Quoted 7 lines
include/linux/prmem.h line 76
Quoted 5 lines
include/linux/prmem.h new file +5
-0+ page =virt_to_page (d); + offset = d & ~PAGE_MASK ; + offset_complement =PAGE_SIZE - offset; + size =min (n_bytes, offset_complement); + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL );Can you even call
vmap()(which sleeps) with interrupts off?
I accidentally disabled sleeping while atomic debugging and I totally missed this problem :-(
However, to answer your question, nothing exploded while I was testing (without that type of debugging).
I suspect I was just "lucky". Or maybe I was simply not triggering the sleeping sub-case.
As I understood the code, sleeping _might_ happen, but it's not going to happen systematically.
I wonder if I could split vmap() into two parts: first the sleeping one, with interrupts enabled, then the non sleeping one, with interrupts disabled.
I need to read the code more carefully, but it seems that sleeping might happen when memory for the mapping meta data is not immediately available.
BTW, wouldn't the might_sleep() call belong more to the part which really sleeps, instead than to the whole vmap() ?
Quoted 7 lines
include/linux/prmem.h line 80
Quoted 4 lines
include/linux/prmem.h new file +4
-0+if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false; + }You really need some kmap_atomic()-style accessors to wrap this stuff for you. This little pattern is repeated over and over.
I really need to learn more about the way the kernel works and is structured. It's a work in progress. Thanks for the advice.
Quoted 6 lines
...
mm/prmem.c line 10
Quoted 2 lines
mm/prmem.c new file +2
-0+const char WR_ERR_RANGE_MSG [] ="Write rare on invalid memory range." ; +const char WR_ERR_PAGE_MSG [] ="Failed to remap write rare page." ;Doesn't the compiler de-duplicate duplicated strings for you? Is there any reason to declare these like this?
I noticed I have made some accidental modifications in a couple of cases, when replicating the command.
So I thought that if I really want to use the same string, why not doing it explicitly? It seemed also easier, in case I want to tweak the message. I need to do it only in one place.
--
igor
Re: [PATCH 02/17] prmem: write rare for static allocation
In reply to Igor StoppaQuoted 1 line · Igor Stoppa
+static
__always_inline
That's far too large for inline.
Quoted 16 lines
include/linux/prmem.h new file +16
-0+bool wr_memset (const void *dst,const int c,size_t n_bytes) +{ +size_t size; +unsigned long flags; +uintptr_t d = (uintptr_t )dst; + +if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +return false; +while (n_bytes) { +struct page *page; +uintptr_t base; +uintptr_t offset; +uintptr_t offset_complement; + +local_irq_save (flags); + page =virt_to_page (d);
include/linux/prmem.h line 82
Quoted 10 lines
include/linux/prmem.h new file +10
-0+ offset = d & ~PAGE_MASK ; + offset_complement =PAGE_SIZE - offset; + size =min (n_bytes, offset_complement); + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false; + } +memset ((void *)(base + offset), c, size); +vunmap ((void *)base);
BUG
include/linux/prmem.h line 98
Quoted 8 lines
include/linux/prmem.h new file +8
-0+ d += size; + n_bytes -= size; +local_irq_restore (flags); + } +return true; +} + +static __always_inline
Similarly large
Quoted 17 lines
include/linux/prmem.h new file +17
-0+bool wr_memcpy (const void *dst,const void *src,size_t n_bytes) +{ +size_t size; +unsigned long flags; +uintptr_t d = (uintptr_t )dst; +uintptr_t s = (uintptr_t )src; + +if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +return false; +while (n_bytes) { +struct page *page; +uintptr_t base; +uintptr_t offset; +uintptr_t offset_complement; + +local_irq_save (flags); + page =virt_to_page (d);
include/linux/prmem.h line 125
Quoted 10 lines
include/linux/prmem.h new file +10
-0+ offset = d & ~PAGE_MASK ; + offset_complement =PAGE_SIZE - offset; + size = (size_t )min (n_bytes, offset_complement); + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false; + } +__write_once_size ((void *)(base + offset), (void *)s, size); +vunmap ((void *)base);
Similarly BUG.
include/linux/prmem.h line 146
Quoted 9 lines
include/linux/prmem.h new file +8
-0+ d += size; + s += size; + n_bytes -= size; +local_irq_restore (flags); + } +return true; +} +static __always_inline
Guess what..
Quoted 10 lines
include/linux/prmem.h new file +10
-0+uintptr_t __wr_rcu_ptr (const void *dst_p_p,const void *src_p) +{ +unsigned long flags; +struct page *page; +void *base; +uintptr_t offset; +const size_t size =sizeof (void *); + +if (WARN (!__is_wr_after_init (dst_p_p, size),WR_ERR_RANGE_MSG )) +return (uintptr_t )NULL ;
include/linux/prmem.h line 166
Quoted 10 lines
include/linux/prmem.h new file +10
-0+local_irq_save (flags); + page =virt_to_page (dst_p_p); + offset = (uintptr_t )dst_p_p & ~PAGE_MASK ; + base =vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return (uintptr_t )NULL ; + } +rcu_assign_pointer ((*(void **)(offset + (uintptr_t )base)), src_p); +vunmap (base);
Also still bug.
include/linux/prmem.h line 169
Quoted 3 lines
include/linux/prmem.h new file +3
-0+local_irq_restore (flags); +return (uintptr_t )src_p; +}
Also, I see an amount of duplication here that shows you're not nearly lazy enough.
Re: [PATCH 02/17] prmem: write rare for static allocation
Quoted 4 lines · Peter Zijlstra
Quoted 1 line · Igor Stoppa
+static
__always_inlineThat's far too large for inline.
The reason for it is that it's supposed to minimize the presence of gadgets that might be used in JOP attacks. I am ready to stand corrected, if I'm wrong, but this is the reason why I did it.
Regarding the function being too large, yes, I would not normally choose it for inlining.
Actually, I would not normally use "__always_inline" and instead I would limit myself to plain "inline", at most.
Quoted 21 lines
Quoted 21 lines
include/linux/prmem.h new file +20
-0+bool wr_memset (const void *dst,const int c,size_t n_bytes) +{ +size_t size; +unsigned long flags; +uintptr_t d = (uintptr_t )dst; + +if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +return false; +while (n_bytes) { +struct page *page; +uintptr_t base; +uintptr_t offset; +uintptr_t offset_complement; + +local_irq_save (flags); + page =virt_to_page (d); + offset = d & ~PAGE_MASK ; + offset_complement =PAGE_SIZE - offset; + size =min (n_bytes, offset_complement); + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL );
Quoted 8 lines
include/linux/prmem.h line 82
Quoted 6 lines
include/linux/prmem.h new file +6
-0+if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false; + } +memset ((void *)(base + offset), c, size); +vunmap ((void *)base);BUG
yes, somehow I managed to drop this debug configuration from the debug builds I made.
[...]
Quoted 2 lines
Also, I see an amount of duplication here that shows you're not nearly lazy enough.
I did notice a certain amount of duplication, but I didn't know how to exploit it.
--
igor
PATCH 3/17 prmem: vmalloc support for dynamic allocation
In reply to Igor StoppaPrepare vmalloc for:
- tagging areas used for dynamic allocation of protected memory
- supporting various tags, related to the property that an area might have
- extrapolating the pool containing a given area
- chaining the areas in each pool
- extrapolating the area containing a given memory address
NOTE:
Since there is a list_head structure that is used only when disposing of the allocation (the field purge_list), there are two pointers for the take, before it comes the time of freeing the allocation.
To avoid increasing the size of the vmap_area structure, instead of using a standard doubly linked list for tracking the chain of vmap_areas, only one pointer is spent for this purpose, in a single linked list, while the other is used to provide a direct connection to the parent pool.
--- include/linux/vmalloc.h | 12 +++++++++++- mm/vmalloc.c | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-)
include/linux/vmalloc.h +11 -1
@@ -21,6 +21,9 @@ struct notifier_block; /* in notifier.h */#define VM_UNINITIALIZED 0x00000020/* vm_struct is not fully initialized */ #define VM_NO_GUARD 0x00000040/* don't add guard page */ #define VM_KASAN 0x00000080/* has allocated kasan shadow memory */ +#define VM_PMALLOC 0x00000100/* pmalloc area - see docs */ +#define VM_PMALLOC_WR 0x00000200/* pmalloc write rare area */ +#define VM_PMALLOC_PROTECTED 0x00000400/* pmalloc protected area */ /* bits [20..32] reserved for arch specific ioremap internals */ /*
@@ -48,7 +51,13 @@ struct vmap_area {unsigned long flags;struct rb_node rb_node;/* address sorted rbtree */ struct list_head list;/* address sorted list */ -struct llist_node purge_list;/* "lazy purge" list */ +union { +struct llist_node purge_list;/* "lazy purge" list */ +struct { +struct vmap_area *next; +struct pmalloc_pool *pool; + }; + };struct vm_struct *vm;struct rcu_head rcu_head; };
@@ -134,6 +143,7 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size, const void *caller);extern struct vm_struct *remove_vm_area (const void *addr);extern struct vm_struct *find_vm_area (const void *addr); +extern struct vmap_area *find_vmap_area (unsigned long addr);extern int map_vm_area (struct vm_struct *area,pgprot_t prot,struct page **pages);
mm/vmalloc.c +1 -1
@@ -742,7 +742,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)free_vmap_area_noflush (va); } -static struct vmap_area *find_vmap_area (unsigned long addr) +struct vmap_area *find_vmap_area (unsigned long addr) {struct vmap_area *va;
Signature · 1 line
2.17.1
Re: [PATCH 03/17] prmem: vmalloc support for dynamic allocation
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */ · line 26
Quoted 3 lines · Igor Stoppa
include/linux/vmalloc.h +3
-0+#define VM_PMALLOC 0x00000100/* pmalloc area - see docs */ +#define VM_PMALLOC_WR 0x00000200/* pmalloc write rare area */ +#define VM_PMALLOC_PROTECTED 0x00000400/* pmalloc protected area */
Please introduce things as you use them. It's impossible to review a patch that just says "see docs" that doesn't contain any docs. :)
Re: [PATCH 03/17] prmem: vmalloc support for dynamic allocation
Quoted 7 lines · Dave Hansen
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */ · line 26
Quoted 3 lines · Igor Stoppa
include/linux/vmalloc.h +3
-0+#define VM_PMALLOC 0x00000100/* pmalloc area - see docs */ +#define VM_PMALLOC_WR 0x00000200/* pmalloc write rare area */ +#define VM_PMALLOC_PROTECTED 0x00000400/* pmalloc protected area */ Please introduce things as you use them. It's impossible to review a patch that just says "see docs" that doesn't contain any docs. :)
Yes, otoh it's a big pain in the neck to keep the docs split into smaller patches interleaved with the code, at least while the code is still in a flux.
And since the docs refer to the sources, for the automated documentation of the API, I cannot just put the documentation at the beginning of the patchset.
Can I keep the docs as they are, for now, till the code is more stable?
--
igor
PATCH 4/17 prmem: dynamic allocation
In reply to Igor StoppaExtension of protected memory to dynamic allocations.
Allocations are performed from "pools".
A pool is a list of virtual memory areas, in various state of protection.
Supported cases
===============
Read Only Pool
--------------
Memory is allocated from the pool, in writable state.
Then it gets written and the content of the pool is write protected and it cannot be altered anymore. It is only possible to destroy the pool.
Auto Read Only Pool
-------------------
Same as the plain read only, but every time a memory area is full and phased out, it is automatically marked as read only.
Write Rare Pool
---------------
Memory is allocated from the pool, in writable state.
Then it gets written and the content of the pool is write protected and it can be altered only by invoking special write rare functions.
Auto Write Rare Pool
--------------------
Same as the plain write rare, but every time a memory area is full and phased out, it is automatically marked as write rare.
Start Write Rare Pool
---------------------
The memory handed out is already in write rare mode and the only way to alter it is to use write rare functions.
When a pool is destroyed, all the memory that was obtained from it is automatically freed. This is the only way to release protected memory.
--- include/linux/prmem.h | 220 +++++++++++++++++++++++++++++++++-- mm/Kconfig | 6 + mm/prmem.c | 263 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 482 insertions(+), 7 deletions(-)
include/linux/prmem.h +213 -7
@@ -7,6 +7,8 @@* * Support for: * - statically allocated write rare data +* - dynamically allocated read only data +* - dynamically allocated write rare data */ #ifndef _LINUX_PRMEM_H
@@ -22,6 +24,11 @@#include <linux/irqflags.h> #include <linux/set_memory.h> +#define VM_PMALLOC_MASK \ + (VM_PMALLOC | VM_PMALLOC_WR | VM_PMALLOC_PROTECTED) +#define VM_PMALLOC_WR_MASK (VM_PMALLOC | VM_PMALLOC_WR) +#define VM_PMALLOC_PROTECTED_MASK (VM_PMALLOC | VM_PMALLOC_PROTECTED) +/* ============================ Write Rare ============================ */ extern const char WR_ERR_RANGE_MSG [];
@@ -45,11 +52,23 @@ static __always_inline bool __is_wr_after_init(const void *ptr, size_t size)return likely (start <= low && low < high && high <= end); } +static __always_inline bool __is_wr_pool(const void *ptr, size_t size) +{ +struct vmap_area *area; + +if (!is_vmalloc_addr (ptr)) +return false; + area =find_vmap_area ((unsigned long )ptr); +return area && area->vm && (area->vm->size >= size) && + ((area->vm->flags & (VM_PMALLOC |VM_PMALLOC_WR )) == + (VM_PMALLOC |VM_PMALLOC_WR )); +} +/** * wr_memset() - sets n bytes of the destination to the c value * @dst: beginning of the memory to write to * @c: byte to replicate -* @size: amount of bytes to copy +* @n_bytes: amount of bytes to copy * * Returns true on success, false otherwise. */
@@ -59,8 +78,10 @@ bool wr_memset(const void *dst, const int c, size_t n_bytes)size_t size;unsigned long flags;uintptr_t d = (uintptr_t )dst; +bool is_virt =__is_wr_after_init (dst, n_bytes); -if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +if (WARN (!(is_virt ||likely (__is_wr_pool (dst, n_bytes))), +WR_ERR_RANGE_MSG ))return false;while (n_bytes) {struct page *page;
@@ -69,7 +90,10 @@ bool wr_memset(const void *dst, const int c, size_t n_bytes)uintptr_t offset_complement;local_irq_save (flags); - page =virt_to_page (d); +if (is_virt) + page =virt_to_page (d); +else + page =vmalloc_to_page ((void *)d); offset = d & ~PAGE_MASK ; offset_complement =PAGE_SIZE - offset; size =min (n_bytes, offset_complement);
@@ -102,8 +126,10 @@ bool wr_memcpy(const void *dst, const void *src, size_t n_bytes)unsigned long flags;uintptr_t d = (uintptr_t )dst;uintptr_t s = (uintptr_t )src; +bool is_virt =__is_wr_after_init (dst, n_bytes); -if (WARN (!__is_wr_after_init (dst, n_bytes),WR_ERR_RANGE_MSG )) +if (WARN (!(is_virt ||likely (__is_wr_pool (dst, n_bytes))), +WR_ERR_RANGE_MSG ))return false;while (n_bytes) {struct page *page;
@@ -112,7 +138,10 @@ bool wr_memcpy(const void *dst, const void *src, size_t n_bytes)uintptr_t offset_complement;local_irq_save (flags); - page =virt_to_page (d); +if (is_virt) + page =virt_to_page (d); +else + page =vmalloc_to_page ((void *)d); offset = d & ~PAGE_MASK ; offset_complement =PAGE_SIZE - offset; size = (size_t )min (n_bytes, offset_complement);
@@ -151,11 +180,13 @@ uintptr_t __wr_rcu_ptr(const void *dst_p_p, const void *src_p)void *base;uintptr_t offset;const size_t size =sizeof (void *); +bool is_virt =__is_wr_after_init (dst_p_p, size); -if (WARN (!__is_wr_after_init (dst_p_p, size),WR_ERR_RANGE_MSG )) +if (WARN (!(is_virt ||likely (__is_wr_pool (dst_p_p, size))), +WR_ERR_RANGE_MSG ))return (uintptr_t )NULL ;local_irq_save (flags); - page =virt_to_page (dst_p_p); + page = is_virt ?virt_to_page (dst_p_p) :vmalloc_to_page (dst_p_p); offset = (uintptr_t )dst_p_p & ~PAGE_MASK ; base =vmap (&page,1 ,VM_MAP ,PAGE_KERNEL );if (WARN (!base,WR_ERR_PAGE_MSG )) {
@@ -210,4 +241,179 @@ bool wr_ptr(const void *dst, const void *val) {return wr_memcpy (dst, &val,sizeof (val)); } + +/* ============================ Allocator ============================ */ + +#define PMALLOC_REFILL_DEFAULT (0) +#define PMALLOC_DEFAULT_REFILL_SIZE PAGE_SIZE +#define PMALLOC_ALIGN_ORDER_DEFAULT ilog2(ARCH_KMALLOC_MINALIGN) + +#define PMALLOC_RO 0x00 +#define PMALLOC_WR 0x01 +#define PMALLOC_AUTO 0x02 +#define PMALLOC_START 0x04 +#define PMALLOC_MASK (PMALLOC_WR | PMALLOC_AUTO | PMALLOC_START) + +#define PMALLOC_MODE_RO PMALLOC_RO +#define PMALLOC_MODE_WR PMALLOC_WR +#define PMALLOC_MODE_AUTO_RO (PMALLOC_RO | PMALLOC_AUTO) +#define PMALLOC_MODE_AUTO_WR (PMALLOC_WR | PMALLOC_AUTO) +#define PMALLOC_MODE_START_WR (PMALLOC_WR | PMALLOC_START) + +struct pmalloc_pool { +struct mutex mutex; +struct list_head pool_node; +struct vmap_area *area; +size_t align; +size_t refill; +size_t offset; +uint8_t mode; +}; + +/* +* The write rare functionality is fully implemented as __always_inline, +* to prevent having an internal function call that is capable of modifying +* write protected memory. +* Fully inlining the function allows the compiler to optimize away its +* interface, making it harder for an attacker to hijack it. +* This still leaves the door open to attacks that might try to reuse part +* of the code, by jumping in the middle of the function, however it can +* be mitigated by having a compiler plugin that enforces Control Flow +* Integrity (CFI). +* Any addition/modification to the write rare path must follow the same +* approach. +*/ + +void pmalloc_init_custom_pool (struct pmalloc_pool *pool,size_t refill, +short align_order,uint8_t mode); + +struct pmalloc_pool *pmalloc_create_custom_pool (size_t refill, +short align_order, +uint8_t mode); + +/** +* pmalloc_create_pool() - create a protectable memory pool +* @mode: can the data be altered after protection +* +* Shorthand for pmalloc_create_custom_pool() with default argument: +* * refill is set to PMALLOC_REFILL_DEFAULT +* * align_order is set to PMALLOC_ALIGN_ORDER_DEFAULT +* +* Returns: +* * pointer to the new pool - success +* * NULL - error +*/ +static inline struct pmalloc_pool *pmalloc_create_pool (uint8_t mode) +{ +return pmalloc_create_custom_pool (PMALLOC_REFILL_DEFAULT , +PMALLOC_ALIGN_ORDER_DEFAULT , + mode); +} + +void *pmalloc (struct pmalloc_pool *pool,size_t size); + +/** +* pzalloc() - zero-initialized version of pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @size: amount of memory (in bytes) requested +* +* Executes pmalloc(), initializing the memory requested to 0, before +* returning its address. +* +* Return: +* * pointer to the memory requested - success +* * NULL - error +*/ +static inline void *pzalloc (struct pmalloc_pool *pool,size_t size) +{ +void *ptr =pmalloc (pool, size); + +if (unlikely (!ptr)) +return ptr; +if ((pool->mode &PMALLOC_MODE_START_WR ) ==PMALLOC_MODE_START_WR ) +wr_memset (ptr,0 , size); +else +memset (ptr,0 , size); +return ptr; +} + +/** +* pmalloc_array() - array version of pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @n: number of elements in the array +* @size: amount of memory (in bytes) requested for each element +* +* Executes pmalloc(), on an array. +* +* Return: +* * the pmalloc result - success +* * NULL - error +*/ + +static inline +void *pmalloc_array (struct pmalloc_pool *pool,size_t n,size_t size) +{ +size_t total_size = n * size; + +if (unlikely (!(n && (total_size / n == size)))) +return NULL ; +return pmalloc (pool, n * size); +} + +/** +* pcalloc() - array version of pzalloc() +* @pool: handle to the pool to be used for memory allocation +* @n: number of elements in the array +* @size: amount of memory (in bytes) requested for each element +* +* Executes pzalloc(), on an array. +* +* Return: +* * the pmalloc result - success +* * NULL - error +*/ +static inline +void *pcalloc (struct pmalloc_pool *pool,size_t n,size_t size) +{ +size_t total_size = n * size; + +if (unlikely (!(n && (total_size / n == size)))) +return NULL ; +return pzalloc (pool, n * size); +} + +/** +* pstrdup() - duplicate a string, using pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @s: string to duplicate +* +* Generates a copy of the given string, allocating sufficient memory +* from the given pmalloc pool. +* +* Return: +* * pointer to the replica - success +* * NULL - error +*/ +static inline char *pstrdup (struct pmalloc_pool *pool,const char *s) +{ +size_t len; +char *buf; + + len =strlen (s) +1 ; + buf =pmalloc (pool, len); +if (unlikely (!buf)) +return buf; +if ((pool->mode &PMALLOC_MODE_START_WR ) ==PMALLOC_MODE_START_WR ) +wr_memcpy (buf, s, len); +else +strncpy (buf, s, len); +return buf; +} + + +void pmalloc_protect_pool (struct pmalloc_pool *pool); + +void pmalloc_make_pool_ro (struct pmalloc_pool *pool); + +void pmalloc_destroy_pool (struct pmalloc_pool *pool);#endif
mm/Kconfig +6 -0
@@ -764,4 +764,10 @@ config GUP_BENCHMARK config ARCH_HAS_PTE_SPECIAL bool +config PRMEM + bool + depends on MMU + depends on ARCH_HAS_SET_MEMORY + default y + endmenu
mm/prmem.c +263 -0
268 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 5/17 prmem: shorthands for write rare on common types
In reply to Igor StoppaWrappers around the basic write rare functionality, addressing several common data types found in the kernel, allowing to specify the new values through immediates, like constants and defines.
Note:
The list is not complete and could be expanded.
--- MAINTAINERS | 1 + include/linux/prmemextra.h | 133 +++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 include/linux/prmemextra.h
MAINTAINERS +1 -0
@@ -9459,6 +9459,7 @@ M: Igor Stoppa <[email protected]> L: [email protected] S: Maintained F: include/linux/prmem.h +F: include/linux/prmemextra.h F: mm/prmem.c MEMORY MANAGEMENT
include/linux/prmemextra.h new file +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 05/17] prmem: shorthands for write rare on common types
Quoted 3 lines · Igor Stoppa
Wrappers around the basic write rare functionality, addressing several common data types found in the kernel, allowing to specify the new values through immediates, like constants and defines.
I have to wonder whether this is the right way, or whether a size-agnostic function like put_user() is the right way. put_user() is certainly easier to use.
Re: [PATCH 05/17] prmem: shorthands for write rare on common types
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Igor Stoppa
Wrappers around the basic write rare functionality, addressing several common data types found in the kernel, allowing to specify the new values through immediates, like constants and defines.
I have to wonder whether this is the right way, or whether a size-agnostic function like
put_user()is the right way.put_user()is certainly easier to use.
I definitely did not like it either.
But it was the best that came to my mind ...
The main purpose of this code was to show what I wanted to do. Once more, thanks for pointing out a better way to do it.
--
igor
PATCH 6/17 prmem: test cases for memory protection
In reply to Igor StoppaThe test cases verify the various interfaces offered by both prmem.h and prmemextra.h
The tests avoid triggering crashes, by not performing actions that would be treated as illegal. That part is handled in the lkdtm patch.
--- MAINTAINERS | 2 + mm/Kconfig.debug | 9 + mm/Makefile | 1 + mm/test_pmalloc.c | 633 +++++++++++++++++++++++++++++++++++++++++++ mm/test_write_rare.c | 236 ++++++++++++++++ 5 files changed, 881 insertions(+) create mode 100644 mm/test_pmalloc.c create mode 100644 mm/test_write_rare.c
MAINTAINERS +2 -0
@@ -9461,6 +9461,8 @@ S: Maintained F: include/linux/prmem.h F: include/linux/prmemextra.h F: mm/prmem.c +F: mm/test_write_rare.c +F: mm/test_pmalloc.c MEMORY MANAGEMENT L: [email protected]
mm/Kconfig.debug +9 -0
@@ -94,3 +94,12 @@ config DEBUG_RODATA_TEST depends on STRICT_KERNEL_RWX ---help--- This option enables a testcase for the setting rodata read-only. + +config DEBUG_PRMEM_TEST + tristate "Run self test for protected memory" + depends on STRICT_KERNEL_RWX + select PRMEM + default n + help + Tries to verify that the memory protection works correctly and that + the memory is effectively protected.
mm/Makefile +1 -0
@@ -65,6 +65,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_PRMEM) += prmem.o +obj-$(CONFIG_DEBUG_PRMEM_TEST) += test_write_rare.o test_pmalloc.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/test_pmalloc.c new file +633 -0
633 lines not on this page · raw message
mm/test_write_rare.c new file +236 -0
236 lines not on this page · raw message
Signature · 1 line
2.17.1
Randy Dunlap
reviewerRe: [PATCH 06/17] prmem: test cases for memory protection
Quoted 7 lines · Igor Stoppa
mm/Kconfig.debug +0
-0@@ -94,3 +94,12 @@ config DEBUG_RODATA_TEST depends on STRICT_KERNEL_RWX ---help---
mm/Kconfig.debug config DEBUG_RODATA_TEST · line 105
Quoted 10 lines
mm/Kconfig.debug +9
-0This option enables a testcase for the setting rodata read-only. + +config DEBUG_PRMEM_TEST + tristate "Run self test for protected memory" + depends on STRICT_KERNEL_RWX + select PRMEM + default n + help + Tries to verify that the memory protection works correctly and that + the memory is effectively protected.
Hi,
a. It seems backwards (or upside down) to have a test case select a feature (PRMEM) instead of depending on that feature.
b. Since PRMEM depends on MMU (in patch 04/17), the "select" here could try to enabled PRMEM even when MMU is not enabled.
Changing this to "depends on PRMEM" would solve both of these issues.
c. Don't use "default n". That is already the default.
thanks,
Signature · 1 line
~Randy
Re: [PATCH 06/17] prmem: test cases for memory protection
Hi,
Quoted 7 lines · Randy Dunlap
a. It seems backwards (or upside down) to have a test case select a feature (PRMEM) instead of depending on that feature.
b. Since PRMEM depends on MMU (in patch 04/17), the "select" here could try to enabled PRMEM even when MMU is not enabled.
Changing this to "depends on PRMEM" would solve both of these issues.
The weird dependency you pointed out is partially caused by the incompleteness of PRMEM.
What I have in mind is to have a fallback version of it for systems without MMU capable of write protection. Possibly defaulting to kvmalloc.
In that case there would not be any need for a configuration option.
Quoted 1 line
c. Don't use "default n". That is already the default.
ok
--
igor
Re: [PATCH 06/17] prmem: test cases for memory protection
In reply to Igor Stoppamm/test_pmalloc.c line 43
Quoted 11 lines
mm/test_pmalloc.c new file +11
-0+static bool is_address_protected (void *p) +{ +struct page *page; +struct vmap_area *area; + +if (unlikely (!is_vmalloc_addr (p))) +return false; + page =vmalloc_to_page (p); +if (unlikely (!page)) +return false; +wmb ();/* Flush changes to the page table - is it needed? */
No.
The rest of this is just pretty verbose and seems to have been very heavily copied and pasted. I guess that's OK for test code, though.
Re: [PATCH 06/17] prmem: test cases for memory protection
Quoted 5 lines · Dave Hansen
Quoted 5 lines
mm/test_pmalloc.c new file +5
-0+static bool is_address_protected (void *p) +{ +struct page *page; +struct vmap_area *area; +
Quoted 8 lines
mm/test_pmalloc.c line 43
Quoted 6 lines
mm/test_pmalloc.c new file +6
-0+if (unlikely (!is_vmalloc_addr (p))) +return false; + page =vmalloc_to_page (p); +if (unlikely (!page)) +return false; +wmb ();/* Flush changes to the page table - is it needed? */ No.
ok
Quoted 2 lines
The rest of this is just pretty verbose and seems to have been very heavily copied and pasted. I guess that's OK for test code, though.
I was tempted to play with macros, as templates to generate tests on the fly, according to the type being passed.
But I was afraid it might generate an even stronger rejection than the rest of the patchset already has.
Would it be acceptable/preferable?
--
igor
PATCH 7/17 prmem: lkdtm tests for memory protection
In reply to Igor StoppaVarious cases meant to verify that illegal operations on protected memory will either BUG() or WARN().
The test cases fall into 2 main categories:
- trying to overwrite (directly) something that is write protected
- trying to use write rare functions on something that is not write rare
--- drivers/misc/lkdtm/core.c | 13 ++ drivers/misc/lkdtm/lkdtm.h | 13 ++ drivers/misc/lkdtm/perms.c | 248 +++++++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+)
drivers/misc/lkdtm/core.c +13 -0
@@ -155,6 +155,19 @@ static const struct crashtype crashtypes[] = {CRASHTYPE (ACCESS_USERSPACE ),CRASHTYPE (WRITE_RO ),CRASHTYPE (WRITE_RO_AFTER_INIT ), +CRASHTYPE (WRITE_WR_AFTER_INIT ), +CRASHTYPE (WRITE_WR_AFTER_INIT_ON_RO_AFTER_INIT ), +CRASHTYPE (WRITE_WR_AFTER_INIT_ON_CONST ), +#ifdef CONFIG_PRMEM +CRASHTYPE (WRITE_RO_PMALLOC ), +CRASHTYPE (WRITE_AUTO_RO_PMALLOC ), +CRASHTYPE (WRITE_WR_PMALLOC ), +CRASHTYPE (WRITE_AUTO_WR_PMALLOC ), +CRASHTYPE (WRITE_START_WR_PMALLOC ), +CRASHTYPE (WRITE_WR_PMALLOC_ON_RO_PMALLOC ), +CRASHTYPE (WRITE_WR_PMALLOC_ON_CONST ), +CRASHTYPE (WRITE_WR_PMALLOC_ON_RO_AFT_INIT ), +#endifCRASHTYPE (WRITE_KERN ),CRASHTYPE (REFCOUNT_INC_OVERFLOW ), CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
drivers/misc/lkdtm/lkdtm.h +13 -0
@@ -38,6 +38,19 @@ void lkdtm_READ_BUDDY_AFTER_FREE(void);void __init lkdtm_perms_init (void );void lkdtm_WRITE_RO (void );void lkdtm_WRITE_RO_AFTER_INIT (void ); +void lkdtm_WRITE_WR_AFTER_INIT (void ); +void lkdtm_WRITE_WR_AFTER_INIT_ON_RO_AFTER_INIT (void ); +void lkdtm_WRITE_WR_AFTER_INIT_ON_CONST (void ); +#ifdef CONFIG_PRMEM +void lkdtm_WRITE_RO_PMALLOC (void ); +void lkdtm_WRITE_AUTO_RO_PMALLOC (void ); +void lkdtm_WRITE_WR_PMALLOC (void ); +void lkdtm_WRITE_AUTO_WR_PMALLOC (void ); +void lkdtm_WRITE_START_WR_PMALLOC (void ); +void lkdtm_WRITE_WR_PMALLOC_ON_RO_PMALLOC (void ); +void lkdtm_WRITE_WR_PMALLOC_ON_CONST (void ); +void lkdtm_WRITE_WR_PMALLOC_ON_RO_AFT_INIT (void ); +#endif void lkdtm_WRITE_KERN (void );void lkdtm_EXEC_DATA (void );void lkdtm_EXEC_STACK (void );
drivers/misc/lkdtm/perms.c +248 -0
270 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 8/17 prmem: struct page: track vmap_area
In reply to Igor StoppaWhen a page is used for virtual memory, it is often necessary to obtain a handler to the corresponding vmap_area, which refers to the virtually continuous area generated, when invoking vmalloc.
The struct page has a "private" field, which can be re-used, to store a pointer to the parent area.
Note: in practice a virtual memory area is characterized both by a struct vmap_area and a struct vm_struct.
The reason for referring from a page to its vmap_area, rather than to the vm_struct, is that the vmap_area contains a struct vm_struct *vm field, which can be used to reach also the information stored in the corresponding vm_struct. This link, however, is unidirectional, and it's not possible to easily identify the corresponding vmap_area, given a reference to a vm_struct.
Furthermore, the struct vmap_area contains a list head node which is normally used only when it's queued for free and can be put to some other use during normal operations.
The connection between each page and its vmap_area avoids more expensive searches through the btree of vmap_areas.
Therefore, it is possible for find_vamp_area to be again a static function, while the rest of the code will rely on the direct reference from struct page.
--- include/linux/mm_types.h | 25 ++++++++++++++++++------- include/linux/prmem.h | 13 ++++++++----- include/linux/vmalloc.h | 1 - mm/prmem.c | 2 +- mm/test_pmalloc.c | 12 ++++-------- mm/vmalloc.c | 9 ++++++++- 6 files changed, 39 insertions(+), 23 deletions(-)
include/linux/mm_types.h +18 -7
@@ -87,13 +87,24 @@ struct page {/* See page-flags.h for PAGE_MAPPING_FLAGS */ struct address_space *mapping;pgoff_t index;/* Our offset within mapping. */ -/** -* @private: Mapping-private opaque data. -* Usually used for buffer_heads if PagePrivate. -* Used for swp_entry_t if PageSwapCache. -* Indicates order in the buddy system if PageBuddy. -*/ -unsigned long private; +union { +/** +* @private: Mapping-private opaque data. +* Usually used for buffer_heads if +* PagePrivate. +* Used for swp_entry_t if PageSwapCache. +* Indicates order in the buddy system if +* PageBuddy. +*/ +unsigned long private; +/** +* @area: reference to the containing area +* For pages that are mapped into a virtually +* contiguous area, avoids performing a more +* expensive lookup. +*/ +struct vmap_area *area; + }; }; struct { /* slab, slob and slub */ union {
include/linux/prmem.h +8 -5
@@ -54,14 +54,17 @@ static __always_inline bool __is_wr_after_init(const void *ptr, size_t size) static __always_inline bool __is_wr_pool(const void *ptr, size_t size) { -struct vmap_area *area; +struct vm_struct *vm; +struct page *page;if (!is_vmalloc_addr (ptr))return false; - area =find_vmap_area ((unsigned long )ptr); -return area && area->vm && (area->vm->size >= size) && - ((area->vm->flags & (VM_PMALLOC |VM_PMALLOC_WR )) == - (VM_PMALLOC |VM_PMALLOC_WR )); + page =vmalloc_to_page (ptr); +if (!(page && page->area && page->area->vm)) +return false; + vm = page->area->vm; +return ((vm->size >= size) && + ((vm->flags &VM_PMALLOC_WR_MASK ) ==VM_PMALLOC_WR_MASK )); }/**
include/linux/vmalloc.h +0 -1
@@ -143,7 +143,6 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size, const void *caller);extern struct vm_struct *remove_vm_area (const void *addr);extern struct vm_struct *find_vm_area (const void *addr); -extern struct vmap_area *find_vmap_area (unsigned long addr);extern int map_vm_area (struct vm_struct *area,pgprot_t prot,struct page **pages);
mm/prmem.c +1 -1
@@ -150,7 +150,7 @@ static int grow(struct pmalloc_pool *pool, size_t min_size)if (WARN (!addr,"Failed to allocate %zd bytes" ,PAGE_ALIGN (size)))return -ENOMEM ; - new_area =find_vmap_area ((uintptr_t )addr); + new_area =vmalloc_to_page (addr)->area; tag_mask =VM_PMALLOC ;if (pool->mode &PMALLOC_WR ) tag_mask |=VM_PMALLOC_WR ;
mm/test_pmalloc.c +4 -8
@@ -38,15 +38,11 @@ static bool is_address_protected(void *p)if (unlikely (!is_vmalloc_addr (p)))return false; page =vmalloc_to_page (p); -if (unlikely (!page)) +if (unlikely (!(page && page->area && page->area->vm)))return false; -wmb ();/* Flush changes to the page table - is it needed? */ - area =find_vmap_area ((uintptr_t )p); -if (unlikely ((!area) || (!area->vm) || - ((area->vm->flags &VM_PMALLOC_PROTECTED_MASK ) != -VM_PMALLOC_PROTECTED_MASK ))) -return false; -return true; + area = page->area; +return (area->vm->flags &VM_PMALLOC_PROTECTED_MASK ) == +VM_PMALLOC_PROTECTED_MASK ; }static bool create_and_destroy_pool (void )
mm/vmalloc.c +8 -1
@@ -742,7 +742,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)free_vmap_area_noflush (va); } -struct vmap_area *find_vmap_area (unsigned long addr) +static struct vmap_area *find_vmap_area (unsigned long addr) {struct vmap_area *va;
@@ -1523,6 +1523,7 @@ static void __vunmap(const void *addr, int deallocate_pages)struct page *page = area->pages[i];BUG_ON (!page); + page->area =NULL ;__free_pages (page,0 ); }
@@ -1731,8 +1732,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, const void *caller) {struct vm_struct *area; +struct vmap_area *va;void *addr;unsigned long real_size = size; +unsigned int i; size =PAGE_ALIGN (size);if (!size || (size >>PAGE_SHIFT ) > totalram_pages)
@@ -1747,6 +1750,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; + va =__find_vmap_area ((unsigned long )addr); +for (i =0 ; i < va->vm->nr_pages; i++) + va->vm->pages[i]->area = va; +/* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized.
Signature · 1 line
2.17.1
Re: [PATCH 08/17] prmem: struct page: track vmap_area
Quoted 2 lines · Igor Stoppa
The connection between each page and its
vmap_areaavoids more expensive searches through the btree ofvmap_areas.
Typo -- it's an rbtree.
Quoted 20 lines
+++
b/include/linux/mm_types.hinclude/linux/mm_types.h +8
-7@@ -87,13 +87,24 @@ struct page {/* See page-flags.h for PAGE_MAPPING_FLAGS */ struct address_space *mapping;pgoff_t index;/* Our offset within mapping. */ -/** -* @private: Mapping-private opaque data. -* Usually used for buffer_heads if PagePrivate. -* Used for swp_entry_t if PageSwapCache. -* Indicates order in the buddy system if PageBuddy. -*/ -unsigned long private; +union { +/** +* @private: Mapping-private opaque data. +* Usually used for buffer_heads if +* PagePrivate. +* Used for swp_entry_t if PageSwapCache. +* Indicates order in the buddy system if +* PageBuddy.
include/linux/mm_types.h struct page { · line 107
Quoted 10 lines
include/linux/mm_types.h +10
-0+*/ +unsigned long private; +/** +* @area: reference to the containing area +* For pages that are mapped into a virtually +* contiguous area, avoids performing a more +* expensive lookup. +*/ +struct vmap_area *area; + };
Not like this. Make it part of a different struct in the existing union, not a part of the pagecache struct. And there's no need to use ->private explicitly.
mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 1755
Quoted 7 lines
mm/vmalloc.c +3
-0@@ -1747,6 +1750,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; + va =__find_vmap_area ((unsigned long )addr); +for (i =0 ; i < va->vm->nr_pages; i++) + va->vm->pages[i]->area = va;
I don't like it that you're calling this for _every_ vmalloc() caller when most of them will never use this. Perhaps have page->va be initially NULL and then cache the lookup in it when it's accessed for the first time.
Re: [PATCH 08/17] prmem: struct page: track vmap_area
Quoted 5 lines · Matthew Wilcox
Quoted 2 lines · Igor Stoppa
The connection between each page and its
vmap_areaavoids more expensive searches through the btree ofvmap_areas.Typo -- it's an rbtree.
ack
Quoted 26 lines
Quoted 26 lines
+++
b/include/linux/mm_types.hinclude/linux/mm_types.h +14
-7@@ -87,13 +87,24 @@ struct page {/* See page-flags.h for PAGE_MAPPING_FLAGS */ struct address_space *mapping;pgoff_t index;/* Our offset within mapping. */ -/** -* @private: Mapping-private opaque data. -* Usually used for buffer_heads if PagePrivate. -* Used for swp_entry_t if PageSwapCache. -* Indicates order in the buddy system if PageBuddy. -*/ -unsigned long private; +union { +/** +* @private: Mapping-private opaque data. +* Usually used for buffer_heads if +* PagePrivate. +* Used for swp_entry_t if PageSwapCache. +* Indicates order in the buddy system if +* PageBuddy. +*/ +unsigned long private; +/** +* @area: reference to the containing area +* For pages that are mapped into a virtually +* contiguous area, avoids performing a more
Quoted 8 lines
include/linux/mm_types.h struct page { · line 107
Quoted 4 lines
include/linux/mm_types.h +4
-0+* expensive lookup. +*/ +struct vmap_area *area; + };Not like this. Make it part of a different struct in the existing union, not a part of the pagecache struct. And there's no need to use ->private explicitly.
Ok, I'll have a look at the googledoc you made
Quoted 4 lines
Quoted 4 lines
mm/vmalloc.c +0
-0@@ -1747,6 +1750,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ;
Quoted 8 lines
mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 1755
Quoted 3 lines
mm/vmalloc.c +3
-0+ va =__find_vmap_area ((unsigned long )addr); +for (i =0 ; i < va->vm->nr_pages; i++) + va->vm->pages[i]->area = va;I don't like it that you're calling this for _every_
vmalloc()caller when most of them will never use this. Perhaps havepage->vabe initially NULL and then cache the lookup in it when it's accessed for the first time.
If __find_vmap_area() was part of the API, this loop could be left out from __vmalloc_node_range() and the user of the allocation could initialize the field, if needed.
What is the reason for keeping __find_vmap_area() private?
--
igor
Re: [PATCH 08/17] prmem: struct page: track vmap_area
Quoted 12 lines · Igor Stoppa
Quoted 11 lines
mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 1754
Quoted 6 lines
mm/vmalloc.c +3
-0@@ -1747,6 +1750,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; + va =__find_vmap_area ((unsigned long )addr); +for (i =0 ; i < va->vm->nr_pages; i++) + va->vm->pages[i]->area = va;I don't like it that you're calling this for _every_
vmalloc()caller when most of them will never use this. Perhaps havepage->vabe initially NULL and then cache the lookup in it when it's accessed for the first time.
Quoted 5 lines
If
__find_vmap_area()was part of the API, this loop could be left out from__vmalloc_node_range()and the user of the allocation could initialize the field, if needed.What is the reason for keeping
__find_vmap_area()private?
Well, for one, you're walking the rbtree without holding the spinlock, so you're going to get crashes. I don't see why we shouldn't export find_vmap_area() though.
Another way we could approach this is to embed the vmap_area in the vm_struct. It'd require a bit of juggling of the alloc/free paths in vmalloc, but it might be worthwhile.
Re: [PATCH 08/17] prmem: struct page: track vmap_area
Quoted 17 lines · Matthew Wilcox
Quoted 16 lines · Igor Stoppa
Quoted 11 lines
mm/vmalloc.c void *__vmalloc_node_range(unsigned long size, unsigned long align, · line 1754
Quoted 6 lines
mm/vmalloc.c +3
-0@@ -1747,6 +1750,10 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,if (!addr)return NULL ; + va =__find_vmap_area ((unsigned long )addr); +for (i =0 ; i < va->vm->nr_pages; i++) + va->vm->pages[i]->area = va;I don't like it that you're calling this for _every_
vmalloc()caller when most of them will never use this. Perhaps havepage->vabe initially NULL and then cache the lookup in it when it's accessed for the first time.If
__find_vmap_area()was part of the API, this loop could be left out from__vmalloc_node_range()and the user of the allocation could initialize the field, if needed.
Quoted 5 lines
Quoted 1 line
What is the reason for keeping
__find_vmap_area()private?Well, for one, you're walking the rbtree without holding the spinlock, so you're going to get crashes. I don't see why we shouldn't export
find_vmap_area()though.
Argh, yes, sorry. But find_vmap_area() would be enough for what I need.
Quoted 3 lines
Another way we could approach this is to embed the
vmap_areain thevm_struct. It'd require a bit of juggling of the alloc/free paths in vmalloc, but it might be worthwhile.
I have a feeling of unease about the whole vmap_area / vm_struct duality. They clearly are different types, with different purposes, but here and there there are functions that are named after some "area", yet they actually refer to vm_struct pointers.
I wouldn't mind spending some time understanding the reason for this apparently bizarre choice, but after I have emerged from the prmem swamp.
For now I'd stick to find_vmap_area().
--
igor
PATCH 9/17 prmem: hardened usercopy
In reply to Igor StoppaPrevent leaks of protected memory to userspace.
The protection from overwrited from userspace is already available, once the memory is write protected.
--- include/linux/prmem.h | 24 ++++++++++++++++++++++++ mm/usercopy.c | 5 +++++ 2 files changed, 29 insertions(+)
include/linux/prmem.h +24 -0
@@ -273,6 +273,30 @@ struct pmalloc_pool {uint8_t mode; }; +void __noreturn usercopy_abort (const char *name,const char *detail, +bool to_user,unsigned long offset, +unsigned long len); + +/** +* check_pmalloc_object() - helper for hardened usercopy +* @ptr: the beginning of the memory to check +* @n: the size of the memory to check +* @to_user: copy to userspace or from userspace +* +* If the check is ok, it will fall-through, otherwise it will abort. +* The function is inlined, to minimize the performance impact of the +* extra check that can end up on a hot path. +* Non-exhaustive micro benchmarking with QEMU x86_64 shows a reduction of +* the time spent in this fragment by 60%, when inlined. +*/ +static inline +void check_pmalloc_object (const void *ptr,unsigned long n,bool to_user) +{ +if (unlikely (__is_wr_after_init (ptr, n) ||__is_wr_pool (ptr, n))) +usercopy_abort ("pmalloc" ,"accessing pmalloc obj" , to_user, + (const unsigned long )ptr, n); +} +/* * The write rare functionality is fully implemented as __always_inline, * to prevent having an internal function call that is capable of modifying
mm/usercopy.c +5 -0
@@ -22,8 +22,10 @@#include <linux/thread_info.h> #include <linux/atomic.h> #include <linux/jump_label.h> +#include <linux/prmem.h> #include <asm/sections.h> +/* * Checks if a given pointer and length is contained by the current * stack frame (if possible).
@@ -284,6 +286,9 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ check_kernel_text_object ((const unsigned long )ptr, n, to_user); + +/* Check if object is from a pmalloc chunk. */ +check_pmalloc_object (ptr, n, to_user); }EXPORT_SYMBOL (__check_object_size);
Signature · 1 line
2.17.1
Re: [PATCH 09/17] prmem: hardened usercopy
Quoted 65 lines · Igor Stoppa
Prevent leaks of protected memory to userspace.
The protection from overwrited from userspace is already available, once the memory is write protected.Signed-off-by Igor Stoppa <[email protected]>CC Kees Cook <[email protected]>CC Chris von Recklinghausen <[email protected]>--- include/linux/prmem.h | 24 ++++++++++++++++++++++++ mm/usercopy.c | 5 +++++ 2 files changed, 29 insertions(+)include/linux/prmem.h +24
-0@@ -273,6 +273,30 @@ struct pmalloc_pool {uint8_t mode; }; +void __noreturn usercopy_abort (const char *name,const char *detail, +bool to_user,unsigned long offset, +unsigned long len); + +/** +* check_pmalloc_object() - helper for hardened usercopy +* @ptr: the beginning of the memory to check +* @n: the size of the memory to check +* @to_user: copy to userspace or from userspace +* +* If the check is ok, it will fall-through, otherwise it will abort. +* The function is inlined, to minimize the performance impact of the +* extra check that can end up on a hot path. +* Non-exhaustive micro benchmarking with QEMU x86_64 shows a reduction of +* the time spent in this fragment by 60%, when inlined. +*/ +static inline +void check_pmalloc_object (const void *ptr,unsigned long n,bool to_user) +{ +if (unlikely (__is_wr_after_init (ptr, n) ||__is_wr_pool (ptr, n))) +usercopy_abort ("pmalloc" ,"accessing pmalloc obj" , to_user, + (const unsigned long )ptr, n); +} +/* * The write rare functionality is fully implemented as __always_inline, * to prevent having an internal function call that is capable of modifying mm/usercopy.c +2
-0@@ -22,8 +22,10 @@#include <linux/thread_info.h> #include <linux/atomic.h> #include <linux/jump_label.h> +#include <linux/prmem.h> #include <asm/sections.h> +/* * Checks if a given pointer and length is contained by the current * stack frame (if possible).
mm/usercopy.c void __check_object_size(const void *ptr, unsigned long n, bool to_user) · line 294
Quoted 9 lines
mm/usercopy.c +3
-0@@ -284,6 +286,9 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ check_kernel_text_object ((const unsigned long )ptr, n, to_user); + +/* Check if object is from a pmalloc chunk. */ +check_pmalloc_object (ptr, n, to_user); }EXPORT_SYMBOL (__check_object_size);
Could you add code somewhere (lkdtm driver if possible) to demonstrate the issue and verify the code change?
Thanks,
Chris
Re: [PATCH 09/17] prmem: hardened usercopy
On 29/10/2018 11:45, Chris von Recklinghausen wrote:
[...]
Quoted 2 lines
Could you add code somewhere (lkdtm driver if possible) to demonstrate the issue and verify the code change?
Sure.
Eventually, I'd like to add test cases for each functionality.
I didn't do it right away for those parts which are either not immediately needed for the main functionality or I'm still not confident enough that they won't change radically.
--
igor
PATCH 10/17 prmem: documentation
In reply to Igor StoppaDocumentation for protected memory.
Topics covered:
* static memory allocation
* dynamic memory allocation
* write-rare
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/prmem.rst | 172 +++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 174 insertions(+) create mode 100644 Documentation/core-api/prmem.rst
Documentation/core-api/index.rst +1 -0
@@ -31,6 +31,7 @@ Core utilities gfp_mask-from-fs-io timekeeping boot-time-mm + prmem Interfaces for kernel debugging ===============================
Documentation/core-api/prmem.rst new file +172 -0
172 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -9463,6 +9463,7 @@ F: include/linux/prmemextra.h F: mm/prmem.c F: mm/test_write_rare.c F: mm/test_pmalloc.c +F: Documentation/core-api/prmem.rst MEMORY MANAGEMENT L: [email protected]
Signature · 1 line
2.17.1
Randy Dunlap
reviewerRe: [PATCH 10/17] prmem: documentation
Hi,
Quoted 38 lines · Igor Stoppa
Documentation for protected memory.
Topics covered:
* static memory allocation
* dynamic memory allocation
* write-rareSigned-off-by Igor Stoppa <[email protected]>CC Jonathan Corbet <[email protected]>CC Randy Dunlap <[email protected]>CC Mike Rapoport <[email protected]>--- Documentation/core-api/index.rst | 1 + Documentation/core-api/prmem.rst | 172 +++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 174 insertions(+) create mode 100644 Documentation/core-api/prmem.rstDocumentation/core-api/prmem.rst new file +11
-0@@ -0,0 +1,172 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _prmem: + +Memory Protection +================= + +:Date: October 2018 +:Author: Igor Stoppa <[email protected]> + +Foreword
Documentation/core-api/prmem.rst line 21
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+-------- +- In a typical system using some sort of RAM as execution environment, + **all** memory is initially writable. + +- It must be initialized with the appropriate content, be it code or data. + +- Said content typically undergoes modifications, i.e. relocations or + relocation-induced changes. + +- The present document doesn't address such transient.
transience.
Quoted 22 lines
Documentation/core-api/prmem.rst new file +22
-0+ +- Kernel code is protected at system level and, unlike data, it doesn't + require special attention. + +Protection mechanism +-------------------- + +- When available, the MMU can write protect memory pages that would be + otherwise writable. + +- The protection has page-level granularity. + +- An attempt to overwrite a protected page will trigger an exception. +- **Write protected data must go exclusively to write protected pages** +- **Writable data must go exclusively to writable pages** + +Available protections for kernel data +------------------------------------- + +- **constant** + Labelled as **const**, the data is never supposed to be altered. + It is statically allocated - if it has any memory footprint at all.
Documentation/core-api/prmem.rst line 53
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ The compiler can even optimize it away, where possible, by replacing + references to a **const** with its actual value. + +- **read only after init** + By tagging an otherwise ordinary statically allocated variable with + **__ro_after_init**, it is placed in a special segment that will + become write protected, at the end of the kernel init phase. + The compiler has no notion of this restriction and it will treat any + write operation on such variable as legal. However, assignments that + are attempted after the write protection is in place, will cause
no comma.
Quoted 50 lines
Documentation/core-api/prmem.rst new file +50
-0+ exceptions. + +- **write rare after init** + This can be seen as variant of read only after init, which uses the + tag **__wr_after_init**. It is also limited to statically allocated + memory. It is still possible to alter this type of variables, after + the kernel init phase is complete, however it can be done exclusively + with special functions, instead of the assignment operator. Using the + assignment operator after conclusion of the init phase will still + trigger an exception. It is not possible to transition a certain + variable from __wr_ater_init to a permanent read-only status, at + runtime. + +- **dynamically allocated write-rare / read-only** + After defining a pool, memory can be obtained through it, primarily + through the **pmalloc()** allocator. The exact writability state of the + memory obtained from **pmalloc()** and friends can be configured when + creating the pool. At any point it is possible to transition to a less + permissive write status the memory currently associated to the pool. + Once memory has become read-only, it the only valid operation, beside + reading, is to released it, by destroying the pool it belongs to. + + +Protecting dynamically allocated memory +--------------------------------------- + +When dealing with dynamically allocated memory, three options are + available for configuring its writability state: + +- **Options selected when creating a pool** + When creating the pool, it is possible to choose one of the following: + - **PMALLOC_MODE_RO** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_WR** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_AUTO_RO** + - Writability at allocation time: + - the latest allocation: *WRITABLE* + - every other allocation: *NONE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_AUTO_WR** + - Writability at allocation time: + - the latest allocation: *WRITABLE* + - every other allocation: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_START_WR** + - Writability at allocation time: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE*
Documentation/core-api/prmem.rst line 113
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ + **Remarks:** + - The "AUTO" modes perform automatic protection of the content, whenever + the current vmap_area is used up and a new one is allocated. + - At that point, the vmap_area being phased out is protected. + - The size of the vmap_area depends on various parameters. + - It might not be possible to know for sure *when* certain data will + be protected. + - The functionality is provided as tradeoff between hardening and speed. + - Its usefulness depends on the specific use case at hand
end above sentence with a period, please, like all of the others above it.
Documentation/core-api/prmem.rst line 114
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+ - The "START_WR" mode is the only one which provides immediate protection, at the cost of speed.
Please try to keep the line above and a few below to < 80 characters in length. (because some of us read rst files as text files, with a text editor, and line wrap is ugly)
Quoted 24 lines
Documentation/core-api/prmem.rst new file +24
-0+ +- **Protecting the pool** + This is achieved with **pmalloc_protect_pool()** + - Any vmap_area currently in the pool is write-protected according to its initial configuration. + - Any residual space still available from the current vmap_area is lost, as the area is protected. + - **protecting a pool after every allocation will likely be very wasteful** + - Using PMALLOC_MODE_START_WR is likely a better choice. + +- **Upgrading the protection level** + This is achieved with **pmalloc_make_pool_ro()** + - it turns the present content of a write-rare pool into read-only + - can be useful when the content of the memory has settled + + +Caveats +------- +- Freeing of memory is not supported. Pages will be returned to the + system upon destruction of their memory pool. + +- The address range available for vmalloc (and thus for pmalloc too) is + limited, on 32-bit systems. However it shouldn't be an issue, since not + much data is expected to be dynamically allocated and turned into + write-protected. +
Documentation/core-api/prmem.rst line 148
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+- Regarding SMP systems, changing state of pages and altering mappings + requires performing cross-processor synchronizations of page tables. + This is an additional reason for limiting the use of write rare. + +- Not only the pmalloc memory must be protected, but also any reference to + it that might become the target for an attack. The attack would replace + a reference to the protected memory with a reference to some other, + unprotected, memory. + +- The users of rare write must take care of ensuring the atomicity of the
s/rare write/write rare/ ?
Documentation/core-api/prmem.rst line 149
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+ action, respect to the way they use the data being altered; for example,
This .. "respect to the way" is awkward, but I don't know what to
change it to.
Documentation/core-api/prmem.rst line 153
Quoted 4 lines
Documentation/core-api/prmem.rst new file +4
-0+ take a lock before making a copy of the value to modify (if it's + relevant), then alter it, issue the call to rare write and finally + release the lock. Some special scenario might be exempt from the need + for locking, but in general rare-write must be treated as an operation
It seemed to me that "write-rare" (or write rare) was the going name, but now it's being called "rare write" (or rare-write). Just be consistent, please.
Documentation/core-api/prmem.rst line 157
Quoted 4 lines
Documentation/core-api/prmem.rst new file +4
-0+ that can incur into races. + +- pmalloc relies on virtual memory areas and will therefore use more + tlb entries. It still does a better job of it, compared to invoking
TLB
Documentation/core-api/prmem.rst line 158
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+ vmalloc for each allocation, but it is undeniably less optimized wrt to
s/wrt/with respect to/
Quoted 4 lines
Documentation/core-api/prmem.rst new file +4
-0+ TLB use than using the physmap directly, through kmalloc or similar. + + +Utilization
Documentation/core-api/prmem.rst line 172
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+----------- + +**add examples here** + +API +--- + +.. kernel-doc:: include/linux/prmem.h +.. kernel-doc:: mm/prmem.c +.. kernel-doc:: include/linux/prmemextra.h
Thanks for the documentation.
Signature · 1 line
~Randy
Re: [PATCH 10/17] prmem: documentation
Hi,
Quoted 3 lines · Randy Dunlap
Hi,
On 10/23/18 2:34 PM, Igor Stoppa wrote:
[...]
Quoted 3 lines
Quoted 1 line
+- The present document doesn't address such transient.
transience.
ok
[...]
Quoted 3 lines
Quoted 1 line
+ are attempted after the write protection is in place, will cause
no comma.
ok
[...]
Quoted 3 lines
Quoted 1 line
+ - Its usefulness depends on the specific use case at hand
end above sentence with a period, please, like all of the others above it.
ok
Quoted 5 lines
Quoted 1 line
+ - The "
START_WR" mode is the only one which provides immediate protection, at the cost of speed.Please try to keep the line above and a few below to < 80 characters in length. (because some of us read rst files as text files, with a text editor, and line wrap is ugly)
ok, I still have to master .rst :-(
[...]
Quoted 3 lines
Quoted 1 line
+- The users of rare write must take care of ensuring the atomicity of the
s/rare write/write rare/ ?
thanks
Quoted 5 lines
Quoted 1 line
+ action, respect to the way they use the data being altered; for example,
This .. "respect to the way" is awkward, but I don't know what to
change it to.
Quoted 7 lines
Documentation/core-api/prmem.rst line 153
Quoted 4 lines
Documentation/core-api/prmem.rst new file +4
-0+ take a lock before making a copy of the value to modify (if it's + relevant), then alter it, issue the call to rare write and finally + release the lock. Some special scenario might be exempt from the need + for locking, but in general rare-write must be treated as an operationIt seemed to me that "write-rare" (or write rare) was the going name, but now it's being called "rare write" (or rare-write). Just be consistent, please.
write-rare it is, because it can be shortened as wr_xxx
rare_write becomes rw_xxx
which wrongly hints at read/write, which it definitely is not
Quoted 3 lines
Quoted 1 line
+ tlb entries. It still does a better job of it, compared to invoking
TLB
ok
Quoted 3 lines
Quoted 1 line
+ vmalloc for each allocation, but it is undeniably less optimized wrt to
s/wrt/with respect to/
yes
Quoted 1 line
Thanks for the documentation.
thanks for the review :-)
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Igor StoppaHi Igor,
Quoted 13 lines · Igor Stoppa
Documentation for protected memory.
Topics covered:
* static memory allocation
* dynamic memory allocation
* write-rareSigned-off-by Igor Stoppa <[email protected]>CC Jonathan Corbet <[email protected]>CC Randy Dunlap <[email protected]>CC Mike Rapoport <[email protected]>
Quoted 3 lines
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/prmem.rst | 172 +++++++++++++++++++++++++++++++
Thanks for having docs a part of the patchset!
Quoted 37 lines
MAINTAINERS | 1 + 3 files changed, 174 insertions(+) create mode 100644 Documentation/core-api/prmem.rstDocumentation/core-api/index.rst +1
-0@@ -31,6 +31,7 @@ Core utilities gfp_mask-from-fs-io timekeeping boot-time-mm + prmem Interfaces for kernel debugging ===============================Documentation/core-api/prmem.rst new file +15
-0@@ -0,0 +1,172 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _prmem: + +Memory Protection +================= + +:Date: October 2018 +:Author: Igor Stoppa <[email protected]> + +Foreword +-------- +- In a typical system using some sort of RAM as execution environment, + **all** memory is initially writable. +
Documentation/core-api/prmem.rst line 25
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+- It must be initialized with the appropriate content, be it code or data. + +- Said content typically undergoes modifications, i.e. relocations or + relocation-induced changes. + +- The present document doesn't address such transient. + +- Kernel code is protected at system level and, unlike data, it doesn't + require special attention. +
I feel that foreword should include a sentence or two saying why we need the memory protection and when it can/should be used.
Quoted 24 lines
Documentation/core-api/prmem.rst new file +24
-0+Protection mechanism +-------------------- + +- When available, the MMU can write protect memory pages that would be + otherwise writable. + +- The protection has page-level granularity. + +- An attempt to overwrite a protected page will trigger an exception. +- **Write protected data must go exclusively to write protected pages** +- **Writable data must go exclusively to writable pages** + +Available protections for kernel data +------------------------------------- + +- **constant** + Labelled as **const**, the data is never supposed to be altered. + It is statically allocated - if it has any memory footprint at all. + The compiler can even optimize it away, where possible, by replacing + references to a **const** with its actual value. + +- **read only after init** + By tagging an otherwise ordinary statically allocated variable with + **__ro_after_init**, it is placed in a special segment that will
Documentation/core-api/prmem.rst line 59
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ become write protected, at the end of the kernel init phase. + The compiler has no notion of this restriction and it will treat any + write operation on such variable as legal. However, assignments that + are attempted after the write protection is in place, will cause + exceptions. + +- **write rare after init** + This can be seen as variant of read only after init, which uses the + tag **__wr_after_init**. It is also limited to statically allocated + memory. It is still possible to alter this type of variables, after
no comma ^
Documentation/core-api/prmem.rst line 64
Quoted 5 lines
Documentation/core-api/prmem.rst new file +5
-0+ the kernel init phase is complete, however it can be done exclusively + with special functions, instead of the assignment operator. Using the + assignment operator after conclusion of the init phase will still + trigger an exception. It is not possible to transition a certain + variable from __wr_ater_init to a permanent read-only status, at
__wr_aFter_init
Documentation/core-api/prmem.rst line 73
Quoted 9 lines
Documentation/core-api/prmem.rst new file +9
-0+ runtime. + +- **dynamically allocated write-rare / read-only** + After defining a pool, memory can be obtained through it, primarily + through the **pmalloc()** allocator. The exact writability state of the + memory obtained from **pmalloc()** and friends can be configured when + creating the pool. At any point it is possible to transition to a less + permissive write status the memory currently associated to the pool. + Once memory has become read-only, it the only valid operation, beside
... become read-only, the only valid operation
Quoted 20 lines
Documentation/core-api/prmem.rst new file +20
-0+ reading, is to released it, by destroying the pool it belongs to. + + +Protecting dynamically allocated memory +--------------------------------------- + +When dealing with dynamically allocated memory, three options are + available for configuring its writability state: + +- **Options selected when creating a pool** + When creating the pool, it is possible to choose one of the following: + - **PMALLOC_MODE_RO** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_WR** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_AUTO_RO** + - Writability at allocation time: + - the latest allocation: *WRITABLE*
Documentation/core-api/prmem.rst line 103
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ - every other allocation: *NONE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_AUTO_WR** + - Writability at allocation time: + - the latest allocation: *WRITABLE* + - every other allocation: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_START_WR** + - Writability at allocation time: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE*
For me this part is completely blind. Maybe arranging this as a table would make the states more clearly visible.
Quoted 22 lines
Documentation/core-api/prmem.rst new file +22
-0+ + **Remarks:** + - The "AUTO" modes perform automatic protection of the content, whenever + the current vmap_area is used up and a new one is allocated. + - At that point, the vmap_area being phased out is protected. + - The size of the vmap_area depends on various parameters. + - It might not be possible to know for sure *when* certain data will + be protected. + - The functionality is provided as tradeoff between hardening and speed. + - Its usefulness depends on the specific use case at hand + - The "START_WR" mode is the only one which provides immediate protection, at the cost of speed. + +- **Protecting the pool** + This is achieved with **pmalloc_protect_pool()** + - Any vmap_area currently in the pool is write-protected according to its initial configuration. + - Any residual space still available from the current vmap_area is lost, as the area is protected. + - **protecting a pool after every allocation will likely be very wasteful** + - Using PMALLOC_MODE_START_WR is likely a better choice. + +- **Upgrading the protection level** + This is achieved with **pmalloc_make_pool_ro()** + - it turns the present content of a write-rare pool into read-only
Documentation/core-api/prmem.rst line 135
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ - can be useful when the content of the memory has settled + + +Caveats +------- +- Freeing of memory is not supported. Pages will be returned to the + system upon destruction of their memory pool. + +- The address range available for vmalloc (and thus for pmalloc too) is + limited, on 32-bit systems. However it shouldn't be an issue, since not
no comma ^
Quoted 42 lines
Documentation/core-api/prmem.rst new file +37
-0+ much data is expected to be dynamically allocated and turned into + write-protected. + +- Regarding SMP systems, changing state of pages and altering mappings + requires performing cross-processor synchronizations of page tables. + This is an additional reason for limiting the use of write rare. + +- Not only the pmalloc memory must be protected, but also any reference to + it that might become the target for an attack. The attack would replace + a reference to the protected memory with a reference to some other, + unprotected, memory. + +- The users of rare write must take care of ensuring the atomicity of the + action, respect to the way they use the data being altered; for example, + take a lock before making a copy of the value to modify (if it's + relevant), then alter it, issue the call to rare write and finally + release the lock. Some special scenario might be exempt from the need + for locking, but in general rare-write must be treated as an operation + that can incur into races. + +- pmalloc relies on virtual memory areas and will therefore use more + tlb entries. It still does a better job of it, compared to invoking + vmalloc for each allocation, but it is undeniably less optimized wrt to + TLB use than using the physmap directly, through kmalloc or similar. + + +Utilization +----------- + +**add examples here** + +API +--- + +.. kernel-doc:: include/linux/prmem.h +.. kernel-doc:: mm/prmem.c +.. kernel-doc:: include/linux/prmemextra.hMAINTAINERS +0
-0
MAINTAINERS F: include/linux/prmemextra.h · line 9466
Quoted 4 lines
MAINTAINERS +1
-0@@ -9463,6 +9463,7 @@ F: include/linux/prmemextra.h F: mm/prmem.c F: mm/test_write_rare.c F: mm/test_pmalloc.c +F: Documentation/core-api/prmem.rst
I think the MAINTAINERS update can go in one chunk as the last patch in the series.
Quoted 5 lines
MAINTAINERS +0
-0MEMORY MANAGEMENT L: [email protected]Signature · 1 line
2.17.1
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH 10/17] prmem: documentation
Hello,
Quoted 2 lines · Mike Rapoport
I feel that foreword should include a sentence or two saying why we need the memory protection and when it can/should be used.
I was somewhat lost about the actual content of this sort of document.
In past reviews of older version of the docs, I go the impression that this should focus more on the use of the API and how to use it.
And it seem that different people expect to find here different type of information.
What is the best place where to put a more extensive description of the feature?
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Igor StoppaJon,
So the below document is a prime example for why I think RST sucks. As a text document readability is greatly diminished by all the markup nonsense.
This stuff should not become write-only content like html and other gunk. The actual text file is still the primary means of reading this.
Quoted 32 lines
Documentation/core-api/index.rst +1
-0@@ -31,6 +31,7 @@ Core utilities gfp_mask-from-fs-io timekeeping boot-time-mm + prmem Interfaces for kernel debugging ===============================Documentation/core-api/prmem.rst new file +14
-0@@ -0,0 +1,172 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _prmem: + +Memory Protection +================= + +:Date: October 2018 +:Author: Igor Stoppa <[email protected]> + +Foreword +-------- +- In a typical system using some sort of RAM as execution environment, + **all** memory is initially writable.
Documentation/core-api/prmem.rst line 24
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ +- It must be initialized with the appropriate content, be it code or data. + +- Said content typically undergoes modifications, i.e. relocations or + relocation-induced changes. + +- The present document doesn't address such transient. + +- Kernel code is protected at system level and, unlike data, it doesn't + require special attention.
What does this even mean?
Documentation/core-api/prmem.rst line 30
Quoted 5 lines
Documentation/core-api/prmem.rst new file +5
-0+Protection mechanism +-------------------- + +- When available, the MMU can write protect memory pages that would be + otherwise writable.
Again; what does this really want to say?
Documentation/core-api/prmem.rst line 32
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+- The protection has page-level granularity.
I don't think Linux supports non-paging MMUs.
Documentation/core-api/prmem.rst line 36
Quoted 3 lines
Documentation/core-api/prmem.rst new file +3
-0+- An attempt to overwrite a protected page will trigger an exception. +- **Write protected data must go exclusively to write protected pages** +- **Writable data must go exclusively to writable pages**
WTH is with all those ** ?
Quoted 27 lines
Documentation/core-api/prmem.rst new file +27
-0+Available protections for kernel data +------------------------------------- + +- **constant** + Labelled as **const**, the data is never supposed to be altered. + It is statically allocated - if it has any memory footprint at all. + The compiler can even optimize it away, where possible, by replacing + references to a **const** with its actual value. + +- **read only after init** + By tagging an otherwise ordinary statically allocated variable with + **__ro_after_init**, it is placed in a special segment that will + become write protected, at the end of the kernel init phase. + The compiler has no notion of this restriction and it will treat any + write operation on such variable as legal. However, assignments that + are attempted after the write protection is in place, will cause + exceptions. + +- **write rare after init** + This can be seen as variant of read only after init, which uses the + tag **__wr_after_init**. It is also limited to statically allocated + memory. It is still possible to alter this type of variables, after + the kernel init phase is complete, however it can be done exclusively + with special functions, instead of the assignment operator. Using the + assignment operator after conclusion of the init phase will still + trigger an exception. It is not possible to transition a certain + variable from __wr_ater_init to a permanent read-only status, at
Documentation/core-api/prmem.rst line 74
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ runtime. + +- **dynamically allocated write-rare / read-only** + After defining a pool, memory can be obtained through it, primarily + through the **pmalloc()** allocator. The exact writability state of the + memory obtained from **pmalloc()** and friends can be configured when + creating the pool. At any point it is possible to transition to a less + permissive write status the memory currently associated to the pool. + Once memory has become read-only, it the only valid operation, beside + reading, is to released it, by destroying the pool it belongs to.
Can we ditch all the ** nonsense and put whitespace in there? More paragraphs and whitespace are more good.
Also, I really don't like how you differentiate between static and dynamic wr.
Quoted 17 lines
Documentation/core-api/prmem.rst new file +17
-0+Protecting dynamically allocated memory +--------------------------------------- + +When dealing with dynamically allocated memory, three options are + available for configuring its writability state: + +- **Options selected when creating a pool** + When creating the pool, it is possible to choose one of the following: + - **PMALLOC_MODE_RO** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_WR** + - Writability at allocation time: *WRITABLE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_AUTO_RO** + - Writability at allocation time: + - the latest allocation: *WRITABLE*
Documentation/core-api/prmem.rst line 103
Quoted 10 lines
Documentation/core-api/prmem.rst new file +10
-0+ - every other allocation: *NONE* + - Writability at protection time: *NONE* + - **PMALLOC_MODE_AUTO_WR** + - Writability at allocation time: + - the latest allocation: *WRITABLE* + - every other allocation: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE* + - **PMALLOC_MODE_START_WR** + - Writability at allocation time: *WRITE-RARE* + - Writability at protection time: *WRITE-RARE*
That's just unreadable gibberish from here. Also what?
We already have RO, why do you need more RO?
Documentation/core-api/prmem.rst line 111
Quoted 8 lines
Documentation/core-api/prmem.rst new file +8
-0+ + **Remarks:** + - The "AUTO" modes perform automatic protection of the content, whenever + the current vmap_area is used up and a new one is allocated. + - At that point, the vmap_area being phased out is protected. + - The size of the vmap_area depends on various parameters. + - It might not be possible to know for sure *when* certain data will + be protected.
Surely that is a problem?
Documentation/core-api/prmem.rst line 112
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+ - The functionality is provided as tradeoff between hardening and speed.
Which you fail to explain.
Documentation/core-api/prmem.rst line 113
Quoted 1 line
Documentation/core-api/prmem.rst new file +1
-0+ - Its usefulness depends on the specific use case at hand
How about you write sensible text inside the option descriptions instead?
This is not a presentation; less bullets, more content.
Documentation/core-api/prmem.rst line 146
Quoted 4 lines
Documentation/core-api/prmem.rst new file +4
-0+- Not only the pmalloc memory must be protected, but also any reference to + it that might become the target for an attack. The attack would replace + a reference to the protected memory with a reference to some other, + unprotected, memory.
I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
And I don't think this document even begins to explain _why_ you're doing any of this. How does it help?
Documentation/core-api/prmem.rst line 154
Quoted 7 lines
Documentation/core-api/prmem.rst new file +7
-0+- The users of rare write must take care of ensuring the atomicity of the + action, respect to the way they use the data being altered; for example, + take a lock before making a copy of the value to modify (if it's + relevant), then alter it, issue the call to rare write and finally + release the lock. Some special scenario might be exempt from the need + for locking, but in general rare-write must be treated as an operation + that can incur into races.
What?!
Re: [PATCH 10/17] prmem: documentation
Quoted 8 lines · Peter Zijlstra
Jon,
So the below document is a prime example for why I think RST sucks. As a text document readability is greatly diminished by all the markup nonsense.
This stuff should not become write-only content like html and other gunk. The actual text file is still the primary means of reading this.
I think Igor neglected to read doc-guide/sphinx.rst:
Specific guidelines for the kernel documentation
------------------------------------------------
Here are some specific guidelines for the kernel documentation:
* Please don't go overboard with reStructuredText markup. Keep it simple. For the most part the documentation should be plain text with just enough consistency in formatting that it can be converted to other formats.
I agree that it's overly marked up.
Re: [PATCH 10/17] prmem: documentation
Quoted 11 lines · Matthew Wilcox
Quoted 8 lines · Peter Zijlstra
Jon,
So the below document is a prime example for why I think RST sucks. As a text document readability is greatly diminished by all the markup nonsense.
This stuff should not become write-only content like html and other gunk. The actual text file is still the primary means of reading this.
I think Igor neglected to read
doc-guide/sphinx.rst:
Guilty as charged :-/
Quoted 11 lines
Specific guidelines for the kernel documentation
------------------------------------------------Here are some specific guidelines for the kernel documentation:
* Please don't go overboard with reStructuredText markup. Keep it simple. For the most part the documentation should be plain text with just enough consistency in formatting that it can be converted to other formats.
I agree that it's overly marked up.
I'll fix it
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Peter ZijlstraQuoted 3 lines · Peter Zijlstra
I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
I can speak to the general goal. The specific implementation here may not fit all the needs, but it's a good starting point for discussion.
One aspect of hardening the kernel against attack is reducing the internal attack surface. Not all flaws are created equal, so there is variation in what limitations an attacker may have when exploiting flaws (not many flaws end up being a fully controlled "write anything, anywhere, at any time"). By making the more sensitive data structures of the kernel read-only, we reduce the risk of an attacker finding a path to manipulating the kernel's behavior in a significant way.
Examples of typical sensitive targets are function pointers, security policy, and page tables. Having these "read only at rest" makes them much harder to control by an attacker using memory integrity flaws. We already have the .rodata section for "const" things. Those are trivially read-only. For example there is a long history of making sure that function pointer tables are marked "const". However, some things need to stay writable. Some of those in .data aren't changed after __init, so we added the __ro_after_init which made them read-only for the life of the system after __init. However, we're still left with a lot of sensitive structures either in .data or dynamically allocated, that need a way to be made read-only for most of the time, but get written to during very specific times.
The "write rarely" name itself may not sufficiently describe what is wanted either (I'll take the blame for the inaccurate name), so I'm open to new ideas there. The implementation requirements for the "sensitive data read-only at rest" feature are rather tricky:
- allow writes only from specific places in the kernel
- keep those locations inline to avoid making them trivial ROP targets
- keep the writeability window open only to a single uninterruptable CPU
- fast enough to deal with page table updates
The proposal I made a while back only covered .data things (and used x86-specific features). Igor's proposal builds on this by including a way to do this with dynamic allocation too, which greatly expands the scope of structures that can be protected. Given that the x86-only method of write-window creation was firmly rejected, this is a new proposal for how to do it (vmap window). Using switch_mm() has also been suggested, etc.
We need to find a good way to do the write-windowing that works well for static and dynamic structures _and_ for the page tables... this continues to be tricky.
Making it resilient against ROP-style targets makes it difficult to deal with certain data structures (like list manipulation). In my earlier RFC, I tried to provide enough examples of where this could get used to let people see some of the complexity[1]. Igor's series expands this to even more examples using dynamic allocation.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=kspp/write-rarely
Signature · 1 line
Kees Cook
Re: [PATCH 10/17] prmem: documentation
Quoted 13 lines · Kees Cook
Quoted 3 lines · Peter Zijlstra
I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
One aspect of hardening the kernel against attack is reducing the internal attack surface. Not all flaws are created equal, so there is variation in what limitations an attacker may have when exploiting flaws (not many flaws end up being a fully controlled "write anything, anywhere, at any time"). By making the more sensitive data structures of the kernel read-only, we reduce the risk of an attacker finding a path to manipulating the kernel's behavior in a significant way.
Quoted 3 lines
Examples of typical sensitive targets are function pointers, security policy, and page tables. Having these "read only at rest" makes them much harder to control by an attacker using memory integrity flaws.
Because 'write-anywhere' exploits are easier than (and the typical first step to) arbitrary code execution thingies?
Quoted 8 lines
The "write rarely" name itself may not sufficiently describe what is wanted either (I'll take the blame for the inaccurate name), so I'm open to new ideas there. The implementation requirements for the "sensitive data read-only at rest" feature are rather tricky:
- allow writes only from specific places in the kernel
- keep those locations inline to avoid making them trivial ROP targets
- keep the writeability window open only to a single uninterruptable CPU
The current patch set does not achieve that because it uses a global address space for the alias mapping (vmap) which is equally accessible from all CPUs.
Quoted 1 line
- fast enough to deal with page table updates
The proposed implementation needs page-tables for the alias; I don't see how you could ever do R/O page-tables when you need page-tables to modify your page-tables.
And this is entirely irrespective of performance.
Quoted 2 lines
The proposal I made a while back only covered .data things (and used x86-specific features).
Oh, right, that CR0.WP stuff.
Quoted 6 lines
Igor's proposal builds on this by including a
way to do this with dynamic allocation too, which greatly expands the scope of structures that can be protected. Given that the x86-only method of write-window creation was firmly rejected, this is a new proposal for how to do it (vmap window). Usingswitch_mm()has also been suggested, etc.
Right... /me goes find the patches we did for text_poke. Hmm, those never seem to have made it:
https://lkml.kernel.org/r/[email protected]
like that. That approach will in fact work and not be a completely broken mess like this thing.
Quoted 9 lines
We need to find a good way to do the write-windowing that works well for static and dynamic structures _and_ for the page tables... this continues to be tricky.
Making it resilient against ROP-style targets makes it difficult to deal with certain data structures (like list manipulation). In my earlier RFC, I tried to provide enough examples of where this could get used to let people see some of the complexity[1]. Igor's series expands this to even more examples using dynamic allocation.
Doing 2 CR3 writes for 'every' WR write doesn't seem like it would be fast enough for much of anything.
And I don't suppose we can take the WP fault and then fix up from there, because if we're doing R/O page-tables, that'll incrase the fault depth and we'll double fault all the time, and tripple fault where we currently double fault. And we all know how _awesome_ tripple faults are.
But duplicating (and wrapping in gunk) whole APIs is just not going to work.
Re: [PATCH 10/17] prmem: documentation
Quoted 26 lines · Peter Zijlstra
Quoted 13 lines · Kees Cook
Quoted 3 lines · Peter Zijlstra
I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
One aspect of hardening the kernel against attack is reducing the internal attack surface. Not all flaws are created equal, so there is variation in what limitations an attacker may have when exploiting flaws (not many flaws end up being a fully controlled "write anything, anywhere, at any time"). By making the more sensitive data structures of the kernel read-only, we reduce the risk of an attacker finding a path to manipulating the kernel's behavior in a significant way.
Quoted 3 lines
Examples of typical sensitive targets are function pointers, security policy, and page tables. Having these "read only at rest" makes them much harder to control by an attacker using memory integrity flaws.
Because 'write-anywhere' exploits are easier than (and the typical first step to) arbitrary code execution thingies?
Quoted 5 lines
The "write rarely" name itself may not sufficiently describe what is wanted either (I'll take the blame for the inaccurate name), so I'm open to new ideas there. The implementation requirements for the "sensitive data read-only at rest" feature are rather tricky:
Quoted 7 lines
Quoted 3 lines
- allow writes only from specific places in the kernel
- keep those locations inline to avoid making them trivial ROP targets
- keep the writeability window open only to a single uninterruptable CPUThe current patch set does not achieve that because it uses a global address space for the alias mapping (vmap) which is equally accessible from all CPUs.
I never claimed to achieve 100% resilience to attacks.
While it's true that the address space is accessible to all the CPUs, it is also true that the access has a limited duration in time, and the location where the access can be performed is not fixed.
Iow, assuming that the CPUs not involved in the write-rare operations are compromised and that they are trying to perform a concurrent access to the data in the writable page, they have a limited window of opportunity.
Said this, this I have posted is just a tentative implementation.
My primary intent was to at least give an idea of what I'd like to do: alter some data in a way that is not easily exploitable.
Quoted 5 lines
Quoted 1 line
- fast enough to deal with page table updates
The proposed implementation needs page-tables for the alias; I don't see how you could ever do R/O page-tables when you need page-tables to modify your page-tables.
It's not all-or-nothing.
I hope we agree at least on the reasoning that having only a limited amount of address space directly attackable, instead of the whole set of pages containing exploitable data, is reducing the attack surface.
Furthermore, if we think about possible limitations that the attack might have (maximum reach), the level of protection might be even higher. I have to use "might" because I cannot foresee the vulnerability.
Furthermore, taking a different angle: your average attacker is not necessarily very social and inclined to share the vulnerability found. It is safe to assume that in most cases each attacker has to identify the attack strategy autonomously.
Reducing the amount of individual who can perform an attack, by increasing the expertise required is also a way of doing damage control.
Quoted 1 line
And this is entirely irrespective of performance.
I have not completely given up on performance, but, being write-rare, I see improved performance as just a way of widening the range of possible recipients for the hardening.
Quoted 12 lines
Quoted 2 lines
The proposal I made a while back only covered .data things (and used x86-specific features).
Oh, right, that CR0.WP stuff.
Quoted 6 lines
Igor's proposal builds on this by including a
way to do this with dynamic allocation too, which greatly expands the scope of structures that can be protected. Given that the x86-only method of write-window creation was firmly rejected, this is a new proposal for how to do it (vmap window). Usingswitch_mm()has also been suggested, etc.
Quoted 7 lines
Right... /me goes find the patches we did for
text_poke. Hmm, those never seem to have made it:https://lkml.kernel.org/r/[email protected]like that. That approach will in fact work and not be a completely broken mess like this thing.
That approach is x86 specific.
I preferred to start with something that would work on a broader set of architectures, even if with less resilience.
It can be done so that individual architectures can implement their own specific way and obtain better results.
Quoted 4 lines
Quoted 4 lines
We need to find a good way to do the write-windowing that works well for static and dynamic structures _and_ for the page tables... this continues to be tricky.
Quoted 8 lines
Quoted 5 lines
Making it resilient against ROP-style targets makes it difficult to deal with certain data structures (like list manipulation). In my earlier RFC, I tried to provide enough examples of where this could get used to let people see some of the complexity[1]. Igor's series expands this to even more examples using dynamic allocation.
Doing 2 CR3 writes for 'every' WR write doesn't seem like it would be fast enough for much of anything.
Part of the reason for the duplication is that, even if the wr_API I came up with, can be collapsed with the regular API, the implementation needs to be different, to be faster.
Ex:
* force the list_head structure to be aligned so that it will always be fully contained by a single page
* create alternate mapping for all the pages involved (max 1 page for each list_head)
* do the lsit operation on the remapped list_heads
* destroy all the mappings
I can try to introduce some wrapper similar to kmap_atomic(), as suggested by Dave Hansen, which can improve the coding, but it will not change the actual set of operations performed.
Quoted 8 lines
And I don't suppose we can take the WP fault and then fix up from there, because if we're doing R/O page-tables, that'll incrase the fault depth and we'll double fault all the time, and tripple fault where we currently double fault. And we all know how _awesome_ tripple faults are.
But duplicating (and wrapping in gunk) whole APIs is just not going to work.
Would something like kmap_atomic() be acceptable?
Do you have some better proposal, now that (I hope) it should be more clear what I'm trying to do and why?
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 31 lines · Igor Stoppa
Quoted 30 lines · Peter Zijlstra
Quoted 13 lines · Kees Cook
Quoted 3 lines · Peter Zijlstra
I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
One aspect of hardening the kernel against attack is reducing the internal attack surface. Not all flaws are created equal, so there is variation in what limitations an attacker may have when exploiting flaws (not many flaws end up being a fully controlled "write anything, anywhere, at any time"). By making the more sensitive data structures of the kernel read-only, we reduce the risk of an attacker finding a path to manipulating the kernel's behavior in a significant way.
Quoted 3 lines
Examples of typical sensitive targets are function pointers, security policy, and page tables. Having these "read only at rest" makes them much harder to control by an attacker using memory integrity flaws.
Because 'write-anywhere' exploits are easier than (and the typical first step to) arbitrary code execution thingies?
Quoted 8 lines
The "write rarely" name itself may not sufficiently describe what is wanted either (I'll take the blame for the inaccurate name), so I'm open to new ideas there. The implementation requirements for the "sensitive data read-only at rest" feature are rather tricky:
- allow writes only from specific places in the kernel
- keep those locations inline to avoid making them trivial ROP targets
- keep the writeability window open only to a single uninterruptable CPU
Quoted 5 lines
Quoted 3 lines
The current patch set does not achieve that because it uses a global address space for the alias mapping (vmap) which is equally accessible from all CPUs.
I never claimed to achieve 100% resilience to attacks.
You never even begun explaining what you were defending against. Let alone how well it achieved its stated goals.
Quoted 7 lines
While it's true that the address space is accessible to all the CPUs, it is also true that the access has a limited duration in time, and the location where the access can be performed is not fixed.
Iow, assuming that the CPUs not involved in the write-rare operations are compromised and that they are trying to perform a concurrent access to the data in the writable page, they have a limited window of opportunity.
That sounds like security through obscurity. Sure it makes it harder, but it doesn't stop anything.
Quoted 3 lines
Said this, this I have posted is just a tentative implementation. My primary intent was to at least give an idea of what I'd like to do: alter some data in a way that is not easily exploitable.
Since you need to modify page-tables in order to achieve this, the page-tables are also there for the attacker to write to.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
- fast enough to deal with page table updates
The proposed implementation needs page-tables for the alias; I don't see how you could ever do R/O page-tables when you need page-tables to modify your page-tables.
It's not all-or-nothing.
I really am still strugging with what it is this thing is supposed to do. As it stands, I see very little value. Yes it makes a few things a little harder, but it doesn't really do away with things.
Quoted 3 lines
I hope we agree at least on the reasoning that having only a limited amount of address space directly attackable, instead of the whole set of pages containing exploitable data, is reducing the attack surface.
I do not in fact agree. Most pages are not interesting for an attack at all. So simply reducing the set of pages you can write to isn't sufficient.
IOW. removing all noninteresting pages from the writable set will satisfy your criteria, but it avoids exactly 0 exploits.
What you need to argue is that we remove common exploit targets, and you've utterly failed to do so.
Kees mentioned: function pointers, page-tables and a few other things. You mentioned nothing.
Quoted 3 lines
Furthermore, if we think about possible limitations that the attack might have (maximum reach), the level of protection might be even higher. I have to use "might" because I cannot foresee the vulnerability.
That's just a bunch of words together that don't really say anything afaict.
Quoted 6 lines
Furthermore, taking a different angle: your average attacker is not necessarily very social and inclined to share the vulnerability found. It is safe to assume that in most cases each attacker has to identify the attack strategy autonomously.
Reducing the amount of individual who can perform an attack, by increasing the expertise required is also a way of doing damage control.
If you make RO all noninteresting pages, you in fact increase the density of interesting targets and make things easier.
Quoted 5 lines
Quoted 1 line
And this is entirely irrespective of performance.
I have not completely given up on performance, but, being write-rare, I see improved performance as just a way of widening the range of possible recipients for the hardening.
What?! Are you saying you don't care about performance?
Quoted 9 lines
Quoted 7 lines
Right... /me goes find the patches we did for
text_poke. Hmm, those never seem to have made it:https://lkml.kernel.org/r/[email protected]like that. That approach will in fact work and not be a completely broken mess like this thing.
That approach is x86 specific.
It is not. Every architecture does switch_mm() with IRQs disabled, as that is exactly what the scheduler does.
And keeping a second init_mm around also isn't very architecture specific I feel.
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
We need to find a good way to do the write-windowing that works well for static and dynamic structures _and_ for the page tables... this continues to be tricky.
Quoted 8 lines
Quoted 5 lines
Making it resilient against ROP-style targets makes it difficult to deal with certain data structures (like list manipulation). In my earlier RFC, I tried to provide enough examples of where this could get used to let people see some of the complexity[1]. Igor's series expands this to even more examples using dynamic allocation.
Doing 2 CR3 writes for 'every' WR write doesn't seem like it would be fast enough for much of anything.
Part of the reason for the duplication is that, even if the
wr_APII came up with, can be collapsed with the regular API, the implementation needs to be different, to be faster.
Quoted 7 lines
Ex:
* force thelist_headstructure to be aligned so that it will always be fully contained by a single page
* create alternate mapping for all the pages involved (max 1 page for eachlist_head)
* do the lsit operation on the remappedlist_heads
* destroy all the mappings
Those constraints are due to single page aliases.
Quoted 3 lines
I can try to introduce some wrapper similar to
kmap_atomic(), as suggested by Dave Hansen, which can improve the coding, but it will not change the actual set of operations performed.
See below, I don't think kmap_atomic() is either correct or workable.
One thing that might be interesting is teaching objtool about the write-enable write-disable markers and making it guarantee we reach a disable after every enable. IOW, ensure we never leak this state.
I think that was part of why we hated on that initial thing Kees did -- well that an disabling all WP is of course completely insane ;-).
Quoted 10 lines
Quoted 8 lines
And I don't suppose we can take the WP fault and then fix up from there, because if we're doing R/O page-tables, that'll incrase the fault depth and we'll double fault all the time, and tripple fault where we currently double fault. And we all know how _awesome_ tripple faults are.
But duplicating (and wrapping in gunk) whole APIs is just not going to work.
Would something like
kmap_atomic()be acceptable?
Don't think so. kmap_atomic() on x86_32 (64bit doesn't have it at all) only does the TLB invalidate on the one CPU, which we've established is incorrect.
Also, kmap_atomic is still page-table based, which means not all page-tables can be read-only.
Quoted 2 lines
Do you have some better proposal, now that (I hope) it should be more clear what I'm trying to do and why?
You've still not talked about any actual attack vectors and how they're mitigated by these patches.
I suppose the 'normal' attack goes like:
1) find buffer-overrun / bound check failure
2) use that to write to 'interesting' location
3) that write results arbitrary code execution
4) win
Of course, if the store of 2 is to the current cred structure, and simply sets the effective uid to 0, we can skip 3.
Which seems to suggest all cred structures should be made r/o like this. But I'm not sure I remember these patches doing that.
Also, there is an inverse situation with all this. If you make everything R/O, then you need this allow-write for everything you do, which then is about to include a case with an overflow / bound check fail, and we're back to square 1.
What are you doing to avoid that?
Re: [PATCH 10/17] prmem: documentation
Quoted 9 lines · Peter Zijlstra
I suppose the 'normal' attack goes like:
1) find buffer-overrun / bound check failure
2) use that to write to 'interesting' location
3) that write results arbitrary code execution
4) winOf course, if the store of 2 is to the current cred structure, and simply sets the effective uid to 0, we can skip 3.
In most cases, yes, gaining root is game over. However, I don't want to discount other threat models: some systems have been designed not to trust root, so a cred attack doesn't always get an attacker full control (e.g. lockdown series, signed modules, encrypted VMs, etc).
Quoted 2 lines
Which seems to suggest all cred structures should be made r/o like this. But I'm not sure I remember these patches doing that.
There are things that attempt to protect cred (and other things, like page tables) via hypervisors (see Samsung KNOX) or via syscall boundary checking (see Linux Kernel Runtime Guard). They're pretty interesting, but I'm not sure if there is a clear way forward on it working in upstream, but that's why I think these discussions are useful.
Quoted 4 lines
Also, there is an inverse situation with all this. If you make everything R/O, then you need this allow-write for everything you do, which then is about to include a case with an overflow / bound check fail, and we're back to square 1.
Sure -- this is the fine line in trying to build these defenses. The point is to narrow the scope of attack. Stupid metaphor follows: right now we have only a couple walls; if we add walls we can focus on make sure the doors and windows are safe. If we make the relatively easy-to-find-in-memory page tables read-only-at-rest then a whole class of very powerful exploits that depend on page table attacks go away.
As part of all of this is the observation that there are two types of things clearly worth protecting: that which is updated rarely (no need to leave it writable for so much of its lifetime), and that which is especially sensitive (page tables, security policy, function pointers, etc). Finding a general purpose way to deal with these (like we have for other data-lifetime cases like const and __ro_after_init) would be very nice. I don't think there is a slippery slope here.
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH 10/17] prmem: documentation
Quoted 42 lines · Kees Cook
Quoted 11 lines · Peter Zijlstra
I suppose the 'normal' attack goes like:
1) find buffer-overrun / bound check failure
2) use that to write to 'interesting' location
3) that write results arbitrary code execution
4) winOf course, if the store of 2 is to the current cred structure, and simply sets the effective uid to 0, we can skip 3.
In most cases, yes, gaining root is game over. However, I don't want to discount other threat models: some systems have been designed not to trust root, so a cred attack doesn't always get an attacker full control (e.g. lockdown series, signed modules, encrypted VMs, etc).
Quoted 2 lines
Which seems to suggest all cred structures should be made r/o like this. But I'm not sure I remember these patches doing that.
There are things that attempt to protect cred (and other things, like page tables) via hypervisors (see Samsung KNOX) or via syscall boundary checking (see Linux Kernel Runtime Guard). They're pretty interesting, but I'm not sure if there is a clear way forward on it working in upstream, but that's why I think these discussions are useful.
Quoted 4 lines
Also, there is an inverse situation with all this. If you make everything R/O, then you need this allow-write for everything you do, which then is about to include a case with an overflow / bound check fail, and we're back to square 1.
Sure -- this is the fine line in trying to build these defenses. The point is to narrow the scope of attack. Stupid metaphor follows: right now we have only a couple walls; if we add walls we can focus on make sure the doors and windows are safe. If we make the relatively easy-to-find-in-memory page tables read-only-at-rest then a whole class of very powerful exploits that depend on page table attacks go away.
Quoted 9 lines
As part of all of this is the observation that there are two types of things clearly worth protecting: that which is updated rarely (no need to leave it writable for so much of its lifetime), and that which is especially sensitive (page tables, security policy, function pointers, etc). Finding a general purpose way to deal with these (like we have for other data-lifetime cases like const and
__ro_after_init) would be very nice. I don't think there is a slippery slope here.
Since I wasn’t cc’d on this series:
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an mm_struct. That mm_struct, just like any sane mm_struct, should only differ from init_mm in that it has extra mappings in the *user* region.
If anyone wants to use CR0.WP instead, I’ll remind them that they have to fix up the entry code and justify the added complexity. And make performance not suck in a VM (i.e. CR0 reads on entry are probably off the table). None of these will be easy.
If anyone wants to use kmap_atomic-like tricks, I’ll point out that we already have enough problems with dangling TLB entries due to SMP issues. The last thing we need is more of them. If someone proposes a viable solution that doesn’t involve CR3 fiddling, I’ll be surprised.
Keep in mind that switch_mm() is actually decently fast on modern CPUs. It’s probably considerably faster than writing CR0, although I haven’t benchmarked it. It’s certainly faster than writing CR4. It’s also faster than INVPCID, surprisingly, which means that it will be quite hard to get better performance using any sort of trickery.
Nadav’s patch set would be an excellent starting point.
P.S. EFI is sort of grandfathered in as a hackish alternate page table hierarchy. We’re not adding another one.
Re: [PATCH 10/17] prmem: documentation
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.
I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to rare_mm which contains a writable mapping to all the kernel data which is rare-write.
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);
Under the covers, rare_modify() would switch to the rare_mm and return (void *)((unsigned long)q + ARCH_RARE_OFFSET). All of the rare data would then be modifiable, although you don't have any other pointers to it. rare_protect() would switch back to the previous mm and return (p - ARCH_RARE_OFFSET).
Does this satisfy Igor's requirements? We wouldn't be able to copy_to/from_user() while rare_mm was active. I think that's a feature though! It certainly satisfies my interests (kernel code be able to mark things as dynamically-allocated-and-read-only-after-initialisation)
Re: [PATCH 10/17] prmem: documentation
Quoted 4 lines · Matthew Wilcox
Does this satisfy Igor's requirements? We wouldn't be able to copy_to/from_user() while
rare_mmwas active. I think that's a feature though! It certainly satisfies my interests (kernel code be able to mark things as dynamically-allocated-and-read-only-after-initialisation)
It has to be more than copy_to/from_user(), though, I think.
rare_modify(q) either has to preempt_disable(), or we need to teach the context-switching code when and how to switch in/out of the rare_mm. preempt_disable() would also keep us from sleeping.
Re: [PATCH 10/17] prmem: documentation
Quoted 11 lines · Dave Hansen
Quoted 4 lines · Matthew Wilcox
Does this satisfy Igor's requirements? We wouldn't be able to copy_to/from_user() while
rare_mmwas active. I think that's a feature though! It certainly satisfies my interests (kernel code be able to mark things as dynamically-allocated-and-read-only-after-initialisation)It has to be more than copy_to/from_user(), though, I think.
rare_modify(q)either has topreempt_disable(), or we need to teach the context-switching code when and how to switch in/out of therare_mm.preempt_disable()would also keep us from sleeping.
Yes, I think we want to have preempt disable at the very least. We could indeed make the context switch code smart and teach is about this state, but I think allowing preemption in such section is a bad idea. We want to keep these sections short and simple (and bounded), such that they can be analyzed for correctness.
Once you allow preemption, things tend to grow large and complex.
Ideally we'd even disabled interrupts over this, to further limit what code runs in the rare_mm context. NMIs need special care anyway.
Re: [PATCH 10/17] prmem: documentation
In reply to Matthew WilcoxQuoted 14 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
Quoted 8 lines
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */To subsequently modify q,
p = rare_modify(q); q->a = y;
Do you mean
p->a = y;
here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Tycho
Quoted 12 lines
rare_protect(p);Under the covers,
rare_modify()would switch to therare_mmand return (void *)((unsigned long)q +ARCH_RARE_OFFSET). All of the rare data would then be modifiable, although you don't have any other pointers to it.rare_protect()would switch back to the previous mm and return (p -ARCH_RARE_OFFSET).Does this satisfy Igor's requirements? We wouldn't be able to copy_to/from_user() while
rare_mmwas active. I think that's a feature though! It certainly satisfies my interests (kernel code be able to mark things as dynamically-allocated-and-read-only-after-initialisation)
Re: [PATCH 10/17] prmem: documentation
Quoted 24 lines · Tycho Andersen
Quoted 14 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
Quoted 8 lines
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */To subsequently modify q,
p = rare_modify(q); q->a = y;
Quoted 6 lines
Do you mean
p->a = y;here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Yes, that was my intent, thanks.
To handle the list case that Igor has pointed out, you might want to do something like this:
list_for_each_entry(x, &xs, entry) {
struct foo *writable = rare_modify(entry);
kref_get(&writable->ref);
rare_protect(writable);
}but we'd probably wrap it in list_for_each_rare_entry(), just to be nicer.
Re: [PATCH 10/17] prmem: documentation
Quoted 14 lines · Matthew Wilcox
Quoted 13 lines · Tycho Andersen
Quoted 12 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */
With pools and memory allocated from vmap_areas, I was able to say
protect(pool)
and that would do a swipe on all the pages currently in use.
In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.
The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
Quoted 13 lines
Quoted 6 lines
Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y;Quoted 6 lines
Do you mean
p->a = y;here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Quoted 7 lines
Yes, that was my intent, thanks.
To handle the list case that Igor has pointed out, you might want to do something like this:
list_for_each_entry(x, &xs, entry) { struct foo *writable = rare_modify(entry);
Would this mapping be impossible to spoof by other cores?
I'm asking this because, from what I understand, local interrupts are enabled here, so an attack could freeze the core performing the write-rare operation, while another scrapes the memory.
But blocking interrupts for the entire body of the loop would make RT latency unpredictable.
Quoted 5 lines
kref_get(&writable->ref); rare_protect(writable); }but we'd probably wrap it in
list_for_each_rare_entry(), just to be nicer.
This seems suspiciously close to the duplication of kernel interfaces that I was roasted for :-)
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 29 lines · Igor Stoppa
Quoted 16 lines · Matthew Wilcox
Quoted 14 lines · Tycho Andersen
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
Quoted 8 lines
and that would do a swipe on all the pages currently in use.
In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
I don’t see why not, although getting the API right will be a tad complicated.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
To subsequently modify q,
p = rare_modify(q); q->a = y;Do you mean
p->a = y;here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Quoted 8 lines
Quoted 5 lines
Yes, that was my intent, thanks.
To handle the list case that Igor has pointed out, you might want to do something like this:list_for_each_entry(x, &xs, entry) { struct foo *writable = rare_modify(entry);Would this mapping be impossible to spoof by other cores?
Indeed. Only the core with the special mm loaded could see it.
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Actually getting a modifiable pointer should be disallowed for two reasons:
1. Some architectures may want to use a special write-different-address-space operation. Heck, x86 could, too: make the actual offset be a secret and shove the offset into FSBASE or similar. Then %fs-prefixed writes would do the rare writes.
2. Alternatively, x86 could set the U bit. Then the actual writes would use the uaccess helpers, giving extra protection via SMAP.
We don’t really want a situation where an unchecked pointer in the rare write region completely defeats the mechanism.
Re: [PATCH 10/17] prmem: documentation
Quoted 72 lines · Andy Lutomirski
Quoted 29 lines · Igor Stoppa
Quoted 16 lines · Matthew Wilcox
Quoted 14 lines · Tycho Andersen
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
Quoted 8 lines
and that would do a swipe on all the pages currently in use.
In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
I don’t see why not, although getting the API right will be a tad complicated.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
To subsequently modify q,
p = rare_modify(q); q->a = y;Do you mean
p->a = y;here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Quoted 8 lines
Quoted 5 lines
Yes, that was my intent, thanks.
To handle the list case that Igor has pointed out, you might want to do something like this:list_for_each_entry(x, &xs, entry) { struct foo *writable = rare_modify(entry);Would this mapping be impossible to spoof by other cores?
Indeed. Only the core with the special mm loaded could see it.
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Quoted 7 lines
Actually getting a modifiable pointer should be disallowed for two reasons:
1. Some architectures may want to use a special write-different-address-space operation. Heck, x86 could, too: make the actual offset be a secret and shove the offset into FSBASE or similar. Then %fs-prefixed writes would do the rare writes.
2. Alternatively, x86 could set the U bit. Then the actual writes would use the uaccess helpers, giving extra protection via SMAP.
We don’t really want a situation where an unchecked pointer in the rare write region completely defeats the mechanism.
We still have to deal with certain structures under the write-rare window. For example, see:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=kspp/write-rarely&id=60430b4d3b113aae4adab66f8339074986276474
They are wrappers to non-inline functions that have the same sanity-checking.
Signature · 1 line
Kees Cook
Re: [PATCH 10/17] prmem: documentation
Quoted 4 lines · Kees Cook
We still have to deal with certain structures under the write-rare window. For example, see:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=kspp/write-rarely&id=60430b4d3b113aae4adab66f8339074986276474
They are wrappers to non-inline functions that have the same sanity-checking.
Even if I also did something similar, it was not intended to be final. Just a stop-gap solution till the write-rare mechanism is identified.
If the size of the whole list_head is used as alignment, then the whole list_head structure can be given an alternate mapping and the plain list function can be used on this alternate mapping.
It can halve the overhead or more.
The typical case is when not only one list_head is contained in one page, but also the other, like when allocating and queuing multiple items from the same pool. One single temporary mapping would be enough.
But it becomes tricky to do it, without generating code that is almost-but-not-quite-identical.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 10 lines · Andy Lutomirski
Quoted 8 lines · Igor Stoppa
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
I don’t see why not, although getting the API right will be a tad complicated.
yes, I have some first-hand experience with this :-/
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
To subsequently modify q,
p = rare_modify(q); q->a = y;Do you mean
p->a = y;here? I assume the intent is that q isn't writable ever, but that's the one we have in the structure at rest.
Quoted 8 lines
Quoted 5 lines
Yes, that was my intent, thanks.
To handle the list case that Igor has pointed out, you might want to do something like this:list_for_each_entry(x, &xs, entry) { struct foo *writable = rare_modify(entry);Would this mapping be impossible to spoof by other cores?
Indeed. Only the core with the special mm loaded could see it.
Quoted 7 lines
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Atomic? RCU?
Yes, they are technically just memory writes, but shouldn't the "normal" operation be executed on the writable mapping?
It is possible to sandwich any call between a rare_modify/rare_protect, however isn't that pretty close to having a write-rare version of these plain function.
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 5 lines · Igor Stoppa
Quoted 3 lines · Andy Lutomirski
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
Quoted 8 lines
Quoted 6 lines
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Atomic? RCU?
RCU can be done, that's not really a problem. Atomics otoh are a problem. Having pointers makes them just work.
Andy; I understand your reason for not wanting them, but I really don't want to duplicate everything. Is there something we can do with static analysis to make you more comfortable with the pointer thing?
Re: [PATCH 10/17] prmem: documentation
Quoted 17 lines · Peter Zijlstra
Quoted 14 lines · Igor Stoppa
Quoted 9 lines · Andy Lutomirski
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Quoted 8 lines
Quoted 1 line
Atomic? RCU?
RCU can be done, that's not really a problem. Atomics otoh are a problem. Having pointers makes them just work.
Andy; I understand your reason for not wanting them, but I really don't want to duplicate everything. Is there something we can do with static analysis to make you more comfortable with the pointer thing?
I’m sure we could do something with static analysis, but I think seeing a real use case where all this fanciness makes sense would be good.
And I don’t know if s390 *can* have an efficient implementation that uses pointers. OTOH they have all kinds of magic stuff, so who knows?
Re: [PATCH 10/17] prmem: documentation
Quoted 26 lines · Andy Lutomirski
Quoted 23 lines
Quoted 11 lines · Peter Zijlstra
On Wed, Oct 31, 2018 at 12:15:46AM +0200, Igor Stoppa wrote:
Quoted 3 lines · Andy Lutomirski
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
Quoted 8 lines
Quoted 6 lines
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Atomic? RCU?
RCU can be done, that's not really a problem. Atomics otoh are a problem. Having pointers makes them just work.
Quoted 7 lines
Quoted 3 lines
Andy; I understand your reason for not wanting them, but I really don't want to duplicate everything. Is there something we can do with static analysis to make you more comfortable with the pointer thing?
I’m sure we could do something with static analysis, but I think seeing a real use case where all this fanciness makes sense would be good.
And I don’t know if s390 *can* have an efficient implementation that uses pointers. OTOH they have all kinds of magic stuff, so who knows?
Also, if we’re using a hypervisor, then there are a couple ways it could be done:
1. VMFUNC. Pointers work fine. This is stronger than any amount of CR3 trickery because it can’t be defeated by page table attacks.
2. A hypercall to do the write. No pointers.
Basically, I think that if we can get away without writable pointers, we get more flexibility and less need for fancy static analysis. If we do need pointers, then so be it.
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 6 lines · Andy Lutomirski
But I dislike allowing regular writes in the protected region. We really only need four write primitives:
1. Just write one value. Call at any time (except NMI).
2. Just copy some bytes. Same as (1) but any number of bytes.
Given the !preempt/!IRQ contraints I'd certainly put an upper limit on the number of bytes there.
Quoted 8 lines
3,4: Same as 1 and 2 but must be called inside a special rare write region. This is purely an optimization.
Actually getting a modifiable pointer should be disallowed for two reasons:
1. Some architectures may want to use a special
write-different-address-space operation.
You're thinking of s390 ? :-)
Quoted 6 lines
Heck, x86 could, too: make
the actual offset be a secret and shove the offset into FSBASE or similar. Then %fs-prefixed writes would do the rare writes.2. Alternatively, x86 could set the U bit. Then the actual writes would use the uaccess helpers, giving extra protection via SMAP.
Cute, and yes, something like that would be nice.
Quoted 2 lines
We don’t really want a situation where an unchecked pointer in the rare write region completely defeats the mechanism.
Agreed.
Re: [PATCH 10/17] prmem: documentation
In reply to Igor StoppaQuoted 15 lines · Igor Stoppa
Quoted 5 lines · Matthew Wilcox
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
and that would do a swipe on all the pages currently in use. In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.
Quoted 8 lines
The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
What Andy is proposing effectively puts all rare allocations into one pool. Although I suppose it could be generalised to multiple pools ... one mm_struct per pool. Andy, what do you think to doing that?
Quoted 4 lines
Quoted 1 line
but we'd probably wrap it in
list_for_each_rare_entry(), just to be nicer.This seems suspiciously close to the duplication of kernel interfaces that I was roasted for :-)
Can you not see the difference between adding one syntactic sugar function and duplicating the entire infrastructure?
Re: [PATCH 10/17] prmem: documentation
Quoted 8 lines · Matthew Wilcox
Quoted 1 line · Igor Stoppa
Would it be possible to do the same here?
What Andy is proposing effectively puts all rare allocations into one pool. Although I suppose it could be generalised to multiple pools ... one
mm_structper pool. Andy, what do you think to doing that?
The reason to have pools is that, continuing the SELinux example, it supports reloading the policyDB.
In this case it seems to me that it would be faster to drop the entire pool in one go, and create a new one when re-initializing the rules.
Or maybe the pool could be flushed, without destroying the metadata.
One more reason for having pools is to assign certain property to the pool and then rely on it to be applied to every subsequent allocation.
I've also been wondering if pools can be expected to have some well defined property.
One might be that they do not need to be created on the fly.
The number of pools should be known at compilation time.
At least the meta-data could be static, but I do not know how this would work with modules.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
but we'd probably wrap it in
list_for_each_rare_entry(), just to be nicer.This seems suspiciously close to the duplication of kernel interfaces that I was roasted for :-)
Can you not see the difference between adding one syntactic sugar function and duplicating the entire infrastructure?
And list_add()? or any of the other list related functions? Don't they end up receiving a similar treatment?
I might have misunderstood your proposal, but it seems to me that they too will need the same type of pairs rare_modify/rare_protect. No?
And same for hlist, including the _rcu variants.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Matthew WilcoxQuoted 21 lines · Matthew Wilcox
Quoted 19 lines · Igor Stoppa
Quoted 5 lines · Matthew Wilcox
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p =
rare_alloc(...); /* writable pointer */
p->a= x;
q =rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
and that would do a swipe on all the pages currently in use. In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.
The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
Quoted 8 lines
Quoted 4 lines
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
What Andy is proposing effectively puts all rare allocations into one pool. Although I suppose it could be generalised to multiple pools ... one
mm_structper pool. Andy, what do you think to doing that?
Hmm. Let's see.
To clarify some of this thread, I think that the fact that rare_write uses an mm_struct and alias mappings under the hood should be completely invisible to users of the API. No one should ever be handed a writable pointer to rare_write memory (except perhaps during bootup or when initializing a large complex data structure that will be rare_write but isn't yet, e.g. the policy db).
For example, there could easily be architectures where having a writable alias is problematic. On such architectures, an entirely different mechanism might work better. And, if a tool like KNOX ever becomes a *part* of the Linux kernel (hint hint!)
If you have multiple pools and one mm_struct per pool, you'll need a way to find the mm_struct from a given allocation. Regardless of how the mm_structs are set up, changing rare_write memory to normal memory or vice versa will require a global TLB flush (all ASIDs and global pages) on all CPUs, so having extra mm_structs doesn't seem to buy much.
(It's just possible that changing rare_write back to normal might be able to avoid the flush if the spurious faults can be handled reliably.)
Re: [PATCH 10/17] prmem: documentation
Adding SELinux folks and the SELinux ml
I think it's better if they participate in this discussion.
Quoted 31 lines · Andy Lutomirski
Quoted 21 lines · Matthew Wilcox
Quoted 19 lines · Igor Stoppa
Quoted 5 lines · Matthew Wilcox
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p =
rare_alloc(...); /* writable pointer */
p->a= x;
q =rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
and that would do a swipe on all the pages currently in use. In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.
The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
Quoted 8 lines
Quoted 4 lines
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
What Andy is proposing effectively puts all rare allocations into one pool. Although I suppose it could be generalised to multiple pools ... one
mm_structper pool. Andy, what do you think to doing that?
Quoted 5 lines
Hmm. Let's see.
To clarify some of this thread, I think that the fact that
rare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API.
I agree.
Quoted 4 lines
No one should ever be
handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).
The policy db doesn't need to be write rare.
Actually, it really shouldn't be write rare.
Maybe it's just a matter of wording, but effectively the policyDB can be trated with this sequence:
1) allocate various data structures in writable form
2) initialize them
3) go back to 1 as needed
4) lock down everything that has been allocated, as Read-Only
The reason why I stress ReadOnly is that differentiating what is really ReadOnly from what is WriteRare provides an extra edge against attacks, because attempts to alter ReadOnly data through a WriteRare API could be detected
5) read any part of the policyDB during regular operations
6) in case of update, create a temporary new version, using steps 1..3
7) if update successful, use the new one and destroy the old one
8) if the update failed, destroy the new one
The destruction at points 7 and 8 is not so much a write operation, as it is a release of the memory.
So, we might have a bit different interpretation of what write-rare means wrt destroying the memory and its content.
To clarify: I've been using write-rare to indicate primarily small operations that one would otherwise achieve with "=", memcpy or memset or more complex variants, like atomic ops, rcu pointer assignment, etc.
Tearing down an entire set of allocations like the policyDB doesn't fit very well with that model.
The only part which _needs_ to be write rare, in the policyDB, is the set of pointers which are used to access all the dynamically allocated data set.
These pointers must be updated when a new policyDB is allocated.
Quoted 4 lines
For example, there could easily be architectures where having a writable alias is problematic. On such architectures, an entirely different mechanism might work better. And, if a tool like KNOX ever becomes a *part* of the Linux kernel (hint hint!)
Something related, albeit not identical is going on here [1] Eventually, it could be expanded to deal also with write rare.
Quoted 2 lines
If you have multiple pools and one
mm_structper pool, you'll need a way to find themm_structfrom a given allocation.
Indeed. In my patchset, based on vmas, I do the following:
* a private field from the page struct points to the vma using that page
* inside the vma there is alist_head used only during deletion
- one pointer is used to chain vmas fro mthe same pool
- one pointer points to the pool struct
* the pool struct has the property to use for all the associated allocations: is it write-rare, read-only, does it auto protect, etc.
Quoted 5 lines
Regardless of how
themm_structsare set up, changingrare_writememory to normal memory or vice versa will require a global TLB flush (all ASIDs and global pages) on all CPUs, so having extramm_structsdoesn't seem to buy much.
1) it supports differnt levels of protection:
temporarily unprotected vs read-only vs write-rare
2) the change of write permission should be possible only toward more restrictive rules (writable -> write-rare -> read-only) and only to the point that was specified while creating the pool, to avoid DOS attacks, where a write-rare is flipped into read-only and further updates fail (ex: prevent IMA from registering modifications to a file, by not letting it store new information - I'm not 100% sure this would work, but it gives the idea, I think)
3) being able to track all the allocations related to a pool would allow to perform mass operations, like reducing the writabilty or destroying all the allocations.
Quoted 3 lines
(It's just possible that changing
rare_writeback to normal might be able to avoid the flush if the spurious faults can be handled reliably.)
I do not see the need for such a case of degrading the write permissions of an allocation, unless it refers to the release of a pool of allocations (see updating the SELinux policy DB)
[1] https://www.openwall.com/lists/kernel-hardening/2018/10/26/11
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 113 lines · Igor Stoppa
Adding SELinux folks and the SELinux ml
I think it's better if they participate in this discussion.
Quoted 31 lines · Andy Lutomirski
Quoted 21 lines · Matthew Wilcox
Quoted 19 lines · Igor Stoppa
Quoted 5 lines · Matthew Wilcox
Quoted 5 lines
Quoted 5 lines
So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */
p->a= x;
q = rare_protect(p); /* read-only pointer */With pools and memory allocated from
vmap_areas, I was able to sayprotect(pool)
and that would do a swipe on all the pages currently in use. In the SELinux policyDB, for example, one doesn't really want to individually protect each allocation.
The loading phase happens usually at boot, when the system can be assumed to be sane (one might even preload a bare-bone set of rules from initramfs and then replace it later on, with the full blown set).
Quoted 8 lines
Quoted 4 lines
There is no need to process each of these tens of thousands allocations and initialization as write-rare.
Would it be possible to do the same here?
What Andy is proposing effectively puts all rare allocations into one pool. Although I suppose it could be generalised to multiple pools ... one
mm_structper pool. Andy, what do you think to doing that?Quoted 5 lines
Hmm. Let's see.
To clarify some of this thread, I think that the fact that
rare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API.I agree.
Quoted 4 lines
No one should ever be
handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).The policy db doesn't need to be write rare.
Actually, it really shouldn't be write rare.Maybe it's just a matter of wording, but effectively the policyDB can be trated with this sequence:
1) allocate various data structures in writable form
2) initialize them
3) go back to 1 as needed
4) lock down everything that has been allocated, as Read-Only
The reason why I stress ReadOnly is that differentiating what is really ReadOnly from what is WriteRare provides an extra edge against attacks, because attempts to alter ReadOnly data through a WriteRare API could be detected5) read any part of the policyDB during regular operations
6) in case of update, create a temporary new version, using steps 1..3
7) if update successful, use the new one and destroy the old one
8) if the update failed, destroy the new one
The destruction at points 7 and 8 is not so much a write operation, as it is a release of the memory.
So, we might have a bit different interpretation of what write-rare means wrt destroying the memory and its content.
To clarify: I've been using write-rare to indicate primarily small operations that one would otherwise achieve with "=", memcpy or memset or more complex variants, like atomic ops, rcu pointer assignment, etc.
Tearing down an entire set of allocations like the policyDB doesn't fit very well with that model.
The only part which _needs_ to be write rare, in the policyDB, is the set of pointers which are used to access all the dynamically allocated data set.
These pointers must be updated when a new policyDB is allocated.
Quoted 4 lines
For example, there could easily be architectures where having a writable alias is problematic. On such architectures, an entirely different mechanism might work better. And, if a tool like KNOX ever becomes a *part* of the Linux kernel (hint hint!)
Something related, albeit not identical is going on here [1] Eventually, it could be expanded to deal also with write rare.
Quoted 2 lines
If you have multiple pools and one
mm_structper pool, you'll need a way to find themm_structfrom a given allocation.Indeed. In my patchset, based on vmas, I do the following:
* a private field from the page struct points to the vma using that page
* inside the vma there isalist_headused only during deletion
- one pointer is used to chain vmas fro mthe same pool
- one pointer points to the pool struct
* the pool struct has the property to use for all the associated allocations: is it write-rare, read-only, does it auto protect, etc.Quoted 5 lines
Regardless of how
themm_structsare set up, changingrare_writememory to normal memory or vice versa will require a global TLB flush (all ASIDs and global pages) on all CPUs, so having extramm_structsdoesn't seem to buy much.1) it supports differnt levels of protection:
temporarily unprotected vs read-only vs write-rare2) the change of write permission should be possible only toward more restrictive rules (writable -> write-rare -> read-only) and only to the point that was specified while creating the pool, to avoid DOS attacks, where a write-rare is flipped into read-only and further updates fail (ex: prevent IMA from registering modifications to a file, by not letting it store new information - I'm not 100% sure this would work, but it gives the idea, I think)
Quoted 7 lines
3) being able to track all the allocations related to a pool would allow to perform mass operations, like reducing the writabilty or destroying all the allocations.
Quoted 3 lines
(It's just possible that changing
rare_writeback to normal might be able to avoid the flush if the spurious faults can be handled reliably.)I do not see the need for such a case of degrading the write permissions of an allocation, unless it refers to the release of a pool of allocations (see updating the SELinux policy DB)
A few more thoughts about pools. Not sure if they are all correct.
Note: I stick to "pool", instead of mm_struct, because what I'll say is mostly independent from the implementation.
- As Peter Zijlstra wrote earlier, protecting a target moves the focus of the attack to something else. In this case, probably, the "something else" would be the metadata of the pool(s).
- The amount of pools needed should be known at compilation time, so the metadata used for pools could be statically allocated and any change to it could be treated as write-rare.
- only certain fields of a pool structure would be writable, even as write-rare, after the pool is initialized.
In case the pool is a mm_struct or a superset (containing also additional properties, like the type of writability: RO or WR), the field
struct vm_area_struct *mmap;
is an example of what could be protected. It should be alterable only when creating/destroying the pool and making the first initialization.
- to speed up and also improve the validation of the target of a write-rare operation, it would be really desirable if the target had some intrinsic property which clearly differentiates it from non-write-rare memory. Its address, for example. The amount of write rare data needed by a full kernel should not exceed a few tens of megabytes. On a 64 bit system it shouldn't be so bad to reserve an address range maybe one order of magnitude lager than that. It could even become a parameter for the creation of a pool.
SELinux, for example, should fit within 10-20MB. Or it could be a command line parameter.
- even if an hypervisor was present, it would be preferable to use it exclusively as extra protection, which triggers an exception only when something abnormal happens. The hypervisor should not become aware of the actual meaning of kernel (meta)data. Ideally, it would be mostly used for trapping unexpected writes to pages which are not supposed to be modified.
- one more reason for using pools is that, if each pool was also acting as memory cache for its users, attacks relying on use-after-free would not have access to possible vulnerability, because the memory and addresses associated with a pool would stay with it.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 6 lines · Andy Lutomirski
To clarify some of this thread, I think that the fact that
rare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API. No one should ever be handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).
Being able to use pointers would make it far easier to do atomics and other things though.
Quoted 2 lines
For example, there could easily be architectures where having a writable alias is problematic.
Mostly we'd just have to be careful of cache aliases, alignment should be able to sort that I think.
Quoted 2 lines
If you have multiple pools and one
mm_structper pool, you'll need a way to find themm_structfrom a given allocation.
Or keep track of it externally. For example by context. If you modify page-tables you pick the page-table pool, if you modify selinux state, you pick the selinux pool.
Quoted 4 lines
Regardless of how the
mm_structsare set up, changingrare_writememory to normal memory or vice versa will require a global TLB flush (all ASIDs and global pages) on all CPUs, so having extramm_structsdoesn't seem to buy much.
The way I understand it, the point is that if you stick page-tables and selinux state in different pools, a stray write in one will never affect the other.
Re: [PATCH 10/17] prmem: documentation
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Andy Lutomirski
To clarify some of this thread, I think that the fact that
rare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API. No one should ever be handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).
Quoted 2 lines
Being able to use pointers would make it far easier to do atomics and other things though.
This stuff is called *rare* write for a reason. Do we really want to allow atomics beyond just store-release? Taking a big lock and then writing in the right order should cover everything, no?
Quoted 14 lines
Quoted 3 lines
For example, there could easily be architectures where having a writable alias is problematic.
Mostly we'd just have to be careful of cache aliases, alignment should be able to sort that I think.
Quoted 2 lines
If you have multiple pools and one
mm_structper pool, you'll need a way to find themm_structfrom a given allocation.Or keep track of it externally. For example by context. If you modify page-tables you pick the page-table pool, if you modify selinux state, you pick the selinux pool.
Quoted 9 lines
Quoted 4 lines
Regardless of how the
mm_structsare set up, changingrare_writememory to normal memory or vice versa will require a global TLB flush (all ASIDs and global pages) on all CPUs, so having extramm_structsdoesn't seem to buy much.The way I understand it, the point is that if you stick page-tables and selinux state in different pools, a stray write in one will never affect the other.
Hmm. That’s not totally crazy, but the API would need to be carefully designed. And some argument would have to be made as to why it’s better to use a different address space as opposed to checking in software along the lines of the uaccess checking.
Re: [PATCH 10/17] prmem: documentation
Quoted 11 lines · Andy Lutomirski
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Andy Lutomirski
To clarify some of this thread, I think that the fact that
rare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API. No one should ever be handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).
Quoted 6 lines
Quoted 2 lines
Being able to use pointers would make it far easier to do atomics and other things though.
This stuff is called *rare* write for a reason. Do we really want to allow atomics beyond just store-release? Taking a big lock and then writing in the right order should cover everything, no?
Ah, so no. That naming is very misleading.
We modify page-tables a _lot_. The point is that only a few sanctioned sites are allowed writing to it, not everybody.
I _think_ the use-case for atomics is updating the reference counts of objects that are in this write-rare domain. But I'm not entirely clear on that myself either. I just really want to avoid duplicating that stuff.
Re: [PATCH 10/17] prmem: documentation
Quoted 25 lines · Peter Zijlstra
Quoted 13 lines · Andy Lutomirski
Quoted 11 lines
Quoted 10 lines · Peter Zijlstra
On Tue, Oct 30, 2018 at 09:41:13PM -0700, Andy Lutomirski wrote:
To clarify some of this thread, I think that the fact thatrare_writeuses anmm_structand alias mappings under the hood should be completely invisible to users of the API. No one should ever be handed a writable pointer torare_writememory (except perhaps during bootup or when initializing a large complex data structure that will berare_writebut isn't yet, e.g. the policy db).Quoted 6 lines
Quoted 2 lines
Being able to use pointers would make it far easier to do atomics and other things though.
This stuff is called *rare* write for a reason. Do we really want to allow atomics beyond just store-release? Taking a big lock and then writing in the right order should cover everything, no?
Ah, so no. That naming is very misleading.
Quoted 7 lines
We modify page-tables a _lot_. The point is that only a few sanctioned sites are allowed writing to it, not everybody.
I _think_ the use-case for atomics is updating the reference counts of objects that are in this write-rare domain. But I'm not entirely clear on that myself either. I just really want to avoid duplicating that stuff.
Sounds nuts. Doing a rare-write is many hundreds of cycles at best. Using that for a reference count sounds wacky.
Can we see a *real* use case before we over complicate the API?
Re: [PATCH 10/17] prmem: documentation
Quoted 6 lines · Andy Lutomirski
Quoted 3 lines · Peter Zijlstra
Quoted 9 lines
Quoted 4 lines
I _think_ the use-case for atomics is updating the reference counts of objects that are in this write-rare domain. But I'm not entirely clear on that myself either. I just really want to avoid duplicating that stuff.
Sounds nuts. Doing a rare-write is many hundreds of cycles at best. Using that for a reference count sounds wacky.
Can we see a *real* use case before we over complicate the API?
Does patch #14 of this set not qualify? ima_htable.len ?
https://www.openwall.com/lists/kernel-hardening/2018/10/23/20
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 17 lines · Igor Stoppa
Quoted 13 lines · Andy Lutomirski
Quoted 10 lines · Peter Zijlstra
I _think_ the use-case for atomics is updating the reference counts of objects that are in this write-rare domain. But I'm not entirely clear on that myself either. I just really want to avoid duplicating that stuff.
Sounds nuts. Doing a rare-write is many hundreds of cycles at best. Using that for a reference count sounds wacky.
Quoted 8 lines
Quoted 2 lines
Can we see a *real* use case before we over complicate the API?
Does patch #14 of this set not qualify? ima_htable.len ?
https://www.openwall.com/lists/kernel-hardening/2018/10/23/20
Do you mean this (sorry for whitespace damage):
+ pratomic_long_inc(&ima_htable.len);
- atomic_long_inc(&ima_htable.len);
if (update_htable) {
key = ima_hash_key(entry->digest);- hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
+ prhlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
}
ISTM you don't need that atomic operation -- you could take a spinlock and then just add one directly to the variable.
--Andy
Re: [PATCH 10/17] prmem: documentation
Quoted 2 lines · Andy Lutomirski
ISTM you don't need that atomic operation -- you could take a spinlock and then just add one directly to the variable.
It was my intention to provide a 1:1 conversion of existing code, as it should be easier to verify the correctness of the conversion, as long as there isn't any significant degradation in performance.
The rework could be done afterward.
--
igor
Re: [PATCH 10/17] prmem: documentation
Igor,
Quoted 10 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
ISTM you don't need that atomic operation -- you could take a spinlock and then just add one directly to the variable.
It was my intention to provide a 1:1 conversion of existing code, as it should be easier to verify the correctness of the conversion, as long as there isn't any significant degradation in performance.
The rework could be done afterward.
Please don't go there. The usual approach is to
1) Rework existing code in a way that the new functionality can be added with minimal effort afterwards and without creating API horrors.
2) Verify correctness of the rework
3) Add the new functionality
That avoids creation of odd functionality and APIs in the first place, so they won't be used in other places and does not leave half cleaned up code around which will stick for a long time.
Thanks,
tglx
Re: [PATCH 10/17] prmem: documentation
Quoted 8 lines · Thomas Gleixner
Quoted 1 line · Igor Stoppa
The rework could be done afterward.
Please don't go there. The usual approach is to
1) Rework existing code in a way that the new functionality can be added with minimal effort afterwards and without creating API horrors.
Thanks a lot for the advice.
It makes things even easier for me, as I can start the rework, while the discussion on the actual write-rare mechanism continues.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 12 lines · Andy Lutomirski
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Andy Lutomirski
This stuff is called *rare* write for a reason. Do we really want to allow atomics beyond just store-release? Taking a big lock and then writing in the right order should cover everything, no?
Ah, so no. That naming is very misleading.
We modify page-tables a _lot_. The point is that only a few sanctioned sites are allowed writing to it, not everybody.
Quoted 6 lines
Quoted 4 lines
I _think_ the use-case for atomics is updating the reference counts of objects that are in this write-rare domain. But I'm not entirely clear on that myself either. I just really want to avoid duplicating that stuff.
Sounds nuts. Doing a rare-write is many hundreds of cycles at best.
Yes, which is why I'm somewhat sceptical of the whole endeavour.
Re: [PATCH 10/17] prmem: documentation
In reply to Matthew WilcoxQuoted 22 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */
Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);
How about:
rare_write(&q->a, y);
Or, for big writes:
rare_write_copy(&q, local_q);
This avoids a whole ton of issues. In practice, actually running with a special mm requires preemption disabled as well as some other stuff, which Nadav carefully dealt with.
Also, can we maybe focus on getting something merged for statically allocated data first?
Finally, one issue: rare_alloc() is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.
Nadav, want to resubmit your series? IIRC the only thing wrong with it was that it was a big change and we wanted a simpler fix to backport. But that’s all done now, and I, at least, rather liked your code. :)
Re: [PATCH 10/17] prmem: documentation
Quoted 30 lines · Andy Lutomirski
Quoted 22 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);How about:
Quoted 7 lines
rare_write(&q->a, y);Or, for big writes:
rare_write_copy(&q, local_q);This avoids a whole ton of issues. In practice, actually running with a special mm requires preemption disabled as well as some other stuff, which Nadav carefully dealt with.
This is what I had before, yes:
https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/commit/?h=kspp/write-rarely&id=9ab0cb2618ebbc51f830ceaa06b7d2182fe1a52d
It just needs the switch_mm() backend.
Signature · 1 line
Kees Cook
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 6 lines · Andy Lutomirski
Finally, one issue:
rare_alloc()is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.
Batching operations is kind of the whole point of the VM ;-) Either this rare memory gets used a lot, in which case we'll want to create slab caches for it, make it a MM zone and the whole nine yeards, or it's not used very much in which case it doesn't matter that performance sucks.
For now, I'd suggest allocating 2MB chunks as needed, and having a shrinker to hand back any unused pieces.
Re: [PATCH 10/17] prmem: documentation
Quoted 8 lines · Matthew Wilcox
Quoted 6 lines · Andy Lutomirski
Finally, one issue:
rare_alloc()is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.
Quoted 7 lines
Batching operations is kind of the whole point of the VM ;-) Either this rare memory gets used a lot, in which case we'll want to create slab caches for it, make it a MM zone and the whole nine yeards, or it's not used very much in which case it doesn't matter that performance sucks.
For now, I'd suggest allocating 2MB chunks as needed, and having a shrinker to hand back any unused pieces.
One of the prime candidates for this sort of protection is IMA.
In the IMA case, there are ever-growing lists which are populated when accessing files.
It's something that ends up on the critical path of any usual performance critical use case, when accessing files for the first time, like at boot/application startup.
Also the SELinux AVC is based on lists. It uses an object cache, but it is still something that grows and is on the critical path of evaluating the callbacks from the LSM hooks. A lot of them.
These are the main two reasons, so far, for me advocating an optimization of the write-rare version of the (h)list.
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 24 lines · Igor Stoppa
Quoted 8 lines · Matthew Wilcox
Quoted 6 lines · Andy Lutomirski
Finally, one issue:
rare_alloc()is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.Quoted 7 lines
Batching operations is kind of the whole point of the VM ;-) Either this rare memory gets used a lot, in which case we'll want to create slab caches for it, make it a MM zone and the whole nine yeards, or it's not used very much in which case it doesn't matter that performance sucks.
For now, I'd suggest allocating 2MB chunks as needed, and having a shrinker to hand back any unused pieces.
One of the prime candidates for this sort of protection is IMA. In the IMA case, there are ever-growing lists which are populated when accessing files.
It's something that ends up on the critical path of any usual performance critical use case, when accessing files for the first time, like at boot/application startup.
Quoted 6 lines
Also the SELinux AVC is based on lists. It uses an object cache, but it is still something that grows and is on the critical path of evaluating the callbacks from the LSM hooks. A lot of them.
These are the main two reasons, so far, for me advocating an optimization of the write-rare version of the (h)list.
I think these are both great examples of why doubly-linked lists _suck_. You have to modify three cachelines to add an entry to a list. Walking a linked list is an exercise in cache misses. Far better to use an XArray / IDR for this purpose.
Re: [PATCH 10/17] prmem: documentation
In reply to Matthew WilcoxQuoted 8 lines · Matthew Wilcox
Quoted 6 lines · Andy Lutomirski
Finally, one issue:
rare_alloc()is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.
Quoted 4 lines
Batching operations is kind of the whole point of the VM ;-) Either this rare memory gets used a lot, in which case we'll want to create slab caches for it, make it a MM zone and the whole nine yeards, or it's not used very much in which case it doesn't matter that performance sucks.
Yes, for the dynamic case something along those lines would be needed. If we have a single rare zone, we could even have __GFP_RARE or whatever that manages this.
The page allocator would have to grow a rare memblock type, and every rare alloc would allocate from a rare memblock, when none is available, creation of a rare block would set up the mappings etc..
Quoted 2 lines
For now, I'd suggest allocating 2MB chunks as needed, and having a shrinker to hand back any unused pieces.
Something like the percpu allocator?
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiFrom: Andy Lutomirski
Sent: October 30, 2018 at 6:51:17 PM GMT
Quoted 55 lines
To: Matthew Wilcox <[email protected]>, [email protected]
Cc: Kees Cook <[email protected]>, Peter Zijlstra <[email protected]>, Igor Stoppa <[email protected]>, Mimi Zohar <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, linux-security-module <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 20 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);How about:
rare_write(&q->a, y);Or, for big writes:
rare_write_copy(&q, local_q);This avoids a whole ton of issues. In practice, actually running with a special mm requires preemption disabled as well as some other stuff, which Nadav carefully dealt with.
Also, can we maybe focus on getting something merged for statically allocated data first?
Finally, one issue:
rare_alloc()is going to utterly suck performance-wise due to the global IPI when the region gets zapped out of the direct map or otherwise made RO. This is the same issue that makes all existing XPO efforts so painful. We need to either optimize the crap out of it somehow or we need to make sure it’s not called except during rare events like device enumeration.
Quoted 3 lines
Nadav, want to resubmit your series? IIRC the only thing wrong with it was that it was a big change and we wanted a simpler fix to backport. But that’s all done now, and I, at least, rather liked your code. :)
I guess since it was based on your ideas…
Anyhow, the only open issue that I have with v2 is Peter’s wish that I would make kgdb use of poke_text() less disgusting. I still don’t know exactly how to deal with it.
Perhaps it (fixing kgdb) can be postponed? In that case I can just resend v2.
Re: [PATCH 10/17] prmem: documentation
Quoted 4 lines · Nadav Amit
Quoted 3 lines
Nadav, want to resubmit your series? IIRC the only thing wrong with it was that it was a big change and we wanted a simpler fix to backport. But that’s all done now, and I, at least, rather liked your code. :)
Quoted 8 lines
I guess since it was based on your ideas…
Anyhow, the only open issue that I have with v2 is Peter’s wish that I would make kgdb use of
poke_text()less disgusting. I still don’t know exactly how to deal with it.Perhaps it (fixing kgdb) can be postponed? In that case I can just resend v2.
Oh man, I completely forgot about kgdb :/
Also, would it be possible to entirely remove that kmap fallback path?
Re: [PATCH 10/17] prmem: documentation
From: Peter Zijlstra
Sent: October 31, 2018 at 9:08:35 AM GMT
Quoted 16 lines
To: Nadav Amit <[email protected]>
Cc: Andy Lutomirski <[email protected]>, Matthew Wilcox <[email protected]>, Kees Cook <[email protected]>, Igor Stoppa <[email protected]>, Mimi Zohar <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, linux-security-module <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 10 lines · Nadav Amit
Quoted 3 lines
Nadav, want to resubmit your series? IIRC the only thing wrong with it was that it was a big change and we wanted a simpler fix to backport. But that’s all done now, and I, at least, rather liked your code. :)
I guess since it was based on your ideas…
Anyhow, the only open issue that I have with v2 is Peter’s wish that I would make kgdb use of
poke_text()less disgusting. I still don’t know exactly how to deal with it.
Quoted 6 lines
Quoted 2 lines
Perhaps it (fixing kgdb) can be postponed? In that case I can just resend v2.
Oh man, I completely forgot about kgdb :/
Also, would it be possible to entirely remove that kmap fallback path?
Let me see what I can do about it.
Re: [PATCH 10/17] prmem: documentation
From: Nadav Amit
Sent: November 1, 2018 at 4:31:59 PM GMT
Quoted 23 lines
To: Peter Zijlstra <[email protected]>
Cc: Andy Lutomirski <[email protected]>, Matthew Wilcox <[email protected]>, Kees Cook <[email protected]>, Igor Stoppa <[email protected]>, Mimi Zohar <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, linux-security-module <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationFrom: Peter Zijlstra
Sent: October 31, 2018 at 9:08:35 AM GMTQuoted 16 lines
To: Nadav Amit <[email protected]>
Cc: Andy Lutomirski <[email protected]>, Matthew Wilcox <[email protected]>, Kees Cook <[email protected]>, Igor Stoppa <[email protected]>, Mimi Zohar <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, linux-security-module <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 10 lines · Nadav Amit
Quoted 3 lines
Nadav, want to resubmit your series? IIRC the only thing wrong with it was that it was a big change and we wanted a simpler fix to backport. But that’s all done now, and I, at least, rather liked your code. :)
I guess since it was based on your ideas…
Anyhow, the only open issue that I have with v2 is Peter’s wish that I would make kgdb use of
poke_text()less disgusting. I still don’t know exactly how to deal with it.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Perhaps it (fixing kgdb) can be postponed? In that case I can just resend v2.
Oh man, I completely forgot about kgdb :/
Also, would it be possible to entirely remove that kmap fallback path?
Let me see what I can do about it.
My patches had several embarrassing bugs. I’m fixing and will resend later, hopefully today.
Re: [PATCH 10/17] prmem: documentation
In reply to Matthew WilcoxQuoted 18 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */
Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);
Why would you have rare_alloc() imply rare_modify() ? Would you have the allocator meta data inside the rare section?
Re: [PATCH 10/17] prmem: documentation
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Matthew Wilcox
Quoted 5 lines · Andy Lutomirski
Quoted 1 line · Kees Cook
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.I'd like to understand this approach a little better. In a syscall path, we run with the user task's mm. What you're proposing is that when we want to modify rare data, we switch to
rare_mmwhich contains a writable mapping to all the kernel data which is rare-write.So the API might look something like this:
void *p = rare_alloc(...); /* writable pointer */ p->a = x; q = rare_protect(p); /* read-only pointer */
Quoted 8 lines
Quoted 5 lines
To subsequently modify q,
p = rare_modify(q); q->a = y; rare_protect(p);Why would you have
rare_alloc()implyrare_modify()? Would you have the allocator meta data inside the rare section?
Normally when I allocate some memory I need to initialise it before doing anything else with it ;-)
I mean, you could do:
ro = rare_alloc(..); rare = rare_modify(ro); rare->a = x; rare_protect(rare);
but that's more typing.
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiHello,
I've been studying v4 of the patch-set [1] that Nadav has been working on. Incidentally, I think it would be useful to cc also the security/hardening ml. The patch-set seems to be close to final, so I am resuming this discussion.
Quoted 1 line · Andy Lutomirski
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.
After reading the code, I see what you meant.
I think I can work with it.
But I have a couple of questions wrt the use of this mechanism, in the context of write rare.
1) mm_struct.
Iiuc, the purpose of the patchset is mostly (only?) to patch kernel code (live patch?), which seems to happen sequentially and in a relatively standardized way, like replacing the NOPs specifically placed in the functions that need patching.
This is a bit different from the more generic write-rare case, applied to data.
As example, I have in mind a system where both IMA and SELinux are in use.
In this system, a file is accessed for the first time.
That would trigger 2 things:
- evaluation of the SELinux rules and probably update of the AVC cache
- IMA measurement and update of the measurements
Both of them could be write protected, meaning that they would both have to be modified through the write rare mechanism.
While the events, for 1 specific file, would be sequential, it's not difficult to imagine that multiple files could be accessed at the same time.
If the update of the data structures in both IMA and SELinux must use the same mm_struct, that would have to be somehow regulated and it would introduce an unnecessary (imho) dependency.
How about having one mm_struct for each writer (core or thread)?
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
Is this a valid concern/approach?
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 39 lines · Igor Stoppa
Hello,
I've been studying v4 of the patch-set [1] that Nadav has been working on. Incidentally, I think it would be useful to cc also thesecurity/hardeningml.
The patch-set seems to be close to final, so I am resuming this discussion.Quoted 1 line · Andy Lutomirski
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.After reading the code, I see what you meant.
I think I can work with it.But I have a couple of questions wrt the use of this mechanism, in the context of write rare.
1)
mm_struct.Iiuc, the purpose of the patchset is mostly (only?) to patch kernel code (live patch?), which seems to happen sequentially and in a relatively standardized way, like replacing the NOPs specifically placed in the functions that need patching.
This is a bit different from the more generic write-rare case, applied to data.
As example, I have in mind a system where both IMA and SELinux are in use.
In this system, a file is accessed for the first time.
That would trigger 2 things:
- evaluation of the SELinux rules and probably update of the AVC cache
- IMA measurement and update of the measurementsBoth of them could be write protected, meaning that they would both have to be modified through the write rare mechanism.
Quoted 9 lines
While the events, for 1 specific file, would be sequential, it's not difficult to imagine that multiple files could be accessed at the same time.
If the update of the data structures in both IMA and SELinux must use the same
mm_struct, that would have to be somehow regulated and it would introduce an unnecessary (imho) dependency.How about having one
mm_structfor each writer (core or thread)?
I don't think that helps anything. I think the mm_struct used for prmem (or rare_write or whatever you want to call it) should be entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's an mm_struct involved. It's also entirely possible that some architectures won't even use an mm_struct behind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.
Quoted 6 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
The reason for the particular architecture of text_poke() is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. For rare_write, you don't actually need it to work as such until fairly late in boot, since the rare_writable data will just be writable early on.
Quoted 5 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
Quoted 7 lines
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original mm_struct (most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.
Re: [PATCH 10/17] prmem: documentation
From: Andy Lutomirski
Sent: November 13, 2018 at 5:16:09 PM GMT
Quoted 82 lines
To: Igor Stoppa <[email protected]>
Cc: Kees Cook <[email protected]>, Peter Zijlstra <[email protected]>, Nadav Amit <[email protected]>, Mimi Zohar <[email protected]>, Matthew Wilcox <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, LSM List <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 38 lines · Igor Stoppa
Hello,
I've been studying v4 of the patch-set [1] that Nadav has been working on. Incidentally, I think it would be useful to cc also thesecurity/hardeningml.
The patch-set seems to be close to final, so I am resuming this discussion.Quoted 1 line · Andy Lutomirski
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.After reading the code, I see what you meant.
I think I can work with it.But I have a couple of questions wrt the use of this mechanism, in the context of write rare.
1)
mm_struct.Iiuc, the purpose of the patchset is mostly (only?) to patch kernel code (live patch?), which seems to happen sequentially and in a relatively standardized way, like replacing the NOPs specifically placed in the functions that need patching.
This is a bit different from the more generic write-rare case, applied to data.
As example, I have in mind a system where both IMA and SELinux are in use.
In this system, a file is accessed for the first time.
That would trigger 2 things:
- evaluation of the SELinux rules and probably update of the AVC cache
- IMA measurement and update of the measurementsBoth of them could be write protected, meaning that they would both have to be modified through the write rare mechanism.
Quoted 8 lines
While the events, for 1 specific file, would be sequential, it's not difficult to imagine that multiple files could be accessed at the same time.
If the update of the data structures in both IMA and SELinux must use the same
mm_struct, that would have to be somehow regulated and it would introduce an unnecessary (imho) dependency.How about having one
mm_structfor each writer (core or thread)?I don't think that helps anything. I think the
mm_structused for prmem (orrare_writeor whatever you want to call it) should be entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's anmm_structinvolved. It's also entirely possible that some architectures won't even use anmm_structbehind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.Quoted 4 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
The reason for the particular architecture of
text_poke()is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. Forrare_write, you don't actually need it to work as such until fairly late in boot, since therare_writabledata will just be writable early on.Quoted 8 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
Quoted 8 lines
Quoted 3 lines
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original
mm_struct(most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.
I guess that enabling IRQs might break some hidden assumptions in the code, but is there a fundamental reason that IRQs need to be disabled? use_mm() got them enabled, although it is only suitable for kernel threads.
Re: [PATCH 10/17] prmem: documentation
Quoted 89 lines · Nadav Amit
From: Andy Lutomirski
Sent: November 13, 2018 at 5:16:09 PM GMTQuoted 86 lines
To: Igor Stoppa <[email protected]>
Cc: Kees Cook <[email protected]>, Peter Zijlstra <[email protected]>, Nadav Amit <[email protected]>, Mimi Zohar <[email protected]>, Matthew Wilcox <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, LSM List <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 38 lines · Igor Stoppa
Hello,
I've been studying v4 of the patch-set [1] that Nadav has been working on. Incidentally, I think it would be useful to cc also thesecurity/hardeningml.
The patch-set seems to be close to final, so I am resuming this discussion.Quoted 1 line · Andy Lutomirski
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.After reading the code, I see what you meant.
I think I can work with it.But I have a couple of questions wrt the use of this mechanism, in the context of write rare.
1)
mm_struct.Iiuc, the purpose of the patchset is mostly (only?) to patch kernel code (live patch?), which seems to happen sequentially and in a relatively standardized way, like replacing the NOPs specifically placed in the functions that need patching.
This is a bit different from the more generic write-rare case, applied to data.
As example, I have in mind a system where both IMA and SELinux are in use.
In this system, a file is accessed for the first time.
That would trigger 2 things:
- evaluation of the SELinux rules and probably update of the AVC cache
- IMA measurement and update of the measurementsBoth of them could be write protected, meaning that they would both have to be modified through the write rare mechanism.
Quoted 8 lines
While the events, for 1 specific file, would be sequential, it's not difficult to imagine that multiple files could be accessed at the same time.
If the update of the data structures in both IMA and SELinux must use the same
mm_struct, that would have to be somehow regulated and it would introduce an unnecessary (imho) dependency.How about having one
mm_structfor each writer (core or thread)?I don't think that helps anything. I think the
mm_structused for prmem (orrare_writeor whatever you want to call it) should be entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's anmm_structinvolved. It's also entirely possible that some architectures won't even use anmm_structbehind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.Quoted 4 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
The reason for the particular architecture of
text_poke()is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. Forrare_write, you don't actually need it to work as such until fairly late in boot, since therare_writabledata will just be writable early on.Quoted 11 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
Quoted 9 lines
Quoted 4 lines
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original
mm_struct(most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.I guess that enabling IRQs might break some hidden assumptions in the code, but is there a fundamental reason that IRQs need to be disabled?
use_mm()got them enabled, although it is only suitable for kernel threads.
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
--Andy
Re: [PATCH 10/17] prmem: documentation
From: Andy Lutomirski
Sent: November 13, 2018 at 5:47:16 PM GMT
Quoted 99 lines
To: Nadav Amit <[email protected]>
Cc: Igor Stoppa <[email protected]>, Kees Cook <[email protected]>, Peter Zijlstra <[email protected]>, Mimi Zohar <[email protected]>, Matthew Wilcox <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, LSM List <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 93 lines · Nadav Amit
From: Andy Lutomirski
Sent: November 13, 2018 at 5:16:09 PM GMTQuoted 90 lines
To: Igor Stoppa <[email protected]>
Cc: Kees Cook <[email protected]>, Peter Zijlstra <[email protected]>, Nadav Amit <[email protected]>, Mimi Zohar <[email protected]>, Matthew Wilcox <[email protected]>, Dave Chinner <[email protected]>, James Morris <[email protected]>, Michal Hocko <[email protected]>, Kernel Hardening <[email protected]>, linux-integrity <[email protected]>, LSM List <[email protected]>, Igor Stoppa <[email protected]>, Dave Hansen <[email protected]>, Jonathan Corbet <[email protected]>, Laura Abbott <[email protected]>, Randy Dunlap <[email protected]>, Mike Rapoport <[email protected]>, open list:DOCUMENTATION <[email protected]>, LKML <[email protected]>, Thomas Gleixner <[email protected]>
Subject: Re: [PATCH 10/17] prmem: documentationQuoted 38 lines · Igor Stoppa
Hello,
I've been studying v4 of the patch-set [1] that Nadav has been working on. Incidentally, I think it would be useful to cc also thesecurity/hardeningml.
The patch-set seems to be close to final, so I am resuming this discussion.Quoted 1 line · Andy Lutomirski
I support the addition of a rare-write mechanism to the upstream kernel. And I think that there is only one sane way to implement it: using an
mm_struct. Thatmm_struct, just like any sanemm_struct, should only differ frominit_mmin that it has extra mappings in the *user* region.After reading the code, I see what you meant.
I think I can work with it.But I have a couple of questions wrt the use of this mechanism, in the context of write rare.
1)
mm_struct.Iiuc, the purpose of the patchset is mostly (only?) to patch kernel code (live patch?), which seems to happen sequentially and in a relatively standardized way, like replacing the NOPs specifically placed in the functions that need patching.
This is a bit different from the more generic write-rare case, applied to data.
As example, I have in mind a system where both IMA and SELinux are in use.
In this system, a file is accessed for the first time.
That would trigger 2 things:
- evaluation of the SELinux rules and probably update of the AVC cache
- IMA measurement and update of the measurementsBoth of them could be write protected, meaning that they would both have to be modified through the write rare mechanism.
Quoted 8 lines
While the events, for 1 specific file, would be sequential, it's not difficult to imagine that multiple files could be accessed at the same time.
If the update of the data structures in both IMA and SELinux must use the same
mm_struct, that would have to be somehow regulated and it would introduce an unnecessary (imho) dependency.How about having one
mm_structfor each writer (core or thread)?I don't think that helps anything. I think the
mm_structused for prmem (orrare_writeor whatever you want to call it) should be entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's anmm_structinvolved. It's also entirely possible that some architectures won't even use anmm_structbehind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.Quoted 4 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
The reason for the particular architecture of
text_poke()is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. Forrare_write, you don't actually need it to work as such until fairly late in boot, since therare_writabledata will just be writable early on.Quoted 11 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original
mm_struct(most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.
Quoted 6 lines
Quoted 3 lines
I guess that enabling IRQs might break some hidden assumptions in the code, but is there a fundamental reason that IRQs need to be disabled?
use_mm()got them enabled, although it is only suitable for kernel threads.For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Oh.. Of course. It is sort of a measure to prevent a single malicious/faulty write from corrupting the sensitive memory. Doing so limits the code that can compromise the security by a write to the protected data-structures (rephrasing for myself).
I think I should add it as a comment in your patch.
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
To access that mapping, the attack should be performed from the ISR, I think.
--
igor
Re: [PATCH 10/17] prmem: documentation
I forgot one sentence :-(
Quoted 13 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
Quoted 6 lines
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
To access that mapping, the attack should be performed from the ISR, I think.
Unless the secondary mapping is also available to other cores, through the shared mm_struct ?
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 21 lines · Igor Stoppa
I forgot one sentence :-(
Quoted 17 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
Quoted 6 lines
Quoted 2 lines
To access that mapping, the attack should be performed from the ISR, I think.
Unless the secondary mapping is also available to other cores, through the shared
mm_struct?
I don't think this matters much. The other cores will only be able to use that mapping when they're doing a rare write.
Re: [PATCH 10/17] prmem: documentation
Quoted 1 line · Andy Lutomirski
On Tue, Nov 13, 2018 at 10:33 AM Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
Quoted 3 lines
Unless the secondary mapping is also available to other cores, through the shared
mm_struct?I don't think this matters much. The other cores will only be able to use that mapping when they're doing a rare write.
Which has now been promoted to target for attacks :-)
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiHi,
Quoted 25 lines · Andy Lutomirski
Quoted 24 lines · Igor Stoppa
I forgot one sentence :-(
Quoted 19 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
To access that mapping, the attack should be performed from the ISR, I think.
Quoted 6 lines
Quoted 3 lines
Unless the secondary mapping is also available to other cores, through the shared
mm_struct?I don't think this matters much. The other cores will only be able to use that mapping when they're doing a rare write.
I'm still mulling over this.
There might be other reasons for replicating the mm_struct.
If I understand correctly how the text patching works, it happens sequentially, because of the text_mutex used by arch_jump_label_transform
Which might be fine for this specific case, but I think I shouldn't introduce a global mutex, when it comes to data.
Most likely, if two or more cores want to perform a write rare operation, there is no correlation between them, they could proceed in parallel. And if there really is, then the user of the API should introduce own locking, for that specific case.
A bit unrelated question, related to text patching: I see that each patching operation is validated, but wouldn't it be more robust to first validate all of then, and only after they are all found to be compliant, to proceed with the actual modifications?
And about the actual implementation of the write rare for the statically allocated variables, is it expected that I use Nadav's function? Or that I refactor the code?
The name, referring to text would definitely not be ok for data. And I would have to also generalize it, to deal with larger amounts of data.
I would find it easier, as first cut, to replicate its behavior and refactor only later, once it has stabilized and possibly Nadav's patches have been acked.
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 35 lines · Igor Stoppa
Hi,
Quoted 21 lines · Andy Lutomirski
Quoted 20 lines · Igor Stoppa
I forgot one sentence :-(
Quoted 17 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
To access that mapping, the attack should be performed from the ISR, I think.
Unless the secondary mapping is also available to other cores, through the shared
mm_struct?I don't think this matters much. The other cores will only be able to use that mapping when they're doing a rare write.
Quoted 7 lines
I'm still mulling over this.
There might be other reasons for replicating themm_struct.If I understand correctly how the text patching works, it happens sequentially, because of the
text_mutexused byarch_jump_label_transformWhich might be fine for this specific case, but I think I shouldn't introduce a global mutex, when it comes to data.
Most likely, if two or more cores want to perform a write rare operation, there is no correlation between them, they could proceed in parallel. And if there really is, then the user of the API should introduce own locking, for that specific case.
I think that if you create per-CPU temporary mms as you proposed, you do not need a global lock. You would need to protect against module unloading, and maybe refactor the code. Specifically, I’m not sure whether protection against IRQs is something that you need. I’m also not familiar with this patch-set so I’m not sure what atomicity guarantees do you need.
Quoted 3 lines
A bit unrelated question, related to text patching: I see that each patching operation is validated, but wouldn't it be more robust to first validate all of then, and only after they are all found to be compliant, to proceed with the actual modifications?
And about the actual implementation of the write rare for the statically allocated variables, is it expected that I use Nadav's function?
It’s not “my” function. ;-)
IMHO the code is in relatively good and stable state. The last couple of versions were due to me being afraid to add BUG_ONs as Peter asked me to.
The code is rather simple, but there are a couple of pitfalls that hopefully I avoided correctly.
Regards,
Nadav
Re: [PATCH 10/17] prmem: documentation
Quoted 1 line · Nadav Amit
Quoted 1 line · Igor Stoppa
[...]
Quoted 9 lines
Quoted 6 lines
There might be other reasons for replicating the
mm_struct.If I understand correctly how the text patching works, it happens sequentially, because of the
text_mutexused byarch_jump_label_transformWhich might be fine for this specific case, but I think I shouldn't introduce a global mutex, when it comes to data.
Most likely, if two or more cores want to perform a write rare operation, there is no correlation between them, they could proceed in parallel. And if there really is, then the user of the API should introduce own locking, for that specific case.I think that if you create per-CPU temporary mms as you proposed, you do not need a global lock. You would need to protect against module unloading,
yes, it's unlikely to happen and probably a bug in the module, if it tries to write while being unloaded, but I can do it
Quoted 3 lines
and
maybe refactor the code. Specifically, I’m not sure whether protection against IRQs is something that you need.
With the initial way I used to do write rare, which was done by creating a temporary mapping visible to every core, disabling IRQs was meant to prevent that the "writer" core would be frozen and then the mappings scrubbed for the page in writable state.
Without shared mapping of the page, the only way to attack it should be to generate an interrupt on the "writer" core, while the writing is ongoing, and to perform the attack from the interrupt itself, because it is on the same core that has the writable mapping.
Maybe it's possible, but it seems to have become quite a corner case.
Quoted 2 lines
I’m also not familiar with this
patch-set so I’m not sure what atomicity guarantees do you need.
At the very least, I think I need to ensure that pointers are updated atomically, like with WRITE_ONCE() And spinlocks. Maybe atomic types can be left out.
Quoted 5 lines
Quoted 3 lines
A bit unrelated question, related to text patching: I see that each patching operation is validated, but wouldn't it be more robust to first validate all of then, and only after they are all found to be compliant, to proceed with the actual modifications?
And about the actual implementation of the write rare for the statically allocated variables, is it expected that I use Nadav's function?
It’s not “my” function. ;-)
:-P
ok, what I meant is that the signature of the __text_poke() function is quite specific to what it's meant to do.
I do not rule out that it might be eventually refactored as a special case of a more generic __write_rare() function, that would operate on different targets, but I'd rather do the refactoring after I have a clear understanding of how to alter write-protected data.
The refactoring could be the last patch of the write rare patchset.
Quoted 5 lines
IMHO the code is in relatively good and stable state. The last couple of versions were due to me being afraid to add
BUG_ONsas Peter asked me to.The code is rather simple, but there are a couple of pitfalls that hopefully I avoided correctly.
Yes, I did not mean to question the quality of the code, but I'd prefer to not have to carry also this patchset, while it's not yet merged.
I actually hope it gets merged soon :-)
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Igor StoppaQuoted 38 lines · Igor Stoppa
Hi,
Quoted 23 lines
Quoted 23 lines · Andy Lutomirski
On Tue, Nov 13, 2018 at 10:33 AM Igor Stoppa <[email protected]> wrote:
I forgot one sentence :-(
Quoted 18 lines
Quoted 5 lines · Igor Stoppa
On 13/11/2018 19:47, Andy Lutomirski wrote:
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
But I see a big difference between my initial implementation and this one.
In my case, by using a shared mapping, visible to all cores, freezing the core that is performing the write would have exposed the writable mapping to a potential attack run from another core.
If the mapping is private to the core performing the write, even if it is frozen, it's much harder to figure out what it had mapped and where, from another core.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
To access that mapping, the attack should be performed from the ISR, I think.
Unless the secondary mapping is also available to other cores, through the shared
mm_struct?I don't think this matters much. The other cores will only be able to use that mapping when they're doing a rare write.
Quoted 7 lines
I'm still mulling over this.
There might be other reasons for replicating themm_struct.If I understand correctly how the text patching works, it happens sequentially, because of the
text_mutexused byarch_jump_label_transformWhich might be fine for this specific case, but I think I shouldn't introduce a global mutex, when it comes to data.
Most likely, if two or more cores want to perform a write rare operation, there is no correlation between them, they could proceed in parallel. And if there really is, then the user of the API should introduce own locking, for that specific case.
Text patching uses the same VA for different physical addresses, so it need a mutex to avoid conflicts. I think that, for rare writes, you should just map each rare-writable address at a *different* VA. You’ll still need a mutex (mmap_sem) to synchronize allocation and freeing of rare-writable ranges, but that shouldn’t have much contention.
Quoted 5 lines
A bit unrelated question, related to text patching: I see that each patching operation is validated, but wouldn't it be more robust to first validate all of then, and only after they are all found to be compliant, to proceed with the actual modifications?
And about the actual implementation of the write rare for the statically allocated variables, is it expected that I use Nadav's function? Or that I refactor the code?
I would either refactor it or create a new function to handle the write. The main thing that Nadav is adding that I think you’ll want to use is the infrastructure for temporarily switching mms from a non-kernel-thread context.
Quoted 6 lines
The name, referring to text would definitely not be ok for data. And I would have to also generalize it, to deal with larger amounts of data.
I would find it easier, as first cut, to replicate its behavior and refactor only later, once it has stabilized and possibly Nadav's patches have been acked.
Sounds reasonable. You’ll still want Nadav’s code for setting up the mm in the first place, though.
Quoted 2 lines
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 1 line · Andy Lutomirski
Quoted 1 line · Igor Stoppa
[...]
Quoted 8 lines
Quoted 6 lines
There might be other reasons for replicating the
mm_struct.If I understand correctly how the text patching works, it happens sequentially, because of the
text_mutexused byarch_jump_label_transformWhich might be fine for this specific case, but I think I shouldn't introduce a global mutex, when it comes to data.
Most likely, if two or more cores want to perform a write rare operation, there is no correlation between them, they could proceed in parallel. And if there really is, then the user of the API should introduce own locking, for that specific case.Text patching uses the same VA for different physical addresses, so it need a mutex to avoid conflicts. I think that, for rare writes, you should just map each rare-writable address at a *different* VA. You’ll still need a mutex (
mmap_sem) to synchronize allocation and freeing of rare-writable ranges, but that shouldn’t have much contention.
I have studied the code involved with Nadav's patchset. I am perplexed about these sentences you wrote.
More to the point (to the best of my understanding):
poking_init()
-------------
1. it gets one random poking address and ensures to have at least 2 consecutive PTEs from the same PMD
2. it then proceeds to map/unmap an address from the first of the 2 consecutive PTEs, so that, later on, there will be no need to allocate pages, which might fail, if poking from atomic context.
3. at this point, the page tables are populated, for the address that was obtained at point 1, and this is ok, because the address is fixed
write_rare
----------
4. it can happen on any available core / thread at any time, therefore each of them needs a different address
5. CPUs support hotplug, but from what I have read, I might get away with having up to nr_cpus different addresses (determined at init) and I would follow the same technique used by Nadav, of forcing the mapping of 1 or 2 (1 could be enough, I have to loop anyway, at some point) pages at each address, to ensure the population of the page tables
so far, so good, but ...
6. the addresses used by each CPU are fixed
7. I do not understand the reference you make to "allocation and freeing of rare-writable ranges", because if I treat the range as such, then there is a risk that I need to populate more entries in the page table, which would have problems with the atomic context, unless write_rare from atomic is ruled out. If write_rare from atomic can be avoided, then I can also have one-use randomized addresses at each write-rare operation, instead of fixed ones, like in point 6.
and, apologies for being dense: the following is still not clear to me:
8. you wrote:
> You’ll still need a mutex (mmap_sem) to synchronize allocation > and freeing of rare-writable ranges, but that shouldn’t have much > contention.
What causes the contention? It's the fact that the various cores are using the same mm, if I understood correctly. However, if there was one mm for each core, wouldn't that make it unnecessary to have any mutex?
I feel there must be some obvious reason why multiple mms are not a good idea, yet I cannot grasp it :-(
switch_mm_irqs_off() seems to have lots of references to "this_cpu_something"; if there is any optimization from having the same next across multiple cores, I'm missing it
[...]
Quoted 1 line
I would either refactor it or create a new function to handle the write. The main thing that Nadav is adding that I think you’ll want to use is the infrastructure for temporarily switching mms from a non-kernel-thread context.
yes
[...]
Quoted 1 line
You’ll still want Nadav’s code for setting up the mm in the first place, though.
yes
--
thanks, igor
Re: [PATCH 10/17] prmem: documentation
Quoted 15 lines · Igor Stoppa
I have studied the code involved with Nadav's patchset. I am perplexed about these sentences you wrote.
More to the point (to the best of my understanding):
poking_init()
-------------
1. it gets one random poking address and ensures to have at least 2 consecutive PTEs from the same PMD
2. it then proceeds to map/unmap an address from the first of the 2 consecutive PTEs, so that, later on, there will be no need to allocate pages, which might fail, if poking from atomic context.
3. at this point, the page tables are populated, for the address that was obtained at point 1, and this is ok, because the address is fixed
Quoted 4 lines
write_rare
----------
4. it can happen on any available core / thread at any time, therefore each of them needs a different address
No? Each CPU has its own CR3 (eg each CPU might be running a different user task). If you have _one_ address for each allocation, it may or may not be mapped on other CPUs at the same time -- you simply don't care.
The writable address can even be a simple formula to calculate from the read-only address, you don't have to allocate an address in the writable mapping space.
Re: [PATCH 10/17] prmem: documentation
Quoted 22 lines · Matthew Wilcox
Quoted 15 lines · Igor Stoppa
I have studied the code involved with Nadav's patchset. I am perplexed about these sentences you wrote.
More to the point (to the best of my understanding):
poking_init()
-------------
1. it gets one random poking address and ensures to have at least 2 consecutive PTEs from the same PMD
2. it then proceeds to map/unmap an address from the first of the 2 consecutive PTEs, so that, later on, there will be no need to allocate pages, which might fail, if poking from atomic context.
3. at this point, the page tables are populated, for the address that was obtained at point 1, and this is ok, because the address is fixedQuoted 4 lines
write_rare
----------
4. it can happen on any available core / thread at any time, therefore each of them needs a different address
Quoted 8 lines
No? Each CPU has its own CR3 (eg each CPU might be running a different user task). If you have _one_ address for each allocation, it may or may not be mapped on other CPUs at the same time -- you simply don't care.
The writable address can even be a simple formula to calculate from the read-only address, you don't have to allocate an address in the writable mapping space.
Agreed. I suggest the formula:
writable_address = readable_address - rare_write_offset. For starters, rare_write_offset can just be a constant. If we want to get fancy later on, it can be randomized.
If we do it like this, then we don't need to modify any pagetables at all when we do a rare write. Instead we can set up the mapping at boot or when we allocate the rare write space, and the actual rare write code can just switch mms and do the write.
Re: [PATCH 10/17] prmem: documentation
Hello,
apologies for the delayed answer.
Please find my reply to both last mails in the thread, below.
Quoted 18 lines · Andy Lutomirski
Quoted 17 lines · Matthew Wilcox
Quoted 15 lines · Igor Stoppa
I have studied the code involved with Nadav's patchset. I am perplexed about these sentences you wrote.
More to the point (to the best of my understanding):
poking_init()
-------------
1. it gets one random poking address and ensures to have at least 2 consecutive PTEs from the same PMD
2. it then proceeds to map/unmap an address from the first of the 2 consecutive PTEs, so that, later on, there will be no need to allocate pages, which might fail, if poking from atomic context.
3. at this point, the page tables are populated, for the address that was obtained at point 1, and this is ok, because the address is fixed
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
write_rare
----------
4. it can happen on any available core / thread at any time, therefore each of them needs a different addressNo? Each CPU has its own CR3 (eg each CPU might be running a different user task). If you have _one_ address for each allocation, it may or may not be mapped on other CPUs at the same time -- you simply don't care.
Yes, somehow I lost track of that aspect.
Quoted 10 lines
Quoted 4 lines
The writable address can even be a simple formula to calculate from the read-only address, you don't have to allocate an address in the writable mapping space.
Agreed. I suggest the formula:
writable_address=readable_address-rare_write_offset. For starters,rare_write_offsetcan just be a constant. If we want to get fancy later on, it can be randomized.
ok, I hope I captured it here [1]
Quoted 4 lines
If we do it like this, then we don't need to modify any pagetables at all when we do a rare write. Instead we can set up the mapping at boot or when we allocate the rare write space, and the actual rare write code can just switch mms and do the write.
I did it. I have little feeling about the actual amount of data involved, but there is a (probably very remote) chance that the remap wouldn't work, at least in the current implementation.
It's a bit different from what I had in mind initially, since I was thinking to have one single approach to both statically allocated memory (is there a better way to describe it) and what is provided from the allocator that would come next.
As I wrote, I do not particularly like the way I implemented multiple functionality vs remapping, but I couldn't figure out any better way to do it, so eventually I kept this one, hoping to get some advice on how to improve it.
I did not provide yet an example, yet, but IMA has some flags that are probably very suitable, since they depend on policy reloading, which can happen multiple times, but could be used to disable it.
[1] https://www.openwall.com/lists/kernel-hardening/2018/12/04/3
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Igor StoppaQuoted 7 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
I'm not really saying they're less sensitive so much as that the considerations are different. I think the original rare-write code is based on ideas from grsecurity, and it was intended to protect static
data like structs full of function pointers. Those targets have some
different properties:
- Static targets are at addresses that are much more guessable, so
they're easier targets for most attacks. (Not spraying attacks like the ones you're interested in, though.)
- Static targets are higher value. No offense to IMA or AVC, but
outright execution of shellcode, hijacking of control flow, or compete disablement of core security features is higher impact than bypassing SELinux or IMA. Why would you bother corrupting the AVC if you could instead just set enforcing=0? (I suppose that corrupting the AVC is less likely to be noticed by monitoring tools.)
- Static targets are small. This means that the interrupt latency
would be negligible, especially in comparison to the latency of replacing the entire SELinux policy object.
Anyway, I'm not all that familiar with SELinux under the hood, but I'm wondering if a different approach to thinks like the policy database might be appropriate. When the policy is changed, rather than allocating rare-write memory and writing to it, what if we instead allocated normal memory, wrote to it, write-protected it, and then used the rare-write infrastructure to do a much smaller write to replace the pointer?
Admittedly, this creates a window where another core could corrupt the data as it's being written. That may not matter so much if an attacker can't force a policy update. Alternatively, the update code could re-verify the policy after write-protecting it, or there could be a fancy API to allocate some temporarily-writable memory (by creating a whole new mm_struct, mapping the memory writable just in that mm_struct, and activating it) so that only the actual policy loader could touch the memory. But I'm mostly speculating here, since I'm not familiar with the code in question.
Anyway, I tend to think that the right way to approach mainlining all this is to first get the basic rare write support for static data into place and then to build on that. I think it's great that you're pushing this effort, but doing this for SELinux and IMA is a bigger project than doing it for static data, and it might make sense to do it in bite-sized pieces.
Does any of this make sense?
Re: [PATCH 10/17] prmem: documentation
Quoted 26 lines · Andy Lutomirski
Quoted 7 lines · Igor Stoppa
Quoted 2 lines · Andy Lutomirski
For general rare-writish stuff, I don't think we want IRQs running with them mapped anywhere for write. For AVC and IMA, I'm less sure.
Why would these be less sensitive?
I'm not really saying they're less sensitive so much as that the considerations are different. I think the original rare-write code is based on ideas from grsecurity, and it was intended to protect static
data like structs full of function pointers. Those targets have some
different properties:- Static targets are at addresses that are much more guessable, so
they're easier targets for most attacks. (Not spraying attacks like the ones you're interested in, though.)- Static targets are higher value. No offense to IMA or AVC, but
outright execution of shellcode, hijacking of control flow, or compete disablement of core security features is higher impact than bypassing SELinux or IMA. Why would you bother corrupting the AVC if you could instead just set enforcing=0? (I suppose that corrupting the AVC is less likely to be noticed by monitoring tools.)
Quoted 3 lines
- Static targets are small. This means that the interrupt latency
would be negligible, especially in comparison to the latency of replacing the entire SELinux policy object.
Your analysis is correct.
In my case, having already taken care of those, I was going *also* after the next target in line.
Admittedly, flipping a bit located at a fixed offset is way easier than spraying dynamically allocated data structured.
However, once the bit is not easily writable, the only options are to either find another way to flip it (unprotect it or subvert something that can write it) or to identify another target that is still writable.
AVC and policyDB fit the latter description.
Quoted 7 lines
Anyway, I'm not all that familiar with SELinux under the hood, but I'm wondering if a different approach to thinks like the policy database might be appropriate. When the policy is changed, rather than allocating rare-write memory and writing to it, what if we instead allocated normal memory, wrote to it, write-protected it, and then used the rare-write infrastructure to do a much smaller write to replace the pointer?
Actually, that's exactly what I did.
I did not want to overload this discussion, but since you brought it up, I'm not sure write rare is enough.
* write_rare is for stuff that sometimes changes all the time, ex: AVC
* dynamic read only is for stuff that at some point should not be modified anymore, but could still be destroyed. Ex: policyDB
I think it would be good to differentiate, at runtime, between the two, to minimize the chance that a write_rare function is used against some read_only data.
Releasing dynamically allocated protected memory is also a big topic. In some cases it's allocated and released continuously, like in the AVC. Maybe it can be optimized, or maybe it can be turned into an object cache of protected object.
But for releasing, it would be good, I think, to have a mechanism for freeing all the memory in one loop, like having a pool containing all the memory that was allocated for a specific use (ex: policyDB)
Quoted 9 lines
Admittedly, this creates a window where another core could corrupt the data as it's being written. That may not matter so much if an attacker can't force a policy update. Alternatively, the update code could re-verify the policy after write-protecting it, or there could be a fancy API to allocate some temporarily-writable memory (by creating a whole new
mm_struct, mapping the memory writable just in thatmm_struct, and activating it) so that only the actual policy loader could touch the memory. But I'm mostly speculating here, since I'm not familiar with the code in question.
They are all corner cases ... possible but unlikely. Another, maybe more critical, one is that the policyDB is not available at boot.
There is a window of opportunity, before it's loaded. But it could be mitigated by loading a barebone set of rules, either from initrd or even as "firmware".
Quoted 8 lines
Anyway, I tend to think that the right way to approach mainlining all this is to first get the basic rare write support for static data into place and then to build on that. I think it's great that you're pushing this effort, but doing this for SELinux and IMA is a bigger project than doing it for static data, and it might make sense to do it in bite-sized pieces.
Does any of this make sense?
Yes, sure.
I *have* to do SELinux, but I do not necessarily have to wait for the final version to be merged upstream. And anyways Android is on a different kernel.
However, I think both SELinux and IMA have a value in being sufficiently complex cases to be used for validating the design as it evolves.
Each of them has static data that could be the first target for protection, in a smaller patch.
Lists of write rare data are probably the next big thing, in terms of defining the API.
But I could start with introducing __wr_after_init.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Andy LutomirskiQuoted 1 line · Andy Lutomirski
On Tue, Nov 13, 2018 at 6:25 AM Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 2 lines
How about having one
mm_structfor each writer (core or thread)?I don't think that helps anything. I think the
mm_structused for prmem (orrare_writeor whatever you want to call it)
write_rare / rarely can be shortened to wr_ which is kinda less confusing than rare_write, since it would become rw_ and easier to confuse with R/W
Any advice for better naming is welcome.
Quoted 3 lines
should be
entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's anmm_structinvolved.
Yes, that is fine. In my proposal I was thinking about tying it to the core/thread that performs the actual write.
The high level API could be something like:
wr_memcpy(void *src, void *dst, uint_t size)
Quoted 4 lines
It's also
entirely possible that some architectures won't even use anmm_structbehind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.
The mm_struct - or whatever is the means to do the write on that architecture - can be kept hidden from the API.
But the reason why I was proposing to have one mm_struct per writer is that, iiuc, the secondary mapping is created in the secondary mm_struct for each writer using it.
So the updating of IMA measurements would have, theoretically, also write access to the SELinux AVC. Which I was trying to avoid. And similarly any other write rare updater. Is this correct?
Quoted 5 lines
Quoted 4 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
Quoted 7 lines
The reason for the particular architecture of
text_poke()is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. Forrare_write, you don't actually need it to work as such until fairly late in boot, since therare_writabledata will just be writable early on.
Yes, that is true. I think it's safe to assume, from an attack pattern, that as long as user space is not started, the system can be considered ok. Even user-space code run from initrd should be ok, since it can be bundled (and signed) as a single binary with the kernel.
Modules loaded from a regular filesystem are a bit more risky, because an attack might inject a rogue key in the key-ring and use it to load malicious modules.
Quoted 8 lines
Quoted 8 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
Quoted 8 lines
Quoted 3 lines
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original
mm_struct(most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.
The only "excuse" I have is that write_rare is opt-in and is "rare". Maybe the enabling/disabling of interrupts - and the consequent switch of mm_struct - could be somehow tied to the latency configuration?
If preemption is disabled, the expectations on the system latency are anyway more relaxed.
But I'm not sure how it would work against I/O.
--
igor
Re: [PATCH 10/17] prmem: documentation
Quoted 22 lines · Igor Stoppa
Quoted 3 lines · Andy Lutomirski
should be
entirely abstracted away by an appropriate API, so neither SELinux nor IMA need to be aware that there's anmm_structinvolved.Yes, that is fine. In my proposal I was thinking about tying it to the core/thread that performs the actual write.
The high level API could be something like:
wr_memcpy(void *src, void *dst, uint_t size)Quoted 4 lines
It's also
entirely possible that some architectures won't even use anmm_structbehind the scenes -- x86, for example, could have avoided it if there were a kernel equivalent of PKRU. Sadly, there isn't.The
mm_struct- or whatever is the means to do the write on that architecture - can be kept hidden from the API.
Quoted 7 lines
But the reason why I was proposing to have one
mm_structper writer is that, iiuc, the secondary mapping is created in the secondarymm_structfor each writer using it.So the updating of IMA measurements would have, theoretically, also write access to the SELinux AVC. Which I was trying to avoid. And similarly any other write rare updater. Is this correct?
If you call a wr_memcpy() function with the signature you suggested, then you can overwrite any memory of this type. Having a different mm_struct under the hood makes no difference. As far as I'm concerned, for *dynamically allocated* rare-writable memory, you might as well allocate all the paging structures at the same time, so the mm_struct will always contain the mappings. If there are serious bugs in wr_memcpy() that cause it to write to the wrong place, we have bigger problems.
I can imagine that we'd want a *typed* wr_memcpy()-like API some day, but that can wait for v2. And it still doesn't obviously need multiple mm_structs.
Quoted 14 lines
Quoted 6 lines
Quoted 5 lines
2) Iiuc, the purpose of the 2 pages being remapped is that the target of the patch might spill across the page boundary, however if I deal with the modification of generic data, I shouldn't (shouldn't I?) assume that the data will not span across multiple pages.
Quoted 7 lines
The reason for the particular architecture of
text_poke()is to avoid memory allocation to get it working. i think that prmem/rare_write should have each rare-writable kernel address map to a unique user address, possibly just by offsetting everything by a constant. Forrare_write, you don't actually need it to work as such until fairly late in boot, since therare_writabledata will just be writable early on.
Quoted 8 lines
Yes, that is true. I think it's safe to assume, from an attack pattern, that as long as user space is not started, the system can be considered ok. Even user-space code run from initrd should be ok, since it can be bundled (and signed) as a single binary with the kernel.
Modules loaded from a regular filesystem are a bit more risky, because an attack might inject a rogue key in the key-ring and use it to load malicious modules.
If a malicious module is loaded, the game is over.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
If the data spans across multiple pages, in unknown amount, I suppose that I should not keep interrupts disabled for an unknown time, as it would hurt preemption.
What I thought, in my initial patch-set, was to iterate over each page that must be written to, in a loop, re-enabling interrupts in-between iterations, to give pending interrupts a chance to be served.
Quoted 8 lines
Quoted 3 lines
This would mean that the data being written to would not be consistent, but it's a problem that would have to be addressed anyways, since it can be still read by other cores, while the write is ongoing.
This probably makes sense, except that enabling and disabling interrupts means you also need to restore the original
mm_struct(most likely), which is slow. I don't think there's a generic way to check whether in interrupt is pending without turning interrupts on.
Quoted 8 lines
The only "excuse" I have is that
write_rareis opt-in and is "rare". Maybe the enabling/disabling of interrupts - and the consequent switch ofmm_struct- could be somehow tied to the latency configuration?If preemption is disabled, the expectations on the system latency are anyway more relaxed.
But I'm not sure how it would work against I/O.
I think it's entirely reasonable for the API to internally break up very large memcpys.
Re: [PATCH 10/17] prmem: documentation
Quoted 1 line · Andy Lutomirski
On Tue, Nov 13, 2018 at 10:26 AM Igor Stoppa <[email protected]> wrote:
[...]
Quoted 3 lines
Quoted 3 lines
The high level API could be something like:
wr_memcpy(void *src, void *dst, uint_t size)
[...]
Quoted 8 lines
If you call a
wr_memcpy()function with the signature you suggested, then you can overwrite any memory of this type. Having a differentmm_structunder the hood makes no difference. As far as I'm concerned, for *dynamically allocated* rare-writable memory, you might as well allocate all the paging structures at the same time, so themm_structwill always contain the mappings. If there are serious bugs inwr_memcpy()that cause it to write to the wrong place, we have bigger problems.
Beside bugs, I'm also thinking about possible vulnerability. It might be overthinking, though.
I do not think it's possible to really protect against control flow attacks, unless there is some support from the HW and/or the compiler.
What is left, are data-based attacks. In this case, it would be an attacker using one existing wr_ invocation with doctored parameters.
However, there is always the objection that it would be possible to come up with a "writing kit" for plowing through the page tables and unprotect anything that might be of value.
Ideally, that should be the only type of data-based attack left.
In practice, it might just be an excess of paranoia from my side.
Quoted 3 lines
I can imagine that we'd want a *typed* wr_memcpy()-like API some day, but that can wait for v2. And it still doesn't obviously need multiple
mm_structs.
I left that out, for now, but yes, typing would play some role here.
[...]
Quoted 2 lines
I think it's entirely reasonable for the API to internally break up very large memcpys.
The criteria for deciding if/how to break it down is not clear to me, though. The single page was easy, but might be (probably is) too much.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Kees CookQuoted 8 lines · Kees Cook
Quoted 7 lines · Peter Zijlstra
I suppose the 'normal' attack goes like:
1) find buffer-overrun / bound check failure
2) use that to write to 'interesting' location
3) that write results arbitrary code execution
4) win
Quoted 7 lines
Quoted 2 lines
Of course, if the store of 2 is to the current cred structure, and simply sets the effective uid to 0, we can skip 3.
In most cases, yes, gaining root is game over. However, I don't want to discount other threat models: some systems have been designed not to trust root, so a cred attack doesn't always get an attacker full control (e.g. lockdown series, signed modules, encrypted VMs, etc).
Reading these points I see where I was not clear.
Maybe I can fix that. SELinux is a good example of safeguard against a takeover of root, so it is a primary target. Unfortunately it contains some state variables that can be used to disable it.
Other attack that comes to my mind, for executing code within the kernel, without sweating too much with ROP is the following:
1) find the rabbit hole to write kernel data, whatever it might be
2) spray the keyring with own key
3) use the module loading infrastructure to load own module, signed with the key at point 2)
Just to say that direct arbitrary code execution is becoming harder to perform, but there are ways around it which rely more on overwriting unprotected data.
They are a lower hanging fruit for an attacker.
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Peter ZijlstraAm 26.10.18 um 11:26 schrieb Peter Zijlstra:
Quoted 14 lines
Quoted 8 lines
Jon,
So the below document is a prime example for why I think RST sucks. As a text document readability is greatly diminished by all the markup nonsense.
This stuff should not become write-only content like html and other gunk. The actual text file is still the primary means of reading this.
I think Igor neglected to read
doc-guide/sphinx.rst:Specific guidelines for the kernel documentation
------------------------------------------------
Quoted 8 lines
Here are some specific guidelines for the kernel documentation:
* Please don't go overboard with reStructuredText markup. Keep it simple. For the most part the documentation should be plain text with just enough consistency in formatting that it can be converted to other formats.
I agree that it's overly marked up.
To add my two cent ..
Quoted 1 line
WTH is with all those ** ?
I guess Igor was looking for a definition list ...
Quoted 8 lines
Documentation/core-api/prmem.rst line 45
Quoted 8 lines
Documentation/core-api/prmem.rst new file +8
-0+Available protections for kernel data +------------------------------------- + +- **constant** + Labelled as **const**, the data is never supposed to be altered. + It is statically allocated - if it has any memory footprint at all. + The compiler can even optimize it away, where possible, by replacing + references to a **const** with its actual value.
+Available protections for kernel data
+-------------------------------------
+
+constant
+ Labelled as const, the data is never supposed to be altered. + It is statically allocated - if it has any memory footprint at all. + The compiler can even optimize it away, where possible, by replacing + references to a const with its actual value.
see "Lists and Quote-like blocks" [1]
[1] http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#lists-and-quote-like-blocks
-- Markus --
Re: [PATCH 10/17] prmem: documentation
Quoted 1 line · Markus Heiser
I guess Igor was looking for a definition list ...
It was meant to be bold.
Even after reading the guideline "keep it simple", what exactly is simple can be subjective.
If certain rst constructs are not acceptable and can be detected unequivocally, maybe it would help to teach them to checkpatch.pl
[...]
Quoted 3 lines
see "Lists and Quote-like blocks" [1]
[1] http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#lists-and-quote-like-blocks
thank you
--
igor
Jonathan Corbet
maintainerRe: [PATCH 10/17] prmem: documentation
In reply to Peter ZijlstraQuoted 5 lines · On Fri, 26 Oct 2018 11:26:09 +0200 Peter Zijlstra <[email protected]> wrote:
Jon,
So the below document is a prime example for why I think RST sucks. As a text document readability is greatly diminished by all the markup nonsense.
Please don't confused RST with the uses of it. The fact that one can do amusing things with the C preprocessor doesn't, on its own, make C suck...
Quoted 3 lines
This stuff should not become write-only content like html and other gunk. The actual text file is still the primary means of reading this.
I agree that the text file comes first, and I agree that the markup in this particular document is excessive.
Igor, just like #ifdef's make code hard to read, going overboard with RST markup makes text hard to read. It's a common trap to fall into, because it lets you make slick-looking web pages, but that is not our primary goal here. Many thanks for writing documentation for this feature, that already puts you way ahead of a discouraging number of contributors. But could I ask you, please, to make a pass over it and reduce the markup to a minimum? Using lists as suggested by Markus would help here.
Thanks,
jon
Re: [PATCH 10/17] prmem: documentation
Quoted 3 lines · Jonathan Corbet
But
could I ask you, please, to make a pass over it and reduce the markup to a minimum? Using lists as suggested by Markus would help here.
sure, it's even easier for me to maintain the doc :-)
as i just wrote in a related reply, it might be worth having checkpatch to detect formatting constructs that are not welcome
--
igor
Re: [PATCH 10/17] prmem: documentation
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
Quoted 2 lines
+- Kernel code is protected at system level and, unlike data, it doesn't + require special attention.What does this even mean?
I was trying to convey the notion that the pages containing kernel code do not require any special handling by the author of a generic kernel component, for example a kernel driver.
Pages containing either statically or dynamically allocated data, instead, are not automatically protected.
But yes, the sentence is far from being clear.
Quoted 8 lines
Documentation/core-api/prmem.rst line 30
Quoted 6 lines
Documentation/core-api/prmem.rst new file +5
-0+Protection mechanism +-------------------- + +- When available, the MMU can write protect memory pages that would be + otherwise writable.Again; what does this really want to say?
That it is possible to use the MMU also for write-protecting pages containing data which was not declared as constant.
Quoted 4 lines
Quoted 2 lines
+- The protection has page-level granularity.
I don't think Linux supports non-paging MMUs.
This probably came from a model I had in mind where a separate execution environment, such as an hypervisor, could trap and filter writes to a certain parts of a page, rejecting some and performing others, effectively emulating sub-page granularity.
Quoted 6 lines
Quoted 4 lines
+- An attempt to overwrite a protected page will trigger an exception. +- **Write protected data must go exclusively to write protected pages** +- **Writable data must go exclusively to writable pages**WTH is with all those ** ?
[...]
Quoted 2 lines
Can we ditch all the ** nonsense and put whitespace in there? More paragraphs and whitespace are more good.
Yes
Quoted 2 lines
Also, I really don't like how you differentiate between static and dynamic wr.
ok, but why? what would you suggest, instead?
[...]
Quoted 1 line
We already have RO, why do you need more RO?
I can explain, but I'm at loss of what is the best place: I was under the impression that this sort of document should focus mostly on the API and its use. I was even considering removing most of the explanation and instead put it in a separate document.
Quoted 14 lines
Documentation/core-api/prmem.rst line 111
Quoted 9 lines
Documentation/core-api/prmem.rst new file +8
-0+ + **Remarks:** + - The "AUTO" modes perform automatic protection of the content, whenever + the current vmap_area is used up and a new one is allocated. + - At that point, the vmap_area being phased out is protected. + - The size of the vmap_area depends on various parameters. + - It might not be possible to know for sure *when* certain data will + be protected.Surely that is a problem?
Quoted 1 line
+ - The functionality is provided as tradeoff between hardening and speed.
Quoted 8 lines
Which you fail to explain.
Quoted 1 line
+ - Its usefulness depends on the specific use case at hand
How about you write sensible text inside the option descriptions instead?
This is not a presentation; less bullets, more content.
I tried to say something, without saying too much, but it was clearly a bad choice.
Where should i put a thoroughly detailed explanation? Here or in a separate document?
Quoted 9 lines
Documentation/core-api/prmem.rst line 146
Quoted 5 lines
+- Not only the pmalloc memory must be protected, but also any reference to
Documentation/core-api/prmem.rst new file +3
-0+ it that might become the target for an attack. The attack would replace + a reference to the protected memory with a reference to some other, + unprotected, memory.I still don't really understand the whole write-rare thing; how does it really help? If we can write in kernel memory, we can write to page-tables too.
It has already been answered by Kees, but I'll provide also my answer:
the exploits used to write in kernel memory are specific to certain products and SW builds, so it's not possible to generalize too much, however there might be some limitation in the reach of a specific vulnerability. For example, if a memory location is referred to as offset from a base address, the maximum size of the offset limits the scope of the attack. Which might make it impossible to use that specific vulnerability for writing directly to the page table. But something else, say a statically allocated variable, might be within reach.
said this, there is also the almost orthogonal use case of providing increased robustness by trapping accidental modifications, caused by bugs.
Quoted 2 lines
And I don't think this document even begins to explain _why_ you're doing any of this. How does it help?
ok, point taken
Quoted 9 lines
Documentation/core-api/prmem.rst line 154
Quoted 7 lines
+- The users of rare write must take care of ensuring the atomicity of the
Documentation/core-api/prmem.rst new file +6
-0+ action, respect to the way they use the data being altered; for example, + take a lock before making a copy of the value to modify (if it's + relevant), then alter it, issue the call to rare write and finally + release the lock. Some special scenario might be exempt from the need + for locking, but in general rare-write must be treated as an operation + that can incur into races.What?!
Probably something along the lines of:
"users of write-rare are responsible of using mechanisms that allow reading/writing data in a consistent way"
and if it seems obvious, I can just drop it.
One of the problems I have faced is to decide what level of knowledge or understanding I should expect from the reader of such document: what I can take for granted and what I should explain.
--
igor
PATCH 11/17 prmem: llist: use designated initializer
In reply to Igor StoppaUsing a list_head in an unnamed union poses a problem with the current implementation of the initializer, since it doesn't specify the names of the fields it is initializing.
This patch makes it use designated initializers.
--- include/linux/list.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/list.h +4 -1
@@ -18,7 +18,10 @@* using the generic single-entry routines. */ -#define LIST_HEAD_INIT (name) { &(name), &(name) } +#define LIST_HEAD_INIT (name) { \ + .next = &(name), \ + .prev = &(name), \ +}#define LIST_HEAD (name) \ struct list_head name = LIST_HEAD_INIT(name)
Signature · 1 line
2.17.1
PATCH 12/17 prmem: linked list: set alignment
In reply to Igor StoppaAs preparation to using write rare on the nodes of various types of lists, specify that the fields in the basic data structures must be aligned to sizeof(void *)
It is meant to ensure that any static allocation will not cross a page boundary, to allow pointers to be updated in one step.
--- include/linux/types.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
include/linux/types.h +16 -4
@@ -183,17 +183,29 @@ typedef struct { } atomic64_t;#endif +#ifdef CONFIG_PRMEM struct list_head { -struct list_head *next, *prev; -}; +struct list_head *next__aligned(sizeof(void *)) ; +struct list_head *prev__aligned(sizeof(void *)) ; +}__aligned(sizeof(void *)) ; -struct hlist_head { -struct hlist_node *first; +struct hlist_node { +struct hlist_node *next__aligned(sizeof(void *)) ; +struct hlist_node **pprev__aligned(sizeof(void *)) ; +}__aligned(sizeof(void *)) ; +#else +struct list_head { +struct list_head *next, *prev; };struct hlist_node {struct hlist_node *next, **pprev; }; +#endif + +struct hlist_head { +struct hlist_node *first; +};struct ustat {__kernel_daddr_t f_tfree;
Signature · 1 line
2.17.1
Re: [PATCH 12/17] prmem: linked list: set alignment
Quoted 32 lines · Igor Stoppa
As preparation to using write rare on the nodes of various types of lists, specify that the fields in the basic data structures must be aligned to
sizeof(void *)It is meant to ensure that any static allocation will not cross a page boundary, to allow pointers to be updated in one step.
Signed-off-by Igor Stoppa <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Andrew Morton <[email protected]>CC Masahiro Yamada <[email protected]>CC Alexey Dobriyan <[email protected]>CC Pekka Enberg <[email protected]>CC "Paul E. McKenney" <[email protected]>CC Lihao Liang <[email protected]>--- include/linux/types.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)include/linux/types.h +1
-2@@ -183,17 +183,29 @@ typedef struct { } atomic64_t;#endif +#ifdef CONFIG_PRMEM struct list_head { -struct list_head *next, *prev; -};
include/linux/types.h typedef struct { · line 195
Quoted 10 lines
include/linux/types.h +7
-2+struct list_head *next__aligned(sizeof(void *)) ; +struct list_head *prev__aligned(sizeof(void *)) ; +}__aligned(sizeof(void *)) ; -struct hlist_head { -struct hlist_node *first; +struct hlist_node { +struct hlist_node *next__aligned(sizeof(void *)) ; +struct hlist_node **pprev__aligned(sizeof(void *)) ; +}__aligned(sizeof(void *)) ;
Argh.. are we really supporting platforms that do not naturally align this? If so, which and can't we fix those?
Also, if you force alignment on a member, the structure as a whole inherits the largest member alignment.
Also, you made something that was simple an unreadable mess without proper justification (ie. you fail to show need).
PATCH 13/17 prmem: linked list: disable layout randomization
In reply to Igor StoppaSome of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers, this might prevent correct type punning between these structures and their write rare counterpart.
It shouldn't be anyway a big loss, in terms of security: with only two fields, there is a 50% chance of guessing correctly the layout. The randomization is disabled only when write rare is enabled.
--- include/linux/types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/types.h +2 -2
@@ -187,12 +187,12 @@ typedef struct { struct list_head { struct list_head *next __aligned(sizeof(void *)); struct list_head *prev __aligned(sizeof(void *)); -} __aligned(sizeof(void *)); +} __no_randomize_layout __aligned(sizeof(void *)); struct hlist_node { struct hlist_node *next __aligned(sizeof(void *)); struct hlist_node **pprev __aligned(sizeof(void *)); -} __aligned(sizeof(void *)); +} __no_randomize_layout __aligned(sizeof(void *)); #else struct list_head { struct list_head *next, *prev;
Signature · 1 line
2.17.1
Re: [PATCH 13/17] prmem: linked list: disable layout randomization
Quoted 3 lines · Igor Stoppa
Some of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers,
Isn't this true for function pointers?
Re: [PATCH 13/17] prmem: linked list: disable layout randomization
Quoted 6 lines · Alexey Dobriyan
Quoted 3 lines · Igor Stoppa
Some of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers,
Isn't this true for function pointers?
Yes, you are right.
Thanks for pointing this out.
I can drop this patch.
--
igor
Re: [PATCH 13/17] prmem: linked list: disable layout randomization
In reply to Igor StoppaQuoted 5 lines · Igor Stoppa
Some of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers, this might prevent correct type punning between these structures and their write rare counterpart.
'might' doesn't really work for me. Either it does or it does not.
Re: [PATCH 13/17] prmem: linked list: disable layout randomization
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Igor Stoppa
Some of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers, this might prevent correct type punning between these structures and their write rare counterpart.
'might' doesn't really work for me. Either it does or it does not.
He means "Depending on the random number generator, the two pointers might be AB or BA. If they're of opposite polarity (50% of the time), it _will_ break, and 50% of the time it _won't_ break."
Re: [PATCH 13/17] prmem: linked list: disable layout randomization
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Igor Stoppa
Some of the data structures used in list management are composed by two pointers. Since the kernel is now configured by default to randomize the layout of data structures soleley composed by pointers, this might prevent correct type punning between these structures and their write rare counterpart.
Quoted 5 lines
Quoted 1 line
'might' doesn't really work for me. Either it does or it does not.
He means "Depending on the random number generator, the two pointers might be AB or BA. If they're of opposite polarity (50% of the time), it _will_ break, and 50% of the time it _won't_ break."
So don't do that then. If he were to include struct list_head inside his prlist_head, then there is only the one randomization and things will just work.
Also, I really don't see why he needs that second type and all that type punning crap in the first place.
PATCH 14/17 prmem: llist, hlist, both plain and rcu
In reply to Igor StoppaIn some cases, all the data needing protection can be allocated from a pool in one go, as directly writable, then initialized and protected. The sequence is relatively short and it's acceptable to leave the entire data set unprotected.
In other cases, this is not possible, because the data will trickle over a relatively long period of time, in a non predictable way, possibly for the entire duration of the operations.
For these cases, the safe approach is to have the memory already write protected, when allocated. However, this will require replacing any direct assignment with calls to functions that can perform write rare.
Since lists are one of the most commonly used data structures in kernel, they are a the first candidate for receiving write rare extensions.
This patch implements basic functionality for altering said lists.
--- MAINTAINERS | 1 + include/linux/prlist.h | 934 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 935 insertions(+) create mode 100644 include/linux/prlist.h
MAINTAINERS +1 -0
@@ -9464,6 +9464,7 @@ F: mm/prmem.c F: mm/test_write_rare.c F: mm/test_pmalloc.c F: Documentation/core-api/prmem.rst +F: include/linux/prlist.h MEMORY MANAGEMENT L: [email protected]
include/linux/prlist.h new file +934 -0
934 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 9 lines · ----- On Oct 23, 2018, at 10:35 PM, Igor Stoppa [email protected] wrote:
In some cases, all the data needing protection can be allocated from a pool in one go, as directly writable, then initialized and protected. The sequence is relatively short and it's acceptable to leave the entire data set unprotected.
In other cases, this is not possible, because the data will trickle over a relatively long period of time, in a non predictable way, possibly for the entire duration of the operations.
Quoted 8 lines
For these cases, the safe approach is to have the memory already write protected, when allocated. However, this will require replacing any direct assignment with calls to functions that can perform write rare.
Since lists are one of the most commonly used data structures in kernel, they are a the first candidate for receiving write rare extensions.
This patch implements basic functionality for altering said lists.
I could not find a description of the overall context of this patch (e.g. a patch 00/17 ?) that would explain the attack vectors this aims to protect against.
This might help figuring out whether this added complexity in the core kernel is worth it.
Also, is it the right approach to duplicate existing APIs, or should we rather hook into page fault handlers and let the kernel do those "shadow" mappings under the hood ?
Adding a new GFP flags for dynamic allocation, and a macro mapping to a section attribute might suffice for allocation or definition of such mostly-read-only/seldom-updated data.
Thanks,
Mathieu
Quoted 973 lines
Signed-off-by Igor Stoppa <[email protected]>CC Thomas Gleixner <[email protected]>CC Kate Stewart <[email protected]>CC "David S. Miller" <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Philippe Ombredanne <[email protected]>CC "Paul E. McKenney" <[email protected]>CC Josh Triplett <[email protected]>CC Steven Rostedt <[email protected]>CC Mathieu Desnoyers <[email protected]>CC Lai Jiangshan <[email protected]>---
MAINTAINERS| 1 +include/linux/prlist.h| 934 +++++++++++++++++++++++++++++++++++++++++2 files changed, 935 insertions(+)create mode 100644
include/linux/prlist.hMAINTAINERS +0
-0@@ -9464,6 +9464,7 @@ F: mm/prmem.cF:
mm/test_write_rare.c
F:mm/test_pmalloc.c
F:Documentation/core-api/prmem.rst
+F:include/linux/prlist.hMEMORY MANAGEMENT
L: [email protected]include/linux/prlist.h new file +934
-1@@ -0,0 +1,934 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* prlist.h: Header for Protected Lists +* +* (C) Copyright 2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +* +* Code from <linux/list.h> and <linux/rculist.h>, adapted to perform +* writes on write-rare data. +* These functions and macros rely on data structures that allow the reuse +* of what is already provided for reading the content of their non-write +* rare variant. +*/ + +#ifndef _LINUX_PRLIST_H +#define _LINUX_PRLIST_H + +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/prmemextra.h> + +/* --------------- Circular Protected Doubly Linked List --------------- */ +union prlist_head { +struct list_head list__aligned(sizeof(void *)) ; +struct { +union prlist_head *next__aligned(sizeof(void *)) ; +union prlist_head *prev__aligned(sizeof(void *)) ; + } __no_randomize_layout; +}__aligned(sizeof(void *)) ; + +static __always_inline +union prlist_head *to_prlist_head(struct list_head *list) +{ + return container_of(list, union prlist_head, list); +} + +#define PRLIST_HEAD_INIT (name) { \ + .list = LIST_HEAD_INIT(name), \ +} + +#define PRLIST_HEAD (name) \ + union prlist_head name __wr_after_init = PRLIST_HEAD_INIT(name.list) + +static __always_inline +struct pmalloc_pool *prlist_create_custom_pool(size_t refill, +unsigned short align_order) +{ +return pmalloc_create_custom_pool (refill, align_order, +PMALLOC_MODE_START_WR ); +} + +static __always_inline struct pmalloc_pool *prlist_create_pool(void) +{ +return prlist_create_custom_pool (PMALLOC_REFILL_DEFAULT , +PMALLOC_ALIGN_ORDER_DEFAULT ); +} + +static __always_inline +void prlist_set_prev(union prlist_head *head, +const union prlist_head *prev) +{ +wr_ptr (&head->prev, prev); +} + +static __always_inline +void prlist_set_next(union prlist_head *head, +const union prlist_head *next) +{ +wr_ptr (&head->next, next); +} + +static __always_inline void INIT_PRLIST_HEAD(union prlist_head *head) +{ +prlist_set_prev (head, head); +prlist_set_next (head, head); +} + +/* +* Insert a new entry between two known consecutive entries. +* +* This is only for internal list manipulation where we know +* the prev/next entries already! +*/ +static __always_inline +void __prlist_add(union prlist_head *new, union prlist_head *prev, +union prlist_head *next) +{ +if (!__list_add_valid (&new->list, &prev->list, &next->list)) +return ; + +prlist_set_prev (next, new); +prlist_set_next (new, next); +prlist_set_prev (new, prev); +prlist_set_next (prev, new); +} + +/** +* prlist_add - add a new entry +* @new: new entry to be added +* @head: prlist head to add it after +* +* Insert a new entry after the specified head. +* This is good for implementing stacks. +*/ +static __always_inline +void prlist_add(union prlist_head *new, union prlist_head *head) +{ +__prlist_add (new, head, head->next); +} + +/** +* prlist_add_tail - add a new entry +* @new: new entry to be added +* @head: list head to add it before +* +* Insert a new entry before the specified head. +* This is useful for implementing queues. +*/ +static __always_inline +void prlist_add_tail(union prlist_head *new, union prlist_head *head) +{ +__prlist_add (new, head->prev, head); +} + +/* +* Delete a prlist entry by making the prev/next entries +* point to each other. +* +* This is only for internal list manipulation where we know +* the prev/next entries already! +*/ +static __always_inline +void __prlist_del(union prlist_head *prev, union prlist_head *next) +{ +prlist_set_prev (next, prev); +prlist_set_next (prev, next); +} + +/** +* prlist_del - deletes entry from list. +* @entry: the element to delete from the list. +* Note: list_empty() on entry does not return true after this, the entry is +* in an undefined state. +*/ +static inline void __prlist_del_entry (union prlist_head *entry) +{ +if (!__list_del_entry_valid (&entry->list)) +return ; +__prlist_del (entry->prev, entry->next); +} + +static __always_inline void prlist_del(union prlist_head *entry) +{ +__prlist_del_entry (entry); +prlist_set_next (entry,LIST_POISON1 ); +prlist_set_prev (entry,LIST_POISON2 ); +} + +/** +* prlist_replace - replace old entry by new one +* @old : the element to be replaced +* @new : the new element to insert +* +* If @old was empty, it will be overwritten. +*/ +static __always_inline +void prlist_replace(union prlist_head *old, union prlist_head *new) +{ +prlist_set_next (new, old->next); +prlist_set_prev (new->next, new); +prlist_set_prev (new, old->prev); +prlist_set_next (new->prev, new); +} + +static __always_inline +void prlist_replace_init(union prlist_head *old, union prlist_head *new) +{ +prlist_replace (old, new); +INIT_PRLIST_HEAD (old); +} + +/** +* prlist_del_init - deletes entry from list and reinitialize it. +* @entry: the element to delete from the list. +*/ +static __always_inline void prlist_del_init(union prlist_head *entry) +{ +__prlist_del_entry (entry); +INIT_PRLIST_HEAD (entry); +} + +/** +* prlist_move - delete from one list and add as another's head +* @list: the entry to move +* @head: the head that will precede our entry +*/ +static __always_inline +void prlist_move(union prlist_head *list, union prlist_head *head) +{ +__prlist_del_entry (list); +prlist_add (list, head); +} + +/** +* prlist_move_tail - delete from one list and add as another's tail +* @list: the entry to move +* @head: the head that will follow our entry +*/ +static __always_inline +void prlist_move_tail(union prlist_head *list, union prlist_head *head) +{ +__prlist_del_entry (list); +prlist_add_tail (list, head); +} + +/** +* prlist_rotate_left - rotate the list to the left +* @head: the head of the list +*/ +static __always_inline void prlist_rotate_left(union prlist_head *head) +{ +union prlist_head *first; + +if (!list_empty (&head->list)) { + first = head->next; +prlist_move_tail (first, head); + } +} + +static __always_inline +void __prlist_cut_position(union prlist_head *list, union prlist_head *head, +union prlist_head *entry) +{ +union prlist_head *new_first = entry->next; + +prlist_set_next (list, head->next); +prlist_set_prev (list->next, list); +prlist_set_prev (list, entry); +prlist_set_next (entry, list); +prlist_set_next (head, new_first); +prlist_set_prev (new_first, head); +} + +/** +* prlist_cut_position - cut a list into two +* @list: a new list to add all removed entries +* @head: a list with entries +* @entry: an entry within head, could be the head itself +* and if so we won't cut the list +* +* This helper moves the initial part of @head, up to and +* including @entry, from @head to @list. You should +* pass on @entry an element you know is on @head. @list +* should be an empty list or a list you do not care about +* losing its data. +* +*/ +static __always_inline +void prlist_cut_position(union prlist_head *list, union prlist_head *head, +union prlist_head *entry) +{ +if (list_empty (&head->list)) +return ; +if (list_is_singular (&head->list) && + (head->next != entry && head != entry)) +return ; +if (entry == head) +INIT_PRLIST_HEAD (list); +else +__prlist_cut_position (list, head, entry); +} + +/** +* prlist_cut_before - cut a list into two, before given entry +* @list: a new list to add all removed entries +* @head: a list with entries +* @entry: an entry within head, could be the head itself +* +* This helper moves the initial part of @head, up to but +* excluding @entry, from @head to @list. You should pass +* in @entry an element you know is on @head. @list should +* be an empty list or a list you do not care about losing +* its data. +* If @entry == @head, all entries on @head are moved to +* @list. +*/ +static __always_inline +void prlist_cut_before(union prlist_head *list, union prlist_head *head, +union prlist_head *entry) +{ +if (head->next == entry) { +INIT_PRLIST_HEAD (list); +return ; + } +prlist_set_next (list, head->next); +prlist_set_prev (list->next, list); +prlist_set_prev (list, entry->prev); +prlist_set_next (list->prev, list); +prlist_set_next (head, entry); +prlist_set_prev (entry, head); +} + +static __always_inline +void __prlist_splice(const union prlist_head *list, union prlist_head *prev, +union prlist_head *next) +{ +union prlist_head *first = list->next; +union prlist_head *last = list->prev; + +prlist_set_prev (first, prev); +prlist_set_next (prev, first); +prlist_set_next (last, next); +prlist_set_prev (next, last); +} + +/** +* prlist_splice - join two lists, this is designed for stacks +* @list: the new list to add. +* @head: the place to add it in the first list. +*/ +static __always_inline +void prlist_splice(const union prlist_head *list, union prlist_head *head) +{ +if (!list_empty (&list->list)) +__prlist_splice (list, head, head->next); +} + +/** +* prlist_splice_tail - join two lists, each list being a queue +* @list: the new list to add. +* @head: the place to add it in the first list. +*/ +static __always_inline +void prlist_splice_tail(union prlist_head *list, union prlist_head *head) +{ +if (!list_empty (&list->list)) +__prlist_splice (list, head->prev, head); +} + +/** +* prlist_splice_init - join two lists and reinitialise the emptied list. +* @list: the new list to add. +* @head: the place to add it in the first list. +* +* The list at @list is reinitialised +*/ +static __always_inline +void prlist_splice_init(union prlist_head *list, union prlist_head *head) +{ +if (!list_empty (&list->list)) { +__prlist_splice (list, head, head->next); +INIT_PRLIST_HEAD (list); + } +} + +/** +* prlist_splice_tail_init - join 2 lists and reinitialise the emptied list +* @list: the new list to add. +* @head: the place to add it in the first list. +* +* Each of the lists is a queue. +* The list at @list is reinitialised +*/ +static __always_inline +void prlist_splice_tail_init(union prlist_head *list, +union prlist_head *head) +{ +if (!list_empty (&list->list)) { +__prlist_splice (list, head->prev, head); +INIT_PRLIST_HEAD (list); + } +} + +/* ---- Protected Doubly Linked List with single pointer list head ---- */ +union prhlist_head { +struct hlist_head head__aligned(sizeof(void *)) ; +union prhlist_node *first__aligned(sizeof(void *)) ; +}__aligned(sizeof(void *)) ; + +union prhlist_node { +struct hlist_node node__aligned(sizeof(void *)) + ; +struct { +union prhlist_node *next__aligned(sizeof(void *)) ; +union prhlist_node **pprev__aligned(sizeof(void *)) ; + } __no_randomize_layout; +}__aligned(sizeof(void *)) ; + +#define PRHLIST_HEAD_INIT { \ + .head = HLIST_HEAD_INIT, \ +} + +#define PRHLIST_HEAD (name) \ + union prhlist_head name __wr_after_init = PRHLIST_HEAD_INIT + + +#define is_static (object) \ + unlikely(wr_check_boundaries(object, sizeof(*object))) + +static __always_inline +struct pmalloc_pool *prhlist_create_custom_pool(size_t refill, +unsigned short align_order) +{ +return pmalloc_create_custom_pool (refill, align_order, +PMALLOC_MODE_AUTO_WR ); +} + +static __always_inline +struct pmalloc_pool *prhlist_create_pool(void) +{ +return prhlist_create_custom_pool (PMALLOC_REFILL_DEFAULT , +PMALLOC_ALIGN_ORDER_DEFAULT ); +} + +static __always_inline +void prhlist_set_first(union prhlist_head *head, union prhlist_node *first) +{ +wr_ptr (&head->first, first); +} + +static __always_inline +void prhlist_set_next(union prhlist_node *node, union prhlist_node *next) +{ +wr_ptr (&node->next, next); +} + +static __always_inline +void prhlist_set_pprev(union prhlist_node *node, union prhlist_node **pprev) +{ +wr_ptr (&node->pprev, pprev); +} + +static __always_inline +void prhlist_set_prev(union prhlist_node *node, union prhlist_node *prev) +{ +wr_ptr (node->pprev, prev); +} + +static __always_inline void INIT_PRHLIST_HEAD(union prhlist_head *head) +{ +prhlist_set_first (head,NULL ); +} + +static __always_inline void INIT_PRHLIST_NODE(union prhlist_node *node) +{ +prhlist_set_next (node,NULL ); +prhlist_set_pprev (node,NULL ); +} + +static __always_inline void __prhlist_del(union prhlist_node *n) +{ +union prhlist_node *next = n->next; +union prhlist_node **pprev = n->pprev; + +wr_ptr (pprev, next); +if (next) +prhlist_set_pprev (next, pprev); +} + +static __always_inline void prhlist_del(union prhlist_node *n) +{ +__prhlist_del (n); +prhlist_set_next (n,LIST_POISON1 ); +prhlist_set_pprev (n,LIST_POISON2 ); +} + +static __always_inline void prhlist_del_init(union prhlist_node *n) +{ +if (!hlist_unhashed (&n->node)) { +__prhlist_del (n); +INIT_PRHLIST_NODE (n); + } +} + +static __always_inline +void prhlist_add_head(union prhlist_node *n, union prhlist_head *h) +{ +union prhlist_node *first = h->first; + +prhlist_set_next (n, first); +if (first) +prhlist_set_pprev (first, &n->next); +prhlist_set_first (h, n); +prhlist_set_pprev (n, &h->first); +} + +/* next must be != NULL */ +static __always_inline +void prhlist_add_before(union prhlist_node *n, union prhlist_node *next) +{ +prhlist_set_pprev (n, next->pprev); +prhlist_set_next (n, next); +prhlist_set_pprev (next, &n->next); +prhlist_set_prev (n, n); +} + +static __always_inline +void prhlist_add_behind(union prhlist_node *n, union prhlist_node *prev) +{ +prhlist_set_next (n, prev->next); +prhlist_set_next (prev, n); +prhlist_set_pprev (n, &prev->next); +if (n->next) +prhlist_set_pprev (n->next, &n->next); +} + +/* after that we'll appear to be on some hlist and hlist_del will work */ +static __always_inline void prhlist_add_fake(union prhlist_node *n) +{ +prhlist_set_pprev (n, &n->next); +} + +/* +* Move a list from one list head to another. Fixup the pprev +* reference of the first entry if it exists. +*/ +static __always_inline +void prhlist_move_list(union prhlist_head *old, union prhlist_head *new) +{ +prhlist_set_first (new, old->first); +if (new->first) +prhlist_set_pprev (new->first, &new->first); +prhlist_set_first (old,NULL ); +} + +/* ------------------------ RCU list and hlist ------------------------ */ + +/* +* INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers +* @head: list to be initialized +* +* It is exactly equivalent to INIT_LIST_HEAD() +*/ +static __always_inline void INIT_PRLIST_HEAD_RCU(union prlist_head *head) +{ +INIT_PRLIST_HEAD (head); +} + +/* +* Insert a new entry between two known consecutive entries. +* +* This is only for internal list manipulation where we know +* the prev/next entries already! +*/ +static __always_inline +void __prlist_add_rcu(union prlist_head *new, union prlist_head *prev, +union prlist_head *next) +{ +if (!__list_add_valid (&new->list, &prev->list, &next->list)) +return ; +prlist_set_next (new, next); +prlist_set_prev (new, prev); +wr_rcu_assign_pointer (list_next_rcu (&prev->list), new); +prlist_set_prev (next, new); +} + +/** +* prlist_add_rcu - add a new entry to rcu-protected prlist +* @new: new entry to be added +* @head: prlist head to add it after +* +* Insert a new entry after the specified head. +* This is good for implementing stacks. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another prlist-mutation primitive, such as prlist_add_rcu() +* or prlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* list_for_each_entry_rcu(). +*/ +static __always_inline +void prlist_add_rcu(union prlist_head *new, union prlist_head *head) +{ +__prlist_add_rcu (new, head, head->next); +} + +/** +* prlist_add_tail_rcu - add a new entry to rcu-protected prlist +* @new: new entry to be added +* @head: prlist head to add it before +* +* Insert a new entry before the specified head. +* This is useful for implementing queues. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another prlist-mutation primitive, such as prlist_add_tail_rcu() +* or prlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* list_for_each_entry_rcu(). +*/ +static __always_inline +void prlist_add_tail_rcu(union prlist_head *new, union prlist_head *head) +{ +__prlist_add_rcu (new, head->prev, head); +} + +/** +* prlist_del_rcu - deletes entry from prlist without re-initialization +* @entry: the element to delete from the prlist. +* +* Note: list_empty() on entry does not return true after this, +* the entry is in an undefined state. It is useful for RCU based +* lockfree traversal. +* +* In particular, it means that we can not poison the forward +* pointers that may still be used for walking the list. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as prlist_del_rcu() +* or prlist_add_rcu(), running on this same prlist. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* list_for_each_entry_rcu(). +* +* Note that the caller is not permitted to immediately free +* the newly deleted entry. Instead, either synchronize_rcu() +* or call_rcu() must be used to defer freeing until an RCU +* grace period has elapsed. +*/ +static __always_inline void prlist_del_rcu(union prlist_head *entry) +{ +__prlist_del_entry (entry); +prlist_set_prev (entry,LIST_POISON2 ); +} + +/** +* prhlist_del_init_rcu - deletes entry from hash list with re-initialization +* @n: the element to delete from the hash list. +* +* Note: list_unhashed() on the node return true after this. It is +* useful for RCU based read lockfree traversal if the writer side +* must know if the list entry is still hashed or already unhashed. +* +* In particular, it means that we can not poison the forward pointers +* that may still be used for walking the hash list and we can only +* zero the pprev pointer so list_unhashed() will return true after +* this. +* +* The caller must take whatever precautions are necessary (such as +* holding appropriate locks) to avoid racing with another +* list-mutation primitive, such as hlist_add_head_rcu() or +* hlist_del_rcu(), running on this same list. However, it is +* perfectly legal to run concurrently with the _rcu list-traversal +* primitives, such as hlist_for_each_entry_rcu(). +*/ +static __always_inline void prhlist_del_init_rcu(union prhlist_node *n) +{ +if (!hlist_unhashed (&n->node)) { +__prhlist_del (n); +prhlist_set_pprev (n,NULL ); + } +} + +/** +* prlist_replace_rcu - replace old entry by new one +* @old : the element to be replaced +* @new : the new element to insert +* +* The @old entry will be replaced with the @new entry atomically. +* Note: @old should not be empty. +*/ +static __always_inline +void prlist_replace_rcu(union prlist_head *old, union prlist_head *new) +{ +prlist_set_next (new, old->next); +prlist_set_prev (new, old->prev); +wr_rcu_assign_pointer (list_next_rcu (&new->prev->list), new); +prlist_set_prev (new->next, new); +prlist_set_prev (old,LIST_POISON2 ); +} + +/** +* __prlist_splice_init_rcu - join an RCU-protected list into an existing list. +* @list: the RCU-protected list to splice +* @prev: points to the last element of the existing list +* @next: points to the first element of the existing list +* @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... +* +* The list pointed to by @prev and @next can be RCU-read traversed +* concurrently with this function. +* +* Note that this function blocks. +* +* Important note: the caller must take whatever action is necessary to prevent +* any other updates to the existing list. In principle, it is possible to +* modify the list as soon as sync() begins execution. If this sort of thing +* becomes necessary, an alternative version based on call_rcu() could be +* created. But only if -really- needed -- there is no shortage of RCU API +* members. +*/ +static __always_inline +void __prlist_splice_init_rcu(union prlist_head *list, +union prlist_head *prev, +union prlist_head *next,void (*sync)(void )) +{ +union prlist_head *first = list->next; +union prlist_head *last = list->prev; + +/* +* "first" and "last" tracking list, so initialize it. RCU readers +* have access to this list, so we must use INIT_LIST_HEAD_RCU() +* instead of INIT_LIST_HEAD(). +*/ + +INIT_PRLIST_HEAD_RCU (list); + +/* +* At this point, the list body still points to the source list. +* Wait for any readers to finish using the list before splicing +* the list body into the new list. Any new readers will see +* an empty list. +*/ + +sync (); + +/* +* Readers are finished with the source list, so perform splice. +* The order is important if the new list is global and accessible +* to concurrent RCU readers. Note that RCU readers are not +* permitted to traverse the prev pointers without excluding +* this function. +*/ + +prlist_set_next (last, next); +wr_rcu_assign_pointer (list_next_rcu (&prev->list), first); +prlist_set_prev (first, prev); +prlist_set_prev (next, last); +} + +/** +* prlist_splice_init_rcu - splice an RCU-protected list into an existing +* list, designed for stacks. +* @list: the RCU-protected list to splice +* @head: the place in the existing list to splice the first list into +* @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... +*/ +static __always_inline +void prlist_splice_init_rcu(union prlist_head *list, +union prlist_head *head, +void (*sync)(void )) +{ +if (!list_empty (&list->list)) +__prlist_splice_init_rcu (list, head, head->next, sync); +} + +/** +* prlist_splice_tail_init_rcu - splice an RCU-protected list into an +* existing list, designed for queues. +* @list: the RCU-protected list to splice +* @head: the place in the existing list to splice the first list into +* @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... +*/ +static __always_inline +void prlist_splice_tail_init_rcu(union prlist_head *list, +union prlist_head *head, +void (*sync)(void )) +{ +if (!list_empty (&list->list)) +__prlist_splice_init_rcu (list, head->prev, head, sync); +} + +/** +* prhlist_del_rcu - deletes entry from hash list without re-initialization +* @n: the element to delete from the hash list. +* +* Note: list_unhashed() on entry does not return true after this, +* the entry is in an undefined state. It is useful for RCU based +* lockfree traversal. +* +* In particular, it means that we can not poison the forward +* pointers that may still be used for walking the hash list. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as hlist_add_head_rcu() +* or hlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* hlist_for_each_entry(). +*/ +static __always_inline void prhlist_del_rcu(union prhlist_node *n) +{ +__prhlist_del (n); +prhlist_set_pprev (n,LIST_POISON2 ); +} + +/** +* prhlist_replace_rcu - replace old entry by new one +* @old : the element to be replaced +* @new : the new element to insert +* +* The @old entry will be replaced with the @new entry atomically. +*/ +static __always_inline +void prhlist_replace_rcu(union prhlist_node *old, union prhlist_node *new) +{ +union prhlist_node *next = old->next; + +prhlist_set_next (new, next); +prhlist_set_pprev (new, old->pprev); +wr_rcu_assign_pointer (*(union prhlist_node __rcu **)new->pprev, new); +if (next) +prhlist_set_pprev (new->next, &new->next); +prhlist_set_pprev (old,LIST_POISON2 ); +} + +/** +* prhlist_add_head_rcu +* @n: the element to add to the hash list. +* @h: the list to add to. +* +* Description: +* Adds the specified element to the specified hlist, +* while permitting racing traversals. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as hlist_add_head_rcu() +* or hlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* hlist_for_each_entry_rcu(), used to prevent memory-consistency +* problems on Alpha CPUs. Regardless of the type of CPU, the +* list-traversal primitive must be guarded by rcu_read_lock(). +*/ +static __always_inline +void prhlist_add_head_rcu(union prhlist_node *n, union prhlist_head *h) +{ +union prhlist_node *first = h->first; + +prhlist_set_next (n, first); +prhlist_set_pprev (n, &h->first); +wr_rcu_assign_pointer (hlist_first_rcu (&h->head), n); +if (first) +prhlist_set_pprev (first, &n->next); +} + +/** +* prhlist_add_tail_rcu +* @n: the element to add to the hash list. +* @h: the list to add to. +* +* Description: +* Adds the specified element to the specified hlist, +* while permitting racing traversals. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as prhlist_add_head_rcu() +* or prhlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* hlist_for_each_entry_rcu(), used to prevent memory-consistency +* problems on Alpha CPUs. Regardless of the type of CPU, the +* list-traversal primitive must be guarded by rcu_read_lock(). +*/ +static __always_inline +void prhlist_add_tail_rcu(union prhlist_node *n, union prhlist_head *h) +{ +union prhlist_node *i, *last =NULL ; + +/* Note: write side code, so rcu accessors are not needed. */ +for (i = h->first; i; i = i->next) + last = i; + +if (last) { +prhlist_set_next (n, last->next); +prhlist_set_pprev (n, &last->next); +wr_rcu_assign_pointer (hlist_next_rcu (&last->node), n); + }else { +prhlist_add_head_rcu (n, h); + } +} + +/** +* prhlist_add_before_rcu +* @n: the new element to add to the hash list. +* @next: the existing element to add the new element before. +* +* Description: +* Adds the specified element to the specified hlist +* before the specified node while permitting racing traversals. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as prhlist_add_head_rcu() +* or prhlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* hlist_for_each_entry_rcu(), used to prevent memory-consistency +* problems on Alpha CPUs. +*/ +static __always_inline +void prhlist_add_before_rcu(union prhlist_node *n, union prhlist_node *next) +{ +prhlist_set_pprev (n, next->pprev); +prhlist_set_next (n, next); +wr_rcu_assign_pointer (hlist_pprev_rcu (&n->node), n); +prhlist_set_pprev (next, &n->next); +} + +/** +* prhlist_add_behind_rcu +* @n: the new element to add to the hash list. +* @prev: the existing element to add the new element after. +* +* Description: +* Adds the specified element to the specified hlist +* after the specified node while permitting racing traversals. +* +* The caller must take whatever precautions are necessary +* (such as holding appropriate locks) to avoid racing +* with another list-mutation primitive, such as prhlist_add_head_rcu() +* or prhlist_del_rcu(), running on this same list. +* However, it is perfectly legal to run concurrently with +* the _rcu list-traversal primitives, such as +* hlist_for_each_entry_rcu(), used to prevent memory-consistency +* problems on Alpha CPUs. +*/ +static __always_inline +void prhlist_add_behind_rcu(union prhlist_node *n, union prhlist_node *prev) +{ +prhlist_set_next (n, prev->next); +prhlist_set_pprev (n, &prev->next); +wr_rcu_assign_pointer (hlist_next_rcu (&prev->node), n); +if (n->next) +prhlist_set_pprev (n->next, &n->next); +} +#endif --2.17.1
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 3 lines · Mathieu Desnoyers
I could not find a description of the overall context of this patch (e.g. a patch 00/17 ?) that would explain the attack vectors this aims to protect against.
Apologies, I have to admit I was a bit baffled about what to do: the patchset spans across several subsystems and I was reluctant at spamming all the mailing lists.
I was hoping that by CC-ing kernel.org, the explicit recipients would get both the mail directly intended for them (as subsystem maintainers/supporters) and the rest.
The next time I'll err in the opposite direction.
In the meanwhile, please find the whole set here:
https://www.openwall.com/lists/kernel-hardening/2018/10/23/3
Quoted 2 lines
This might help figuring out whether this added complexity in the core kernel is worth it.
I hope it will.
Quoted 3 lines
Also, is it the right approach to duplicate existing APIs, or should we rather hook into page fault handlers and let the kernel do those "shadow" mappings under the hood ?
This question is probably a good candidate for the small Q&A section I have in the 00/17.
Quoted 3 lines
Adding a new GFP flags for dynamic allocation, and a macro mapping to a section attribute might suffice for allocation or definition of such mostly-read-only/seldom-updated data.
I think what you are proposing makes sense from a pure hardening standpoint. From a more defensive one, I'd rather minimise the chances of giving a free pass to an attacker.
Maybe there is a better implementation of this, than what I have in mind. But, based on my current understanding of what you are describing, there would be few issues:
1) where would the pool go? The pool is a way to manage multiple vmas and express common property they share. Even before a vma is associated to the pool.
2) there would be more code that can seamlessly deal with both protected and regular data. Based on what? Some parameter, I suppose.
That parameter would be the new target.
If the code is "duplicated", as you say, the actual differences are baked in at compile time. The "duplication" would also allow to have always inlined functions for write-rare and leave more freedom to the compiler for their non-protected version.
Besides, I think the separate wr version also makes it very clear, to the user of the API, that there will be a price to pay, in terms of performance. The more seamlessly alternative might make this price less obvious.
--
igor
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 33 lines · Igor Stoppa
Quoted 3 lines · Mathieu Desnoyers
Also, is it the right approach to duplicate existing APIs, or should we rather hook into page fault handlers and let the kernel do those "shadow" mappings under the hood ?
This question is probably a good candidate for the small Q&A section I have in the 00/17.
Quoted 3 lines
Adding a new GFP flags for dynamic allocation, and a macro mapping to a section attribute might suffice for allocation or definition of such mostly-read-only/seldom-updated data.
I think what you are proposing makes sense from a pure hardening standpoint. From a more defensive one, I'd rather minimise the chances of giving a free pass to an attacker.
Maybe there is a better implementation of this, than what I have in mind. But, based on my current understanding of what you are describing, there would be few issues:
1) where would the pool go? The pool is a way to manage multiple vmas and express common property they share. Even before a vma is associated to the pool.
2) there would be more code that can seamlessly deal with both protected and regular data. Based on what? Some parameter, I suppose.
That parameter would be the new target.
If the code is "duplicated", as you say, the actual differences are baked in at compile time. The "duplication" would also allow to have always inlined functions for write-rare and leave more freedom to the compiler for their non-protected version.
Quoted 3 lines
Besides, I think the separate wr version also makes it very clear, to the user of the API, that there will be a price to pay, in terms of performance. The more seamlessly alternative might make this price less obvious.
What about something in the middle, where we move list to list_impl.h, and add a few macros where you have list_set_prev() in prlist now, so we could do,
// prlist.h
#define list_set_next(head, next) wr_ptr(&head->next, next) #define list_set_prev(head, prev) wr_ptr(&head->prev, prev)
#include <linux/list_impl.h>
// list.h
#define list_set_next(next) (head->next = next)
#define list_set_next(prev) (head->prev = prev)
#include <linux/list_impl.h>
I wonder then if you can get rid of some of the type punning too? It's not clear exactly why that's necessary from the series, but perhaps I'm missing something obvious :)
I also wonder how much the actual differences being baked in at compile time makes. Most (all?) of this code is inlined.
Cheers,
Tycho
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 51 lines · Tycho Andersen
Quoted 33 lines · Igor Stoppa
Quoted 3 lines · Mathieu Desnoyers
Also, is it the right approach to duplicate existing APIs, or should we rather hook into page fault handlers and let the kernel do those "shadow" mappings under the hood ?
This question is probably a good candidate for the small Q&A section I have in the 00/17.
Quoted 3 lines
Adding a new GFP flags for dynamic allocation, and a macro mapping to a section attribute might suffice for allocation or definition of such mostly-read-only/seldom-updated data.
I think what you are proposing makes sense from a pure hardening standpoint.
From a more defensive one, I'd rather minimise the chances of giving a free
pass to an attacker.Maybe there is a better implementation of this, than what I have in mind. But, based on my current understanding of what you are describing, there would be few issues:
1) where would the pool go? The pool is a way to manage multiple vmas and express common property they share. Even before a vma is associated to the pool.
2) there would be more code that can seamlessly deal with both protected and regular data. Based on what? Some parameter, I suppose.
That parameter would be the new target.
If the code is "duplicated", as you say, the actual differences are baked in at compile time. The "duplication" would also allow to have always inlined functions for write-rare and leave more freedom to the compiler for their non-protected version.Quoted 3 lines
Besides, I think the separate wr version also makes it very clear, to the user of the API, that there will be a price to pay, in terms of performance. The more seamlessly alternative might make this price less obvious.
What about something in the middle, where we move list to
list_impl.h, and add a few macros where you havelist_set_prev()in prlist now, so we could do,//
prlist.h#define
list_set_next(head, next)wr_ptr(&head->next, next)#definelist_set_prev(head, prev)wr_ptr(&head->prev, prev)#include <
linux/list_impl.h>//
list.h
Quoted 8 lines
#define
list_set_next(next)(head->next= next)
#definelist_set_next(prev)(head->prev= prev)#include <
linux/list_impl.h>I wonder then if you can get rid of some of the type punning too? It's not clear exactly why that's necessary from the series, but perhaps I'm missing something obvious :)
nothing obvious, probably there is only half a reference in the slides I linked-to in the cover letter :-)
So far I have minimized the number of "intrinsic" write rare functions,
mostly because I would want first to reach an agreement on the implementation of the core write-rare.
However, once that is done, it might be good to convert also the prlists to be "intrinsics". A list node is 2 pointers. If that was the alignment, i.e. __align(sizeof(list_head)), it might be possible to speed up a lot the list handling even as write rare.
Taking as example the insertion operation, it would be probably sufficient, in most cases, to have only two remappings:
- one covering the page with the latest two nodes
- one covering the page with the list head
That is 2 vs 8 remappings, and a good deal of memory barriers less.
This would be incompatible with what you are proposing, yet it would be justifiable, I think, because it would provide better performance to prlist, potentially widening its adoption, where performance is a concern.
Quoted 2 lines
I also wonder how much the actual differences being baked in at compile time makes. Most (all?) of this code is inlined.
If the inlined function expects to receive a prlist_head *, instead of a list_head *, doesn't it help turning runtime bugs into buildtime bugs?
Or maybe I miss your point?
--
igor
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 78 lines · Igor Stoppa
Quoted 51 lines · Tycho Andersen
Quoted 33 lines · Igor Stoppa
Quoted 3 lines · Mathieu Desnoyers
Also, is it the right approach to duplicate existing APIs, or should we rather hook into page fault handlers and let the kernel do those "shadow" mappings under the hood ?
This question is probably a good candidate for the small Q&A section I have in the 00/17.
Quoted 3 lines
Adding a new GFP flags for dynamic allocation, and a macro mapping to a section attribute might suffice for allocation or definition of such mostly-read-only/seldom-updated data.
I think what you are proposing makes sense from a pure hardening standpoint.
From a more defensive one, I'd rather minimise the chances of giving a free
pass to an attacker.Maybe there is a better implementation of this, than what I have in mind. But, based on my current understanding of what you are describing, there would be few issues:
1) where would the pool go? The pool is a way to manage multiple vmas and express common property they share. Even before a vma is associated to the pool.
2) there would be more code that can seamlessly deal with both protected and regular data. Based on what? Some parameter, I suppose.
That parameter would be the new target.
If the code is "duplicated", as you say, the actual differences are baked in at compile time. The "duplication" would also allow to have always inlined functions for write-rare and leave more freedom to the compiler for their non-protected version.Quoted 3 lines
Besides, I think the separate wr version also makes it very clear, to the user of the API, that there will be a price to pay, in terms of performance. The more seamlessly alternative might make this price less obvious.
What about something in the middle, where we move list to
list_impl.h, and add a few macros where you havelist_set_prev()in prlist now, so we could do,//
prlist.h#define
list_set_next(head, next)wr_ptr(&head->next, next)#definelist_set_prev(head, prev)wr_ptr(&head->prev, prev)#include <
linux/list_impl.h>//
list.hQuoted 8 lines
#define
list_set_next(next)(head->next= next)
#definelist_set_next(prev)(head->prev= prev)#include <
linux/list_impl.h>I wonder then if you can get rid of some of the type punning too? It's not clear exactly why that's necessary from the series, but perhaps I'm missing something obvious :)
nothing obvious, probably there is only half a reference in the slides I linked-to in the cover letter :-)
So far I have minimized the number of "intrinsic" write rare functions, mostly because I would want first to reach an agreement on the implementation of the core write-rare.
However, once that is done, it might be good to convert also the prlists to be "intrinsics". A list node is 2 pointers.
If that was the alignment, i.e. __align(sizeof(list_head)), it might be possible to speed up a lot the list handling even as write rare.Taking as example the insertion operation, it would be probably sufficient, in most cases, to have only two remappings:
- one covering the page with the latest two nodes
- one covering the page with the list head
Quoted 5 lines
That is 2 vs 8 remappings, and a good deal of memory barriers less.
This would be incompatible with what you are proposing, yet it would be justifiable, I think, because it would provide better performance to prlist, potentially widening its adoption, where performance is a concern.
I guess the writes to these are rare, right? So perhaps it's not such a big deal :)
Quoted 5 lines
Quoted 2 lines
I also wonder how much the actual differences being baked in at compile time makes. Most (all?) of this code is inlined.
If the inlined function expects to receive a
prlist_head*, instead of alist_head*, doesn't it help turning runtime bugs into buildtime bugs?
In principle it's not a bug to use the prmem helpers where the regular ones would do, it's just slower (assuming the types are the same). But mostly, it's a way to avoid actually copying and pasting most of the implementations of most of the data structures. I see some other replies in the thread already, but this seems not so good to me.
Tycho
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
In reply to Igor StoppaQuoted 5 lines · On Wed, 24 Oct 2018 17:03:01 +0300 Igor Stoppa <[email protected]> wrote:
I was hoping that by CC-ing kernel.org, the explicit recipients would get both the mail directly intended for them (as subsystem maintainers/supporters) and the rest.
The next time I'll err in the opposite direction.
Please don't.
Quoted 4 lines
In the meanwhile, please find the whole set here:
https://www.openwall.com/lists/kernel-hardening/2018/10/23/3
Note, it is critical that every change log stands on its own. You do not need to Cc the entire patch set to everyone. Each patch should have enough information in it to know exactly what the patch does.
It's OK if each change log has duplicate information from other patches. The important part is that one should be able to look at the change log of a specific patch and understand exactly what the patch is doing.
This is because 5 years from now, if someone does a git blame and comes up with this commit, they wont have access to the patch series. All they will have is this single commit log to explain why these changes were done.
If a change log depends on other commits for context, it is insufficient.
Thanks,
-- Steve
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
Quoted 2 lines · Steven Rostedt
If a change log depends on other commits for context, it is insufficient.
ok, I will adjust the change logs accordingly
--
thanks, igor
Re: [PATCH 14/17] prmem: llist, hlist, both plain and rcu
In reply to Igor StoppaQuoted 30 lines · Igor Stoppa
In some cases, all the data needing protection can be allocated from a pool in one go, as directly writable, then initialized and protected. The sequence is relatively short and it's acceptable to leave the entire data set unprotected.
In other cases, this is not possible, because the data will trickle over a relatively long period of time, in a non predictable way, possibly for the entire duration of the operations.
For these cases, the safe approach is to have the memory already write protected, when allocated. However, this will require replacing any direct assignment with calls to functions that can perform write rare.
Since lists are one of the most commonly used data structures in kernel, they are a the first candidate for receiving write rare extensions.
This patch implements basic functionality for altering said lists.
Signed-off-by Igor Stoppa <[email protected]>CC Thomas Gleixner <[email protected]>CC Kate Stewart <[email protected]>CC "David S. Miller" <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Philippe Ombredanne <[email protected]>CC "Paul E. McKenney" <[email protected]>CC Josh Triplett <[email protected]>CC Steven Rostedt <[email protected]>CC Mathieu Desnoyers <[email protected]>CC Lai Jiangshan <[email protected]>
Quoted 3 lines
--- MAINTAINERS | 1 + include/linux/prlist.h | 934 +++++++++++++++++++++++++++++++++++++++++
I'm not at all sure I understand the Changelog, or how it justifies duplicating almost 1k lines of code.
Sure lists aren't the most complicated thing we have, but duplicating that much is still very _very_ bad form. Why are we doing this?
PATCH 15/17 prmem: test cases for prlist and prhlist
In reply to Igor StoppaThese test cases focus on the basic operations required to operate both prlist and prhlist data, in particular creating, growing, shrinking, destroying.
They can also be useful as reference for practical use of write-rare lists.
--- MAINTAINERS | 1 + lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/test_prlist.c | 252 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 lib/test_prlist.c
MAINTAINERS +1 -0
@@ -9465,6 +9465,7 @@ F: mm/test_write_rare.c F: mm/test_pmalloc.c F: Documentation/core-api/prmem.rst F: include/linux/prlist.h +F: lib/test_prlist.c MEMORY MANAGEMENT L: [email protected]
lib/Kconfig.debug +9 -0
@@ -2034,6 +2034,15 @@ config IO_STRICT_DEVMEM If in doubt, say Y. +config DEBUG_PRLIST_TEST + bool "Testcase for Protected Linked List" + depends on STRICT_KERNEL_RWX && PRMEM + help + This option enables the testing of an implementation of linked + list based on write rare memory. + The test cases can also be used as examples for how to use the + prlist data structure(s). + source "arch/$(SRCARCH)/Kconfig.debug" endmenu # Kernel hacking
lib/Makefile +1 -0
@@ -270,3 +270,4 @@ obj-$(CONFIG_GENERIC_LIB_LSHRDI3) += lshrdi3.o obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o +obj-$(CONFIG_DEBUG_PRLIST_TEST) += test_prlist.o
lib/test_prlist.c new file +252 -0
252 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 16/17 prmem: pratomic-long
In reply to Igor StoppaMinimalistic functionality for having the write rare version of atomic_long_t data.
--- MAINTAINERS | 1 + include/linux/pratomic-long.h | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 include/linux/pratomic-long.h
MAINTAINERS +1 -0
@@ -9466,6 +9466,7 @@ F: mm/test_pmalloc.c F: Documentation/core-api/prmem.rst F: include/linux/prlist.h F: lib/test_prlist.c +F: include/linux/pratomic-long.h MEMORY MANAGEMENT L: [email protected]
include/linux/pratomic-long.h new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 23 lines · Igor Stoppa
include/linux/pratomic-long.h new file +23
-0+static __always_inline +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) +{ +struct page *page; +uintptr_t base; +uintptr_t offset; +unsigned long flags; +size_t size =sizeof (*l); +bool is_virt =__is_wr_after_init (l, size); + +if (WARN (!(is_virt ||likely (__is_wr_pool (l, size))), +WR_ERR_RANGE_MSG )) +return false; +local_irq_save (flags); +if (is_virt) + page =virt_to_page (l); +else +vmalloc_to_page (l); + offset = (~PAGE_MASK ) & (uintptr_t )l; + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false;
include/linux/pratomic-long.h line 49
Quoted 10 lines
include/linux/pratomic-long.h new file +10
-0+ } +if (inc) +atomic_long_inc ((atomic_long_t *)(base + offset)); +else +atomic_long_dec ((atomic_long_t *)(base + offset)); +vunmap ((void *)base); +local_irq_restore (flags); +return true; + +}
That's just hideously nasty.. and horribly broken.
We're not going to duplicate all these kernel interfaces wrapped in gunk like that. Also, you _cannot_ call vunmap() with IRQs disabled. Clearly you've never tested this with debug bits enabled.
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 37 lines · Peter Zijlstra
Quoted 23 lines · Igor Stoppa
include/linux/pratomic-long.h new file +23
-0+static __always_inline +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) +{ +struct page *page; +uintptr_t base; +uintptr_t offset; +unsigned long flags; +size_t size =sizeof (*l); +bool is_virt =__is_wr_after_init (l, size); + +if (WARN (!(is_virt ||likely (__is_wr_pool (l, size))), +WR_ERR_RANGE_MSG )) +return false; +local_irq_save (flags); +if (is_virt) + page =virt_to_page (l); +else +vmalloc_to_page (l); + offset = (~PAGE_MASK ) & (uintptr_t )l; + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false;include/linux/pratomic-long.h line 49
Quoted 10 lines
include/linux/pratomic-long.h new file +10
-0+ } +if (inc) +atomic_long_inc ((atomic_long_t *)(base + offset)); +else +atomic_long_dec ((atomic_long_t *)(base + offset)); +vunmap ((void *)base); +local_irq_restore (flags); +return true; + +}That's just hideously nasty.. and horribly broken.
Quoted 2 lines
We're not going to duplicate all these kernel interfaces wrapped in gunk like that.
one possibility would be to have macros which use typeof() on the parameter being passed, to decide what implementation to use: regular or write-rare
This means that type punning would still be needed, to select the implementation.
Would this be enough? Is there some better way?
Quoted 2 lines
Also, you _cannot_ call
vunmap()with IRQs disabled. Clearly you've never tested this with debug bits enabled.
I thought I had them. And I _did_ have them enabled, at some point. But I must have messed up with the configuration and I failed to notice this.
I can think of a way it might work, albeit it's not going to be very pretty:
* for the vmap(): if I understand correctly, it might sleep while obtaining memory for creating the mapping. This part could be executed before disabling interrupts. The rest of the function, instead, would be executed after interrupts are disabled.
* for vunmap(): after the writing is done, change also the alternate mapping to read only, then enable interrupts and destroy the alternate mapping. Making also the secondary mapping read only makes it equally secure as the primary, which means that it can be visible also with interrupts enabled.
--
igor
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 43 lines · Igor Stoppa
Quoted 37 lines · Peter Zijlstra
Quoted 23 lines · Igor Stoppa
include/linux/pratomic-long.h new file +23
-0+static __always_inline +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) +{ +struct page *page; +uintptr_t base; +uintptr_t offset; +unsigned long flags; +size_t size =sizeof (*l); +bool is_virt =__is_wr_after_init (l, size); + +if (WARN (!(is_virt ||likely (__is_wr_pool (l, size))), +WR_ERR_RANGE_MSG )) +return false; +local_irq_save (flags); +if (is_virt) + page =virt_to_page (l); +else +vmalloc_to_page (l); + offset = (~PAGE_MASK ) & (uintptr_t )l; + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false;include/linux/pratomic-long.h line 49
Quoted 10 lines
include/linux/pratomic-long.h new file +10
-0+ } +if (inc) +atomic_long_inc ((atomic_long_t *)(base + offset)); +else +atomic_long_dec ((atomic_long_t *)(base + offset)); +vunmap ((void *)base); +local_irq_restore (flags); +return true; + +}That's just hideously nasty.. and horribly broken.
Quoted 2 lines
We're not going to duplicate all these kernel interfaces wrapped in gunk like that.
Quoted 7 lines
one possibility would be to have macros which use
typeof()on the parameter being passed, to decide what implementation to use: regular or write-rareThis means that type punning would still be needed, to select the implementation.
Would this be enough? Is there some better way?
Like mentioned elsewhere; if you do write_enable() + write_disable() thingies, it all becomes:
write_enable(); atomic_foo(&bar); write_disable();
No magic gunk infested duplication at all. Of course, ideally you'd then teach objtool about this (or a GCC plugin I suppose) to ensure any enable reached a disable.
The alternative is something like:
#define ALLOW_WRITE(stmt) do { write_enable(); do { stmt; } while (0); write_disable(); } while (0)
which then allows you to write:
ALLOW_WRITE(atomic_foo(&bar));
No duplication.
Quoted 14 lines
Quoted 2 lines
Also, you _cannot_ call
vunmap()with IRQs disabled. Clearly you've never tested this with debug bits enabled.I thought I had them. And I _did_ have them enabled, at some point. But I must have messed up with the configuration and I failed to notice this.
I can think of a way it might work, albeit it's not going to be very pretty:
* for the
vmap(): if I understand correctly, it might sleep while obtaining memory for creating the mapping. This part could be executed before disabling interrupts. The rest of the function, instead, would be executed after interrupts are disabled.
Quoted 4 lines
* for
vunmap(): after the writing is done, change also the alternate mapping to read only, then enable interrupts and destroy the alternate mapping. Making also the secondary mapping read only makes it equally secure as the primary, which means that it can be visible also with interrupts enabled.
That doesn't work if you wanted to do this write while you already have IRQs disabled for example.
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 55 lines · Peter Zijlstra
Quoted 43 lines · Igor Stoppa
Quoted 37 lines · Peter Zijlstra
Quoted 23 lines · Igor Stoppa
include/linux/pratomic-long.h new file +23
-0+static __always_inline +bool __pratomic_long_op(bool inc, struct pratomic_long_t *l) +{ +struct page *page; +uintptr_t base; +uintptr_t offset; +unsigned long flags; +size_t size =sizeof (*l); +bool is_virt =__is_wr_after_init (l, size); + +if (WARN (!(is_virt ||likely (__is_wr_pool (l, size))), +WR_ERR_RANGE_MSG )) +return false; +local_irq_save (flags); +if (is_virt) + page =virt_to_page (l); +else +vmalloc_to_page (l); + offset = (~PAGE_MASK ) & (uintptr_t )l; + base = (uintptr_t )vmap (&page,1 ,VM_MAP ,PAGE_KERNEL ); +if (WARN (!base,WR_ERR_PAGE_MSG )) { +local_irq_restore (flags); +return false;include/linux/pratomic-long.h line 49
Quoted 10 lines
include/linux/pratomic-long.h new file +10
-0+ } +if (inc) +atomic_long_inc ((atomic_long_t *)(base + offset)); +else +atomic_long_dec ((atomic_long_t *)(base + offset)); +vunmap ((void *)base); +local_irq_restore (flags); +return true; + +}That's just hideously nasty.. and horribly broken.
Quoted 2 lines
We're not going to duplicate all these kernel interfaces wrapped in gunk like that.
Quoted 7 lines
one possibility would be to have macros which use
typeof()on the parameter being passed, to decide what implementation to use: regular or write-rareThis means that type punning would still be needed, to select the implementation.
Would this be enough? Is there some better way?
Like mentioned elsewhere; if you do
write_enable()+write_disable()thingies, it all becomes:
Quoted 7 lines
write_enable(); atomic_foo(&bar); write_disable();No magic gunk infested duplication at all. Of course, ideally you'd then teach objtool about this (or a GCC plugin I suppose) to ensure any enable reached a disable.
Isn't the issue here that we don't want to change the page tables for the mapping of &bar, but instead want to create a temporary writable alias at a random virtual address?
So you'd want:
wbar = write_enable(&bar); atomic_foo(wbar); write_disable(wbar);
which is probably better expressed as a map/unmap API. I suspect this would also be the only way to do things for cmpxchg() loops, where you want to create the mapping outside of the loop to minimise your time in the critical section.
Will
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 18 lines · Will Deacon
Quoted 10 lines · Peter Zijlstra
Like mentioned elsewhere; if you do
write_enable()+write_disable()thingies, it all becomes:write_enable(); atomic_foo(&bar); write_disable();No magic gunk infested duplication at all. Of course, ideally you'd then teach objtool about this (or a GCC plugin I suppose) to ensure any enable reached a disable.
Isn't the issue here that we don't want to change the page tables for the mapping of &bar, but instead want to create a temporary writable alias at a random virtual address?
So you'd want:
Quoted 8 lines
wbar = write_enable(&bar); atomic_foo(wbar); write_disable(wbar);which is probably better expressed as a map/unmap API. I suspect this would also be the only way to do things for
cmpxchg()loops, where you want to create the mapping outside of the loop to minimise your time in the critical section.
Ah, so I was thikning that the altnerative mm would have stuff in the same location, just RW instead of RO.
But yes, if we, like Andy suggets, use the userspace address range for the aliases, then we need to do as you suggest.
Re: [PATCH 16/17] prmem: pratomic-long
Quoted 23 lines · Peter Zijlstra
Quoted 22 lines · Will Deacon
Quoted 10 lines · Peter Zijlstra
Like mentioned elsewhere; if you do
write_enable()+write_disable()thingies, it all becomes:write_enable(); atomic_foo(&bar); write_disable();No magic gunk infested duplication at all. Of course, ideally you'd then teach objtool about this (or a GCC plugin I suppose) to ensure any enable reached a disable.
Isn't the issue here that we don't want to change the page tables for the mapping of &bar, but instead want to create a temporary writable alias at a random virtual address?
So you'd want:
wbar = write_enable(&bar); atomic_foo(wbar); write_disable(wbar);
Quoted 7 lines
Quoted 4 lines
which is probably better expressed as a map/unmap API. I suspect this would also be the only way to do things for
cmpxchg()loops, where you want to create the mapping outside of the loop to minimise your time in the critical section.Ah, so I was thikning that the altnerative mm would have stuff in the same location, just RW instead of RO.
I was hoping for the same location too. That allows use to use a gcc plugin to mark, say, function pointer tables, as read-only, and annotate their rare updates with write_rare() without any recalculation.
-Kees
Signature · 1 line
Kees Cook
PATCH 17/17 prmem: ima: turn the measurements list write rare
In reply to Igor StoppaThe measurement list is moved to write rare memory, including related data structures.
Various boilerplate linux data structures and related functions are replaced by their write-rare counterpart.
--- security/integrity/ima/ima.h | 18 ++++++++------ security/integrity/ima/ima_api.c | 29 +++++++++++++---------- security/integrity/ima/ima_fs.c | 12 +++++----- security/integrity/ima/ima_main.c | 6 +++++ security/integrity/ima/ima_queue.c | 28 +++++++++++++--------- security/integrity/ima/ima_template.c | 14 ++++++----- security/integrity/ima/ima_template_lib.c | 14 +++++++---- 7 files changed, 74 insertions(+), 47 deletions(-)
security/integrity/ima/ima.h +11 -7
@@ -24,6 +24,8 @@#include <linux/hash.h> #include <linux/tpm.h> #include <linux/audit.h> +#include <linux/prlist.h> +#include <linux/pratomic-long.h> #include <crypto/hash_info.h> #include "../integrity.h"
@@ -84,7 +86,7 @@ struct ima_template_field {/* IMA template descriptor definition */ struct ima_template_desc { -struct list_head list; +union prlist_head list;char *name;char *fmt;int num_fields;
@@ -100,11 +102,13 @@ struct ima_template_entry { };struct ima_queue_entry { -struct hlist_node hnext;/* place in hash collision list */ -struct list_head later;/* place in ima_measurements list */ +union prhlist_node hnext;/* place in hash collision list */ +union prlist_head later;/* place in ima_measurements list */ struct ima_template_entry *entry; }; -extern struct list_head ima_measurements;/* list of all measurements */ + +/* list of all measurements */ +extern union prlist_head ima_measurements __wr_after_init;/* Some details preceding the binary serialized measurement list */ struct ima_kexec_hdr {
@@ -160,9 +164,9 @@ void ima_init_template_list(void);extern spinlock_t ima_queue_lock;struct ima_h_table { -atomic_long_t len;/* number of stored measurements in the list */ -atomic_long_t violations; -struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE ]; +struct pratomic_long_t len;/* # of measurements in the list */ +struct pratomic_long_t violations; +union prhlist_head queue[IMA_MEASURE_HTABLE_SIZE ]; };extern struct ima_h_table ima_htable;
security/integrity/ima/ima_api.c +17 -12
@@ -19,9 +19,12 @@#include <linux/xattr.h> #include <linux/evm.h> #include <linux/iversion.h> +#include <linux/prmemextra.h> +#include <linux/pratomic-long.h> #include "ima.h" +extern struct pmalloc_pool ima_pool;/* * ima_free_template_entry - free an existing template entry */
@@ -29,10 +32,10 @@ void ima_free_template_entry(struct ima_template_entry *entry) {int i; -for (i =0 ; i < entry->template_desc->num_fields; i++) -kfree (entry->template_data[i].data); +// for (i = 0; i < entry->template_desc->num_fields; i++) +// kfree(entry->template_data[i].data); -kfree (entry); +// kfree(entry); }/*
@@ -44,12 +47,13 @@ int ima_alloc_init_template(struct ima_event_data *event_data,struct ima_template_desc *template_desc =ima_template_desc_current ();int i, result =0 ; - *entry =kzalloc (sizeof (**entry) + template_desc->num_fields * -sizeof (struct ima_field_data ),GFP_NOFS ); + *entry =pzalloc (&ima_pool, +sizeof (**entry) + template_desc->num_fields * +sizeof (struct ima_field_data ));if (!*entry)return -ENOMEM ; - (*entry)->template_desc = template_desc; +wr_ptr (&((*entry)->template_desc), template_desc);for (i =0 ; i < template_desc->num_fields; i++) {struct ima_template_field *field = template_desc->fields[i];u32 len;
@@ -59,9 +63,10 @@ int ima_alloc_init_template(struct ima_event_data *event_data,if (result !=0 ) goto out; - len = (*entry)->template_data[i].len; - (*entry)->template_data_len +=sizeof (len); - (*entry)->template_data_len += len; + len = (*entry)->template_data_len +sizeof (len) + + (*entry)->template_data[i].len; +wr_memcpy (&(*entry)->template_data_len, &len, +sizeof (len)); }return 0 ; out:
@@ -113,9 +118,9 @@ int ima_store_template(struct ima_template_entry *entry, audit_cause, result, 0);return result; } -memcpy (entry->digest, hash.hdr.digest, hash.hdr.length); +wr_memcpy (entry->digest, hash.hdr.digest, hash.hdr.length); } - entry->pcr = pcr; +wr_int (&entry->pcr, pcr); result =ima_add_template_entry (entry, violation, op, inode, filename);return result; }
@@ -139,7 +144,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,int result;/* can overflow, only indicator */ -atomic_long_inc (&ima_htable.violations); +pratomic_long_inc (&ima_htable.violations); result =ima_alloc_init_template (&event_data, &entry);if (result <0 ) {
security/integrity/ima/ima_fs.c +6 -6
@@ -57,7 +57,8 @@ static ssize_t ima_show_htable_violations(struct file *filp,char __user *buf, size_t count, loff_t *ppos) { - return ima_show_htable_value(buf, count, ppos, &ima_htable.violations); + return ima_show_htable_value(buf, count, ppos, + &ima_htable.violations.l); } static const struct file_operations ima_htable_violations_ops = {
@@ -69,8 +70,7 @@ static ssize_t ima_show_measurements_count(struct file *filp,char __user *buf, size_t count, loff_t *ppos) { - return ima_show_htable_value(buf, count, ppos, &ima_htable.len); - + return ima_show_htable_value(buf, count, ppos, &ima_htable.len.l); } static const struct file_operations ima_measurements_count_ops = {
@@ -86,7 +86,7 @@ static void *ima_measurements_start(struct seq_file *m, loff_t *pos)/* we need a lock since pos could point beyond last element */ rcu_read_lock (); -list_for_each_entry_rcu (qe, &ima_measurements, later) { +list_for_each_entry_rcu (qe, &ima_measurements.list, later.list) {if (!l--) {rcu_read_unlock ();return qe;
@@ -303,7 +303,7 @@ static ssize_t ima_read_policy(char *path) size -= rc; } -vfree (data); +// vfree(data); if (rc <0 )return rc;else if (size)
@@ -350,7 +350,7 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf, }mutex_unlock (&ima_write_mutex); out_free: -kfree (data); +// kfree(data); out:if (result <0 ) valid_policy =0 ;
security/integrity/ima/ima_main.c +6 -0
@@ -29,6 +29,7 @@#include <linux/ima.h> #include <linux/iversion.h> #include <linux/fs.h> +#include <linux/prlist.h> #include "ima.h"
@@ -536,10 +537,15 @@ int ima_load_data(enum kernel_load_data_id id)return 0 ; } +struct pmalloc_pool ima_pool; + +#define IMA_POOL_ALLOC_CHUNK (16 * PAGE_SIZE)static int __init init_ima (void ) {int error; +pmalloc_init_custom_pool (&ima_pool,IMA_POOL_ALLOC_CHUNK ,3 , +PMALLOC_MODE_START_WR );ima_init_template_list ();hash_setup (CONFIG_IMA_DEFAULT_HASH ); error =ima_init ();
security/integrity/ima/ima_queue.c +17 -11
@@ -24,11 +24,14 @@#include <linux/module.h> #include <linux/rculist.h> #include <linux/slab.h> +#include <linux/prmemextra.h> +#include <linux/prlist.h> +#include <linux/pratomic-long.h> #include "ima.h" #define AUDIT_CAUSE_LEN_MAX 32 -LIST_HEAD (ima_measurements);/* list of all measurements */ +PRLIST_HEAD (ima_measurements);/* list of all measurements */ #ifdef CONFIG_IMA_KEXEC static unsigned long binary_runtime_size;#else
@@ -36,9 +39,9 @@ static unsigned long binary_runtime_size = ULONG_MAX;#endif /* key: inode (before secure-hashing a file) */ -struct ima_h_table ima_htable = { - .len =ATOMIC_LONG_INIT (0 ), - .violations =ATOMIC_LONG_INIT (0 ), +struct ima_h_table ima_htable __wr_after_init = { + .len =PRATOMIC_LONG_INIT (0 ), + .violations =PRATOMIC_LONG_INIT (0 ), .queue[0 ...IMA_MEASURE_HTABLE_SIZE -1 ] =HLIST_HEAD_INIT };
@@ -58,7 +61,7 @@ static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value, key =ima_hash_key (digest_value);rcu_read_lock (); -hlist_for_each_entry_rcu (qe, &ima_htable.queue[key], hnext) { +hlist_for_each_entry_rcu (qe, &ima_htable.queue[key], hnext.node) { rc =memcmp (qe->entry->digest, digest_value,TPM_DIGEST_SIZE );if ((rc ==0 ) && (qe->entry->pcr == pcr)) { ret = qe;
@@ -87,6 +90,8 @@ static int get_binary_runtime_size(struct ima_template_entry *entry)return size; } +extern struct pmalloc_pool ima_pool; +/* ima_add_template_entry helper function: * - Add template entry to the measurement list and hash table, for * all entries except those carried across kexec.
@@ -99,20 +104,21 @@ static int ima_add_digest_entry(struct ima_template_entry *entry,struct ima_queue_entry *qe;unsigned int key; - qe =kmalloc (sizeof (*qe),GFP_KERNEL ); + qe =pmalloc (&ima_pool,sizeof (*qe));if (qe ==NULL ) {pr_err ("OUT OF MEMORY ERROR creating queue entry\n" );return -ENOMEM ; } - qe->entry = entry; +wr_ptr (&qe->entry, entry); +INIT_PRLIST_HEAD (&qe->later); +prlist_add_tail_rcu (&qe->later, &ima_measurements); + -INIT_LIST_HEAD (&qe->later); -list_add_tail_rcu (&qe->later, &ima_measurements); +pratomic_long_inc (&ima_htable.len); -atomic_long_inc (&ima_htable.len);if (update_htable) { key =ima_hash_key (entry->digest); -hlist_add_head_rcu (&qe->hnext, &ima_htable.queue[key]); +prhlist_add_head_rcu (&qe->hnext, &ima_htable.queue[key]); }if (binary_runtime_size !=ULONG_MAX ) {
security/integrity/ima/ima_template.c +8 -6
@@ -22,14 +22,15 @@enum header_fields {HDR_PCR ,HDR_DIGEST ,HDR_TEMPLATE_NAME ,HDR_TEMPLATE_DATA ,HDR__LAST }; -static struct ima_template_desc builtin_templates[] = { +static struct ima_template_desc builtin_templates[] __wr_after_init = { {.name =IMA_TEMPLATE_IMA_NAME , .fmt =IMA_TEMPLATE_IMA_FMT }, {.name ="ima-ng" , .fmt ="d-ng|n-ng" }, {.name ="ima-sig" , .fmt ="d-ng|n-ng|sig" }, {.name ="" , .fmt ="" },/* placeholder for a custom format */ }; -static LIST_HEAD (defined_templates); +static PRLIST_HEAD (defined_templates); +static DEFINE_SPINLOCK (template_list);static struct ima_template_field supported_fields[] = {
@@ -114,7 +115,8 @@ static struct ima_template_desc *lookup_template_desc(const char *name)int found =0 ;rcu_read_lock (); -list_for_each_entry_rcu (template_desc, &defined_templates, list) { +list_for_each_entry_rcu (template_desc, &defined_templates.list, + list.list) {if ((strcmp (template_desc->name, name) ==0 ) || (strcmp (template_desc->fmt, name) ==0 )) { found =1 ;
@@ -207,12 +209,12 @@ void ima_init_template_list(void) {int i; -if (!list_empty (&defined_templates)) +if (!list_empty (&defined_templates.list))return ;spin_lock (&template_list);for (i =0 ; i <ARRAY_SIZE (builtin_templates); i++) { -list_add_tail_rcu (&builtin_templates[i].list, +prlist_add_tail_rcu (&builtin_templates[i].list, &defined_templates); }spin_unlock (&template_list);
@@ -266,7 +268,7 @@ static struct ima_template_desc *restore_template_fmt(char *template_name) goto out;spin_lock (&template_list); -list_add_tail_rcu (&template_desc->list, &defined_templates); +prlist_add_tail_rcu (&template_desc->list, &defined_templates);spin_unlock (&template_list); out:return template_desc;
security/integrity/ima/ima_template_lib.c +9 -5
@@ -15,8 +15,12 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/printk.h> +#include <linux/prmemextra.h> #include "ima_template_lib.h" +extern struct pmalloc_pool ima_pool; +static bool ima_template_hash_algo_allowed (u8 algo) {if (algo ==HASH_ALGO_SHA1 || algo ==HASH_ALGO_MD5 )
@@ -42,11 +46,11 @@ static int ima_write_template_field_data(const void *data, const u32 datalen,if (datafmt ==DATA_FMT_STRING ) buflen = datalen +1 ; - buf =kzalloc (buflen,GFP_KERNEL ); + buf =pzalloc (&ima_pool, buflen);if (!buf)return -ENOMEM ; -memcpy (buf, data, datalen); +wr_memcpy (buf, data, datalen);/* * Replace all space characters with underscore for event names and
@@ -58,11 +62,11 @@ static int ima_write_template_field_data(const void *data, const u32 datalen,if (datafmt ==DATA_FMT_STRING ) {for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)if (*buf_ptr ==' ' ) - *buf_ptr ='_' ; +wr_char (buf_ptr,'_' ); } - field_data->data = buf; - field_data->len = buflen; +wr_ptr (&field_data->data, buf); +wr_memcpy (&field_data->len, &buflen,sizeof (buflen));return 0 ; }
Signature · 1 line
2.17.1
Quoted 4 lines · Igor Stoppa
-- Summary --
Preliminary version of memory protection patchset, including a sample use case, turning into write-rare the IMA measurement list.
I haven't looked at the patches yet, but I see a significant issue from the subject lines. "prmem" is very similar to "pmem" (persistent memory) and that's going to cause confusion. Especially if people start talking about prmem and pmem in the context of write protect pmem with prmem...
Naming is hard :/
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Quoted 6 lines · Dave Chinner
Quoted 4 lines · Igor Stoppa
-- Summary --
Preliminary version of memory protection patchset, including a sample use case, turning into write-rare the IMA measurement list.
Quoted 7 lines
I haven't looked at the patches yet, but I see a significant issue from the subject lines. "prmem" is very similar to "pmem" (persistent memory) and that's going to cause confusion. Especially if people start talking about prmem and pmem in the context of write protect pmem with prmem...
Naming is hard :/
yes, at some point I had to go from rare-write to write-rare because I realized that the acronym "rw" was likely to be interpreted as "read/write"
I propose to keep prmem for the time being, just to avoid adding extra changes that are not functional. Once the code has somewhat settled down, I can proceed with the renaming. In the meanwhile, a better name could be discussed.
For example, would "prmemory" be sufficiently unambiguous?
--
igor