Skip to content

Commit 89f6e24

Browse files
didrikrMaureenHelm
authored andcommitted
rand: xoroshiro128: fix buffer overflow
If rand32_xoroshiro128::z_impl_sys_rand_get is called with outlen not divisible by 4, it will overflow the dst buffer. This happens because blocksize is not changed from 4 to the difference between outlen and len. If outlen is < 4, z_impl_sys_rand_get will be stuck in an infinite loop that keeps writing random bytes outside the buffer. If outlen is > 4, z_impl_sys_rand_get returns after the correct number of loops, but it writes every byte to the buffer, not just outlen number of bytes. This causes the buffer to be overflowed with up to and including 3 bytes. Signed-off-by: Didrik Rokhaug <didrik.rokhaug@gmail.com>
1 parent 202adf5 commit 89f6e24

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

subsys/random/rand32_xoroshiro128.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void z_impl_sys_rand_get(void *dst, size_t outlen)
106106
while (len < outlen) {
107107
ret = xoroshiro128_next();
108108
if ((outlen-len) < sizeof(ret)) {
109-
blocksize = len;
109+
blocksize = outlen - len;
110110
(void)memcpy(udst, &ret, blocksize);
111111
} else {
112112
(*udst++) = ret;

0 commit comments

Comments
 (0)