mm: security: ro protection for dynamic data
Subsystems (7)
v11 cover mm: security: ro protection for dynamic data
This patch-set introduces the possibility of protecting memory that has been allocated dynamically.
The memory is managed in pools: when a memory pool is turned into R/O, all the memory that is part of it, will become R/O.
A R/O pool can be destroyed, to recover its memory, but it cannot be turned back into R/W mode.
This is intentional. This feature is meant for data that doesn't need further modifications after initialization.
However the data might need to be released, for example as part of module unloading.
To do this, the memory must first be freed, then the pool can be destroyed.
An example is provided, in the form of self-testing.
Changes since the v10 version:
Initially I tried to provide support for hardening the LSM hooks, but the LSM code was too much in a flux to have some chance to be merged.
Several drop-in replacement for kmalloc based functions, for example kzalloc.
Quoted 1 line
From this perspective I have also modified genalloc, to make its free
functionality follow more closely the kfree, which doesn't need to be told the size of the allocation being released. This was sent out for review twice, but it has not received any feedback, so far.
Also genalloc now comes with self-testing.
The latest can be found also here:
https://www.spinics.net/lists/kernel/msg2696152.html
The need to integrate with hardened user copy has driven an optimization in the management of vmap_areas, where each struct page in a vmalloc area has a reference to it, saving the search through the various areas.
I was planning - and can still do it - to provide hardening for some IMA data, but in the meanwhile it seems that the XFS developers might be interested in htis functionality:
http://www.openwall.com/lists/kernel-hardening/2018/01/24/1
So I'm sending it out as preview.
Igor Stoppa (6):
genalloc: track beginning of allocations
genalloc: selftest
struct page: add field for vm_struct
Protectable Memory
Documentation for Pmalloc
Pmalloc: self-test
Documentation/core-api/pmalloc.txt | 104 ++++++++ include/linux/genalloc-selftest.h | 30 +++ include/linux/genalloc.h | 6 +- include/linux/mm_types.h | 1 + include/linux/pmalloc.h | 215 ++++++++++++++++ include/linux/vmalloc.h | 1 + init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/genalloc-selftest.c | 402 +++++++++++++++++++++++++++++ lib/genalloc.c | 444 +++++++++++++++++++++---------- mm/Kconfig | 7 + mm/Makefile | 2 + mm/pmalloc-selftest.c | 65 +++++ mm/pmalloc-selftest.h | 30 +++ mm/pmalloc.c | 516 +++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 +- mm/vmalloc.c | 18 +- 18 files changed, 1744 insertions(+), 140 deletions(-) create mode 100644 Documentation/core-api/pmalloc.txt create mode 100644 include/linux/genalloc-selftest.h create mode 100644 include/linux/pmalloc.h create mode 100644 lib/genalloc-selftest.c create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h create mode 100644 mm/pmalloc.c
Signature · 1 line
2.9.3
PATCH v11 1/6 genalloc: track beginning of allocations
The genalloc library is only capable of tracking if a certain unit of allocation is in use or not.
It is not capable of discerning where the memory associated to an allocation request begins and where it ends.
The reason is that units of allocations are tracked by using a bitmap, where each bit represents that the unit is either allocated (1) or available (0).
The user of the API must keep track of how much space was requested, if it ever needs to be freed.
This can cause errors being undetected.
Ex:
* Only a subset of the memory provided to an allocation request is freed
* The memory from a subsequent allocation is freed
* The memory being freed doesn't start at the beginning of an allocation.
The bitmap is used because it allows to perform lockless read/write access, where this is supported by hw through cmpxchg. Similarly, it is possible to scan the bitmap for a sufficiently long sequence of zeros, to identify zones available for allocation.
--
This patch doubles the space reserved in the bitmap for each allocation. By using 2 bits per allocation, it is possible to encode also the information of where the allocation starts:
(msb to the left, lsb to the right, in the following "dictionary")
11: first allocation unit in the allocation
10: any subsequent allocation unit (if any) in the allocation 00: available allocation unit
01: invalid
Ex, with the same notation as above - MSb.......LSb:
...000010111100000010101011 <-- Read in this direction.
\__|\__|\|\____|\______|
| | | | \___ 4 used allocation units
| | | \___________ 3 empty allocation units
| | \_________________ 1 used allocation unit
| \___________________ 2 used allocation units
\_______________________ 2 empty allocation unitsBecause of the encoding, the previous lockless operations are still possible. The only caveat is to change the parameter of the zero-finding function which establishes the alignment at which to perform the test for first zero.
The original value of the parameter is 0, meaning that an allocation can start at any point in the bitmap, while the new value is 1, meaning that allocations can start only at even places (bit 0, bit 2, etc.) The number of zeroes to look for, must therefore be doubled.
When it's time to free the memory associated to an allocation request, it's a matter of checking if the corresponding allocation unit is really the beginning of an allocation (both bits are set to 1). Looking for the ending can also be performed locklessly. It's sufficient to identify the first mapped allocation unit that is represented either as free (00) or busy (11).
Even if the allocation status should change in the meanwhile, it doesn't matter, since it can only transition between free (00) and first-allocated (11).
The parameter indicating to the *_free() function the size of the space that should be freed is not currently removed, to facilitate the transition, but it is verified, whenever it is not zero.
If it is set to zero, then the free function will autonomously decide the size to be free, by scanning the bitmap.
About the implementation: the patch introduces the concept of "bitmap entry", which has a 1:1 mapping with allocation units, while the code being patched has a 1:1 mapping between allocation units and bits.
This means that, now, the bitmap can be extended (by following powers of 2), to track also other properties of the allocations, if ever needed.
--- include/linux/genalloc.h | 3 +- lib/genalloc.c | 417 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 289 insertions(+), 131 deletions(-)
include/linux/genalloc.h +2 -1
@@ -32,6 +32,7 @@#include <linux/types.h> #include <linux/spinlock_types.h> +#include <linux/slab.h> struct device ;struct device_node ;
@@ -75,7 +76,7 @@ struct gen_pool_chunk {phys_addr_t phys_addr;/* physical starting address of memory chunk */ unsigned long start_addr;/* start address of memory chunk */ unsigned long end_addr;/* end address of memory chunk (inclusive) */ -unsigned long bits[0 ];/* bitmap for allocating memory chunk */ +unsigned long entries[0 ];/* bitmap for allocating memory chunk */ };/*
lib/genalloc.c +287 -130
603 lines not on this page · raw message
Signature · 1 line
2.9.3
PATCH v11 2/6 genalloc: selftest
In reply to Igor StoppaIntroduce a set of macros for writing concise test cases for genalloc.
The test cases are meant to provide regression testing, when working on new functionality for genalloc.
Primarily they are meant to confirm that the various allocation strategy will continue to work as expected.
The execution of the self testing is controlled through a Kconfig option.
--- include/linux/genalloc-selftest.h | 30 +++ init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/genalloc-selftest.c | 402 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 450 insertions(+) create mode 100644 include/linux/genalloc-selftest.h create mode 100644 lib/genalloc-selftest.c
include/linux/genalloc-selftest.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* genalloc-selftest.h +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + + +#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__ + + +#ifdef CONFIG_GENERIC_ALLOCATOR_SELFTEST + +#include <linux/genalloc.h> + +void genalloc_selftest (void ); + +#else + +static inline void genalloc_selftest (void ){}; + +#endif + +#endif
init/main.c +2 -0
@@ -87,6 +87,7 @@#include <linux/io.h> #include <linux/cache.h> #include <linux/rodata_test.h> +#include <linux/genalloc-selftest.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -649,6 +650,7 @@ asmlinkage __visible void __init start_kernel(void)*/ mem_encrypt_init (); +genalloc_selftest ();#ifdef CONFIG_BLK_DEV_INITRD if (initrd_start && !initrd_below_start_ok &&page_to_pfn (virt_to_page ((void *)initrd_start)) < min_low_pfn) {
lib/Kconfig +15 -0
@@ -291,6 +291,21 @@ config DECOMPRESS_LZ4 config GENERIC_ALLOCATOR bool +config GENERIC_ALLOCATOR_SELFTEST + bool "genalloc tester" + default n + select GENERIC_ALLOCATOR + help + Enable automated testing of the generic allocator. + The testing is primarily for the tracking of allocated space. + +config GENERIC_ALLOCATOR_SELFTEST_VERBOSE + bool "make the genalloc tester more verbose" + default n + select GENERIC_ALLOCATOR_SELFTEST + help + More information will be displayed during the self-testing. + # # reed solomon support is select'ed if needed #
lib/Makefile +1 -0
@@ -106,6 +106,7 @@ obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_CRC8) += crc8.o obj-$(CONFIG_XXHASH) += xxhash.o obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o +obj-$(CONFIG_GENERIC_ALLOCATOR_SELFTEST) += genalloc-selftest.o obj-$(CONFIG_842_COMPRESS) += 842/ obj-$(CONFIG_842_DECOMPRESS) += 842/
lib/genalloc-selftest.c new file +402 -0
402 lines not on this page · raw message
Signature · 1 line
2.9.3
PATCH v11 3/6 struct page: add field for vm_struct
In reply to Igor StoppaWhen a page is used for virtual memory, it is often necessary to obtian a handler to the corresponding vm_struct, which refers to the virtually continuous area generated when invoking vmalloc.
The struct page has a "mapping" field, which can be re-used, to store a pointer to the parent area. This will avoid more expensive searches.
As example, the function find_vm_area is reimplemented, to take advantage of the newly introduced field.
--- include/linux/mm_types.h | 1 + mm/vmalloc.c | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/mm_types.h +1 -0
@@ -84,6 +84,7 @@ struct page {void *s_mem;/* slab first object */ atomic_t compound_mapcount;/* first tail page */ /* page_deferred_list().next -- second tail page */ +struct vm_struct *area; };/* Second double word */
mm/vmalloc.c +13 -5
@@ -1466,13 +1466,16 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,*/ struct vm_struct *find_vm_area (const void *addr) { -struct vmap_area *va; +struct page *page; - va =find_vmap_area ((unsigned long )addr); -if (va && va->flags &VM_VM_AREA ) -return va->vm; +if (unlikely (!is_vmalloc_addr (addr))) +return NULL ; -return NULL ; + page =vmalloc_to_page (addr); +if (unlikely (!page)) +return NULL ; + +return page->area; }/**
@@ -1536,6 +1539,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 ); }
@@ -1744,6 +1748,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, const void *caller) {struct vm_struct *area; +unsigned int page_counter;void *addr;unsigned long real_size = size;
@@ -1769,6 +1774,9 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,kmemleak_vmalloc (area, size, gfp_mask); +for (page_counter =0 ; page_counter < area->nr_pages; page_counter++) + area->pages[page_counter]->area = area; +return addr; fail:
Signature · 1 line
2.9.3
PATCH v11 4/6 Protectable Memory
In reply to Igor StoppaThe MMU available in many systems running Linux can often provide R/O protection to the memory pages it handles.
However, the MMU-based protection works efficiently only when said pages contain exclusively data that will not need further modifications.
Statically allocated variables can be segregated into a dedicated section, but this does not sit very well with dynamically allocated ones.
Dynamic allocation does not provide, currently, any means for grouping variables in memory pages that would contain exclusively data suitable for conversion to read only access mode.
The allocator here provided (pmalloc - protectable memory allocator) introduces the concept of pools of protectable memory.
A module can request a pool and then refer any allocation request to the pool handler it has received.
Once all the chunks of memory associated to a specific pool are initialized, the pool can be protected.
After this point, the pool can only be destroyed (it is up to the module to avoid any further references to the memory from the pool, after the destruction is invoked).
The latter case is mainly meant for releasing memory, when a module is unloaded.
A module can have as many pools as needed, for example to support the protection of data that is initialized in sufficiently distinct phases.
--- include/linux/genalloc.h | 3 + include/linux/pmalloc.h | 215 ++++++++++++++++++++ include/linux/vmalloc.h | 1 + lib/genalloc.c | 27 +++ mm/Makefile | 1 + mm/pmalloc.c | 513 +++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 ++- 7 files changed, 781 insertions(+), 4 deletions(-) create mode 100644 include/linux/pmalloc.h create mode 100644 mm/pmalloc.c
include/linux/genalloc.h +3 -0
@@ -121,6 +121,9 @@ extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,extern void *gen_pool_dma_alloc (struct gen_pool *pool,size_t size,dma_addr_t *dma);extern void gen_pool_free (struct gen_pool *,unsigned long ,size_t ); + +extern void gen_pool_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk);extern void gen_pool_for_each_chunk (struct gen_pool *,void (*)(struct gen_pool *,struct gen_pool_chunk *,void *),void *);extern size_t gen_pool_avail (struct gen_pool *);
include/linux/pmalloc.h new file +215 -0
215 lines not on this page · raw message
include/linux/vmalloc.h +1 -0
@@ -20,6 +20,7 @@ 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 */ /* bits [20..32] reserved for arch specific ioremap internals */ /*
lib/genalloc.c +27 -0
@@ -519,6 +519,33 @@ void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size) }EXPORT_SYMBOL (gen_pool_free); + +/** +* gen_pool_flush_chunk - drops all the allocations from a specific chunk +* @pool: the generic memory pool +* @chunk: The chunk to wipe clear. +* +* This is meant to be called only while destroying a pool. It's up to the +* caller to avoid races, but really, at this point the pool should have +* already been retired and have become unavailable for any other sort of +* operation. +*/ +void gen_pool_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk) +{ +size_t size; + +if (unlikely (!(pool && chunk))) +return ; + + size = chunk->end_addr +1 - chunk->start_addr; +memset (chunk->entries,0 , +DIV_ROUND_UP (size >> pool->min_alloc_order *BITS_PER_ENTRY , +BITS_PER_BYTE )); +atomic_set (&chunk->avail, size); +} + +/** * gen_pool_for_each_chunk - call func for every chunk of generic memory pool * @pool: the generic memory pool
mm/Makefile +1 -0
@@ -65,6 +65,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_ARCH_HAS_SET_MEMORY) += pmalloc.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/pmalloc.c new file +513 -0
513 lines not on this page · raw message
mm/usercopy.c +21 -4
@@ -15,6 +15,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/mm.h> +#include <linux/pmalloc.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/sched/task.h>
@@ -222,6 +223,7 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n,void __check_object_size (const void *ptr,unsigned long n,bool to_user) {const char *err; +int retv;/* Skip all tests if size is zero. */ if (!n)
@@ -229,12 +231,12 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for invalid addresses. */ err =check_bogus_address (ptr, n); -if (err) +if (unlikely (err)) goto report;/* Check for bad heap object. */ err =check_heap_object (ptr, n, to_user); -if (err) +if (unlikely (err)) goto report;/* Check for bad stack object. */
@@ -257,8 +259,23 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ err =check_kernel_text_object (ptr, n); -if (!err) -return ; +if (unlikely (err)) + goto report; + +/* Check if object is from a pmalloc chunk. +*/ + retv =is_pmalloc_object (ptr, n); +if (unlikely (retv)) { +if (unlikely (!to_user)) { + err ="<trying to write to pmalloc object>" ; + goto report; + } +if (retv <0 ) { + err ="<invalid pmalloc object>" ; + goto report; + } + } +return ; report:report_usercopy (ptr, n, to_user, err);
Signature · 1 line
2.9.3
PATCH v11 5/6 Documentation for Pmalloc
In reply to Igor StoppaDetailed documentation about the protectable memory allocator.
--- Documentation/core-api/pmalloc.txt | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/core-api/pmalloc.txt
Documentation/core-api/pmalloc.txt new file +104 -0
@@ -0,0 +1,104 @@ +============================ +Protectable memory allocator +============================ + +Introduction +------------ + +When trying to perform an attack toward a system, the attacker typically +wants to alter the execution flow, in a way that allows actions which +would otherwise be forbidden. + +In recent years there has been lots of effort in preventing the execution +of arbitrary code, so the attacker is progressively pushed to look for +alternatives. + +If code changes are either detected or even prevented, what is left is to +alter kernel data. + +As countermeasure, constant data is collected in a section which is then +marked as readonly. +To expand on this, also statically allocated variables which are tagged +as __ro_after_init will receive a similar treatment. +The difference from constant data is that such variables can be still +altered freely during the kernel init phase. + +However, such solution does not address those variables which could be +treated essentially as read-only, but whose size is not known at compile +time or cannot be fully initialized during the init phase. + + +Design +------ + +pmalloc builds on top of genalloc, using the same concept of memory pools +A pool is a handle to a group of chunks of memory of various sizes. +When created, a pool is empty. It will be populated by allocating chunks +of memory, either when the first memory allocation request is received, or +when a pre-allocation is performed. + +Either way, one or more memory pages will be obtaiend from vmalloc and +registered in the pool as chunk. Subsequent requests will be satisfied by +either using any available free space from the current chunks, or by +allocating more vmalloc pages, should the current free space not suffice. + +This is the key point of pmalloc: it groups data that must be protected +into a set of pages. The protection is performed through the mmu, which +is a prerequisite and has a minimum granularity of one page. + +If the relevant variables were not grouped, there would be a problem of +allowing writes to other variables that might happen to share the same +page, but require further alterations over time. + +A pool is a group of pages that are write protected at the same time. +Ideally, they have some high level correlation (ex: they belong to the +same module), which justifies write protecting them all together. + +To keep it to a minimum, locking is left to the user of the API, in +those cases where it's not strictly needed. +Ideally, no further locking is required, since each module can have own +pool (or pools), which should, for example, avoid the need for cross +module or cross thread synchronization about write protecting a pool. + +The overhead of creating an additional pool is minimal: a handful of bytes +from kmalloc space for the metadata and then what is left unused from the +page(s) registered as chunks. + +Compared to plain use of vmalloc, genalloc has the advantage of tightly +packing the allocations, reducing the number of pages used and therefore +the pressure on the TLB. The slight overhead in execution time of the +allocation should be mostly irrelevant, because pmalloc memory is not +meant to be allocated/freed in tight loops. Rather it ought to be taken +in use, initialized and write protected. Possibly destroyed. + +Considering that not much data is supposed to be dynamically allocated +and then marked as read-only, it shouldn't be an issue that the address +range for pmalloc is limited, on 32-bit systemd. + +Regarding SMP systems, the allocations are expected to happen mostly +during an initial transient, after which there should be no more need to +perform cross-processor synchronizations of page tables. + + +Use +--- + +The typical sequence, when using pmalloc, is: + +1. create a pool +2. [optional] pre-allocate some memory in the pool +3. issue one or more allocation requests to the pool +4. initialize the memory obtained + - iterate over points 3 & 4 as needed - +5. write protect the pool +6. use in read-only mode the handlers obtained throguh the allocations +7. [optional] destroy the pool + + +In a scenario where, for example due to some error, part or all of the +allocations performed at point 3 must be reverted, it is possible to free +them, as long as point 5 has not been executed, and the pool is still +modifiable. Such freed memory can be re-used. +Performing a free operation on a write-protected pool will, instead, +simply release the corresponding memory from the accounting, but it will +be still impossible to alter its content.
Signature · 1 line
2.9.3
PATCH v11 6/6 Pmalloc: self-test
In reply to Igor StoppaAdd basic self-test functionality for pmalloc.
--- mm/Kconfig | 7 ++++++ mm/Makefile | 1 + mm/pmalloc-selftest.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/pmalloc-selftest.h | 30 ++++++++++++++++++++++++ mm/pmalloc.c | 3 +++ 5 files changed, 106 insertions(+) create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h
mm/Kconfig +7 -0
@@ -760,3 +760,10 @@ config GUP_BENCHMARK performance of get_user_pages_fast(). See tools/testing/selftests/vm/gup_benchmark.c + +config PROTECTABLE_MEMORY_SELFTEST + bool "Run self test for pmalloc memory allocator" + default n + help + Tries to verify that pmalloc works correctly and that the memory + is effectively protected.
mm/Makefile +1 -0
@@ -66,6 +66,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_ARCH_HAS_SET_MEMORY) += pmalloc.o +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/pmalloc-selftest.c new file +65 -0
65 lines not on this page · raw message
mm/pmalloc-selftest.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* pmalloc-selftest.h +* +* (C) Copyright 2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + + +#ifndef __PMALLOC_SELFTEST_H__ +#define __PMALLOC_SELFTEST_H__ + + +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST + +#include <linux/pmalloc.h> + +void pmalloc_selftest (void ); + +#else + +static inline void pmalloc_selftest (void ){}; + +#endif + +#endif
mm/pmalloc.c +3 -0
@@ -25,6 +25,8 @@#include <asm/cacheflush.h> #include <asm/page.h> +#include "pmalloc-selftest.h" +/** * pmalloc_data contains the data specific to a pmalloc pool, * in a format compatible with the design of gen_alloc.
@@ -508,6 +510,7 @@ static int __init pmalloc_late_init(void) } }mutex_unlock (&pmalloc_mutex); +pmalloc_selftest ();return 0 ; }late_initcall (pmalloc_late_init);
Signature · 1 line
2.9.3
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Igor StoppaQuoted 14 lines · Igor Stoppa
The MMU available in many systems running Linux can often provide R/O protection to the memory pages it handles.
However, the MMU-based protection works efficiently only when said pages contain exclusively data that will not need further modifications.
Statically allocated variables can be segregated into a dedicated section, but this does not sit very well with dynamically allocated ones.
Dynamic allocation does not provide, currently, any means for grouping variables in memory pages that would contain exclusively data suitable for conversion to read only access mode.
Quoted 8 lines
The allocator here provided (pmalloc - protectable memory allocator) introduces the concept of pools of protectable memory.
A module can request a pool and then refer any allocation request to the pool handler it has received.
Once all the chunks of memory associated to a specific pool are initialized, the pool can be protected.
I'm not entirely convinced by the approach of marking small parts of kernel memory as readonly for hardening.
Comments on some details are inline.
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */ · line 23
Quoted 9 lines
include/linux/vmalloc.h +1
-0@@ -20,6 +20,7 @@ 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 */
Is "see docs" specific enough to actually guide the reader to the right documentation?
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */
Quoted 7 lines
include/linux/vmalloc.h +7
-0+#define pmalloc_attr_init (data, attr_name) \ +do { \ + sysfs_attr_init(&data->attr_##attr_name.attr); \ + data->attr_##attr_name.attr.name = #attr_name; \ + data->attr_##attr_name.attr.mode = VERIFY_OCTAL_PERMISSIONS(0444); \ + data->attr_##attr_name.show = pmalloc_pool_show_##attr_name; \ +} while (0)
Is there a good reason for making all these files mode 0444 (as opposed to setting them to 0400 and then allowing userspace to make them accessible if desired)? /proc/slabinfo contains vaguely similar data and is mode 0400 (or mode 0600, depending on the kernel config) AFAICS.
Quoted 2 lines
include/linux/vmalloc.h +2
-0+void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{
[...]
include/linux/vmalloc.h
Quoted 3 lines
include/linux/vmalloc.h +3
-0+/* Expand pool */ + chunk_size =roundup (size,PAGE_SIZE ); + chunk =vmalloc (chunk_size);
You're allocating with vmalloc(), which, as far as I know, establishes a second mapping in the vmalloc area for pages that are already mapped as RW through the physmap. AFAICS, later, when you're trying to make pages readonly, you're only changing the protections on the second mapping in the vmalloc area, therefore leaving the memory writable through the physmap. Is that correct? If so, please either document the reasoning why this is okay or change it.
[kernel-hardening] Re: [PATCH 5/6] Documentation for Pmalloc
In reply to Igor Stoppa2 Minor typos inline below:
Quoted 44 lines · Igor Stoppa
Detailed documentation about the protectable memory allocator.
Signed-off-by Igor Stoppa <[email protected]>--- Documentation/core-api/pmalloc.txt | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+)create mode 100644
Documentation/core-api/pmalloc.txtDocumentation/core-api/pmalloc.txt new file +30
-0@@ -0,0 +1,104 @@ +============================ +Protectable memory allocator +============================ + +Introduction +------------ + +When trying to perform an attack toward a system, the attacker typically +wants to alter the execution flow, in a way that allows actions which +would otherwise be forbidden. + +In recent years there has been lots of effort in preventing the execution +of arbitrary code, so the attacker is progressively pushed to look for +alternatives. + +If code changes are either detected or even prevented, what is left is to +alter kernel data. + +As countermeasure, constant data is collected in a section which is then +marked as readonly. +To expand on this, also statically allocated variables which are tagged +as __ro_after_init will receive a similar treatment. +The difference from constant data is that such variables can be still +altered freely during the kernel init phase. + +However, such solution does not address those variables which could be +treated essentially as read-only, but whose size is not known at compile +time or cannot be fully initialized during the init phase. + +
Documentation/core-api/pmalloc.txt line 40
Quoted 10 lines
Documentation/core-api/pmalloc.txt new file +10
-0+Design +------ + +pmalloc builds on top of genalloc, using the same concept of memory pools +A pool is a handle to a group of chunks of memory of various sizes. +When created, a pool is empty. It will be populated by allocating chunks +of memory, either when the first memory allocation request is received, or +when a pre-allocation is performed. + +Either way, one or more memory pages will be obtaiend from vmalloc and
obtained
Quoted 44 lines
Documentation/core-api/pmalloc.txt new file +44
-0+registered in the pool as chunk. Subsequent requests will be satisfied by +either using any available free space from the current chunks, or by +allocating more vmalloc pages, should the current free space not suffice. + +This is the key point of pmalloc: it groups data that must be protected +into a set of pages. The protection is performed through the mmu, which +is a prerequisite and has a minimum granularity of one page. + +If the relevant variables were not grouped, there would be a problem of +allowing writes to other variables that might happen to share the same +page, but require further alterations over time. + +A pool is a group of pages that are write protected at the same time. +Ideally, they have some high level correlation (ex: they belong to the +same module), which justifies write protecting them all together. + +To keep it to a minimum, locking is left to the user of the API, in +those cases where it's not strictly needed. +Ideally, no further locking is required, since each module can have own +pool (or pools), which should, for example, avoid the need for cross +module or cross thread synchronization about write protecting a pool. + +The overhead of creating an additional pool is minimal: a handful of bytes +from kmalloc space for the metadata and then what is left unused from the +page(s) registered as chunks. + +Compared to plain use of vmalloc, genalloc has the advantage of tightly +packing the allocations, reducing the number of pages used and therefore +the pressure on the TLB. The slight overhead in execution time of the +allocation should be mostly irrelevant, because pmalloc memory is not +meant to be allocated/freed in tight loops. Rather it ought to be taken +in use, initialized and write protected. Possibly destroyed. + +Considering that not much data is supposed to be dynamically allocated +and then marked as read-only, it shouldn't be an issue that the address +range for pmalloc is limited, on 32-bit systemd. + +Regarding SMP systems, the allocations are expected to happen mostly +during an initial transient, after which there should be no more need to +perform cross-processor synchronizations of page tables. + + +Use +---
Documentation/core-api/pmalloc.txt line 94
Quoted 10 lines
Documentation/core-api/pmalloc.txt new file +10
-0+ +The typical sequence, when using pmalloc, is: + +1. create a pool +2. [optional] pre-allocate some memory in the pool +3. issue one or more allocation requests to the pool +4. initialize the memory obtained + - iterate over points 3 & 4 as needed - +5. write protect the pool +6. use in read-only mode the handlers obtained throguh the allocations
through
Quoted 11 lines
Documentation/core-api/pmalloc.txt new file +10
-0+7. [optional] destroy the pool + + +In a scenario where, for example due to some error, part or all of the +allocations performed at point 3 must be reverted, it is possible to free +them, as long as point 5 has not been executed, and the pool is still +modifiable. Such freed memory can be re-used. +Performing a free operation on a write-protected pool will, instead, +simply release the corresponding memory from the accounting, but it will +be still impossible to alter its content.
[kernel-hardening] Re: [PATCH 5/6] Documentation for Pmalloc
Quoted 1 line · Ralph Campbell
2 Minor typos inline below:
thanks for proof-reading, will fix accordingly.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Jann HornHi,
thanks for the review. My reply below.
Quoted 2 lines · Jann Horn
I'm not entirely convinced by the approach of marking small parts of kernel memory as readonly for hardening.
Because of the physmap you mention later?
Regarding small parts vs big parts (what is big enough?) I did propose the use of a custom zone at the very beginning, however I met 2 objections:
1. It's not a special case and there was no will to reserve another zone This might be mitigated by aliasing with a zone that is already defined, but not in use. For example DMA or DMA32.
But it looks like a good way to replicate the confusion that is page struct. Anyway, I found the next objection more convincing.
2. What would be the size of this zone? It would become something that is really application specific. At the very least it should become a command line parameter. A distro would have to allocate a lot of memory for it, because it cannot really know upfront what its users will do. But, most likely, the vast majority of users would never need that much.
If you have some idea of how to address these objections without using vmalloc, or at least without using the same page provider that vmalloc is using now, I'd be interested to hear it.
Besides the double mapping problem, the major benefit I can see from having a contiguous area is that it simplifies the hardened user copy verification, because there is a fixed range to test for overlap.
Quoted 1 line
Comments on some details are inline.
thank you
Quoted 4 lines
Quoted 4 lines
include/linux/vmalloc.h +0
-0
Quoted 8 lines
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */ · line 23
Quoted 5 lines
include/linux/vmalloc.h +1
-0@@ -20,6 +20,7 @@ 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 */ Is "see docs" specific enough to actually guide the reader to the right documentation?
The doc file is named pmalloc.txt, but I can be more explicit.
Quoted 5 lines
Quoted 5 lines
include/linux/vmalloc.h +5
-0+#define pmalloc_attr_init (data, attr_name) \ +do { \ + sysfs_attr_init(&data->attr_##attr_name.attr); \ + data->attr_##attr_name.attr.name = #attr_name; \ + data->attr_##attr_name.attr.mode = VERIFY_OCTAL_PERMISSIONS(0444); \
Quoted 8 lines
include/linux/vmalloc.h
Quoted 2 lines
include/linux/vmalloc.h +2
-0+ data->attr_##attr_name.show = pmalloc_pool_show_##attr_name; \ +} while (0)Is there a good reason for making all these files mode 0444 (as opposed to setting them to 0400 and then allowing userspace to make them accessible if desired)?
/proc/slabinfocontains vaguely similar data and is mode 0400 (or mode 0600, depending on the kernel config) AFAICS.
ok, you do have a point, so far I have been mostly focusing on the
"drop-in replacement for kmalloc" aspect.
Quoted 7 lines
Quoted 2 lines
include/linux/vmalloc.h +2
-0+void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{[...]
include/linux/vmalloc.h
Quoted 3 lines
include/linux/vmalloc.h +3
-0+/* Expand pool */ + chunk_size =roundup (size,PAGE_SIZE ); + chunk =vmalloc (chunk_size);
Quoted 7 lines
You're allocating with
vmalloc(), which, as far as I know, establishes a second mapping in the vmalloc area for pages that are already mapped as RW through the physmap. AFAICS, later, when you're trying to make pages readonly, you're only changing the protections on the second mapping in the vmalloc area, therefore leaving the memory writable through the physmap. Is that correct? If so, please either document the reasoning why this is okay or change it.
About why vmalloc as backend for pmalloc, please refer to this:
http://www.openwall.com/lists/kernel-hardening/2018/01/24/11
I tried to give a short summary of what took me toward vmalloc. vmalloc is also a convenient way of obtaining arbitrarily (within reason) large amounts of virtually contiguous memory.
Your objection is toward the unprotected access, through the alternate mapping, rather than to the idea of having pools that can be protected individually, right?
In the mail I linked, I explained that I could not use kmalloc because of the problem of splitting huge pages on ARM.
kmalloc does require the physmap, for performance reason.
However, vmalloc is already doing mapping of individual pages, because it must ensure that they are virtually contiguous, so would it be possible to have vmalloc _always_ outside of the physmap?
If I have understood correctly, the actual extension of physmap is highly architecture and platform dependant, so it might be (but I have not checked) that in some cases (like some 32bit systems) vmalloc is typically outside of physmap, but probably that is not the case on 64bit?
Also, I need to understand how physmap works against vmalloc vs how it works against kernel text and const/__ro_after_init sections.
Can they also be accessed (and written?) through the physmap?
But, to take a different angle: if an attacker knows where kernel symbols are and has gained capability to write at arbitrary location(s) in kernel data, what prevents a modification of mappings and permissions?
What is considered robust enough?
I have the impression that, without support from HW, to have some one-way mechanism that protects some page permanently, it's always possible to undo the various protections we are talking about, only harder.
Quoted 1 line
From the perspective of protecting against accidental overwrites,
instead, the current implementation should be ok, since it's less likely that some stray pointer happens to assume a value that goes through the physmap.
But I'm interested to hear, if you have some suggestion about how to prevent the side access through the physmap.
--
thanks, igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 134 lines · Igor Stoppa
Hi,
thanks for the review. My reply below.
Quoted 2 lines · Jann Horn
I'm not entirely convinced by the approach of marking small parts of kernel memory as readonly for hardening.
Because of the physmap you mention later?
Regarding small parts vs big parts (what is big enough?) I did propose the use of a custom zone at the very beginning, however I met 2 objections:
1. It's not a special case and there was no will to reserve another zone This might be mitigated by aliasing with a zone that is already defined, but not in use. For example DMA or DMA32.
But it looks like a good way to replicate the confusion that is page struct. Anyway, I found the next objection more convincing.2. What would be the size of this zone? It would become something that is really application specific. At the very least it should become a command line parameter. A distro would have to allocate a lot of memory for it, because it cannot really know upfront what its users will do. But, most likely, the vast majority of users would never need that much.
If you have some idea of how to address these objections without using vmalloc, or at least without using the same page provider that vmalloc is using now, I'd be interested to hear it.
Besides the double mapping problem, the major benefit I can see from having a contiguous area is that it simplifies the hardened user copy verification, because there is a fixed range to test for overlap.
Quoted 1 line
Comments on some details are inline.
thank you
Quoted 4 lines
Quoted 4 lines
include/linux/vmalloc.h +0
-0Quoted 8 lines
include/linux/vmalloc.h struct notifier_block; /* in notifier.h */ · line 23
Quoted 5 lines
include/linux/vmalloc.h +1
-0@@ -20,6 +20,7 @@ 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 */ Is "see docs" specific enough to actually guide the reader to the right documentation?
The doc file is named pmalloc.txt, but I can be more explicit.
Quoted 5 lines
Quoted 5 lines
include/linux/vmalloc.h +5
-0+#define pmalloc_attr_init (data, attr_name) \ +do { \ + sysfs_attr_init(&data->attr_##attr_name.attr); \ + data->attr_##attr_name.attr.name = #attr_name; \ + data->attr_##attr_name.attr.mode = VERIFY_OCTAL_PERMISSIONS(0444); \Quoted 8 lines
include/linux/vmalloc.h
Quoted 2 lines
include/linux/vmalloc.h +2
-0+ data->attr_##attr_name.show = pmalloc_pool_show_##attr_name; \ +} while (0)Is there a good reason for making all these files mode 0444 (as opposed to setting them to 0400 and then allowing userspace to make them accessible if desired)?
/proc/slabinfocontains vaguely similar data and is mode 0400 (or mode 0600, depending on the kernel config) AFAICS.ok, you do have a point, so far I have been mostly focusing on the
"drop-in replacement for kmalloc" aspect.
Quoted 7 lines
Quoted 2 lines
include/linux/vmalloc.h +2
-0+void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{[...]
include/linux/vmalloc.h
Quoted 3 lines
include/linux/vmalloc.h +3
-0+/* Expand pool */ + chunk_size =roundup (size,PAGE_SIZE ); + chunk =vmalloc (chunk_size);Quoted 7 lines
You're allocating with
vmalloc(), which, as far as I know, establishes a second mapping in the vmalloc area for pages that are already mapped as RW through the physmap. AFAICS, later, when you're trying to make pages readonly, you're only changing the protections on the second mapping in the vmalloc area, therefore leaving the memory writable through the physmap. Is that correct? If so, please either document the reasoning why this is okay or change it.About why vmalloc as backend for pmalloc, please refer to this:
http://www.openwall.com/lists/kernel-hardening/2018/01/24/11
I tried to give a short summary of what took me toward vmalloc. vmalloc is also a convenient way of obtaining arbitrarily (within reason) large amounts of virtually contiguous memory.
Your objection is toward the unprotected access, through the alternate mapping, rather than to the idea of having pools that can be protected individually, right?
In the mail I linked, I explained that I could not use kmalloc because of the problem of splitting huge pages on ARM.
kmalloc does require the physmap, for performance reason.
However, vmalloc is already doing mapping of individual pages, because it must ensure that they are virtually contiguous, so would it be possible to have vmalloc _always_ outside of the physmap?
If I have understood correctly, the actual extension of physmap is highly architecture and platform dependant, so it might be (but I have not checked) that in some cases (like some 32bit systems) vmalloc is typically outside of physmap, but probably that is not the case on 64bit?
Also, I need to understand how physmap works against vmalloc vs how it works against kernel text and const/__ro_after_init sections.
Can they also be accessed (and written?) through the physmap?
But, to take a different angle: if an attacker knows where kernel symbols are and has gained capability to write at arbitrary location(s) in kernel data, what prevents a modification of mappings and permissions?
What is considered robust enough?
I have the impression that, without support from HW, to have some one-way mechanism that protects some page permanently, it's always possible to undo the various protections we are talking about, only harder.
From the perspective of protecting against accidental overwrites, instead, the current implementation should be ok, since it's less likely that some stray pointer happens to assume a value that goes through the physmap.
Quoted 5 lines
But I'm interested to hear, if you have some suggestion about how to prevent the side access through the physmap.
--
thanks, igor
DMA/physmap access coupled with a knowledge of which virtual mappings are in the physical space should be enough for an attacker to bypass the gating mechanism this work imposes. Not trivial, but not impossible. Since there's no way to prevent that sort of access in current hardware (especially something like a NIC or GPU working independently of the CPU altogether), we have the option of checking contents of a sealed page against a checksum/hash of the page prior to returning its contents to the caller (since it needs to be read to be verified), or some other mechanism within the read path to ensure that no event since the last read affected the page/allocation. If the structure containing the list of verifiers is separate from the page, the attacker needs to resolve and change the contents of those signatures for the pages they're affecting via DMA before the kernel checks one against the other in the read path. I cant speak to overhead, but it should complicate the logic of a successful attack chain.
Off the cuff, if the allocator sums the contents when sealing a page, stores it in a lookup table, and forces verification on every read/lookup, it should prevent _use_ of memory which was modified unless our attacker is clever enough to fix that up prior to the next access. Since its write-once memory, race conditions on subsequent access shouldn't be a problem.
Signature · 3 lines
Boris Lukashev Systems Architect Semper Victus
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 1 line · Boris Lukashev
On Thu, Jan 25, 2018 at 6:59 AM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
DMA/physmap access coupled with a knowledge of which virtual mappings are in the physical space should be enough for an attacker to bypass the gating mechanism this work imposes. Not trivial, but not impossible. Since there's no way to prevent that sort of access in current hardware (especially something like a NIC or GPU working independently of the CPU altogether)
I am not saying this is impossible but this is unlikely they are several mecanisms. First you have IOMMU it has been defaulted to on by OEM for last few years (it use to be enabled only on server for virtualization).
Which means that a given device only can access memory that is mapped to it through the IOMMU page table (usualy each device get their own distinct IOMMU page table).
Then on device like GPU you have an MMU (no GPU without an MMU for the last 10 years or more). The MMU is under the control of the kernel driver of the GPU and for the open source driver we try hard to make sure it can not be abuse and circumvent by userspace ie we restrict userspace process to only access memory they own.
I am not saying that this can not happen but that we are trying our best to avoid it.
Cheers,
Jérôme
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Jann HornQuoted 2 lines · Jann Horn
I'm not entirely convinced by the approach of marking small parts of kernel memory as readonly for hardening.
It depends how significant the data stored in there are. For example, storing function pointers in read-only memory provides significant hardening.
Quoted 7 lines
You're allocating with
vmalloc(), which, as far as I know, establishes a second mapping in the vmalloc area for pages that are already mapped as RW through the physmap. AFAICS, later, when you're trying to make pages readonly, you're only changing the protections on the second mapping in the vmalloc area, therefore leaving the memory writable through the physmap. Is that correct? If so, please either document the reasoning why this is okay or change it.
Yes, this is still vulnerable to attacks through the physmap. That's also true for marking structs as const. We should probably fix that at some point, but at least they're not vulnerable to heap overruns by small amounts ... you have to be able to overrun some other array by terabytes.
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API. We can have a separate discussion about an API to remove pages from the physmap.
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 16 lines · Matthew Wilcox
Quoted 2 lines · Jann Horn
I'm not entirely convinced by the approach of marking small parts of kernel memory as readonly for hardening.
It depends how significant the data stored in there are. For example, storing function pointers in read-only memory provides significant hardening.
Quoted 7 lines
You're allocating with
vmalloc(), which, as far as I know, establishes a second mapping in the vmalloc area for pages that are already mapped as RW through the physmap. AFAICS, later, when you're trying to make pages readonly, you're only changing the protections on the second mapping in the vmalloc area, therefore leaving the memory writable through the physmap. Is that correct? If so, please either document the reasoning why this is okay or change it.
Quoted 4 lines
Yes, this is still vulnerable to attacks through the physmap. That's also true for marking structs as const. We should probably fix that at some point, but at least they're not vulnerable to heap overruns by small amounts ... you have to be able to overrun some other array by terabytes.
Actually, I think there is something to say in favor of using a vmalloc based approach, precisely because of the physmap :-P
If I understood correctly, the physmap is primarily meant to speed up access to physical memory through the TLB. In particular, for kmalloc based allocations.
Which means that, to perform a physmap-based attack to a kmalloced allocation, one needs to know:
- the address of the target variable in the kmalloc range
- the randomized offset of the kernel
- the location of the physmap
But, for a vmalloc based allocation, there is one extra hoop: since the mapping is really per page, now the attacker has actually to walk the page table, to figure out where to poke in the physmap.
One more thought about physmap: does it map also code? Because, if it does, and one wants to use it for an attack, isn't it easier to look for some security test and replace a bne with be or equivalent?
Quoted 2 lines
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API.
pmalloc is meant to be useful where the attack surface is made up of lots of small allocations - my first use case was the SE Linux policy DB, where there is a variety of elements being allocated, in large amount. To the point where having ready made caches would be wasteful.
Then there is the issue I already mentioned about arm/arm64 which would require to break down large mappings, which seems to be against current policy, as described in my previous mail:
http://www.openwall.com/lists/kernel-hardening/2018/01/24/11
I do not know exactly what you have in mind wrt slab, but my impression is that it will most likely gravitate toward the pmalloc implementation. It will need:
- "pools" or anyway some means to lock only a certain group of pages, related to a specific kernel user
- (mostly) lockless allocation
- a way to manage granularity (or order of allocation)
Most of this is already provided by genalloc, which is what I ended up almost re-implementing, before being pointed to it :-)
I only had to add the tracking of end of allocations, which is what the patch 1/6 does - as side note, is anybody maintaining it?
I could not find an entry in MAINTAINERS
As I mentioned above, using vmalloc adds even an extra layer of protection.
The major downside is the increased TLB use, however this is not so relevant for the volumes of data that I had to deal with so far:
only few 4K pages.
But you might have in mind something else.
I'd be interested to know what and what would be an obstacle in using pmalloc. Maybe it can be solved.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Jerome GlisseQuoted 11 lines · Jerome Glisse
Quoted 1 line · Boris Lukashev
On Thu, Jan 25, 2018 at 6:59 AM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
DMA/physmap access coupled with a knowledge of which virtual mappings are in the physical space should be enough for an attacker to bypass the gating mechanism this work imposes. Not trivial, but not impossible. Since there's no way to prevent that sort of access in current hardware (especially something like a NIC or GPU working independently of the CPU altogether)
[...]
Quoted 2 lines
I am not saying that this can not happen but that we are trying our best to avoid it.
How about an opt-in verification, similar to what proposed by Boris Lukashev?
When reading back the data, one could access the pointer directly and bypass the verification, or could use a function that explicitly checks the integrity of the data.
Starting from an unprotected kmalloc allocation, even just turning the data into R/O is an improvement, but if one can afford the overhead of performing the verification, why not?
It would still be better if the service was provided by the library, instead than implemented by individual users, I think.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 21 lines · Igor Stoppa
Quoted 11 lines · Jerome Glisse
Quoted 1 line · Boris Lukashev
On Thu, Jan 25, 2018 at 6:59 AM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
DMA/physmap access coupled with a knowledge of which virtual mappings are in the physical space should be enough for an attacker to bypass the gating mechanism this work imposes. Not trivial, but not impossible. Since there's no way to prevent that sort of access in current hardware (especially something like a NIC or GPU working independently of the CPU altogether)
[...]
Quoted 2 lines
I am not saying that this can not happen but that we are trying our best to avoid it.
How about an opt-in verification, similar to what proposed by Boris Lukashev?
Quoted 8 lines
When reading back the data, one could access the pointer directly and bypass the verification, or could use a function that explicitly checks the integrity of the data.
Starting from an unprotected kmalloc allocation, even just turning the data into R/O is an improvement, but if one can afford the overhead of performing the verification, why not?
I like the idea of making the verification call optional for consumers allowing for fast/slow+hard paths depending on their needs. Cant see any additional vectors for abuse (other than the original ones effecting out-of-band modification) introduced by having verify/normal callers, but i've not had enough coffee yet. Any access races or things like that come to mind for anyone? Shouldn't happen with a write-once allocation, but again, lacking coffee.
Quoted 5 lines
It would still be better if the service was provided by the library, instead than implemented by individual users, I think.
--
igor
-Boris
[kernel-hardening] Re: [PATCH 4/6] Protectable Memory
In reply to Igor StoppaOn 24/01/18 19:56, Igor Stoppa wrote:
[...]
Quoted 2 lines
(quoted diff) +2
-0+bool pmalloc_prealloc(struct gen_pool *pool, size_t size) +{
[...]
Review
Quoted 2 lines
(quoted diff) +2
-0+abort: + vfree(chunk);
this should be vfree_atomic()
[...]
Quoted 2 lines
mm/pmalloc.c new file +2
-0+void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{
[...]
mm/pmalloc.c line 404
Quoted 2 lines
mm/pmalloc.c new file +2
-0+free: +vfree (chunk);
and this one too
I will fix them in the next iteration.
I am waiting to see if any more comments arrive. Otherwise, I'll send it out probably next Tuesday.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Boris LukashevQuoted 6 lines · Boris Lukashev
I like the idea of making the verification call optional for consumers allowing for fast/slow+hard paths depending on their needs. Cant see any additional vectors for abuse (other than the original ones effecting out-of-band modification) introduced by having verify/normal callers, but i've not had enough coffee yet. Any access races or things like that come to mind for anyone?
Well, the devil is in the details.
In this case, the question is how to perform the verification in a way that is sufficiently robust against races.
After thinking about it for a while, I doubt it can be done reliably. It might work for some small data types, but the typical use case I have found myself dealing with, is protecting data structures.
That also brings up a separate problem: what would be the size of data to hash? At one extreme there is a page, but it's probably too much, so what is the correct size? it cannot be smaller than a specific allocation, however that would imply looking for the hash related to the data being accessed, with extra overhead.
And the data being accessed might be a field in a struct, for which we would not have any hash.
There would be a hash only for the containing struct that was allocated ...
Overall, it seems a good idea in theory, but when I think about its implementation, it seems like the overhead is so big that it would discourage its use for almost any practical purpose.
If one really wants to be paranoid could, otoh have redundancy in a different pool.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Matthew WilcoxQuoted 3 lines · Matthew Wilcox
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API. We can have a separate discussion about an API to remove pages from the physmap.
We could even do this in a more thorough way. Can we use a ring 1 / 2 distinction to create a hardened OS core that policies the rest of the ever expanding kernel with all its modules and this and that feature?
I think that will long term be a better approach and allow more than the current hardening approaches can get you. It seems that we are willing to tolerate significant performance regressions now. So lets use the protection mechanisms that the hardware offers.
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
+Boris Lukashev
Quoted 9 lines · Christopher Lameter
Quoted 3 lines · Matthew Wilcox
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API. We can have a separate discussion about an API to remove pages from the physmap.
We could even do this in a more thorough way. Can we use a ring 1 / 2 distinction to create a hardened OS core that policies the rest of the ever expanding kernel with all its modules and this and that feature?
What would be the differentiating criteria? Furthermore, what are the chances
of invalidating the entire concept, because there is already an hypervisor using
the higher level features?
That is what you are proposing, if I understand correctly.
But more on this below ...
Quoted 4 lines
I think that will long term be a better approach and allow more than the current hardening approaches can get you. It seems that we are willing to tolerate significant performance regressions now. So lets use the protection mechanisms that the hardware offers.
I would rather *not* propose significant performance regression :-P
There might be some one-off case or anyway rare event which is penalized, but my preference goes to not introducing any significant performance penalty, during regular use.
After all, the lower the penalty, the wider the (potential) adoption.
More in detail: there are 2 major cases for wanting some form of read-only protection.
1) extra ward against accidental corruption
The kernel provides many debugging tools and they can detect lots of errors during development, but they require time and knowledge to use them, which are not always available.
Furthermore, it is objectively true that not all the code has the same level of maturity, especially when non-upstream code is used in some custom product. It's not my main goal, but it would be nice if that case too could be addressed by the protection.
Corruption *can* happen.
Having live guards against it, will definitely help spotting bugs or, at the very least, crash/reboot a device before it can cause permanent data corruption.
Protection against accidental corruption should be used as widely as possible, therefore it cannot have an high price tag, in terms of lost performance. Otherwise, there's the risk that it will be just a debug feature, more like lockdep or ubsan.
2) protection against malicious attacks
This is harder, of course, but what is realistically to be expected?
If an attacker can gain full control of the kernel, the only way to do damage control is to have HW and/or higher privilege SW that can somehow limit the reach of the attacker.
To make it work for real, it should be mandated that either these extra HW/SW means can tell apart legitimate kernel activity from rogue actions, or they operate so independently from the kernel that a compromise kernel cannot use any API to influence them.
The consensus seems to be to put aside (for now) this concern and instead focus on what is a typical scenario:
- some bug is found that allows to read/write kernel memory
- some other bug is found, which leaks the address of a well known variable, effectively revealing the randomized offset of each symbol placed in linear memory, once their relative location is known.
What is described above is a toolkit that effectively can allow - with patience - to attack anything that is writable by the kernel. Including page tables and permissions.
However the typical attack is more like: "let's flip some bit(s)". Which is where __ro_after_init has its purpose to exist.
My proposal is to extend the same sort of protection also to variables allocated dynamically.
* make the pages read only, once the data is initialized
* use vmalloc to prevent that exfiltrating the address of an unrelated variable can easily give away the location of the real target, because of the individual page mapping vs linear mapping.
Boris Lukashev proposed additional hardening, when accessing a certain variable, in the form of hash/checksum, but I could not come up with an implementation that did not have too much overhead.
Re-considering this, one option would be to have a function "pool_validate()" - probably expensive - that could be invoked by a piece of code before using the data from the pool.
Not perfect, because it would not be atomic, but it could be used once, at the beginning of a function, without adding overhead to each access to the pool that the function would perform.
An attacker would have to time the attack so that the corruption of the data wold happen after the pool is validated and before the data is read from it.
Possible, but way tricker than the current unprotected situation.
What I am trying to say, is that even after having multi-ring implementation (which would be more dependent on HW features), there would be still the problem of validating the legitimacy of the use of the API that such implementation would expose.
I'd rather try to preserve performance and still provide a defense against the more trivial attacks, since other types of attacks are much harder to perform in the wild.
Of course, I'm interested in alternatives (I'll comment separately on the compound pages)
The way pmalloc is designed is to take advantage of any page provider. So far, vmalloc seems to me the best option, but something else might emerge that works better.
Yet the pmalloc API is, I think, what would be still needed, to let the rest of the kernel take advantage of this feature.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 4 lines
Quoted 4 lines · Matthew Wilcox
Quoted 3 lines
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API.
I'd love to have some feedback specifically about the API.
I have also some idea about userspace and how to extend the pmalloc concept to it:
http://www.openwall.com/lists/kernel-hardening/2018/01/30/20
I'll be AFK intermittently for about 2 weeks, so i might not be able to reply immediately, but from my perspective this would be just the beginning of a broader hardening of both kernel and userspace that I'd like to pursue.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 11 lines · Igor Stoppa
Quoted 4 lines
Quoted 4 lines · Matthew Wilcox
Quoted 3 lines
It's worth having a discussion about whether we want the pmalloc API or whether we want a slab-based API.
I'd love to have some feedback specifically about the API.
I have also some idea about userspace and how to extend the pmalloc concept to it:
http://www.openwall.com/lists/kernel-hardening/2018/01/30/20
Quoted 7 lines
I'll be AFK intermittently for about 2 weeks, so i might not be able to reply immediately, but from my perspective this would be just the beginning of a broader hardening of both kernel and userspace that I'd like to pursue.
--
igor
Regarding the notion of validated protected memory, is there a method by which the resulting checksum could be used in a lookup table/function to resolve the location of the protected data? Effectively a hash table of protected allocations, with a benefit of dedup since any data matching the same key would be the same data (multiple identical cred structs being pushed around). Should leave the resolver address/csum in recent memory to check against, right?
Signature · 3 lines
Boris Lukashev Systems Architect Semper Victus
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 3 lines · Boris Lukashev
Regarding the notion of validated protected memory, is there a method by which the resulting checksum could be used in a lookup table/function to resolve the location of the protected data?
What I have in mind is a checksum at page/vmap_area level, so there would be no 1:1 mapping between a specific allocation and the checksum.
An extreme case would be the one where an allocation crosses one or more page boundaries, while the checksum refers to a (partially) overlapping memory area.
Code accessing a pool could perform one (relatively expensive) validation. But still something that would require a more sophisticated attack, to subvert.
Quoted 4 lines
Effectively a hash table of protected allocations, with a benefit of dedup since any data matching the same key would be the same data (multiple identical cred structs being pushed around). Should leave the resolver address/csum in recent memory to check against, right?
I see where you are trying to land, but I do not see how it would work without a further intermediate step.
pmalloc dishes out virtual memory addresses, when called.
It doesn't know what the user of the allocation will put in it. The user, otoh, has the direct address of the memory it got.
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
If I misunderstood, then I'd need a step by step description of what happens, because it's not clear to me how else the data would be accessed if not through the address that was obtained when pmalloc was invoked.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 32 lines · Igor Stoppa
Quoted 3 lines · Boris Lukashev
Regarding the notion of validated protected memory, is there a method by which the resulting checksum could be used in a lookup table/function to resolve the location of the protected data?
What I have in mind is a checksum at page/vmap_area level, so there would be no 1:1 mapping between a specific allocation and the checksum.
An extreme case would be the one where an allocation crosses one or more page boundaries, while the checksum refers to a (partially) overlapping memory area.
Code accessing a pool could perform one (relatively expensive) validation. But still something that would require a more sophisticated attack, to subvert.
Quoted 4 lines
Effectively a hash table of protected allocations, with a benefit of dedup since any data matching the same key would be the same data (multiple identical cred structs being pushed around). Should leave the resolver address/csum in recent memory to check against, right?
I see where you are trying to land, but I do not see how it would work without a further intermediate step.
pmalloc dishes out virtual memory addresses, when called.
It doesn't know what the user of the allocation will put in it. The user, otoh, has the direct address of the memory it got.
Quoted 7 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool which consumers would explicitly opt into. Say there's a maintenance cycle on a <name some scary thing controlled by Linux> and it wants to make sure that the instructions it read in are what they should have been before running them, those consumers might well take the penalty if it keeps <said scary big thing> from doing <the thing we're scared of it doing>. If such a resolver could be implemented in a manner which doesnt break all the things (including acceptable performance for at least a significant number of workloads), it might be useful as a general tool for handing out memory to userspace, even in rw, as it provides execution context in which other requirements can be forcibly resolved, preventing unauthorized access to pages the consumer shouldn't get in a very generic way. Spectre comes to mind as a potential class of issues to be addressed this way, since speculative load could be prevented if the resolution were to fail.
Quoted 8 lines
If I misunderstood, then I'd need a step by step description of what happens, because it's not clear to me how else the data would be accessed if not through the address that was obtained when pmalloc was invoked.
--
igor
Signature · 3 lines
Boris Lukashev Systems Architect Semper Victus
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Igor StoppaQuoted 10 lines · Igor Stoppa
Quoted 3 lines
We could even do this in a more thorough way. Can we use a ring 1 / 2 distinction to create a hardened OS core that policies the rest of the ever expanding kernel with all its modules and this and that feature?
What would be the differentiating criteria? Furthermore, what are the chances
of invalidating the entire concept, because there is already an hypervisor using
the higher level features?
That is what you are proposing, if I understand correctly.
Were there not 4 rings as well as methods by the processor vendors to virtualize them as well?
Quoted 6 lines
Quoted 4 lines
I think that will long term be a better approach and allow more than the current hardening approaches can get you. It seems that we are willing to tolerate significant performance regressions now. So lets use the protection mechanisms that the hardware offers.
I would rather *not* propose significant performance regression :-P
But we already have implemented significant kernel hardening which causes performance regressions. Using hardware capabilities allows the processor vendor to further optimize these mechanisms whereas the software preventative measures are eating up more and more performance as the pile them on. Plus these are methods that can be worked around. Restrictions implemented in a higher ring can be enforced and are much better than just "hardening" (which is making life difficult for the hackers and throwing away performannce for the average user).
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 13 lines · Christopher Lameter
Quoted 10 lines · Igor Stoppa
Quoted 3 lines
We could even do this in a more thorough way. Can we use a ring 1 / 2 distinction to create a hardened OS core that policies the rest of the ever expanding kernel with all its modules and this and that feature?
What would be the differentiating criteria? Furthermore, what are the chances
of invalidating the entire concept, because there is already an hypervisor using
the higher level features?
That is what you are proposing, if I understand correctly.
Quoted 2 lines
Were there not 4 rings as well as methods by the processor vendors to virtualize them as well?
I think you are talking x86, mostly.
On ARM there are ELx and they are often (typically?) already used. For x86 I cannot comment.
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
I think that will long term be a better approach and allow more than the current hardening approaches can get you. It seems that we are willing to tolerate significant performance regressions now. So lets use the protection mechanisms that the hardware offers.
I would rather *not* propose significant performance regression :-P
Quoted 8 lines
But we already have implemented significant kernel hardening which causes performance regressions. Using hardware capabilities allows the processor vendor to further optimize these mechanisms whereas the software preventative measures are eating up more and more performance as the pile them on. Plus these are methods that can be worked around. Restrictions implemented in a higher ring can be enforced and are much better than just "hardening" (which is making life difficult for the hackers and throwing away performannce for the average user).
What you are proposing requires major restructuring of the memory management - at the very least - provided that it doesn't cause the conflicts I mentioned above.
Even after you do that, the system will still be working with memory pages, there will be still a need to segregate data within certain pages, or pay the penalty of handling exceptions, when data with different permissions coexist within the same page.
The way the pmalloc API is designed is meant to facilitate the segregation and to actually improve performance, by grouping types of data with same scope and permission.
WRT the implementation, there is a minimal exposure to the memory provider, both for allocation and release.
Same goes for the protection mechanism.
It's a single call to the function which makes pages read only. It would be trivial to swap it out with a call to whatever framework you want to come up with, for implementing ring/EL based protection.
Quoted 1 line
From this perspective, you can easily provide patches that implement
what you are proposing, against pmalloc, if you really think that it's the way to go.
I'll be happy to use them, if they provide improved performance and same or better protection.
The way I designed pmalloc was really to be able to switch to some alternate memory provider and/or protection mechanism, should a better one arise.
But it can be done in a separate step, I think, since you are not proposing to just change pmalloc, you are proposing to re-design how the overall kernel memory hardening works (including executable pages, const data, __ro_after_init, etc.)
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Igor StoppaQuoted 17 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
Quoted 6 lines
ok, that seems more like an extension though.
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 37 lines · Laura Abbott
Quoted 36 lines · Kees Cook
Quoted 31 lines · Igor Stoppa
Quoted 3 lines · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
Quoted 6 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become
traps that get resolved through a lookup table that is built based on the content of each allocation.Quoted 8 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
Quoted 8 lines
Quoted 2 lines
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
What's our exposure on physmap for other regions? e.g. things that are executable, or made read-only later (like __ro_after_init)?
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 42 lines · Kees Cook
Quoted 41 lines · Laura Abbott
Quoted 38 lines · Kees Cook
Quoted 24 lines · Igor Stoppa
Quoted 3 lines · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
Quoted 6 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become
traps that get resolved through a lookup table that is built based on the content of each allocation.Quoted 8 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
Quoted 7 lines
ok, that seems more like an extension though.
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Quoted 7 lines
Quoted 4 lines
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
What's our exposure on physmap for other regions? e.g. things that are executable, or made read-only later (like
__ro_after_init)?
I just checked on a system with a 4.9 kernel, and there seems to be no physical memory that is mapped as writable in the init PGD and executable elsewhere.
Ah, I think I missed something. At least on X86, set_memory_ro, set_memory_rw, set_memory_nx and set_memory_x all use change_page_attr_clear/change_page_attr_set, which use change_page_attr_set_clr, which calls __change_page_attr_set_clr() with a second parameter "checkalias" that is set to 1 unless the bit being changed is the NX bit, and that parameter causes the invocation of cpa_process_alias(), which will, for mapped ranges, also change the attributes of physmap ranges. set_memory_ro() and so on are also used by the module loading code.
But in the ARM64 code, I don't see anything similar. Does anyone with a better understanding of ARM64 want to check whether I missed something? Or maybe, with a recent kernel, check whether executable module pages show up with a second writable mapping in the "kernel_page_tables" file in debugfs?
RE: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura Abbotthi,
apologies for (probably) breaking any email etiquette, but i'm travelling and i have available only the corporate mail client. I'll reply more extensively to all the comments i go next week, when i'm back to the office.
In the meanwhile i would like to point out that I had already addressed this, in past thread, but got no reply.
To recap:
-1) vmalloced memory is harder to attack than kmalloced, because it requires the attacker to figuere out also the physical address. Currently it's sufficient to identify the randomized base address and the offset in memory of the victim.
I have not seen comments about this statement I made. Is it incorrect?
-2) this patchset is about protecting something that right now is not protected at all. That should be the starting point for comparison. If it was possible to have separate section like const or _ro_after init, the situation would be different, but i was told that it's not possible. furthermore, it would require reserving a fixed size "zone", i think. -3)What is the attack we want to make harder to perform? Because even const data can be attacked, if we assume that the attacker can alter page mappings. In reality, the only safe way would be to have one-way only protection. But we do not have it. Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
I'm all for hardening what is possible, but I feel I do not have full understanding of some of the assumptions being made here.
Getting some answers to my questions above might help me seeing the point being made.
--
thanks, igor
--------------------------------------------------
Igor Stoppa Igor Stoppa
M:
E: [email protected]<mailto:[email protected]>
2012<tel:2012>实验室-赫尔辛基研究所
2012<tel:2012> Laboratories-Helsinki Research Center
From:Laura Abbott
To:Kees Cook,Igor Stoppa,
Cc:Boris Lukashev,Christopher Lameter,Matthew Wilcox,Jann Horn,Jerome Glisse,Michal Hocko,Christoph Hellwig,linux-security-module,Linux-MM,kernel list,Kernel Hardening, Date:2018-02-13 00:40:54
Subject:Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
RE: [PATCH 4/6] Protectable Memory
In reply to Laura Abbotthi,
apologies for (probably) breaking any email etiquette, but i'm travelling and i have available only the corporate mail client. I'll reply more extensively to all the comments i go next week, when i'm back to the office.
In the meanwhile i would like to point out that I had already addressed this, in past thread, but got no reply.
To recap:
-1) vmalloced memory is harder to attack than kmalloced, because it requires the attacker to figuere out also the physical address. Currently it's sufficient to identify the randomized base address and the offset in memory of the victim.
I have not seen comments about this statement I made. Is it incorrect?
-2) this patchset is about protecting something that right now is not protected at all. That should be the starting point for comparison. If it was possible to have separate section like const or _ro_after init, the situation would be different, but i was told that it's not possible. furthermore, it would require reserving a fixed size "zone", i think. -3)What is the attack we want to make harder to perform? Because even const data can be attacked, if we assume that the attacker can alter page mappings. In reality, the only safe way would be to have one-way only protection. But we do not have it. Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
I'm all for hardening what is possible, but I feel I do not have full understanding of some of the assumptions being made here.
Getting some answers to my questions above might help me seeing the point being made.
--
thanks, igor
--------------------------------------------------
Igor Stoppa Igor Stoppa
M:
E: [email protected]<mailto:[email protected]>
2012<tel:2012>实验室-赫尔辛基研究所
2012<tel:2012> Laboratories-Helsinki Research Center
From:Laura Abbott
To:Kees Cook,Igor Stoppa,
Cc:Boris Lukashev,Christopher Lameter,Matthew Wilcox,Jann Horn,Jerome Glisse,Michal Hocko,Christoph Hellwig,linux-security-module,Linux-MM,kernel list,Kernel Hardening, Date:2018-02-13 00:40:54
Subject:Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
RE: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura Abbotthi,
apologies for (probably) breaking any email etiquette, but i'm travelling and i have available only the corporate mail client. I'll reply more extensively to all the comments i go next week, when i'm back to the office.
In the meanwhile i would like to point out that I had already addressed this, in past thread, but got no reply.
To recap:
-1) vmalloced memory is harder to attack than kmalloced, because it requires the attacker to figuere out also the physical address. Currently it's sufficient to identify the randomized base address and the offset in memory of the victim.
I have not seen comments about this statement I made. Is it incorrect?
-2) this patchset is about protecting something that right now is not protected at all. That should be the starting point for comparison. If it was possible to have separate section like const or _ro_after init, the situation would be different, but i was told that it's not possible. furthermore, it would require reserving a fixed size "zone", i think. -3)What is the attack we want to make harder to perform? Because even const data can be attacked, if we assume that the attacker can alter page mappings. In reality, the only safe way would be to have one-way only protection. But we do not have it. Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
I'm all for hardening what is possible, but I feel I do not have full understanding of some of the assumptions being made here.
Getting some answers to my questions above might help me seeing the point being made.
--
thanks, igor
--------------------------------------------------
Igor Stoppa Igor Stoppa
M:
E: [email protected]<mailto:[email protected]>
2012<tel:2012>实验室-赫尔辛基研究所
2012<tel:2012> Laboratories-Helsinki Research Center
From:Laura Abbott
To:Kees Cook,Igor Stoppa,
Cc:Boris Lukashev,Christopher Lameter,Matthew Wilcox,Jann Horn,Jerome Glisse,Michal Hocko,Christoph Hellwig,linux-security-module,Linux-MM,kernel list,Kernel Hardening, Date:2018-02-13 00:40:54
Subject:Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
RE: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura Abbotthi,
apologies for (probably) breaking any email etiquette, but i'm travelling and i have available only the corporate mail client. I'll reply more extensively to all the comments i go next week, when i'm back to the office.
In the meanwhile i would like to point out that I had already addressed this, in past thread, but got no reply.
To recap:
-1) vmalloced memory is harder to attack than kmalloced, because it requires the attacker to figuere out also the physical address. Currently it's sufficient to identify the randomized base address and the offset in memory of the victim.
I have not seen comments about this statement I made. Is it incorrect?
-2) this patchset is about protecting something that right now is not protected at all. That should be the starting point for comparison. If it was possible to have separate section like const or _ro_after init, the situation would be different, but i was told that it's not possible. furthermore, it would require reserving a fixed size "zone", i think. -3)What is the attack we want to make harder to perform? Because even const data can be attacked, if we assume that the attacker can alter page mappings. In reality, the only safe way would be to have one-way only protection. But we do not have it. Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
I'm all for hardening what is possible, but I feel I do not have full understanding of some of the assumptions being made here.
Getting some answers to my questions above might help me seeing the point being made.
--
thanks, igor
--------------------------------------------------
Igor Stoppa Igor Stoppa
M:
E: [email protected]<mailto:[email protected]>
2012<tel:2012>实验室-赫尔辛基研究所
2012<tel:2012> Laboratories-Helsinki Research Center
From:Laura Abbott
To:Kees Cook,Igor Stoppa,
Cc:Boris Lukashev,Christopher Lameter,Matthew Wilcox,Jann Horn,Jerome Glisse,Michal Hocko,Christoph Hellwig,linux-security-module,Linux-MM,kernel list,Kernel Hardening, Date:2018-02-13 00:40:54
Subject:Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
RE: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura Abbotthi,
apologies for (probably) breaking any email etiquette, but i'm travelling and i have available only the corporate mail client. I'll reply more extensively to all the comments i go next week, when i'm back to the office.
In the meanwhile i would like to point out that I had already addressed this, in past thread, but got no reply.
To recap:
-1) vmalloced memory is harder to attack than kmalloced, because it requires the attacker to figuere out also the physical address. Currently it's sufficient to identify the randomized base address and the offset in memory of the victim.
I have not seen comments about this statement I made. Is it incorrect?
-2) this patchset is about protecting something that right now is not protected at all. That should be the starting point for comparison. If it was possible to have separate section like const or _ro_after init, the situation would be different, but i was told that it's not possible. furthermore, it would require reserving a fixed size "zone", i think. -3)What is the attack we want to make harder to perform? Because even const data can be attacked, if we assume that the attacker can alter page mappings. In reality, the only safe way would be to have one-way only protection. But we do not have it. Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
I'm all for hardening what is possible, but I feel I do not have full understanding of some of the assumptions being made here.
Getting some answers to my questions above might help me seeing the point being made.
--
thanks, igor
--------------------------------------------------
Igor Stoppa Igor Stoppa
M:
E: [email protected]<mailto:[email protected]>
2012<tel:2012>实验室-赫尔辛基研究所
2012<tel:2012> Laboratories-Helsinki Research Center
From:Laura Abbott
To:Kees Cook,Igor Stoppa,
Cc:Boris Lukashev,Christopher Lameter,Matthew Wilcox,Jann Horn,Jerome Glisse,Michal Hocko,Christoph Hellwig,linux-security-module,Linux-MM,kernel list,Kernel Hardening, Date:2018-02-13 00:40:54
Subject:Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
Quoted 20 lines · Kees Cook
Quoted 19 lines · Igor Stoppa
Quoted 1 line · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 5 lines
Quoted 5 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become traps that get resolved through a lookup table that is built based on the content of each allocation.
Quoted 7 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
ok, that seems more like an extension though.
Quoted 7 lines
Quoted 4 lines
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
Thanks,
Laura
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Jann HornQuoted 65 lines · Jann Horn
Quoted 42 lines · Kees Cook
Quoted 41 lines · Laura Abbott
Quoted 38 lines · Kees Cook
Quoted 24 lines · Igor Stoppa
Quoted 3 lines · Boris Lukashev
On Sat, Feb 3, 2018 at 3:32 PM, Igor Stoppa <[email protected]> wrote:
[...]
Quoted 6 lines
Quoted 6 lines
What you are suggesting, if I have understood it correctly, is that, when the pool is protected, the addresses already given out, will become
traps that get resolved through a lookup table that is built based on the content of each allocation.Quoted 8 lines
Quoted 2 lines
That seems to generate a lot of overhead, not to mention the fact that it might not play very well with the MMU.
That is effectively what i'm suggesting - as a form of protection for consumers against direct reads of data which may have been corrupted by some irrelevant means. In the context of pmalloc, it would probably be a separate type of ro+verified pool
Quoted 7 lines
ok, that seems more like an extension though.
ATM I am having problems gaining traction to get even the basic merged :-)
I would consider this as a possibility for future work, unless it is said that it's necessary for pmalloc to be accepted ...
I would agree: let's get basic functionality in first. Both verification and the physmap part can be done separately, IMO.
Quoted 7 lines
Quoted 4 lines
Skipping over physmap leaves a pretty big area of exposure that could be difficult to solve later. I appreciate this might block basic functionality but I don't think we should just gloss over it without at least some idea of what we would do.
What's our exposure on physmap for other regions? e.g. things that are executable, or made read-only later (like
__ro_after_init)?I just checked on a system with a 4.9 kernel, and there seems to be no physical memory that is mapped as writable in the init PGD and executable elsewhere.
Ah, I think I missed something. At least on X86,
set_memory_ro,set_memory_rw,set_memory_nxandset_memory_xall use change_page_attr_clear/change_page_attr_set, which usechange_page_attr_set_clr, which calls__change_page_attr_set_clr()with a second parameter "checkalias" that is set to 1 unless the bit being changed is the NX bit, and that parameter causes the invocation ofcpa_process_alias(), which will, for mapped ranges, also change the attributes of physmap ranges.set_memory_ro()and so on are also used by the module loading code.
Quoted 6 lines
But in the ARM64 code, I don't see anything similar. Does anyone with a better understanding of ARM64 want to check whether I missed something? Or maybe, with a recent kernel, check whether executable module pages show up with a second writable mapping in the "
kernel_page_tables" file in debugfs?
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime. CONFIG_PAGE_POISONING does use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.
To the original question, it does sound like we are actually okay with the physmap.
Thanks,
Laura
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to a message not in the archiveQuoted 2 lines · Igor Stoppa
Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
Alterations are certainly a risk but with the physmap the mapping is already there. Find the address and you have access vs. needing to actually modify the properties then do the access. I could also be complete off base on my threat model here so please correct me if I'm wrong.
I think your other summaries are good points though and should go in the cover letter.
Thanks,
Laura
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura AbbottQuoted 4 lines · Laura Abbott
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime.
CONFIG_PAGE_POISONINGdoes use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.
Errr, so that means even modules and kernel code are writable via the arm64 physmap? That seems extraordinarily bad. :(
-Kees
Signature · 2 lines
Kees Cook Pixel Security
arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
Quoted 11 lines · Kees Cook
Quoted 4 lines · Laura Abbott
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime.
CONFIG_PAGE_POISONINGdoes use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.Errr, so that means even modules and kernel code are writable via the arm64 physmap? That seems extraordinarily bad. :(
-Kees
(adding linux-arm-kernel and changing the subject)
Kernel code should be fine, if it isn't that is a bug that should be fixed. Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias if PAGE_POISONING is enabled but I don't know who would actually use that in production. Performance is very poor at that point.
Thanks,
Laura
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
Quoted 13 lines · Laura Abbott
Quoted 12 lines · Kees Cook
Quoted 5 lines · Laura Abbott
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime.
CONFIG_PAGE_POISONINGdoes use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.Errr, so that means even modules and kernel code are writable via the arm64 physmap? That seems extraordinarily bad. :(
Quoted 7 lines
Quoted 2 lines
-Kees
(adding linux-arm-kernel and changing the subject)
Kernel code should be fine, if it isn't that is a bug that should be fixed.
We take care to ensure that the linear alias of the core kernel's .text and .rodata segments are mapped read-only. When we first moved the kernel out of the linear region, we did not map it there at all anymore, but that broke hibernation so we had to put something back.
Quoted 7 lines
Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.
As long as the linear alias of the module is mapped down to pages, we should be able to tweak the permissions. I take it that PAGE_POISONING does more than just that?
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Laura AbbottQuoted 13 lines · Laura Abbott
Quoted 12 lines · Kees Cook
Quoted 5 lines · Laura Abbott
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime.
CONFIG_PAGE_POISONINGdoes use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.Errr, so that means even modules and kernel code are writable via the arm64 physmap? That seems extraordinarily bad. :(
Quoted 7 lines
Quoted 2 lines
-Kees
(adding linux-arm-kernel and changing the subject)
Kernel code should be fine, if it isn't that is a bug that should be fixed. Modules yes are not fully protected. The conclusion from past
I think that's a pretty serious problem: we can't have aliases with mismatched permissions; this degrades a deterministic protection (read-only) to a probabilistic protection (knowing where the alias of a target is mapped). Having an attack be "needs some info leaks" instead of "need execution control to change perms" is a much lower bar, IMO.
Quoted 5 lines
experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.
Why does using finer granularity on the physmap degrade performance? I assume TLB pressure, but what is heavily using that area? (I must not be understanding what physmap actually gets used for -- I thought it was just a convenience to have a 1:1 virt/phys map for some lookups?)
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
Quoted 4 lines · Kees Cook
Why does using finer granularity on the physmap degrade performance? I assume TLB pressure, but what is heavily using that area? (I must not be understanding what physmap actually gets used for -- I thought it was just a convenience to have a 1:1
virt/physmap for some lookups?)
Jann has sorted me out: it's that physmap isn't an _alias_ for the buddy allocator memory areas; it's used directly.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Laura AbbottQuoted 6 lines · Laura Abbott
fixed. Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.
XPFO forces 4K pages on the physmap[1] for similar reasons. I have no doubt about performance changes, but I'd be curious to see real numbers. Did anyone do benchmarks on just the huge/4K change? (Without also the XPFO overhead?)
If this, XPFO, and PAGE_POISONING all need it, I think we have to start a closer investigation. :)
-Kees
[1] http://www.openwall.com/lists/kernel-hardening/2017/09/07/13
Signature · 2 lines
Kees Cook Pixel Security
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Ard BiesheuvelQuoted 35 lines · Ard Biesheuvel
Quoted 13 lines · Laura Abbott
Quoted 12 lines · Kees Cook
Quoted 5 lines · Laura Abbott
No, arm64 doesn't fixup the aliases, mostly because arm64 uses larger page sizes which can't be broken down at runtime.
CONFIG_PAGE_POISONINGdoes use 4K pages which could be adjusted at runtime. So yes, you are right we would have physmap exposure on arm64 as well.Errr, so that means even modules and kernel code are writable via the arm64 physmap? That seems extraordinarily bad. :(
Quoted 7 lines
Quoted 2 lines
-Kees
(adding linux-arm-kernel and changing the subject)
Kernel code should be fine, if it isn't that is a bug that should be fixed.
We take care to ensure that the linear alias of the core kernel's .text and .rodata segments are mapped read-only. When we first moved the kernel out of the linear region, we did not map it there at all anymore, but that broke hibernation so we had to put something back.
Quoted 7 lines
Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.
Quoted 4 lines
As long as the linear alias of the module is mapped down to pages, we should be able to tweak the permissions. I take it that
PAGE_POISONINGdoes more than just that?
Page poisoning does exactly that. The argument I was trying to make was that if nobody really uses page poisoning except for debugging it might not be worth it to fix up the alias. Thinking a bit more, this is a terrible argument for many reasons so yes I agree that we can just fix up the alias if PAGE_POISONING (or other features) are enabled.
Thanks,
Laura
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Kees CookQuoted 8 lines · Kees Cook
Quoted 6 lines · Laura Abbott
fixed. Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.
Quoted 7 lines
XPFO forces 4K pages on the physmap[1] for similar reasons. I have no doubt about performance changes, but I'd be curious to see real numbers. Did anyone do benchmarks on just the huge/4K change? (Without also the XPFO overhead?)
If this, XPFO, and
PAGE_POISONINGall need it, I think we have to start a closer investigation. :)
I haven't but it shouldn't be too hard. What benchmarks are you thinking?
Tycho
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
Quoted 14 lines · Tycho Andersen
Quoted 13 lines · Kees Cook
Quoted 6 lines · Laura Abbott
fixed. Modules yes are not fully protected. The conclusion from past experience has been that we cannot safely break down larger page sizes at runtime like x86 does. We could theoretically
add support for fixing up the alias ifPAGE_POISONINGis enabled but I don't know who would actually use that in production. Performance is very poor at that point.XPFO forces 4K pages on the physmap[1] for similar reasons. I have no doubt about performance changes, but I'd be curious to see real numbers. Did anyone do benchmarks on just the huge/4K change? (Without also the XPFO overhead?)
Quoted 5 lines
Quoted 2 lines
If this, XPFO, and
PAGE_POISONINGall need it, I think we have to start a closer investigation. :)I haven't but it shouldn't be too hard. What benchmarks are you thinking?
Unless I'm looking at some specific micro benchmark, I tend to default to looking at kernel build benchmarks but that gets pretty noisy. Laura regularly uses hackbench, IIRC. I'm not finding the pastebin I had for that, though.
I wonder if we need a benchmark subdirectory in tools/testing/, so we could collect some of these common tools? All benchmarks are terrible, but at least we'd have the same terrible benchmarks. :)
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Kees CookQuoted 1 line · Kees Cook
On Wed, Feb 14, 2018 at 11:06 AM, Laura Abbott <[email protected]> wrote:
[...]
Quoted 9 lines
Quoted 2 lines
Kernel code should be fine, if it isn't that is a bug that should be fixed. Modules yes are not fully protected. The conclusion from past
I think that's a pretty serious problem: we can't have aliases with mismatched permissions; this degrades a deterministic protection (read-only) to a probabilistic protection (knowing where the alias of a target is mapped). Having an attack be "needs some info leaks" instead of "need execution control to change perms" is a much lower bar, IMO.
Why "need execution control to change permission"? Or, iow, what does it mean exactly? ROP/JOP? Data-oriented control flow hijack?
Unless I misunderstand the meaning of "need execution control", I think that "need write capability to arbitrary data address" should be sufficient, albeit uncomfortable to use.
OTOH, "need read/write capability from/to arbitrary data address" would be enough, I think, assuming that one knows the offset where to write to
- but that information could be inferred, for example, by scanning the memory for known patterns.
IMHO the attack surface is so vast that it's not unreasonable to expect that it will be possible to fish out means to perform arbitrary R/W into kernel address space. Ex: some more recent/less tested driver.
One can argue that this sort of R/W activity probably does require some form of execution control, but AFAIK, the only way to to prevent it, is to have CFI - btw, is there any standardization in that sense?
So, from my (pessimistic?) perspective, the best that can be hoped for, is to make it much harder to figure out where the data is located.
Virtual mapping has this side effect, compared to linear mapping.
But, once easier attack targets are removed, I suspect the page mapping will become the next target.
--
igor
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Laura AbbottQuoted 10 lines · Laura Abbott
Quoted 2 lines · Igor Stoppa
Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
Alterations are certainly a risk but with the physmap the mapping is already there. Find the address and you have access vs. needing to actually modify the properties then do the access. I could also be complete off base on my threat model here so please correct me if I'm wrong.
It's difficult for me to comment on this without knowing *how* the attack would be performed, in your model.
Ex: my expectation is that the attacked has R/W access to kernel data and has knowledge of the location of static variables.
This is not just a guess, but a real-life scenario, found in attacks that, among other things, are capable of disabling SELinux, to proceed toward gaining full root capability.
At that point, I think that variables which are allocated dynamically, in vmalloc address space, are harder to locate, because of the virtual mapping and the randomness of the address chosen (this I have not confirmed yet, but I suppose there is some randomness in picking the address to assign to a certain allocation request to vmalloc, otherwise, it could be added).
Quoted 2 lines
I think your other summaries are good points though and should go in the cover letter.
Ok, I'm just afraid it risks becoming a lengthy dissertation :-)
--
igor
Re: arm64 physmap (was Re: [kernel-hardening] [PATCH 4/6] Protectable Memory)
In reply to Igor StoppaQuoted 17 lines · Igor Stoppa
Quoted 1 line · Kees Cook
On Wed, Feb 14, 2018 at 11:06 AM, Laura Abbott <[email protected]> wrote:
[...]
Quoted 9 lines
Quoted 2 lines
Kernel code should be fine, if it isn't that is a bug that should be fixed. Modules yes are not fully protected. The conclusion from past
I think that's a pretty serious problem: we can't have aliases with mismatched permissions; this degrades a deterministic protection (read-only) to a probabilistic protection (knowing where the alias of a target is mapped). Having an attack be "needs some info leaks" instead of "need execution control to change perms" is a much lower bar, IMO.
Quoted 3 lines
Why "need execution control to change permission"? Or, iow, what does it mean exactly? ROP/JOP? Data-oriented control flow hijack?
Right, I mean, if an attacker has already gained execute control, they can just call the needed functions to change memory permissions. But that isn't needed if there is a mismatch between physmap and virtmap:
i.e. they can write to the physmap without needing to change perms first.
Quoted 3 lines
One can argue that this sort of R/W activity probably does require some form of execution control, but AFAIK, the only way to to prevent it, is to have CFI - btw, is there any standardization in that sense?
I meant that I don't want a difference in protection between physmap and virtmap. I'd like to be able to reason the smae about the exposures in either.
Quoted 4 lines
So, from my (pessimistic?) perspective, the best that can be hoped for, is to make it much harder to figure out where the data is located.
Virtual mapping has this side effect, compared to linear mapping.
Right, this is good, for sure. No complaints there at all. It's why I think pmalloc and arm64 physmap perms are separate issues.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH 4/6] Protectable Memory
In reply to Igor StoppaQuoted 24 lines · Igor Stoppa
Quoted 10 lines · Laura Abbott
Quoted 2 lines · Igor Stoppa
Why alterations of page properties are not considered a risk and the physmap is? And how would it be easier (i suppose) to attack the latter?
Alterations are certainly a risk but with the physmap the mapping is already there. Find the address and you have access vs. needing to actually modify the properties then do the access. I could also be complete off base on my threat model here so please correct me if I'm wrong.
It's difficult for me to comment on this without knowing *how* the attack would be performed, in your model.
Ex: my expectation is that the attacked has R/W access to kernel data and has knowledge of the location of static variables.
This is not just a guess, but a real-life scenario, found in attacks that, among other things, are capable of disabling SELinux, to proceed toward gaining full root capability.
Quoted 6 lines
At that point, I think that variables which are allocated dynamically, in vmalloc address space, are harder to locate, because of the virtual mapping and the randomness of the address chosen (this I have not confirmed yet, but I suppose there is some randomness in picking the address to assign to a certain allocation request to vmalloc, otherwise, it could be added).
Machine-to-machine runtime variation certainly affects the mapping location, but for early boot allocations, these become surprisingly deterministic, especially across similar hardware/memory layouts (both the virtmap and physmap locations). However, using CONFIG_RANDOMIZE_MEMORY makes it MUCH more difficult. (Note that RANDOMIZE_BASE on arm64 effectively includes RANDOMIZE_MEMORY, as it uses the entropy for multiple base offsets, including the physmap, IIRC.)
Quoted 4 lines
Quoted 2 lines
I think your other summaries are good points though and should go in the cover letter.
Ok, I'm just afraid it risks becoming a lengthy dissertation :-)
It's rare to have anyone say "your commit log is too long". :)
-Kees
Signature · 2 lines
Kees Cook Pixel Security