Skip to contentSkip to conversation

linux-next: mm: hardening: Track genalloc allocations

PATCH Superseded Igor Stoppa 4 participants 11 messages Apr 29, 2018 – Apr 30, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover linux-next: mm: hardening: 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, when free is invoked, is correct, 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.

Later on, the "size" parameter can be dropped, and each caller can be adjusted accordingly.

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 +++---
 init/main.c                         |   2 +
 lib/Kconfig                         |  15 +
 lib/Makefile                        |   1 +
 lib/genalloc.c                      | 742 ++++++++++++++++++++++++++----------
 lib/test_genalloc.c                 | 410 ++++++++++++++++++++
 7 files changed, 1031 insertions(+), 255 deletions(-)
 create mode 100644 lib/test_genalloc.c
Signature · 1 line
2.14.1
#1 lore raw

PATCH 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.
Examples:
* 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, 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) could be dropped, in favor of a simplified API.

---
 include/linux/genalloc.h | 112 +++----
 lib/genalloc.c           | 742 ++++++++++++++++++++++++++++++++++-------------
 2 files changed, 599 insertions(+), 255 deletions(-)
include/linux/genalloc.h +60 -52
index 872f930f1b06..ff7229520656 100644
@@ -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
#2 lore raw

PATCH 2/3 Add label and license to genalloc.rst

In reply to Igor Stoppa

Add 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
index 6b38a39fab24..0b5ade832ee8 100644
@@ -1,3 +1,7 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+.. _genalloc:
+
 The genalloc/genpool subsystem
 ==============================
 
Signature · 1 line
2.14.1
#3 lore raw

PATCH 3/3 genalloc: selftest

In reply to Igor Stoppa

Introduce 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.

The testing takes place in the very early stages of main.c, to ensure that failures in genalloc are caught before they can cause unexplained erratic behavior in any of genalloc users.

Therefore, it would not be advisable to implement it as module.

---
 init/main.c         |   2 +
 lib/Kconfig         |  15 ++
 lib/Makefile        |   1 +
 lib/test_genalloc.c | 410 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 428 insertions(+)
 create mode 100644 lib/test_genalloc.c
init/main.c +2 -0
index b795aa341a3a..b3b319d91b0e 100644
@@ -91,6 +91,7 @@
 #include <linux/cache.h>
 #include <linux/rodata_test.h>
 #include <linux/jump_label.h>
+#include <linux/test_genalloc.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>
@@ -679,6 +680,7 @@ asmlinkage __visible void __init start_kernel(void)
 	 */
 	mem_encrypt_init();
 
+	test_genalloc();
 #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
index 09565d779324..2bf89af50728 100644
@@ -303,6 +303,21 @@ config DECOMPRESS_LZ4
 config GENERIC_ALLOCATOR
 	bool
 
+config TEST_GENERIC_ALLOCATOR
+	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 TEST_GENERIC_ALLOCATOR_VERBOSE
+	bool "make the genalloc tester more verbose"
+	default n
+	select TEST_GENERIC_ALLOCATOR
+	help
+	  More information will be displayed during the self-testing.
+
 #
 # reed solomon support is select'ed if needed
 #
lib/Makefile +1 -0
index 384713ff70d3..2c66346ab246 100644
@@ -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 +410 -0

410 lines not on this page · raw message

Signature · 1 line
2.14.1
reviewer
#4 lore raw

Re: [PATCH 3/3] genalloc: selftest

Quoted 10 lines · Igor Stoppa

Introduce 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.

Quoted 7 lines

The testing takes place in the very early stages of main.c, to ensure that failures in genalloc are caught before they can cause unexplained erratic behavior in any of genalloc users.

Therefore, it would not be advisable to implement it as module.

Hi,

Quoted 21 lines
---
 init/main.c         |   2 +
 lib/Kconfig         |  15 ++
 lib/Makefile        |   1 +
 lib/test_genalloc.c | 410 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 428 insertions(+)
 create mode 100644 lib/test_genalloc.c
init/main.c +1 -0
index b795aa341a3a..b3b319d91b0e 100644
@@ -91,6 +91,7 @@
 #include <linux/cache.h>
 #include <linux/rodata_test.h>
 #include <linux/jump_label.h>
+#include <linux/test_genalloc.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>

init/main.c asmlinkage __visible void __init start_kernel(void) · line 683

Quoted 4 lines
init/main.c +1 -0
@@ -679,6 +680,7 @@ asmlinkage __visible void __init start_kernel(void)
 	 */
 	mem_encrypt_init();
 
+	test_genalloc();

Is there a stub for test_genalloc() when its config option is not enabled? I don't see it.

lib/Kconfig config DECOMPRESS_LZ4 · line 305

Quoted 11 lines
init/main.c +0 -0
 #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 +0 -0
index 09565d779324..2bf89af50728 100644
@@ -303,6 +303,21 @@ config DECOMPRESS_LZ4
 config GENERIC_ALLOCATOR
 	bool
 

These TEST_ kconfig symbols should be in lib/Kconfig.debug, not lib/Kconfig.

lib/Kconfig config DECOMPRESS_LZ4 · line 309

Quoted 4 lines
lib/Kconfig +4 -0
+config TEST_GENERIC_ALLOCATOR
+	bool "genalloc tester"
+	default n
+	select GENERIC_ALLOCATOR

This should depend on GENERIC_ALLOCATOR, not select it.

See TEST_PARMAN, TEST_BPF, TEST_FIRMWARE, TEST_SYSCTL, TEST_DEBUG_VIRTUAL in lib/Kconfig.debug.

lib/Kconfig config DECOMPRESS_LZ4 · line 317

Quoted 8 lines
lib/Kconfig +8 -0
+	help
+	  Enable automated testing of the generic allocator.
+	  The testing is primarily for the tracking of allocated space.
+
+config TEST_GENERIC_ALLOCATOR_VERBOSE
+	bool "make the genalloc tester more verbose"
+	default n
+	select TEST_GENERIC_ALLOCATOR

depends on TEST_GENERIC_ALLOCATOR

Quoted 26 lines
lib/Kconfig +3 -0
+	help
+	  More information will be displayed during the self-testing.
+
 #
 # reed solomon support is select'ed if needed
 #
lib/test_genalloc.c new file +13 -0
new file mode 100644
index 000000000000..ab9984861517
@@ -0,0 +1,410 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * test_genalloc.c
+ *
+ * (C) Copyright 2017 Huawei Technologies Co. Ltd.
+ * Author: Igor Stoppa <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/init.h>
+#include <linux/vmalloc.h>
+#include <linux/string.h>

lib/test_genalloc.c line 23

Quoted 10 lines
lib/test_genalloc.c new file +10 -0
+#include <linux/debugfs.h>
+#include <linux/atomic.h>
+#include <linux/genalloc.h>
+
+#include <linux/test_genalloc.h>
+
+
+/*
+ * In case of failure of any of these tests, memory corruption is almost
+ * guarranteed; allowing the boot to continue means risking to corrupt
      guaranteed;
Quoted 3 lines
lib/test_genalloc.c new file +3 -0
+ * also any filesystem/block device accessed write mode.
+ * Therefore, BUG_ON() is used, when testing.
+ */
Signature · 1 line
~Randy
#5 lore raw

Re: [PATCH 3/3] genalloc: selftest

Quoted 1 line · Randy Dunlap

On 04/28/2018 07:45 PM, Igor Stoppa wrote:

[...]

Quoted 4 lines

init/main.c asmlinkage __visible void __init start_kernel(void) · line 683

Quoted 1 line
init/main.c +1 -0
+	test_genalloc();

Is there a stub for test_genalloc() when its config option is not enabled? I don't see it.

I failed to add to the patch include/linux/test_genalloc.h :-/ That's where the stub is hiding.

Quoted 10 lines

lib/Kconfig config DECOMPRESS_LZ4 · line 305

Quoted 8 lines
lib/Kconfig +0 -0
index 09565d779324..2bf89af50728 100644
@@ -303,6 +303,21 @@ config DECOMPRESS_LZ4
  config GENERIC_ALLOCATOR
  	bool
 

These TEST_ kconfig symbols should be in lib/Kconfig.debug, not lib/Kconfig.

ok, I will fix it

Quoted 9 lines

lib/Kconfig config DECOMPRESS_LZ4 · line 309

Quoted 4 lines
lib/Kconfig +4 -0
+config TEST_GENERIC_ALLOCATOR
+	bool "genalloc tester"
+	default n
+	select GENERIC_ALLOCATOR

This should depend on GENERIC_ALLOCATOR, not select it.

See TEST_PARMAN, TEST_BPF, TEST_FIRMWARE, TEST_SYSCTL, TEST_DEBUG_VIRTUAL in lib/Kconfig.debug.

I was actually wondering about this.
The dependency I came up with allows to perform the test even if nothing is selecting genalloc, but ok, if this is how it is done, I'll adjust to it.

Quoted 10 lines

lib/Kconfig config DECOMPRESS_LZ4 · line 317

Quoted 8 lines
lib/Kconfig +8 -0
+	help
+	  Enable automated testing of the generic allocator.
+	  The testing is primarily for the tracking of allocated space.
+
+config TEST_GENERIC_ALLOCATOR_VERBOSE
+	bool "make the genalloc tester more verbose"
+	default n
+	select TEST_GENERIC_ALLOCATOR

depends on TEST_GENERIC_ALLOCATOR

ok

[...]

Quoted 3 lines

lib/Kconfig config DECOMPRESS_LZ4 · line 318

Quoted 1 line
lib/Kconfig +1 -0
+ * guarranteed; allowing the boot to continue means risking to corrupt
       guaranteed;

hmmm

--

thanks, igor

#6 lore raw

Re: [PATCH 3/3] genalloc: selftest

In reply to Igor Stoppa
Thank you for the patch! Yet something to improve:

Hi Igor,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.17-rc3 next-20180426]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/linux-next-mm-hardening-Track-genalloc-allocations/20180430-064850
config: i386-tinyconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

>> init/main.c:94:10: fatal error: linux/test_genalloc.h: No such file or directory
    #include <linux/test_genalloc.h>
             ^~~~~~~~~~~~~~~~~~~~~~~

compilation terminated.

vim +94 init/main.c

    13	
    14	#include <linux/types.h>
    15	#include <linux/extable.h>
    16	#include <linux/module.h>
    17	#include <linux/proc_fs.h>
    18	#include <linux/binfmts.h>
    19	#include <linux/kernel.h>
    20	#include <linux/syscalls.h>
    21	#include <linux/stackprotector.h>
    22	#include <linux/string.h>
    23	#include <linux/ctype.h>
    24	#include <linux/delay.h>
    25	#include <linux/ioport.h>
    26	#include <linux/init.h>
    27	#include <linux/initrd.h>
    28	#include <linux/bootmem.h>
    29	#include <linux/acpi.h>
    30	#include <linux/console.h>
    31	#include <linux/nmi.h>
    32	#include <linux/percpu.h>
    33	#include <linux/kmod.h>
    34	#include <linux/vmalloc.h>
    35	#include <linux/kernel_stat.h>
    36	#include <linux/start_kernel.h>
    37	#include <linux/security.h>
    38	#include <linux/smp.h>
    39	#include <linux/profile.h>
    40	#include <linux/rcupdate.h>
    41	#include <linux/moduleparam.h>
    42	#include <linux/kallsyms.h>
    43	#include <linux/writeback.h>
    44	#include <linux/cpu.h>
    45	#include <linux/cpuset.h>
    46	#include <linux/cgroup.h>
    47	#include <linux/efi.h>
    48	#include <linux/tick.h>
    49	#include <linux/sched/isolation.h>
    50	#include <linux/interrupt.h>
    51	#include <linux/taskstats_kern.h>
    52	#include <linux/delayacct.h>
    53	#include <linux/unistd.h>
    54	#include <linux/utsname.h>
    55	#include <linux/rmap.h>
    56	#include <linux/mempolicy.h>
    57	#include <linux/key.h>
    58	#include <linux/buffer_head.h>
    59	#include <linux/page_ext.h>
    60	#include <linux/debug_locks.h>
    61	#include <linux/debugobjects.h>
    62	#include <linux/lockdep.h>
    63	#include <linux/kmemleak.h>
    64	#include <linux/pid_namespace.h>
    65	#include <linux/device.h>
    66	#include <linux/kthread.h>
    67	#include <linux/sched.h>
    68	#include <linux/sched/init.h>
    69	#include <linux/signal.h>
    70	#include <linux/idr.h>
    71	#include <linux/kgdb.h>
    72	#include <linux/ftrace.h>
    73	#include <linux/async.h>
    74	#include <linux/sfi.h>
    75	#include <linux/shmem_fs.h>
    76	#include <linux/slab.h>
    77	#include <linux/perf_event.h>
    78	#include <linux/ptrace.h>
    79	#include <linux/pti.h>
    80	#include <linux/blkdev.h>
    81	#include <linux/elevator.h>
    82	#include <linux/sched_clock.h>
    83	#include <linux/sched/task.h>
    84	#include <linux/sched/task_stack.h>
    85	#include <linux/context_tracking.h>
    86	#include <linux/random.h>
    87	#include <linux/list.h>
    88	#include <linux/integrity.h>
    89	#include <linux/proc_ns.h>
    90	#include <linux/io.h>
    91	#include <linux/cache.h>
    92	#include <linux/rodata_test.h>
    93	#include <linux/jump_label.h>
  > 94	#include <linux/test_genalloc.h>
    95	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#7 lore raw

Re: [PATCH 3/3] genalloc: selftest

In reply to Igor Stoppa
Thank you for the patch! Yet something to improve:

Hi Igor,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.17-rc3 next-20180426]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/linux-next-mm-hardening-Track-genalloc-allocations/20180430-064850
config: um-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=um 

All errors (new ones prefixed by >>):

>> lib/test_genalloc.c:18:10: fatal error: linux/test_genalloc.h: No such file or directory
    #include <linux/test_genalloc.h>
             ^~~~~~~~~~~~~~~~~~~~~~~

compilation terminated.

vim +18 lib/test_genalloc.c

    17	
  > 18	#include <linux/test_genalloc.h>
    19	
    20	

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#8 lore raw
In reply to Igor Stoppa
Quoted 9 lines · Igor Stoppa

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.

This is a good point. It is worth hardening genalloc.
But I still don't like the cost of the bitmap. I've been reading about allocators and I found Bonwick's paper from 2001:
https://www.usenix.org/legacy/event/usenix01/full_papers/bonwick/bonwick.pdf
Section 4 describes the vmem allocator which would seem to have superior performance and lower memory overhead than the current genalloc allocator, never mind the hardened allocator.

Maybe there's been an advnace in resource allocator technology since then that someone more familiar with CS research can point out.

#9 lore raw
Quoted 19 lines · Matthew Wilcox
Quoted 9 lines · Igor Stoppa

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.

This is a good point. It is worth hardening genalloc.
But I still don't like the cost of the bitmap. I've been reading about allocators and I found Bonwick's paper from 2001:
https://www.usenix.org/legacy/event/usenix01/full_papers/bonwick/bonwick.pdf
Section 4 describes the vmem allocator which would seem to have superior performance and lower memory overhead than the current genalloc allocator, never mind the hardened allocator.

Quoted 2 lines

Maybe there's been an advnace in resource allocator technology since then that someone more familiar with CS research can point out.

A quick search on google shows that there have been tons of improvements.

I found various implementation of vmem, not all with GPLv2 compatible license.

The most interesting one seems to be a libvmem from Intel[1], made to use jemalloc[2], for persistent memory.

jemalloc is, apaprently, the coolest kid on the block, when it comes to modern memory management.

But this is clearly a very large lump of work.

First of all, it should be assessed if jemalloc is really what the kernel could benefit from (my guess is yes, but it's just a guess), then if the license is compatible or if it can be relicensed for use in the kernel.

And, last, but not least, how to integrate the ongoing work in a way that doesn't require lots of effort to upgrade to new releases.

Even if it looks very interesting, I simply do not have time to do this, not for the next 5-6 months, for sure.

What I *can* offer to do, is the cleanup of the users of genalloc, by working with the various maintainers to remove the "size" parameter in the calls to gen_pool_free(), iff the patch I submitted can be merged.

This is work that has to be done anyway and does not preclude, later on, to phase out genalloc in favor of jemalloc or whatever is deemed to be the most effective solution.

There are 2 goals here:
1) plug potential security holes in the use of genalloc
2) see if some new allocator can improve the performance (and it might well be that the improvement can be extended also to other allocators used in kernel)

We seem to agree that 1) is a real need. Regarding 2), I think it should have a separate life.

going back to 1), your objections so far, as far as I can tell are:

a) it will use more memory for the bitmap
b) it will be slower, because the bitmap is doubled
c) the "ends" or "starts" bitmaps should be separate

I think I have already answered them, but I'll recap my replies:

a) yes, it will double it, but if it was ok to "waste" some memory when I was asked to rewrite the pmalloc allocator to not use genalloc, in favor of speed, I think the same criteria applies here: on average it will probably take at most one more page per pool. It doesn't seem a huge loss.

b) the bitmap size is doubled, that much is true, however interleaving the "busy" and "start" bitmaps will ensure locality of the meta data and between the cache prefetch algorithm and the hints give to the compiler, it shouldn't make a huge difference, compared to the pre-patch case. Oh, and the size of a bitmap seems to be overall negligible, from what users I saw.

c) "busy" and "start" are interleaved, to avoid having to do explicit locking, instead of relying on the intrinsic atomicity of accessing bitfields coming from the same word, as it is now.

And I'm anyway proposing to merge this into linux-next, so that there are more eyeballs looking for problems. I'm not proposing to merge it straight in the next kernel release.
Removing the size parameter from the various gen_pool_free() will impact not only the direct callers, but also their callers and so on, which means that it will take some time to purge all the layers of calls from "size".

During this time, it's likely that issues will surface, if there is any lurking.

And the removal of the parameter will require getting ACK from each user, so it should be enough to ensure that everyone is happy about the overall performance.

But I would start addressing the security issue, since I think the cost of doubling the bitmap will not be noticeable.

I'd like to hear if you disagree with my reasoning.

And did I overlook some other objection?

--
igor

#10 lore raw

oops, sorry, I forgot the references :-(

Quoted 73 lines · Igor Stoppa
Quoted 16 lines · Matthew Wilcox
Quoted 9 lines · Igor Stoppa

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.

This is a good point.  It is worth hardening genalloc. But I still don't like the cost of the bitmap.  I've been reading about allocators and I found Bonwick's paper from 2001:
https://www.usenix.org/legacy/event/usenix01/full_papers/bonwick/bonwick.pdf

Quoted 6 lines

Section 4 describes the vmem allocator which would seem to have superior performance and lower memory overhead than the current genalloc allocator, never mind the hardened allocator.

Maybe there's been an advnace in resource allocator technology since then that someone more familiar with CS research can point out.

A quick search on google shows that there have been tons of improvements.

I found various implementation of vmem, not all with GPLv2 compatible license.

The most interesting one seems to be a libvmem from Intel[1], made to use jemalloc[2], for persistent memory.

jemalloc is, apaprently, the coolest kid on the block, when it comes to modern memory management.

But this is clearly a very large lump of work.

First of all, it should be assessed if jemalloc is really what the kernel could benefit from (my guess is yes, but it's just a guess), then if the license is compatible or if it can be relicensed for use in the kernel.

And, last, but not least, how to integrate the ongoing work in a way that doesn't require lots of effort to upgrade to new releases.

Even if it looks very interesting, I simply do not have time to do this, not for the next 5-6 months, for sure.

What I *can* offer to do, is the cleanup of the users of genalloc, by working with the various maintainers to remove the "size" parameter in the calls to gen_pool_free(), iff the patch I submitted can be merged.

This is work that has to be done anyway and does not preclude, later on, to phase out genalloc in favor of jemalloc or whatever is deemed to be the most effective solution.

There are 2 goals here:
1) plug potential security holes in the use of genalloc
2) see if some new allocator can improve the performance (and it might well be that the improvement can be extended also to other allocators used in kernel)

We seem to agree that 1) is a real need. Regarding 2), I think it should have a separate life.

going back to 1), your objections so far, as far as I can tell are:

a) it will use more memory for the bitmap
b) it will be slower, because the bitmap is doubled
c) the "ends" or "starts" bitmaps should be separate

I think I have already answered them, but I'll recap my replies:

a) yes, it will double it, but if it was ok to "waste" some memory when I was asked to rewrite the pmalloc allocator to not use genalloc, in favor of speed, I think the same criteria applies here: on average it will probably take at most one more page per pool. It doesn't seem a huge loss.

b) the bitmap size is doubled, that much is true, however interleaving the "busy" and "start" bitmaps will ensure locality of the meta data and between the cache prefetch algorithm and the hints give to the compiler, it shouldn't make a huge difference, compared to the pre-patch case. Oh, and the size of a bitmap seems to be overall negligible, from what users I saw.

c) "busy" and "start" are interleaved, to avoid having to do explicit locking, instead of relying on the intrinsic atomicity of accessing bitfields coming from the same word, as it is now.

And I'm anyway proposing to merge this into linux-next, so that there are more eyeballs looking for problems. I'm not proposing to merge it straight in the next kernel release.
Removing the size parameter from the various gen_pool_free() will impact not only the direct callers, but also their callers and so on, which means that it will take some time to purge all the layers of calls from "size".

During this time, it's likely that issues will surface, if there is any lurking.

Quoted 8 lines

And the removal of the parameter will require getting ACK from each user, so it should be enough to ensure that everyone is happy about the overall performance.

But I would start addressing the security issue, since I think the cost of doubling the bitmap will not be noticeable.

I'd like to hear if you disagree with my reasoning.

And did I overlook some other objection?

[1] https://github.com/pmem/pmdk/tree/master/src/libvmem
[2] http://jemalloc.net/