linux-next: mm: Track genalloc allocations
Subsystems (4)
v2 cover linux-next: mm: Track genalloc allocations
This patchset was created as part of an older version of pmalloc, however it has value per-se, as it hardens the memory management for the generic allocator genalloc.
Genalloc does not currently track the size of the allocations it hands out.
Either by mistake, or due to an attack, it is possible that more memory than what was initially allocated is freed, leaving behind dangling pointers, ready for an use-after-free attack.
With this patch, genalloc becomes capable of tracking the size of each allocation it has handed out, when it's time to free it.
It can either verify that the size received is correct, when free is invoked, or it can decide autonomously how much memory to free, if the value received for the size parameter is 0.
These patches are proposed for beign merged into linux-next, to verify that they do not introduce regressions, by comparing the value received from the callers of the free function with the internal tracking.
For this reason, the patchset does not contain the removal of the size parameter from users of the free() function.
Later on, the "size" parameter can be dropped, and each caller can be adjusted accordingly.
However, I do not have access to most of the HW required for confirming that all of its users are not negatively affected.
This is where I believe having the patches in linux-next would help to coordinate with the maintaiers of the code that uses gen_alloc.
Since there were comments about the (lack-of) efficiency introduced by this patchset, I have added some more explanations and calculations to the description of the first patch, the one adding the bitmap.
My conclusion is that this patch should not cause any major perfomance problem.
Regarding the possibility of completely changing genalloc into some other type of allocator, I think it should not be a showstopper for this patchset, which aims to plug a security hole in genalloc, without introducing any major regression.
The security flaw is clear and present, while the benefit of introducing a new allocator is not clear, at least for the current users of genalloc.
And anyway the users of genalloc should be fixed to not pass any size parameter, which can be done after this patch is merged.
A newer, more efficient allocator will still benefit from not receiving a spurious parameter (size), when freeing memory.
Changes since v1:
[http://www.openwall.com/lists/kernel-hardening/2018/04/29/1]
* make the tester code a kernel module
* turn selftest BUG() error exit paths into WARN()
* add analysis of impact on current users of genalloc
Igor Stoppa (3):
genalloc: track beginning of allocations
Add label and license to genalloc.rst
genalloc: selftest
Documentation/core-api/genalloc.rst | 4 + include/linux/genalloc.h | 112 +++--- lib/Kconfig.debug | 23 ++ lib/Makefile | 1 + lib/genalloc.c | 742 ++++++++++++++++++++++++++---------- lib/test_genalloc.c | 419 ++++++++++++++++++++ 6 files changed, 1046 insertions(+), 255 deletions(-) create mode 100644 lib/test_genalloc.c
Signature · 1 line
2.14.1
PATCH v2 1/3 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.
Quoted 1 line
From a security standpoint, such errors could be exploited - or even
forcibly caused - by an attacker, trying to obtain memory still in use by live pointer.
Examples:
* Only a subset of the memory provided to an allocation request is freed
* The memory from a subsequent allocation is freed (potential attack)
* The memory being freed doesn't start at the beginning of an allocation. (potential attack)
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, to track their beginning.
For details, see the documentation inside lib/genalloc.c
The primary effect of this patch is that code using the gen_alloc library does not need anymore to keep track of the size of the allocations it makes.
Prior to this patch, it was necessary to keep track of the size of the allocation, so that it would be possible, later on, to know how much space should be freed.
Now, users of the api can choose to etiher still specify explicitly the size, or let the library determine it, by giving a value of 0.
However, even when the value is specified, the library still uses its on understanding of the space associated with a certain allocation, to confirm that they are consistent.
This verification also confirms that the patch works correctly.
Eventually, the extra parameter (and the corresponding verification) must be dropped, in favor of a simplified API.
But this patch allows to transition them individually, while ensuring that the memory being freed has both a legal start and size.
Quoted 1 line
From the perspective of performance imapact, the patch doubles the size
of the bitmap that must be parsed, however the lockless allocations is preserved.
Regarding the actual amount of bits occupied, the current users of genalloc are of 2 main types and the size of one allocation unit in the bitmap corresponds respectively to the following:
* SRAM / MURAM / small memory allocators, one allocation unit corresponds to either 1 or few bytes of special memory.
However these kind of memory come in small sizes (few tens of kbytes), because they are often on-die and tend to take up space.
Ex: 48kB SRAM, each byte mapped individually.
48kB -> 6kB bitmap prepatch, 12kB bitmap after patch
This type of memory is usually allocated during init or anyway at system startup and then used to preserve system status while the rest of the core/SoC is powered down.
* DMA memory pools, one allocation unit corresponds to 1 page
Ex: 1MB allocation request, with PAGE_SIZE = 4kB
1MB -> 32B bitmap prepatch, 64B bitmap after patch.
Assuming a cache line size of 64B, they would both fit in one. Also in this case, DMA buffers are typically established when either the system or a specific peripheral is brought up, but they are not continuously allocated and destroyed. Even if the allocation were to become slightly slower, it would not bring any significant performance loss.
Furthermore, since the data in the bitmap is accessed sequentially, it should be prefetched for read, which means tha the increased time spent parsing the bitmap should be negligible, compared to the actual use of the memory being allocated.
--- include/linux/genalloc.h | 112 +++---- lib/genalloc.c | 742 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 599 insertions(+), 255 deletions(-)
include/linux/genalloc.h +60 -52
@@ -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 */ };/*
@@ -93,74 +93,82 @@ struct genpool_data_fixed {unsigned long offset;/* The offset of the specific region */ }; -extern struct gen_pool *gen_pool_create (int ,int ); -extern phys_addr_t gen_pool_virt_to_phys (struct gen_pool *pool,unsigned long ); -extern int gen_pool_add_virt (struct gen_pool *,unsigned long ,phys_addr_t , -size_t ,int ); -/** -* gen_pool_add - add a new chunk of special memory to the pool -* @pool: pool to add new memory chunk to -* @addr: starting address of memory chunk to add to pool -* @size: size in bytes of the memory chunk to add to pool -* @nid: node id of the node the chunk structure and bitmap should be -* allocated on, or -1 -* -* Add a new chunk of special memory to the specified pool. -* -* Returns 0 on success or a -ve errno on failure. -*/ +struct gen_pool *gen_pool_create (int min_alloc_order,int nid); + +int gen_pool_add_virt (struct gen_pool *pool,unsigned long virt, +phys_addr_t phys,size_t size,int nid); + +static inline int gen_pool_add (struct gen_pool *pool,unsigned long addr,size_t size,int nid) {return gen_pool_add_virt (pool, addr,-1 , size, nid); } -extern void gen_pool_destroy (struct gen_pool *); -extern unsigned long gen_pool_alloc (struct gen_pool *,size_t ); -extern unsigned long gen_pool_alloc_algo (struct gen_pool *,size_t , -genpool_algo_t algo,void *data); -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_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 *); -extern size_t gen_pool_size (struct gen_pool *); -extern void gen_pool_set_algo (struct gen_pool *pool,genpool_algo_t algo, -void *data); +phys_addr_t gen_pool_virt_to_phys (struct gen_pool *pool,unsigned long addr); -extern unsigned long gen_pool_first_fit (unsigned long *map,unsigned long size, -unsigned long start,unsigned int nr,void *data, -struct gen_pool *pool); +void gen_pool_destroy (struct gen_pool *pool); -extern unsigned long gen_pool_fixed_alloc (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); +unsigned long gen_pool_alloc (struct gen_pool *pool,size_t size); -extern unsigned long gen_pool_first_fit_align (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); +unsigned long gen_pool_alloc_algo (struct gen_pool *pool,size_t size, +genpool_algo_t algo,void *data); +void *gen_pool_dma_alloc (struct gen_pool *pool,size_t size,dma_addr_t *dma); -extern unsigned long gen_pool_first_fit_order_align (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); -extern unsigned long gen_pool_best_fit (unsigned long *map,unsigned long size, -unsigned long start,unsigned int nr,void *data, -struct gen_pool *pool); +void gen_pool_free (struct gen_pool *pool,unsigned long addr,size_t size); -extern struct gen_pool *devm_gen_pool_create (struct device *dev, -int min_alloc_order,int nid,const char *name); -extern struct gen_pool *gen_pool_get (struct device *dev,const char *name); +void gen_pool_for_each_chunk (struct gen_pool *pool, +void (*func)(struct gen_pool *pool, +struct gen_pool_chunk *chunk, +void *data), +void *data);bool addr_in_gen_pool (struct gen_pool *pool,unsigned long start, -size_t size); +size_t size); + +size_t gen_pool_avail (struct gen_pool *pool); + +size_t gen_pool_size (struct gen_pool *pool); + +void gen_pool_set_algo (struct gen_pool *pool,genpool_algo_t algo,void *data); + +unsigned long gen_pool_first_fit (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + + +unsigned long gen_pool_first_fit_align (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr,void *data, +struct gen_pool *pool); + +unsigned long gen_pool_fixed_alloc (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + + +unsigned long gen_pool_first_fit_order_align (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr,void *data, +struct gen_pool *pool); + +unsigned long gen_pool_best_fit (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + +struct gen_pool *gen_pool_get (struct device *dev,const char *name); + +struct gen_pool *devm_gen_pool_create (struct device *dev,int min_alloc_order, +int nid,const char *name);#ifdef CONFIG_OF -extern struct gen_pool *of_gen_pool_get (struct device_node *np, -const char *propname,int index); +struct gen_pool *of_gen_pool_get (struct device_node *np, +const char *propname,int index);#else static inline struct gen_pool *of_gen_pool_get (struct device_node *np,const char *propname,int index)
lib/genalloc.c +539 -203
1,090 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v2 2/3 Add label and license to genalloc.rst
In reply to Igor StoppaAdd SPDX license to genalloc.rst, then a label, to allow cross-referencing.
--- Documentation/core-api/genalloc.rst | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/core-api/genalloc.rst +4 -0
@@ -1,3 +1,7 @@ +.. SPDX-License-Identifier: GPL-2.0 + +.. _genalloc: + The genalloc/genpool subsystem ==============================
Signature · 1 line
2.14.1
PATCH v2 3/3 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.
While it is possible to compile and executethe test as kenrel module, it is mostly useful to confirm that there are no problems.
In case there were problems, the system is likely to crash well before modules can be loaded. When troubleshooting a crash, it is recommended to compile the tests into the monolithic kernel.
--- lib/Kconfig.debug | 23 +++ lib/Makefile | 1 + lib/test_genalloc.c | 419 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 443 insertions(+) create mode 100644 lib/test_genalloc.c
lib/Kconfig.debug +23 -0
@@ -1943,6 +1943,29 @@ config TEST_DEBUG_VIRTUAL If unsure, say N. +config TEST_GENERIC_ALLOCATOR + tristate "genalloc tester" + default n + depends on GENERIC_ALLOCATOR + help + Enable automated testing of the generic allocator. + The testing is primarily for the tracking of allocated space, + in particular, it tests that the size of each allcoation can be + determined correctly. + + If unsure, say N. + +config TEST_GENERIC_ALLOCATOR_VERBOSE + bool "make the genalloc tester more verbose" + default n + depends on TEST_GENERIC_ALLOCATOR + help + During the self-testing, it will be possibe to visualize the bit + patterns that are expected to be produced by the sequence of + memory-oriented operations. + + If unsure, say N + endif # RUNTIME_TESTING_MENU config MEMTEST
lib/Makefile +1 -0
@@ -114,6 +114,7 @@ obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_CRC8) += crc8.o obj-$(CONFIG_XXHASH) += xxhash.o obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o +obj-$(CONFIG_TEST_GENERIC_ALLOCATOR) += test_genalloc.o obj-$(CONFIG_842_COMPRESS) += 842/ obj-$(CONFIG_842_DECOMPRESS) += 842/
lib/test_genalloc.c new file +419 -0
419 lines not on this page · raw message
Signature · 1 line
2.14.1
Andrew Morton
maintainerQuoted 53 lines · On Wed, 2 May 2018 05:05:19 +0400 Igor Stoppa <[email protected]> wrote:
This patchset was created as part of an older version of pmalloc, however it has value per-se, as it hardens the memory management for the generic allocator genalloc.
Genalloc does not currently track the size of the allocations it hands out.
Either by mistake, or due to an attack, it is possible that more memory than what was initially allocated is freed, leaving behind dangling pointers, ready for an use-after-free attack.
With this patch, genalloc becomes capable of tracking the size of each allocation it has handed out, when it's time to free it.
It can either verify that the size received is correct, when free is invoked, or it can decide autonomously how much memory to free, if the value received for the size parameter is 0.
These patches are proposed for beign merged into linux-next, to verify that they do not introduce regressions, by comparing the value received from the callers of the free function with the internal tracking.
For this reason, the patchset does not contain the removal of the size parameter from users of the
free()function.Later on, the "size" parameter can be dropped, and each caller can be adjusted accordingly.
However, I do not have access to most of the HW required for confirming that all of its users are not negatively affected.
This is where I believe having the patches in linux-next would help to coordinate with the maintaiers of the code that usesgen_alloc.Since there were comments about the (lack-of) efficiency introduced by this patchset, I have added some more explanations and calculations to the description of the first patch, the one adding the bitmap.
My conclusion is that this patch should not cause any major perfomance problem.Regarding the possibility of completely changing genalloc into some other type of allocator, I think it should not be a showstopper for this patchset, which aims to plug a security hole in genalloc, without introducing any major regression.
The security flaw is clear and present, while the benefit of introducing a new allocator is not clear, at least for the current users of genalloc.
And anyway the users of genalloc should be fixed to not pass any size parameter, which can be done after this patch is merged.
A newer, more efficient allocator will still benefit from not receiving a spurious parameter (size), when freeing memory.
Quoted 8 lines
...
Documentation/core-api/genalloc.rst | 4 + include/linux/genalloc.h | 112 +++--- lib/Kconfig.debug | 23 ++ lib/Makefile | 1 + lib/genalloc.c | 742 ++++++++++++++++++++++++++---------- lib/test_genalloc.c | 419 ++++++++++++++++++++
That's a big patch, and I'm having trouble believing that it's justified? We're trying to reduce the harm in bugs (none of which are known to exist) in a small number of drivers to avoid exploits, none of which are known to exist and which may not even be possible.
Or something like that. Perhaps all this is taking defensiveness a bit too far?
And a bitmap is a pretty crappy way of managing memory anyway, surely? If this code is indeed performance-sensitive then perhaps a reimplementation with some standard textbook allocator(?) is warranted?
Quoted 57 lines · Andrew Morton
Quoted 55 lines · On Wed, 2 May 2018 05:05:19 +0400 Igor Stoppa <[email protected]> wrote:
This patchset was created as part of an older version of pmalloc, however it has value per-se, as it hardens the memory management for the generic allocator genalloc.
Genalloc does not currently track the size of the allocations it hands out.
Either by mistake, or due to an attack, it is possible that more memory than what was initially allocated is freed, leaving behind dangling pointers, ready for an use-after-free attack.
With this patch, genalloc becomes capable of tracking the size of each allocation it has handed out, when it's time to free it.
It can either verify that the size received is correct, when free is invoked, or it can decide autonomously how much memory to free, if the value received for the size parameter is 0.
These patches are proposed for beign merged into linux-next, to verify that they do not introduce regressions, by comparing the value received from the callers of the free function with the internal tracking.
For this reason, the patchset does not contain the removal of the size parameter from users of the
free()function.Later on, the "size" parameter can be dropped, and each caller can be adjusted accordingly.
However, I do not have access to most of the HW required for confirming that all of its users are not negatively affected.
This is where I believe having the patches in linux-next would help to coordinate with the maintaiers of the code that usesgen_alloc.Since there were comments about the (lack-of) efficiency introduced by this patchset, I have added some more explanations and calculations to the description of the first patch, the one adding the bitmap.
My conclusion is that this patch should not cause any major perfomance problem.Regarding the possibility of completely changing genalloc into some other type of allocator, I think it should not be a showstopper for this patchset, which aims to plug a security hole in genalloc, without introducing any major regression.
The security flaw is clear and present, while the benefit of introducing a new allocator is not clear, at least for the current users of genalloc.
And anyway the users of genalloc should be fixed to not pass any size parameter, which can be done after this patch is merged.
A newer, more efficient allocator will still benefit from not receiving a spurious parameter (size), when freeing memory.
...
Quoted 8 lines
Quoted 6 lines
Documentation/core-api/genalloc.rst | 4 + include/linux/genalloc.h | 112 +++--- lib/Kconfig.debug | 23 ++ lib/Makefile | 1 + lib/genalloc.c | 742 ++++++++++++++++++++++++++---------- lib/test_genalloc.c | 419 ++++++++++++++++++++That's a big patch,
True, but I am afraid I do not see how to split it further without braking bisection.
and I'm having trouble believing that it's
Quoted 3 lines
justified? We're trying to reduce the harm in bugs (none of which are known to exist) in a small number of drivers to avoid exploits, none of which are known to exist and which may not even be possible.
Should I create one, to justify the patch?
Maybe, what we are really discussing if security should be reactive or preventive. And what amount of extra complexity is acceptable, without a current, present threat that has already materialized.
My personal take is, if I see something that I think I could exploit, most likely those who do write exploits for a (really well paid) living can do much more harm than I can even think of.
Quoted 2 lines
Or something like that. Perhaps all this is taking defensiveness a bit too far?
My main goal was to remove the "size" parameter from the free() call, without introducing noticeable performance regression.
Is that a reasonable endeavor?
After all, we have IOMMUs also for preventing similar types of attack.
The current users of genalloc are primarily:
* SRAM memory managers, which are attractive because they are used for example to store system wide state inbetween transitions to off, when some components (like the MMU) might not be even active.
* DMA page allocators, another nice side channel, where a DMA controller could be used to completely side-step the type of protection enforced by the MMU
Quoted 1 line
And a bitmap is a pretty crappy way of managing memory anyway, surely?
I did not put it there :-P
It also depends what one needs it for and if it's good enough. Or if something better is justified.
Quoted 2 lines
If this code is indeed performance-sensitive then perhaps a reimplementation with some standard textbook allocator(?) is warranted?
But, is it really performance sensitive?
I might be wrong, but I think this change that I am proposing is not really affecting performance.
I did get a question/comment about performance implications.
I have explained why I think my patches are not adding any real performance problem, in the comment of the patch that does the actual change to the bitmap, providing numbers that I think represent the current real use cases.
I was hoping in a reply to that. And a review of the code, also from performance perspective.
If I am making some wrong assumption or some mistake, I'll be the first one to acknowledge it, once it is pointed out, however I have not received specific comments about *why* this patch is either bad or wrong, besides "bitmaps are crappy".
From my POV, providing a better allocator would be nice, but I do not have time for it, right now.
And I am not even sure if it would make any real difference, with the current users of genalloc.
A new allocator would be a great thing for intensive allocation-release patterns, with lots of fragmentation.
The users of genalloc do not do that. If they did, I suspect someone else would have already come up with a patch to replace genalloc.
If a new allocator is being considered for the kernel, what I found to be possibly the best available at the moment is jemalloc [jemalloc.net]
It might even be better than other allocators currently in use in the kernel. But it would really need its own project, imho. It shouldn't be done as side activity of kernel hardening.
Coming back to genalloc, what I think *can* be said about it, is that:
- it's risky because it blindly relies on freeing what its callers asks.
- its current users probably wouldn't benefit from a better allocator
- hardening the API, provided that there is no performance regression, is a separate activity from rewriting the implementation
Maybe genalloc should be renamed to low_frequency_alloc :-P
--
igor