Skip to content

Commit 91fd6d0

Browse files
Flavio Ceolinandrewboie
Flavio Ceolin
authored andcommitted
kernel: thread: Fix randomness problem with stack pointer random
In some platforms the size of size_t can be different of 4 bytes. Use sys_rand_get to proper fill this variable. Signed-off-by: Flavio Ceolin <[email protected]>
1 parent 8134001 commit 91fd6d0

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
lines changed

kernel/include/kernel_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extern void z_smp_init(void);
112112

113113
extern void smp_timer_init(void);
114114

115-
extern u32_t z_early_boot_rand32_get(void);
115+
extern void z_early_boot_rand_get(u8_t *buf, size_t length);
116116

117117
#if CONFIG_STACK_POINTER_RANDOM
118118
extern int z_stack_adjust_initialized;

kernel/init.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static FUNC_NORETURN void switch_to_main_thread(void)
426426
}
427427
#endif /* CONFIG_MULTITHREADING */
428428

429-
static void z_early_boot_rand_get(u8_t *buf, size_t length)
429+
void z_early_boot_rand_get(u8_t *buf, size_t length)
430430
{
431431
int n = sizeof(u32_t);
432432
#ifdef CONFIG_ENTROPY_HAS_DRIVER
@@ -482,15 +482,6 @@ static void z_early_boot_rand_get(u8_t *buf, size_t length)
482482
}
483483
}
484484

485-
u32_t z_early_boot_rand32_get(void)
486-
{
487-
u32_t retval;
488-
489-
z_early_boot_rand_get((u8_t *)&retval, sizeof(retval));
490-
491-
return retval;
492-
}
493-
494485
/**
495486
*
496487
* @brief Initialize kernel

kernel/thread.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ static inline size_t adjust_stack_size(size_t stack_size)
403403
size_t random_val;
404404

405405
if (!z_stack_adjust_initialized) {
406-
random_val = z_early_boot_rand32_get();
406+
z_early_boot_rand_get((u8_t *)&random_val, sizeof(random_val));
407407
} else {
408-
random_val = sys_rand32_get();
408+
sys_rand_get((u8_t *)&random_val, sizeof(random_val));
409409
}
410410

411411
/* Don't need to worry about alignment of the size here,

tests/crypto/rand32/src/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/*
1010
* This tests the following random number routines:
11-
* u32_t z_early_boot_rand32_get(void);
11+
* void z_early_boot_rand_get(u8_t *buf, size_t length)
1212
* u32_t sys_rand32_get(void);
1313
*/
1414

@@ -35,9 +35,10 @@ void test_rand32(void)
3535
u32_t buf[N_VALUES];
3636

3737
/* Test early boot random number generation function */
38-
last_gen = z_early_boot_rand32_get();
39-
zassert_true(last_gen != z_early_boot_rand32_get(),
40-
"z_early_boot_rand32_get failed");
38+
z_early_boot_rand_get((u8_t *)&last_gen, sizeof(last_gen));
39+
z_early_boot_rand_get((u8_t *)&gen, sizeof(gen));
40+
zassert_true(last_gen != gen,
41+
"z_early_boot_rand_get failed");
4142

4243
/*
4344
* Test subsequently calls sys_rand32_get(), checking

0 commit comments

Comments
 (0)