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

   depends-on: apache/nuttx/pull/21902
   
   ## Summary
   
   `arc4random_buf()` read the random device 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. `getrandom()` sets errno, so the kernel set the errno of the caller 
(in a kernel build in the TLS of the caller, in user memory). When the device 
could not be opened, `arc4random_buf()` used a hash of `clock()`, which every 
process can compute. A path resolves in the root directory of the caller, so in 
a `chroot()` jail without `/dev` the open fails.
   
   - The first commit makes `arc4random_buf()` open and read the device itself 
with the `_NX_FILE` macros of apache/nuttx#21902, so it does not set errno in 
the flat build and in the kernel copy. The source of the bytes does not change. 
A read of 0 bytes now ends the loop with `-EIO`.
   - 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, in the flat build and in the kernel copy. The pool replaces only this 
weak fallback: the hardware random device stays the first source.
   
   This PR needs apache/nuttx#21902 (the `_NX_FILE` macros) and stays a draft 
until it merges.
   
   ## Impact
   
   `libs/libc/stdlib/lib_arc4random.c` only. In the flat build, `arc4random()` 
of an application no longer sets errno; it has no error result. The user copy 
of the protected and kernel builds uses `open()` and `read()`, as `getrandom()` 
did: errno is set as before. Without `CONFIG_CRYPTO_RANDOM_POOL` the second 
commit changes nothing; with it, the bytes change only when no device can be 
read. No API, Kconfig or documentation change.
   
   ## 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`, with apache/nuttx#21902:
   
   | Case | master | this PR, no pool | this PR, with 
`CONFIG_CRYPTO_RANDOM_POOL` |
   |---|---|---|---|
   | udp-fdfull | FAIL: the port is the hash, errno `EMFILE` | PASS, errno 0 | 
PASS |
   | tcp-jail | FAIL: the port is the hash, errno `ENOENT` | FAIL, as expected: 
no device, no pool | PASS |
   
   The same in the kernel and in the flat build. With apache/nuttx#21902 alone, 
`getrandom()` takes no descriptor any more, so the descriptor part of 
udp-fdfull is fixed there; this PR keeps errno unchanged when the open or the 
read fails, and adds the pool. ostest passes (status 0) on qemu-armv8a:knsh and 
qemu-armv8a:nsh. Builds with no new warning: rv-virt:pnsh64 (protected, RISC-V) 
with `CONFIG_DEV_URANDOM`, and mps2-an500:citest_protected. `nm`: the kernel 
copy (and the flat libc) uses `file_open()` and `file_read()` (and 
`up_rngbuf()` with the pool), and the user copy of the protected build uses 
`open()`, `read()` and `close()`. 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