mm: security: ro protection for dynamic data
Subsystems (6)
v13 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 v12
[https://lkml.org/lkml/2018/1/30/397]
- fixed Kconfig dependency for pmalloc-test
- fixed warning for size_t treated as %ul on i386
- moved to SPDX license reference
- rewrote pmalloc docs
Igor Stoppa (6):
genalloc: track beginning of allocations
genalloc: selftest
struct page: add field for vm_struct
Protectable Memory
Pmalloc: self-test
Documentation for Pmalloc
Documentation/core-api/pmalloc.rst | 114 ++++++++ include/linux/genalloc-selftest.h | 30 +++ include/linux/genalloc.h | 7 +- include/linux/mm_types.h | 1 + include/linux/pmalloc.h | 211 +++++++++++++++ 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 | 9 + mm/Makefile | 2 + mm/pmalloc-selftest.c | 61 +++++ mm/pmalloc-selftest.h | 26 ++ mm/pmalloc.c | 514 +++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 +- mm/vmalloc.c | 18 +- 18 files changed, 1742 insertions(+), 141 deletions(-) create mode 100644 Documentation/core-api/pmalloc.rst 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 v13 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 | 4 +- lib/genalloc.c | 417 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 289 insertions(+), 132 deletions(-)
include/linux/genalloc.h +2 -2
@@ -32,7 +32,7 @@#include <linux/types.h> #include <linux/spinlock_types.h> -#include <linux/atomic.h> +#include <linux/slab.h> struct device ;struct device_node ;
@@ -76,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 v13 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
@@ -89,6 +89,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>
@@ -660,6 +661,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
@@ -287,6 +287,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
@@ -108,6 +108,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 v13 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
@@ -56,6 +56,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 v13 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 | 211 +++++++++++++++++++ include/linux/vmalloc.h | 1 + lib/genalloc.c | 27 +++ mm/Makefile | 1 + mm/pmalloc.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 ++- 7 files changed, 778 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 +211 -0
211 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_long_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 +514 -0
514 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