casaroli opened a new pull request, #21902:
URL: https://github.com/apache/nuttx/pull/21902

   ## Summary
   
   `arc4random_buf()` read `/dev/random`, then `/dev/urandom`, with 
`getrandom()`. The kernel uses it for example for the first local port of a 
socket (`NET_PORT_RANDOM_INIT()`), the TCP initial sequence key and the crypto 
keys and IVs. This had three problems:
   
   - `getrandom()` sets errno. A successful `bind()` changed the errno of the 
caller (ENOENT when `/dev/random` does not exist). In a kernel build errno is 
in the TLS of the caller, in user memory.
   - `getrandom()` opens the device with a descriptor of the caller. When the 
descriptor table of the caller is full, the open fails and `arc4random_buf()` 
uses its fallback, a hash of `clock()`, which every process can compute.
   - The path resolves in the root directory of the caller. In a `chroot()` 
jail without `/dev`, both opens fail and the kernel also uses the `clock()` 
hash.
   
   The first commit makes `arc4random_buf()` read the device with `file_open()` 
and `file_read()`: no descriptor of the caller and no errno. The source of the 
bytes does not change. The second commit uses the entropy pool (`up_rngbuf()`) 
in place of the `clock()` hash when `CONFIG_CRYPTO_RANDOM_POOL` is set and no 
device can be read. The pool replaces only this weak fallback: the hardware 
random device stays the first source.
   
   ## Impact
   
   Both commits change the flat build and the kernel copy (libkc) of the 
protected and kernel builds: the usual libc test `CONFIG_BUILD_FLAT || 
__KERNEL__`. The user copy of the protected and kernel builds does not change. 
In a flat build, `arc4random()` of an application no longer takes a descriptor 
and no longer sets errno; it has no error result. Without `CONFIG_DEV_URANDOM` 
and `CONFIG_DEV_RANDOM` the first commit changes nothing. Without 
`CONFIG_CRYPTO_RANDOM_POOL` the second commit changes nothing; with it, the 
bytes change only when no device can be read. No API change, no documentation 
change. libc code only, no architecture code.
   
   ## Testing
   
   A test program binds the first UDP and the first TCP socket after boot to 
port 0 and compares the port with the `clock()` hash of the fallback (`clock()` 
read before and after `bind()`). Case udp-fdfull: all descriptors of the caller 
are in use; the port must not be the hash, and errno must not change. Case 
tcp-jail: the caller is in `chroot("/proc")`, without `/dev`; the port must not 
be the hash.
   
   qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build), arm64, 
HVF, with `CONFIG_NET` (IPv4, TCP, UDP, loopback), `CONFIG_FS_CHROOT` and 
`CONFIG_DEV_URANDOM`:
   
   - master, with the entropy pool, kernel and flat build: udp-fdfull FAIL (the 
port is the hash, errno EMFILE), tcp-jail FAIL (the port is the hash, errno 
ENOENT).
   - this PR, without the pool (xorshift128 `/dev/urandom`), kernel and flat 
build: udp-fdfull PASS (errno 0); tcp-jail FAIL, as expected: no device in the 
jail and no pool, so the `clock()` hash stays.
   - this PR, with the pool (`CONFIG_CRYPTO_RANDOM_POOL`, 
`CONFIG_DEV_URANDOM_RANDOM_POOL`), kernel and flat build: udp-fdfull PASS, 
tcp-jail PASS.
   
   ostest passes (status 0) on qemu-armv8a:knsh and qemu-armv8a:nsh in every 
run. Builds with no new warning: rv-virt:pnsh64 (protected, RISC-V) with 
`CONFIG_DEV_URANDOM`, without and with the pool, and 
stm32f777zit6-meadow:f7corecomp (flat, Cortex-M7, sets 
`CONFIG_CRYPTO_RANDOM_POOL`). `nm` shows that the kernel copy (and the flat 
libc) uses `file_open()`/`file_read()` (and `up_rngbuf()` with the pool), and 
that the user copy of the protected build still uses `getrandom()`. checkpatch 
passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to