mm: Add the ram_latent_entropy kernel parameter
Subsystems (3)
PATCH 1/1 mm: Add the ram_latent_entropy kernel parameter
From: Emese Revfy <[email protected]>
When "ram_latent_entropy" is passed on the kernel command line, entropy will be extracted from up to the first 4GB of RAM while the runtime memory allocator is being initialized. This entropy isn't cryptographically secure, but does help provide additional unpredictability on otherwise low-entropy systems.
Based on work created by the PaX Team.
---
This patch has been extracted from the latent_entropy gcc plugin, as suggested by Linus: https://lkml.org/lkml/2016/8/8/840
--- Documentation/kernel-parameters.txt | 5 +++++ mm/page_alloc.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+)
Documentation/kernel-parameters.txt +5 -0
@@ -3245,6 +3245,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. raid= [HW,RAID] See Documentation/md.txt. + ram_latent_entropy + Enable a very simple form of latent entropy extraction + from the first 4GB of memory as the bootmem allocator + passes the memory pages to the buddy allocator. + ramdisk_size= [RAM] Sizes of RAM disks in kilobytes See Documentation/blockdev/ramdisk.txt.
mm/page_alloc.c +21 -0
@@ -64,6 +64,7 @@#include <linux/page_owner.h> #include <linux/kthread.h> #include <linux/memcontrol.h> +#include <linux/random.h> #include <asm/sections.h> #include <asm/tlbflush.h>
@@ -1236,6 +1237,15 @@ static void __free_pages_ok(struct page *page, unsigned int order)local_irq_restore (flags); } +bool __meminitdata ram_latent_entropy; + +static int __init setup_ram_latent_entropy (char *str) +{ + ram_latent_entropy = true; +return 0 ; +} +early_param ("ram_latent_entropy" , setup_ram_latent_entropy); +static void __init __free_pages_boot_core (struct page *page,unsigned int order) {unsigned int nr_pages =1 << order;
@@ -1251,6 +1261,17 @@ static void __init __free_pages_boot_core(struct page *page, unsigned int order)__ClearPageReserved (p);set_page_count (p,0 ); +if (ram_latent_entropy && !PageHighMem (page) && +page_to_pfn (page) <0x100000 ) { +u64 hash =0 ; +size_t index, end =PAGE_SIZE * nr_pages /sizeof (hash); +const u64 *data =lowmem_page_address (page); + +for (index =0 ; index < end; index++) + hash ^= hash + data[index]; +add_device_randomness ((const void *)&hash,sizeof (hash)); + } +page_zone (page)->managed_pages += nr_pages;set_page_refcounted (page);__free_pages (page, order);
Signature · 6 lines
2.7.4 -- Kees Cook Nexus Security
mm/page_alloc.c static void __init __free_pages_boot_core(struct page *page, unsigned int order) · line 1273
Quoted 10 lines · Kees Cook
mm/page_alloc.c +10
-0+if (ram_latent_entropy && !PageHighMem (page) && +page_to_pfn (page) <0x100000 ) { +u64 hash =0 ; +size_t index, end =PAGE_SIZE * nr_pages /sizeof (hash); +const u64 *data =lowmem_page_address (page); + +for (index =0 ; index < end; index++) + hash ^= hash + data[index]; +add_device_randomness ((const void *)&hash,sizeof (hash)); + }
When I was first reading this, I thought it was using the _addresses_ of the freed memory for entropy. But it's actually using the _contents_. The description could probably use a wee bit of sprucing up.
It might also be nice to say in the patch description (and the Documentation/) what you expect to be in this memory. It will obviously be zeros for the vast majority of the space, but I do wonder what else ends up in there in practice.
Why is it limited to 4GB? Just so it doesn't go and try to XOR the contents of a multi-TB system if it got turned on there? :)
Quoted 67 lines · Kees Cook
From: Emese Revfy <[email protected]>
When "
ram_latent_entropy" is passed on the kernel command line, entropy will be extracted from up to the first 4GB of RAM while the runtime memory allocator is being initialized. This entropy isn't cryptographically secure, but does help provide additional unpredictability on otherwise low-entropy systems.Based on work created by the PaX Team.
Signed-off-by Emese Revfy <[email protected]>[kees: renamed parameter, dropped relationship with plugin, updated log]Signed-off-by Kees Cook <[email protected]>---
This patch has been extracted from thelatent_entropygcc plugin, as suggested by Linus: https://lkml.org/lkml/2016/8/8/840--- Documentation/kernel-parameters.txt | 5 +++++ mm/page_alloc.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+)Documentation/kernel-parameters.txt +5
-0@@ -3245,6 +3245,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted. raid= [HW,RAID] See Documentation/md.txt. + ram_latent_entropy + Enable a very simple form of latent entropy extraction + from the first 4GB of memory as the bootmem allocator + passes the memory pages to the buddy allocator. + ramdisk_size= [RAM] Sizes of RAM disks in kilobytes See Documentation/blockdev/ramdisk.txt.mm/page_alloc.c +10
-0@@ -64,6 +64,7 @@#include <linux/page_owner.h> #include <linux/kthread.h> #include <linux/memcontrol.h> +#include <linux/random.h> #include <asm/sections.h> #include <asm/tlbflush.h> @@ -1236,6 +1237,15 @@ static void __free_pages_ok(struct page *page, unsigned int order)local_irq_restore (flags); } +bool __meminitdata ram_latent_entropy; + +static int __init setup_ram_latent_entropy (char *str) +{ + ram_latent_entropy = true; +return 0 ; +} +early_param ("ram_latent_entropy" , setup_ram_latent_entropy); +static void __init __free_pages_boot_core (struct page *page,unsigned int order) {unsigned int nr_pages =1 << order;@@ -1251,6 +1261,17 @@ static void __init __free_pages_boot_core(struct page *page, unsigned int order)__ClearPageReserved (p);
mm/page_alloc.c static void __init __free_pages_boot_core(struct page *page, unsigned int order) · line 1271
Quoted 10 lines
mm/page_alloc.c +8
-0set_page_count (p,0 ); +if (ram_latent_entropy && !PageHighMem (page) && +page_to_pfn (page) <0x100000 ) { +u64 hash =0 ; +size_t index, end =PAGE_SIZE * nr_pages /sizeof (hash); +const u64 *data =lowmem_page_address (page); + +for (index =0 ; index < end; index++) + hash ^= hash + data[index];
Won't the hash be the same across boots? Is this entropy addition for KASLR, since it is so early in boot?q
mm/page_alloc.c static void __init __free_pages_boot_core(struct page *page, unsigned int order) · line 1277
Quoted 7 lines
mm/page_alloc.c +3
-0+add_device_randomness ((const void *)&hash,sizeof (hash)); + } +page_zone (page)->managed_pages += nr_pages;set_page_refcounted (page);__free_pages (page, order);
Balbir Singh