Skip to content

Commit ffe4dfe

Browse files
CHIPOPO64bittehcaster
authored andcommitted
mm/slab_common: Replace invocation of weak PRNG
The Slab allocator randomization inside slab_common.c uses the prandom_u32 PRNG. That was added to prevent attackers to obtain information on the heap state. However, this PRNG turned out to be weak, as noted in commit c51f8f8 To fix it, we have changed the invocation of prandom_u32_state to get_random_u32 to ensure the PRNG is strong. Since a modulo operation is applied right after that, in the Fisher-Yates shuffle, we used get_random_u32_below, to achieve uniformity. Signed-off-by: David Keisar Schmidt <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent f7e466e commit ffe4dfe

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

mm/slab_common.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ EXPORT_SYMBOL(kmalloc_large_node);
11411141

11421142
#ifdef CONFIG_SLAB_FREELIST_RANDOM
11431143
/* Randomize a generic freelist */
1144-
static void freelist_randomize(struct rnd_state *state, unsigned int *list,
1144+
static void freelist_randomize(unsigned int *list,
11451145
unsigned int count)
11461146
{
11471147
unsigned int rand;
@@ -1152,8 +1152,7 @@ static void freelist_randomize(struct rnd_state *state, unsigned int *list,
11521152

11531153
/* Fisher-Yates shuffle */
11541154
for (i = count - 1; i > 0; i--) {
1155-
rand = prandom_u32_state(state);
1156-
rand %= (i + 1);
1155+
rand = get_random_u32_below(i + 1);
11571156
swap(list[i], list[rand]);
11581157
}
11591158
}
@@ -1162,7 +1161,6 @@ static void freelist_randomize(struct rnd_state *state, unsigned int *list,
11621161
int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
11631162
gfp_t gfp)
11641163
{
1165-
struct rnd_state state;
11661164

11671165
if (count < 2 || cachep->random_seq)
11681166
return 0;
@@ -1171,10 +1169,7 @@ int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
11711169
if (!cachep->random_seq)
11721170
return -ENOMEM;
11731171

1174-
/* Get best entropy at this stage of boot */
1175-
prandom_seed_state(&state, get_random_long());
1176-
1177-
freelist_randomize(&state, cachep->random_seq, count);
1172+
freelist_randomize(cachep->random_seq, count);
11781173
return 0;
11791174
}
11801175

0 commit comments

Comments
 (0)