Swatantra-66 opened a new pull request, #21681:
URL: https://github.com/apache/nuttx/pull/21681

   ## Summary
   POSIX IEEE Std 1003.1 explicitly mandates for both `sendmsg()` and 
`recvmsg()`:
   > "[EMSGSIZE] The msg_iovlen member of the msghdr structure is less than or 
equal to 0 or is greater than {IOV_MAX}."
   
   In `net/ieee802154`:
   1. In `ieee802154_sendmsg()` and `ieee802154_recvmsg()`, 
`msg->msg_iov->iov_base` and `msg->msg_iov->iov_len` were dereferenced during 
local variable declarations before validating `msg->msg_iovlen` or 
`msg->msg_iov`. Passing `msg_iovlen == 0` or `msg_iov == NULL` resulted in an 
immediate out-of-bounds pointer dereference (CWE-476 / CWE-125).
   2. Passing `msg_iovlen <= 0` or `msg_iovlen > IOV_MAX` returned `-ENOTSUP` 
instead of POSIX-mandated `-EMSGSIZE`.
   3. In `ieee802154_recvfrom_rxqueue()`, `copylen` was not clamped to 
`pstate->ir_buflen`, allowing incoming frames larger than the user receive 
buffer to cause a buffer overflow (CWE-120).
   4. In `ieee802154_recvmsg()` and `ieee802154_recvfrom_eventhandler()`, 
completion checked `(ret > 0)`. When receiving a zero-length frame or zero-byte 
read, `ret == 0` was not recognized as completion, causing the calling thread 
to sleep indefinitely on `state.ir_sem`.
   
   This patch:
   - Adds upfront validation of `msg->msg_iovlen == 0 || msg->msg_iovlen > 
IOV_MAX`, returning `-EMSGSIZE`.
   - Preserves `-ENOTSUP` when `msg->msg_iovlen > 1` (scatter/gather not 
supported).
   - Validates `msg->msg_iov == NULL` and non-null buffer pointers for positive 
lengths (`len > 0 && buf == NULL`), returning `-EINVAL`.
   - Defers accessing `msg->msg_iov[0]` until all bounds and nullity checks 
pass.
   - Clamps `copylen` to `pstate->ir_buflen` in `ieee802154_recvfrom_rxqueue()` 
and sets `MSG_TRUNC` in `msg->msg_flags` when truncation occurs per datagram 
semantics.
   - Changes completion checks to `(ret >= 0)` to allow zero-byte frames to 
complete cleanly.
   - Adds `#include <limits.h>` in both files.
   
   ### Before & After Comparison
   
   | Check / Behavior | Before (Master) | After (This PR) |
   | :--- | :--- | :--- |
   | **`msg->msg_iov` Dereference** | Dereferenced during local variable 
declaration before checking `msg_iovlen` or NULL $\rightarrow$ Crashes on 
empty/NULL vector (CWE-476 / CWE-125) | Deferred until all pointer, length, and 
bounds checks pass |
   | **`msg_iovlen <= 0` or `> IOV_MAX`** | Returned `-ENOTSUP` (POSIX 
violation) | Returns `-EMSGSIZE` per POSIX IEEE Std 1003.1 |
   | **`msg_iov == NULL` or `(len > 0 && buf == NULL)`** | Unhandled / NULL 
pointer dereference | Validated upfront, returns `-EINVAL` |
   | **Incoming Datagram Overflow** | Unbounded `memcpy` of `copylen = 
iob->io_len - iob->io_offset` into `ir_buffer` (CWE-120) | Clamped to 
`ir_buflen`, flags `MSG_TRUNC` in `msg->msg_flags` |
   | **Zero-byte Frame / Read** | Checked `ret > 0`, causing 0-byte frames to 
fail completion check and hang forever on `state.ir_sem` | Checked `ret >= 0`, 
cleanly completing and returning `0` |
   
   ## Impact
   - **Is new feature added?** NO
   - **Impact on user:** Fixes potential invalid pointer dereferences, buffer 
overflows, and thread hangs in IEEE 802.15.4 sockets; ensures strict POSIX 
compliance.
   - **Impact on build:** None.
   - **Impact on hardware:** None.
   - **Impact on documentation:** None.
   - **Impact on security:** Prevents CWE-476 (NULL pointer dereference), 
CWE-125 (Out-of-bounds Read), and CWE-120 (Buffer Overflow).
   
   ## Testing
   - **Host OS:** Linux (Ubuntu 22.04 Docker container `nuttx-builder`)
   - **Target:** `sim:pf_ieee802154`
   
   ### Build Command:
   ```bash
   ./tools/configure.sh -l sim:pf_ieee802154
   make -j$(nproc)
   ```
   
   ### Build & Link Output:
   ```text
   CC:  net/ieee802154_sendmsg.c
   CC:  net/ieee802154_recvmsg.c
   IN: net/libnet.a -> staging/libnet.a
   LD:  nuttx
   Pac SIM with dynamic libs..
   '/lib/x86_64-linux-gnu/libz.so.1' -> 'sim-pac/libs/libz.so.1'
   '/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
   '/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
   SIM elf with dynamic libs archive in nuttx.tgz
   ```
   
   ### Code Style & Checkpatch:
   ```text
   $ ./tools/nxstyle net/ieee802154/ieee802154_sendmsg.c
   $ ./tools/nxstyle net/ieee802154/ieee802154_recvmsg.c
   (0 errors, 0 warnings)
   
   $ ./tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD
   ✔️ All checks pass.
   ```


-- 
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