From 606e105f49c06f5cdc3b87ced6097959cdaf405b Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Thu, 11 Jun 2026 00:05:21 -0300 Subject: [PATCH] csprng: fix undefined behavior in entropy pool initialization Fixed cppcheck error: shifting 64-bit value by 504 bits is undefined. The TSC has only 64 bits (8 bytes), but the loop was iterating 64 times. Changed to extract only 8 bytes from TSC and fill remaining pool with tick count variations. Test: make test-all PASS (131/131 smoke, 38/38 analyzer, 111/111 host) --- src/kernel/csprng.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/kernel/csprng.c b/src/kernel/csprng.c index 432a6cf0..89f0b8ba 100644 --- a/src/kernel/csprng.c +++ b/src/kernel/csprng.c @@ -113,11 +113,15 @@ void csprng_init(void) { g_csprng_chacha.counter = 1; g_csprng_chacha.initialized = 1; - - /* Initialize entropy pool */ - for (int i = 0; i < 64; i++) { + + /* Initialize entropy pool with TSC bytes (8 bytes) */ + for (int i = 0; i < 8; i++) { g_entropy_pool.pool[i] = (uint8_t)(tsc >> (i * 8)); } + /* Fill remaining pool with tick count variations */ + for (int i = 8; i < 64; i++) { + g_entropy_pool.pool[i] = (uint8_t)(ticks ^ (i * 0x9E3779B9)); + } g_entropy_pool.pool_pos = 0; g_entropy_pool.reseed_counter = 0; -- 2.43.0