Skip to content

net/ieee802154: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21681

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/ieee802154-iovlen-bounds
Nov 17, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/ieee802154-iovlen-bounds

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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:

./tools/configure.sh -l sim:pf_ieee802154
make -j$(nproc)

Build & Link Output:

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:

$ ./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.

@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small labels Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216, It looks like Linux (arm-nxp) hit a transient timeout/flakiness unrelated to net/ieee802154.
Could you please re-run the failed job

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 commit:
- 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.
- Changes completion checks to (ret >= 0) to allow zero-byte frames to
  complete cleanly.
- Adds #include <limits.h> in both files.

Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/ieee802154-iovlen-bounds branch from 32d84a3 to e432e67 Compare November 14, 2026 07:26
@GUIDINGLI
GUIDINGLI merged commit 6ffb500 into apache:master Nov 17, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants