casaroli opened a new pull request, #21614: URL: https://github.com/apache/nuttx/pull/21614
## Summary Fixes of Unix domain sockets (`net/local`), one commit per fix: - net/local: Use only addrlen bytes of an address. `bind()`, `connect()` and `sendto()` read the name until a NUL and did not use `addrlen`. No NUL has to follow a name (an abstract name has none), so the bytes after the address became part of the name: `bind()` made a wrong name and `sendto()` to an abstract name failed with `ENOENT`. In a kernel build the bytes after the copied address are what an earlier system call left on the stack. Now the address is copied to the stack with the rest cleared (`local_copyaddr()`). - net/local: Do not take the lock twice when a listener closes. `connect()` returns before `accept()`. When a listening socket closed with such a connection in its queue, `local_release()` held `g_local_lock` and called `local_subref()`, which calls `local_release()` again and takes the lock again: an assertion with `DEBUG_ASSERTIONS`, else the thread waits for itself and no local socket works after it. Now each connection leaves the queue with the lock held and is released without it. - net/local: Fix SO_PEERCRED on a socket without a peer. `getsockopt(SO_PEERCRED)` read `conn->lc_peer` without a check. The ends of `socketpair()`, an unconnected and a listening socket have no `lc_peer`, so the kernel read through a NULL pointer. Now it does as Linux: the ends of a pair (`lc_pair`) give the credentials of the process that made the pair, a listening socket gives its own, and any other socket gives pid 0, uid -1 and gid -1 (the call succeeds). - net/local: Give a connection the credentials of the listening socket. `connect()` makes the server side of the connection in the context of the client, so `SO_PEERCRED` on the client gave the client itself. Now the server side takes the credentials of the listener. - net/local: Send the header of a datagram on a socket pair. Since 9b2e7c7e9a4, `send()` on a `SOCK_DGRAM` socket pair fails with `EDESTADDRREQ`; before that it wrote only the data, without the header that `recv()` reads first. Now it writes the header (address length 0) and the data, whole or not at all: `EAGAIN` when it must not wait and the FIFO has no space, `EMSGSIZE` when the datagram is larger than the FIFO, `ECONNREFUSED` after the close of the peer (as on Linux). `recv()` returns 0 when the peer was closed and no datagram is left. - net/local: Fix poll() on a datagram socket pair. A pair used the poll code of an unconnected datagram socket, which always reports `POLLOUT`. Now a pair uses the code of a connected stream socket (the two FIFOs): `POLLOUT` when more than half of the FIFO to the peer is free, `POLLHUP` after the close of the peer. `local_set_pollthreshold()` never set the `POLLOUT` threshold; now it does, also after `SO_RCVBUF`. - net/local: Let a signal end a blocked recv() of a local socket. `local_fifo_read()` read again after `-EINTR`. In a kernel or protected build the signal action runs only when the system call returns, so a handler did not run and `SIGTERM` did not end a process that waited in `recv()` or `read()`. In a flat build the handler ran and the call went on. Now the read returns `-EINTR` before the first byte; a datagram is read whole once its first byte is in. ## Impact Local sockets only. The same code in flat, protected and kernel builds; no architecture code. What a program sees: - An address whose name has no NUL in `addrlen` now works; an address with a NUL behaves as before. Stack cost: one `struct sockaddr_un` in `psock_local_bind()`, `local_connect()` and `local_sendto()`. - A listening socket that closes with unaccepted clients no longer stops all local sockets; the clients see the end of the connection. - `SO_PEERCRED` (with `NET_LOCAL_SCM`), as on Linux: an end of `socketpair()` gives its creator, a listening socket gives its own process, an unconnected socket gives pid 0, uid -1 and gid -1 (all three were a kernel fault); the client of a stream connection gets the server, not itself. - Datagram socket pairs: `send()` works (it failed with `EDESTADDRREQ`); after the close of the peer `send()` gives `ECONNREFUSED`, and `recv()` returns 0 when no datagram is left (Linux waits); `poll()` gives `POLLOUT` only when the peer has space, and `POLLHUP` after the close of the peer. - `recv()`, `recvfrom()`, `recvmsg()` and `read()` of a local socket fail with `EINTR` when a signal handler interrupts the wait for data, also in a flat build (before, the call went on), and also with `SA_RESTART`, which NuttX does not implement. A signal whose default action ends the process now ends a process that waits in these calls. `send()` and `write()` that wait for space do not change. - `struct local_conn_s`: a `bool lc_pair` in the padding after `lc_state`; `lc_inout_fds` also without `NET_LOCAL_STREAM`. - Documentation: no page describes Unix domain sockets or the pipe ioctls; no change. ## Testing Tests: the nuttx-apps PR of branch apps-net-local-fixes-test-up (`cmocka_net_local_dgram`: 75 existing and 8 new cases; `cmocka_net_local`: `SO_PEERCRED` cases), and the programs `dgramabs` (addresses), `listenclose`, `peercred`, `dgrampair` (socket pairs), `sockintr` (signals, each case in a child) and `dgramconn` (`connect()`). On Linux (7.0, arm64 container) the new cmocka cases and `peercred` pass; the `SO_PEERCRED` results for an unconnected and a listening socket were checked there. qemu-armv8a (arm64, HVF, 1 CPU), each case alone, a boot ends at a crash or a hang. knsh: kernel build, programs from `/system/bin`; nsh: flat build, builtins. Options: `NET`, `NET_IPv4`, `NET_LOOPBACK`, `NET_TCP`, `NET_UDP`, `NET_SOCKOPTS`, `NET_LOCAL` with stream, datagram and SCM, `PIPES`, `FS_TMPFS`, `DEBUG_ASSERTIONS`, `SIG_DEFAULT`, `SCHED_WAITPID`. | | master cb52e915d95 | this PR | |---|---|---| | `cmocka_net_local_dgram`, knsh and nsh | 75 existing cases PASS; the 8 new cases FAIL: `addrlen` (`ENAMETOOLONG`), `stream_listenclose` (`Assertion failed at semaphore/sem_wait.c:135`; nsh: the system stops), `pair_send`, `pair_close`, `poll_pair` (`send()` -1), `pair_full` (no `EAGAIN`), `recv_intr`, `stream_intr` (the call returns the data sent after the signal) | 83 PASS | | `cmocka_net_local` | knsh: kernel panic in `test_local_peercred_pair`; nsh: the system stops there | knsh 6 PASS, nsh 4 PASS (no `fork()`) | | `dgramabs` | knsh 5, nsh 6 of 11 FAIL | knsh 11 PASS; nsh 10 PASS, `recvtcplong` FAIL (a TCP case, the same on master) | | `listenclose` | knsh: the assertion, `close`, `eof` FAIL; nsh: the system stops | 3 PASS | | `peercred` (knsh) | kernel panic | 5 PASS | | `dgrampair` | `sendrecv`, `bounds` FAIL, then a hang | 18 PASS | | `sockintr` | knsh: the recv cases FAIL, then a hang; nsh: 10 of 18 FAIL | the recv cases PASS; the 2 `SA_RESTART` cases FAIL (see Impact); the cases of a signal during a `send()` that waits FAIL, and on knsh the run stops there: this PR does not change `send()` | | `dgramconn` | 21 PASS | 21 PASS | | ostest, knsh and nsh | status 0 | status 0 | Builds with `EXTRAFLAGS="-Wno-cpp -Werror"`, with `NET`, `NET_LOCAL`, `NETDEV_LATEINIT`, `NET_SOCKOPTS`, `PIPES`: qemu-armv8a:nsh (flat) with stream, datagram and `NET_LOCAL_SCM`, OK, no warning. An earlier form of this branch (the same 7 commits and two more) also built with datagram sockets only (`lc_inout_fds` is new there), with stream sockets only, with pipes and no network, and as rv-virt:knsh64 (kernel, riscv64), mps2-an500:knsh (protected, armv7-m) and qemu-armv7a:knsh (kernel, armv7-a), each with no warning in this code. Compilers: aarch64-none-elf GCC 14.2, arm-none-eabi GCC 15.3, riscv-none-elf GCC 14.3. `tools/checkpatch.sh -c -u -m -g` 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]
