Skip to content

Commit dd0f0cf

Browse files
mpetorvalds
authored andcommitted
random: Fix crashes with sparse node ids
On a system with sparse node ids, eg. a powerpc system with 4 nodes numbered like so: node 0: [mem 0x0000000000000000-0x00000007ffffffff] node 1: [mem 0x0000000800000000-0x0000000fffffffff] node 16: [mem 0x0000001000000000-0x00000017ffffffff] node 17: [mem 0x0000001800000000-0x0000001fffffffff] The code in rand_initialize() will allocate 4 pointers for the pool array, and initialise them correctly. However when go to use the pool, in eg. extract_crng(), we use the numa_node_id() to index into the array. For the higher numbered node ids this leads to random memory corruption, depending on what was kmalloc'ed adjacent to the pool array. Fix it by using nr_node_ids to size the pool array. Fixes: 1e7f583 ("random: make /dev/urandom scalable for silly userspace programs") Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7f155c7 commit dd0f0cf

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
#include <linux/genhd.h>
250250
#include <linux/interrupt.h>
251251
#include <linux/mm.h>
252+
#include <linux/nodemask.h>
252253
#include <linux/spinlock.h>
253254
#include <linux/kthread.h>
254255
#include <linux/percpu.h>
@@ -1656,7 +1657,6 @@ static int rand_initialize(void)
16561657
{
16571658
#ifdef CONFIG_NUMA
16581659
int i;
1659-
int num_nodes = num_possible_nodes();
16601660
struct crng_state *crng;
16611661
struct crng_state **pool;
16621662
#endif
@@ -1666,8 +1666,7 @@ static int rand_initialize(void)
16661666
crng_initialize(&primary_crng);
16671667

16681668
#ifdef CONFIG_NUMA
1669-
pool = kmalloc(num_nodes * sizeof(void *),
1670-
GFP_KERNEL|__GFP_NOFAIL|__GFP_ZERO);
1669+
pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL);
16711670
for_each_online_node(i) {
16721671
crng = kmalloc_node(sizeof(struct crng_state),
16731672
GFP_KERNEL | __GFP_NOFAIL, i);

0 commit comments

Comments
 (0)