casaroli opened a new pull request, #21616: URL: https://github.com/apache/nuttx/pull/21616
## Summary The loopback driver polls with `devif_poll(dev, NULL)`. `devif_poll_out()` accepts a NULL callback: it only loops the packet back with `devif_loopback()`. Three paths call the callback directly, so a user program can make the HPWORK thread jump to address 0 with a socket on `lo`. One commit for each fix: - **net/can: Bind a CAN socket only to a CAN device.** `can_conn_bind()` (net/can/can.c) accepted the index of any device. A `CAN_RAW` socket bound to `lo` crashed the kernel on the first `write()`: `devif_poll_can_connections()` calls the callback. `can_conn_bind()` now returns `-ENODEV` for a device that is not `NET_LL_CAN`, as Linux does. `bind()` is the only call that sets the device of a CAN socket: `sendto()` does not use the address, and `SO_BINDTODEVICE` does not change the CAN device. - **net/devif: Fix a NULL callback call in the packet socket poll.** `devif_poll_pkt_connections()`: a `send()` on an `AF_PACKET` socket bound to `lo`. It now calls `devif_loopback()` when there is no callback. It does not use `devif_poll_out()`: that function builds the L2 header with `arp_out()` or `neighbor_out()` and fragments IP packets, but the frame of a packet socket already has its L2 header. - **net/devif: Fix a NULL callback call in devif_poll_queue().** When the reassembly of an IPv4 fragment that came in on `lo` times out, the ICMP Time Exceeded message goes to the `d_fragout` queue of `lo`. A packet socket bound to `lo` can send such a fragment. The same one-line fix. The other direct callback calls in `devif_poll.c` do not get a NULL callback: the bluetooth and IEEE 802.15.4 paths poll radio devices only, `arp_poll()` Ethernet devices only, and the flat buffer path of `devif_poll()` devices with a `d_buf` only. ## Impact - `bind()` of a CAN socket to a device that is not a CAN device now fails with `ENODEV`. Before, it returned 0 and the socket could not work: a write crashed the kernel on `lo`, and on another device the poll gave the CAN frame to the driver of that device. Every CAN driver in the tree registers as `NET_LL_CAN`, so a bind to a CAN device or to index 0 (all CAN devices) does not change. - A frame that a packet socket sends on `lo` now follows the loopback rules of the other protocols: an IP packet to the address of `lo` goes to the packet sockets and to the IP stack, and other frames are dropped. - Drivers with a poll callback see no change. Common net code: flat, protected and kernel builds, all architectures. No header or Kconfig change. No documentation change: Documentation/ does not describe these paths, and Documentation/os/networking/socketcan.rst binds to `can0` only. ## Testing The tests are in apache/nuttx-apps#NNNN (`testing/nettest`): `cmocka_net_can` (bind to a missing index gives `ENODEV`; bind to index 0 gives 0; bind to `can0`, skipped without a CAN device; `SO_BINDTODEVICE` to `lo` and `sendto()` with `lo` in the address on an unbound socket fail; bind to `lo` gives `ENODEV`, and if it gives 0 the test writes a frame) and `cmocka_net_pkt` (an IPv4 packet sent on `lo` by a packet socket comes back on an `ETH_P_ALL` socket; the first fragment of an IPv4 packet sent on `lo` times out in the reassembly, and the ICMP Time Exceeded message comes back). Options: `NET_IPv4`, `NET_LOOPBACK`, `NETDEV_IFINDEX`, `NET_UDP`, `NET_ICMP`, `NET_IPFRAG`, `NET_PKT`, `NET_CAN`, `NET_SOCKOPTS`, `NET_BINDTODEVICE`, `DEBUG_ASSERTIONS`; `NET_PKT_WRITE_BUFFERS` in the kernel build only (the flat build uses the unbuffered send path); cmocka with `TESTING_NET_TEST`, `TESTING_NET_CAN`, `TESTING_NET_PKT`. | Configuration | master c8c0661c997 | this PR | ostest (this PR) | |---|---|---|---| | qemu-armv8a:knsh (kernel build, arm64, HVF) | `cmocka_net_can`: 4 pass, `test_can_bind_lo` panics in hpwork (ELR 0); `cmocka_net_pkt`: `test_pkt_lo_send` panics in hpwork (ELR 0) | `cmocka_net_can` 5 pass, 1 skipped (no `can0`); `cmocka_net_pkt` 2 pass | status 0 | | qemu-armv8a:nsh (flat build, arm64, HVF) | the same as knsh | the same as knsh | status 0 | An earlier run of the two devif commits alone, with a test program instead of `cmocka_net_pkt`: mps2-an500:knsh (protected build, Cortex-M7, arm-none-eabi-gcc 15.3, QEMU TCG) hard fault in hpwork with PC 0 on master, pass with the commits; with only the first devif commit, the flat build panics in hpwork after the reassembly timeout. `tools/checkpatch.sh -c -u -m -g` passes. Not run: a bind to a real CAN device (on qemu-armv8a:netnsh the QEMU `kvaser_pci` card makes the boot fault in `kvaser_probe()` before any socket is used, and QEMU did not attach `ctucan_pci` to its CAN bus), CMake builds, sim, a real Ethernet device (the call with a callback does not change). -- 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]
