casaroli opened a new pull request, #22470: URL: https://github.com/apache/nuttx/pull/22470
## Summary `getsockname()` and `getpeername()` of a netlink socket did not handle a buffer shorter than `struct sockaddr_nl`, and `getsockname()` of a `CAN_RAW` socket failed with `EINVAL` for a buffer shorter than `struct sockaddr_can`. POSIX says that the stored address is truncated in this case, and Linux does so. Now these functions make the address in a local structure, copy as much of it as the buffer holds, and set `*addrlen` to the length of the whole address, as Linux does (getsockname(2): "addrlen will return a value greater than was supplied to the call"). So the caller sees the truncation. Local, Bluetooth, IEEE 802.15.4 and CAN ISO-TP sockets already truncate the address. The third commit updates `Documentation/os/networking/socketcan.rst`, which said that a short buffer gets `EINVAL`. The fourth commit makes `bind()` and `connect()` of a netlink socket return `EINVAL` for an address length shorter than `struct sockaddr_nl`, as Linux does; before, only a debug check named this case. ## Impact A short buffer now gets the truncated address and a return value of 0. A full buffer gets the same address as before. A netlink `bind()` or `connect()` with a short address length fails with `EINVAL`. No API, ABI or Kconfig change. The same in all build modes and on all architectures. ## Testing New case `test_can_name_short` in `cmocka_net_can` (apache/nuttx-apps#NNNN): `getsockname()` of a bound `CAN_RAW` socket into 2 bytes. On qemu-armv8a:nsh (flat) with netlink, CAN and loopback, Arm GNU 13.2, QEMU TCG: | | master | this PR | |---|---|---| | test_can_name_short | FAILED (`EINVAL`) | PASSED | | the other cases of cmocka_net_can | 4 PASSED, 2 SKIPPED (no can0) | 4 PASSED, 2 SKIPPED (no can0) | A test program calls the netlink `getsockname()` and `getpeername()` with a 4-byte buffer: with this PR both return 0, store the first 4 bytes of the address and set the length to 12. The same program calls `bind()` and `connect()` with an address length of 4: with this PR both fail with `EINVAL`. Builds with `-Werror` in the CI image, with netlink and CAN: qemu-armv8a:nsh (flat), qemu-armv8a:knsh (kernel, with the apps), rv-virt:netnsh64 (flat), rv-virt:knetnsh64 (kernel, with the apps). -- 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]
