]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
csprng: fix undefined behavior in entropy pool initialization
authorTulio A M Mendes <[email protected]>
Thu, 11 Jun 2026 03:05:21 +0000 (00:05 -0300)
committerTulio A M Mendes <[email protected]>
Thu, 11 Jun 2026 03:05:21 +0000 (00:05 -0300)
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

index 432a6cf0361aa9d4f97100d062a2faeb4644ec37..89f0b8ba10920b997e08669fe20e575ecf400133 100644 (file)
@@ -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;