This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 28db4789c34ac9afac20ce8499c0c2b888dd7d6c
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 15 06:50:18 2026 +0100

    libs/libc: Fall back to the entropy pool in arc4random_buf().
    
    arc4random_buf() reads /dev/random, then /dev/urandom.  When both
    fail, it used a hash of clock(), which every process can compute.  A
    path resolves in the root directory of the calling process: in a
    chroot() jail without /dev both opens fail.  Thus the random bytes of
    the kernel, for example the first local port of a socket or the TCP
    initial sequence key, came from the clock() hash.
    
    With CONFIG_CRYPTO_RANDOM_POOL, arc4random_buf() now takes the bytes
    from the entropy pool with up_rngbuf() in place of the clock() hash, in
    the flat build and in the kernel copy of the protected and kernel
    builds.  The pool replaces only this weak fallback: the hardware random
    device stays the first source.  The user copy of the protected and
    kernel builds does not change, and without CONFIG_CRYPTO_RANDOM_POOL
    nothing changes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/stdlib/lib_arc4random.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libs/libc/stdlib/lib_arc4random.c 
b/libs/libc/stdlib/lib_arc4random.c
index b1b760e69eb..ae68c56140e 100644
--- a/libs/libc/stdlib/lib_arc4random.c
+++ b/libs/libc/stdlib/lib_arc4random.c
@@ -36,6 +36,7 @@
 
 #include <nuttx/fs/fs.h>
 #include <nuttx/hashtable.h>
+#include <nuttx/random.h>
 
 /****************************************************************************
  * Private Functions
@@ -117,6 +118,14 @@ void arc4random_buf(FAR void *bytes, size_t nbytes)
     }
 #endif
 
+#if defined(CONFIG_CRYPTO_RANDOM_POOL) && \
+    (defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__))
+  /* No random device could be read: use the entropy pool */
+
+  up_rngbuf(bytes, nbytes);
+  return;
+#endif
+
   /* Fallback to hash of clock_systime_ticks(), minus nbytes to avoid getting
    * same tick count when looping more than once.
    */

Reply via email to