mm: security: ro protection for dynamic data
Subsystems (8)
v22 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 protected, all the memory that is currently part of it, will become R/O.
A R/O pool can be expanded (adding more protectable memory). It can also 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. The pool, therefore, can be destroyed.
An example is provided, in the form of self-testing.
Since it was advised to give an example of protecting real kernel data [1],
a well known vulnerability has been used to demo an effective use of pmalloc.
[1] http://www.openwall.com/lists/kernel-hardening/2018/03/29/7
However it turned out to be almost an how-to for attacking the kernel, so it was sent first to [email protected], for obtaining clearance about the
publication.
Changes since v21:
[http://www.openwall.com/lists/kernel-hardening/2018/03/27/23]
* fixed type mismatch error in use of max(), detected by gcc 7.3
* converted internal types into size_t
* fixed leak of vmalloc memory in the self-test code
Igor Stoppa (6):
struct page: add field for vm_struct
vmalloc: rename llist field in vmap_area Protectable Memory
Documentation for Pmalloc
Pmalloc selftest
lkdtm: crash on overwriting protected pmalloc var
Igor Stoppa (6):
struct page: add field for vm_struct
vmalloc: rename llist field in vmap_area Protectable Memory
Documentation for Pmalloc
Pmalloc selftest
lkdtm: crash on overwriting protected pmalloc var
Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 107 +++++++++++++++ drivers/misc/lkdtm/core.c | 3 + drivers/misc/lkdtm/lkdtm.h | 1 + drivers/misc/lkdtm/perms.c | 25 ++++ include/linux/mm_types.h | 1 + include/linux/pmalloc.h | 166 +++++++++++++++++++++++ include/linux/test_pmalloc.h | 24 ++++ include/linux/vmalloc.h | 5 +- init/main.c | 2 + mm/Kconfig | 16 +++ mm/Makefile | 2 + mm/pmalloc.c | 265 +++++++++++++++++++++++++++++++++++++ mm/test_pmalloc.c | 137 +++++++++++++++++++ mm/usercopy.c | 33 +++++ mm/vmalloc.c | 10 +- 16 files changed, 793 insertions(+), 5 deletions(-) create mode 100644 Documentation/core-api/pmalloc.rst create mode 100644 include/linux/pmalloc.h create mode 100644 include/linux/test_pmalloc.h create mode 100644 mm/pmalloc.c create mode 100644 mm/test_pmalloc.c
Signature · 1 line
2.14.1
PATCH v22 1/6 struct page: add field for vm_struct
When a page is used for virtual memory, it is often necessary to obtain 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, later on.
--- include/linux/mm_types.h | 1 + mm/vmalloc.c | 2 ++ 2 files changed, 3 insertions(+)
include/linux/mm_types.h +1 -0
@@ -86,6 +86,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 +2 -0
@@ -1536,6 +1536,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 ); }
@@ -1705,6 +1706,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, area->nr_pages = i; goto fail; } + page->area = area; area->pages[i] = page;if (gfpflags_allow_blocking (gfp_mask|highmem_mask))cond_resched ();
Signature · 1 line
2.14.1
PATCH v22 2/6 vmalloc: rename llist field in vmap_area
In reply to Igor StoppaThe vmap_area structure has a field of type struct llist_node, named purge_list and is used when performing lazy purge of the area.
Such field is left unused during the actual utilization of the structure.
This patch renames the field to a more generic "area_list", to allow for utilization outside of the purging phase.
Since the purging happens after the vmap_area is dismissed, its use is mutually exclusive with any use performed while the area is allocated.
--- include/linux/vmalloc.h | 2 +- mm/vmalloc.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/vmalloc.h +1 -1
@@ -47,7 +47,7 @@ struct vmap_area {unsigned long flags;struct rb_node rb_node;/* address sorted rbtree */ struct list_head list;/* address sorted list */ -struct llist_node purge_list;/* "lazy purge" list */ +struct llist_node area_list;/* generic list of areas */ struct vm_struct *vm;struct rcu_head rcu_head; };
mm/vmalloc.c +3 -3
@@ -682,7 +682,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)lockdep_assert_held (&vmap_purge_lock); valist =llist_del_all (&vmap_purge_list); -llist_for_each_entry (va, valist, purge_list) { +llist_for_each_entry (va, valist, area_list) {if (va->va_start < start) start = va->va_start;if (va->va_end > end)
@@ -696,7 +696,7 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end)flush_tlb_kernel_range (start, end);spin_lock (&vmap_area_lock); -llist_for_each_entry_safe (va, n_va, valist, purge_list) { +llist_for_each_entry_safe (va, n_va, valist, area_list) {int nr = (va->va_end - va->va_start) >>PAGE_SHIFT ;__free_vmap_area (va);
@@ -743,7 +743,7 @@ static void free_vmap_area_noflush(struct vmap_area *va) &vmap_lazy_nr);/* After this point, we may free va at any time */ -llist_add (&va->purge_list, &vmap_purge_list); +llist_add (&va->area_list, &vmap_purge_list);if (unlikely (nr_lazy >lazy_max_pages ()))try_purge_vmap_area_lazy ();
Signature · 1 line
2.14.1
PATCH v22 3/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 (that's how __ro_after_init works), 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 instantiate a pool, and then refer any allocation request to the pool handler it has received.
A pool is organized ias list of areas of virtually contiguous memory. Whenever the protection functionality is invoked on a pool, all the areas it contains that are not yet read-only are write-protected.
The process of growing and protecting the pool can be iterated at will. Each iteration will prevent further allocation from the memory area currently active, turn it into read-only mode and then proceed to secure whatever other area might still be unprotected.
Write-protcting some part of a pool before completing all the allocations can be wasteful, however it will guarrantee the minimum window of vulnerability, sice the data can be allocated, initialized and protected in a single sweep.
There are pros and cons, depending on the allocation patterns, the size of the areas being allocated, the time intervals between initialization and protection.
Dstroying a pool is the only way to claim back the associated memory. It is up to its user to avoid any further references to the memory that was allocated, once the destruction is invoked.
An example where it is desirable to destroy a pool and claim back its memory is when unloading a kernel module.
A module can have as many pools as needed.
Since pmalloc memory is obtained from vmalloc, an attacker that has gained access to the physical mapping, still has to identify where the target of the attack (in virtually contiguous mapping) is located.
Compared to plain vmalloc, pmalloc does not generate as much TLB trashing, since it can host multiple allocations in the same page, where present.
--- include/linux/pmalloc.h | 166 ++++++++++++++++++++++++++++++ include/linux/vmalloc.h | 3 + mm/Kconfig | 6 ++ mm/Makefile | 1 + mm/pmalloc.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 33 ++++++ mm/vmalloc.c | 2 +- 7 files changed, 475 insertions(+), 1 deletion(-) create mode 100644 include/linux/pmalloc.h create mode 100644 mm/pmalloc.c
include/linux/pmalloc.h new file +166 -0
@@ -0,0 +1,166 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* pmalloc.h: Header for Protectable Memory Allocator +* +* (C) Copyright 2017-18 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + +#ifndef _LINUX_PMALLOC_H +#define _LINUX_PMALLOC_H + + +#include <linux/string.h> +#include <linux/slab.h> + +/* +* Library for dynamic allocation of pools of protectable memory. +* A pool is a single linked list of vmap_area structures. +* Whenever a pool is protected, all the areas it contain at that point +* are write protected. +* More areas can be added and protected, in the same way. +* Memory in a pool cannot be individually unprotected, but the pool can +* be destroyed. +* Upon destruction of a certain pool, all the related memory is released, +* including its metadata. +* +* Pmalloc memory is intended to complement __read_only_after_init. +* It can be used, for example, where there is a write-once variable, for +* which it is not possible to know the initialization value before init +* is completed (which is what __read_only_after_init requires). +* +* It can be useful also where the amount of data to protect is not known +* at compile time and the memory can only be allocated dynamically. +* +* Finally, it can be useful also when it is desirable to control +* dynamically (for example throguh the command line) if something ought +* to be protected or not, without having to rebuild the kernel (like in +* the build used for a linux distro). +*/ + + +#define PMALLOC_REFILL_DEFAULT (0) +#define PMALLOC_ALIGN_DEFAULT ARCH_KMALLOC_MINALIGN + +struct pmalloc_pool *pmalloc_create_custom_pool (size_t refill, +unsigned short align_order); + +/** +* pmalloc_create_pool() - create a protectable memory pool +* +* Shorthand for pmalloc_create_custom_pool() with default argument: +* * refill is set to PMALLOC_REFILL_DEFAULT +* * align_order is set to PMALLOC_ALIGN_DEFAULT +* +* Return: +* * pointer to the new pool - success +* * NULL - error +*/ +static inline struct pmalloc_pool *pmalloc_create_pool (void ) +{ +return pmalloc_create_custom_pool (PMALLOC_REFILL_DEFAULT , +PMALLOC_ALIGN_DEFAULT ); +} + + +void *pmalloc (struct pmalloc_pool *pool,size_t size); + + +/** +* pzalloc() - zero-initialized version of pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @size: amount of memory (in bytes) requested +* +* Executes pmalloc(), initializing the memory requested to 0, before +* returning its address. +* +* Return: +* * pointer to the memory requested - success +* * NULL - error +*/ +static inline void *pzalloc (struct pmalloc_pool *pool,size_t size) +{ +void *ptr =pmalloc (pool, size); + +if (likely (ptr)) +memset (ptr,0 , size); +return ptr; +} + + +/** +* pmalloc_array() - array version of pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @n: number of elements in the array +* @size: amount of memory (in bytes) requested for each element +* +* Executes pmalloc(), on an array. +* +* Return: +* * the pmalloc result - success +* * NULL - error +*/ + +static inline void *pmalloc_array (struct pmalloc_pool *pool,size_t n, +size_t size) +{ +if (unlikely (size !=0 ) &&unlikely (n >SIZE_MAX / size)) +return NULL ; +return pmalloc (pool, n * size); +} + + +/** +* pcalloc() - array version of pzalloc() +* @pool: handle to the pool to be used for memory allocation +* @n: number of elements in the array +* @size: amount of memory (in bytes) requested for each element +* +* Executes pzalloc(), on an array. +* +* Return: +* * the pmalloc result - success +* * NULL - error +*/ +static inline void *pcalloc (struct pmalloc_pool *pool,size_t n, +size_t size) +{ +if (unlikely (size !=0 ) &&unlikely (n >SIZE_MAX / size)) +return NULL ; +return pzalloc (pool, n * size); +} + + +/** +* pstrdup() - duplicate a string, using pmalloc() +* @pool: handle to the pool to be used for memory allocation +* @s: string to duplicate +* +* Generates a copy of the given string, allocating sufficient memory +* from the given pmalloc pool. +* +* Return: +* * pointer to the replica - success +* * NULL - error +*/ +static inline char *pstrdup (struct pmalloc_pool *pool,const char *s) +{ +size_t len; +char *buf; + + len =strlen (s) +1 ; + buf =pmalloc (pool, len); +if (likely (buf)) +strncpy (buf, s, len); +return buf; +} + + +void pmalloc_protect_pool (struct pmalloc_pool *pool); + + +void pmalloc_destroy_pool (struct pmalloc_pool *pool); + + +int is_pmalloc_object (const void *ptr,const unsigned long n); +#endif
include/linux/vmalloc.h +3 -0
@@ -20,6 +20,8 @@ struct notifier_block; /* in notifier.h */#define VM_UNINITIALIZED 0x00000020/* vm_struct is not fully initialized */ #define VM_NO_GUARD 0x00000040/* don't add guard page */ #define VM_KASAN 0x00000080/* has allocated kasan shadow memory */ +#define VM_PMALLOC 0x00000100/* pmalloc area - see docs */ +#define VM_PMALLOC_PROTECTED 0x00000200/* protected area - see docs */ /* bits [20..32] reserved for arch specific ioremap internals */ /*
@@ -133,6 +135,7 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size, const void *caller);extern struct vm_struct *remove_vm_area (const void *addr);extern struct vm_struct *find_vm_area (const void *addr); +extern struct vmap_area *find_vmap_area (unsigned long addr);extern int map_vm_area (struct vm_struct *area,pgprot_t prot,struct page **pages);
mm/Kconfig +6 -0
@@ -752,3 +752,9 @@ config GUP_BENCHMARK performance of get_user_pages_fast(). See tools/testing/selftests/vm/gup_benchmark.c + +config PROTECTABLE_MEMORY + bool + depends on MMU + depends on ARCH_HAS_SET_MEMORY + default y
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_PROTECTABLE_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 +265 -0
265 lines not on this page · raw message
mm/usercopy.c +33 -0
@@ -240,6 +240,36 @@ static inline void check_heap_object(const void *ptr, unsigned long n, } } +#ifdef CONFIG_PROTECTABLE_MEMORY + +int is_pmalloc_object (const void *ptr,const unsigned long n); + +static void check_pmalloc_object (const void *ptr,unsigned long n, +bool to_user) +{ +int retv; + + retv =is_pmalloc_object (ptr, n); +if (unlikely (retv)) { +if (unlikely (!to_user)) +usercopy_abort ("pmalloc" , +"trying to write to pmalloc object" , + to_user, (const unsigned long )ptr, n); +if (retv <0 ) +usercopy_abort ("pmalloc" , +"invalid pmalloc object" , + to_user, (const unsigned long )ptr, n); + } +} + +#else + +static void check_pmalloc_object (const void *ptr,unsigned long n, +bool to_user) +{ +} +#endif +/* * Validates that the given object is: * - not bogus address
@@ -277,5 +307,8 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ check_kernel_text_object ((const unsigned long )ptr, n, to_user); + +/* Check if object is from a pmalloc chunk. */ +check_pmalloc_object (ptr, n, to_user); }EXPORT_SYMBOL (__check_object_size);
mm/vmalloc.c +1 -1
@@ -759,7 +759,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)free_vmap_area_noflush (va); } -static struct vmap_area *find_vmap_area (unsigned long addr) +struct vmap_area *find_vmap_area (unsigned long addr) {struct vmap_area *va;
Signature · 1 line
2.14.1
PATCH v22 4/6 Documentation for Pmalloc
In reply to Igor StoppaDetailed documentation about the protectable memory allocator.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 107 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 Documentation/core-api/pmalloc.rst
Documentation/core-api/index.rst +1 -0
@@ -25,6 +25,7 @@ Core utilities genalloc errseq printk-formats + pmalloc Interfaces for kernel debugging ===============================
Documentation/core-api/pmalloc.rst new file +107 -0
107 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v22 5/6 Pmalloc selftest
In reply to Igor StoppaAdd basic self-test functionality for pmalloc.
The testing is introduced as early as possible, right after the main dependency, genalloc, has passed successfully, so that it can help diagnosing failures in pmalloc users.
--- include/linux/test_pmalloc.h | 24 ++++++++ init/main.c | 2 + mm/Kconfig | 10 ++++ mm/Makefile | 1 + mm/test_pmalloc.c | 137 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 include/linux/test_pmalloc.h create mode 100644 mm/test_pmalloc.c
include/linux/test_pmalloc.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* test_pmalloc.h +* +* (C) Copyright 2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + + +#ifndef __LINUX_TEST_PMALLOC_H +#define __LINUX_TEST_PMALLOC_H + + +#ifdef CONFIG_TEST_PROTECTABLE_MEMORY + +void test_pmalloc (void ); + +#else + +static inline void test_pmalloc (void ){}; + +#endif + +#endif
init/main.c +2 -0
@@ -91,6 +91,7 @@#include <linux/cache.h> #include <linux/rodata_test.h> #include <linux/jump_label.h> +#include <linux/test_pmalloc.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -679,6 +680,7 @@ asmlinkage __visible void __init start_kernel(void)*/ mem_encrypt_init (); +test_pmalloc ();#ifdef CONFIG_BLK_DEV_INITRD if (initrd_start && !initrd_below_start_ok &&page_to_pfn (virt_to_page ((void *)initrd_start)) < min_low_pfn) {
mm/Kconfig +10 -0
@@ -758,3 +758,13 @@ config PROTECTABLE_MEMORY depends on MMU depends on ARCH_HAS_SET_MEMORY default y + +config TEST_PROTECTABLE_MEMORY + bool "Run self test for pmalloc memory allocator" + depends on MMU + depends on ARCH_HAS_SET_MEMORY + select PROTECTABLE_MEMORY + 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_PROTECTABLE_MEMORY) += pmalloc.o +obj-$(CONFIG_TEST_PROTECTABLE_MEMORY) += test_pmalloc.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/test_pmalloc.c new file +137 -0
137 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v22 6/6 lkdtm: crash on overwriting protected pmalloc var
In reply to Igor StoppaVerify that pmalloc read-only protection is in place: trying to overwrite a protected variable will crash the kernel.
--- drivers/misc/lkdtm/core.c | 3 +++ drivers/misc/lkdtm/lkdtm.h | 1 + drivers/misc/lkdtm/perms.c | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+)
drivers/misc/lkdtm/core.c +3 -0
@@ -155,6 +155,9 @@ static const struct crashtype crashtypes[] = {CRASHTYPE (ACCESS_USERSPACE ),CRASHTYPE (WRITE_RO ),CRASHTYPE (WRITE_RO_AFTER_INIT ), +#ifdef CONFIG_PROTECTABLE_MEMORY +CRASHTYPE (WRITE_RO_PMALLOC ), +#endifCRASHTYPE (WRITE_KERN ),CRASHTYPE (REFCOUNT_INC_OVERFLOW ), CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
drivers/misc/lkdtm/lkdtm.h +1 -0
@@ -38,6 +38,7 @@ void lkdtm_READ_BUDDY_AFTER_FREE(void);void __init lkdtm_perms_init (void );void lkdtm_WRITE_RO (void );void lkdtm_WRITE_RO_AFTER_INIT (void ); +void lkdtm_WRITE_RO_PMALLOC (void );void lkdtm_WRITE_KERN (void );void lkdtm_EXEC_DATA (void );void lkdtm_EXEC_STACK (void );
drivers/misc/lkdtm/perms.c +25 -0
@@ -9,6 +9,7 @@#include <linux/vmalloc.h> #include <linux/mman.h> #include <linux/uaccess.h> +#include <linux/pmalloc.h> #include <asm/cacheflush.h> /* Whether or not to fill the target memory area with do_nothing(). */
@@ -104,6 +105,30 @@ void lkdtm_WRITE_RO_AFTER_INIT(void) *ptr ^=0xabcd1234 ; } +#ifdef CONFIG_PROTECTABLE_MEMORY +void lkdtm_WRITE_RO_PMALLOC (void ) +{ +struct pmalloc_pool *pool; +int *i; + + pool =pmalloc_create_pool (); +if (WARN (!pool,"Failed preparing pool for pmalloc test." )) +return ; + + i = (int *)pmalloc (pool,sizeof (int )); +if (WARN (!i,"Failed allocating memory for pmalloc test." )) { +pmalloc_destroy_pool (pool); +return ; + } + + *i =INT_MAX ; +pmalloc_protect_pool (pool); + +pr_info ("attempting bad pmalloc write at %p\n" , i); + *i =0 ; +} +#endif +void lkdtm_WRITE_KERN (void ) {size_t size;
Signature · 1 line
2.14.1