Skip to content

Commit f7e466e

Browse files
CHIPOPO64bittehcaster
authored andcommitted
mm/slab: Replace invocation of weak PRNG
The Slab allocator randomization uses the prandom_u32 PRNG. That was added to prevent attackers to obtain information on the heap state, by randomizing the freelists 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, we used get_random_u32_below, to achieve uniformity. In addition, we changed the freelist_init_state union to struct, since the rnd_state inside which is used to store the state of prandom_u32, is not needed anymore, since get_random_u32 maintains its own state. Signed-off-by: David Keisar Schmidt <david.keisarschm@mail.huji.ac.il> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent 44c026a commit f7e466e

1 file changed

Lines changed: 9 additions & 20 deletions

File tree

mm/slab.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,44 +2359,34 @@ static void cache_init_objs_debug(struct kmem_cache *cachep, struct slab *slab)
23592359

23602360
#ifdef CONFIG_SLAB_FREELIST_RANDOM
23612361
/* Hold information during a freelist initialization */
2362-
union freelist_init_state {
2363-
struct {
2364-
unsigned int pos;
2365-
unsigned int *list;
2366-
unsigned int count;
2367-
};
2368-
struct rnd_state rnd_state;
2362+
struct freelist_init_state {
2363+
unsigned int pos;
2364+
unsigned int *list;
2365+
unsigned int count;
23692366
};
23702367

23712368
/*
23722369
* Initialize the state based on the randomization method available.
23732370
* return true if the pre-computed list is available, false otherwise.
23742371
*/
2375-
static bool freelist_state_initialize(union freelist_init_state *state,
2372+
static bool freelist_state_initialize(struct freelist_init_state *state,
23762373
struct kmem_cache *cachep,
23772374
unsigned int count)
23782375
{
23792376
bool ret;
2380-
unsigned int rand;
2381-
2382-
/* Use best entropy available to define a random shift */
2383-
rand = get_random_u32();
2384-
2385-
/* Use a random state if the pre-computed list is not available */
23862377
if (!cachep->random_seq) {
2387-
prandom_seed_state(&state->rnd_state, rand);
23882378
ret = false;
23892379
} else {
23902380
state->list = cachep->random_seq;
23912381
state->count = count;
2392-
state->pos = rand % count;
2382+
state->pos = get_random_u32_below(count);
23932383
ret = true;
23942384
}
23952385
return ret;
23962386
}
23972387

23982388
/* Get the next entry on the list and randomize it using a random shift */
2399-
static freelist_idx_t next_random_slot(union freelist_init_state *state)
2389+
static freelist_idx_t next_random_slot(struct freelist_init_state *state)
24002390
{
24012391
if (state->pos >= state->count)
24022392
state->pos = 0;
@@ -2417,7 +2407,7 @@ static void swap_free_obj(struct slab *slab, unsigned int a, unsigned int b)
24172407
static bool shuffle_freelist(struct kmem_cache *cachep, struct slab *slab)
24182408
{
24192409
unsigned int objfreelist = 0, i, rand, count = cachep->num;
2420-
union freelist_init_state state;
2410+
struct freelist_init_state state;
24212411
bool precomputed;
24222412

24232413
if (count < 2)
@@ -2446,8 +2436,7 @@ static bool shuffle_freelist(struct kmem_cache *cachep, struct slab *slab)
24462436

24472437
/* Fisher-Yates shuffle */
24482438
for (i = count - 1; i > 0; i--) {
2449-
rand = prandom_u32_state(&state.rnd_state);
2450-
rand %= (i + 1);
2439+
rand = get_random_u32_below(i + 1);
24512440
swap_free_obj(slab, i, rand);
24522441
}
24532442
} else {

0 commit comments

Comments
 (0)