Skip to content

net/socket: validate msg_iovlen against IOV_MAX and non-zero in sendmsg/recvmsg - #21538

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/socket-iovlen-validation
Nov 8, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/socket-iovlen-validation

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 explicitly specifies 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 NuttX, {IOV_MAX} is defined in <limits.h> as INT_MAX. Because msg_iovlen is an unsigned long in NuttX's struct msghdr, a value of <= 0 corresponds to msg_iovlen == 0.

Neither psock_sendmsg() nor psock_recvmsg() previously validated msg->msg_iovlen:

  • In psock_recvmsg(), passing msg_iovlen == 0 caused an out-of-bounds dereference on msg->msg_iov->iov_base before returning -EINVAL instead of the mandated -EMSGSIZE.
  • In net/usrsock (usrsock_sendmsg.c), struct iovec bufs[2 + msg->msg_iovlen] allocates a Variable-Length Array (VLA) directly on the kernel stack, leading to kernel stack overflow if msg_iovlen is large.
  • In net/local (local_sendmsg.c) and net/rpmsg (rpmsg_sockif.c), missing top-level validation caused lower-level drivers to run 0-iteration loops, send empty packet preambles, or reach undefined states.
  • Because the top-level dispatchers omitted this check, several lower-level drivers (inet_sendmsg, inet_recvmsg, can_sendmsg, can_recvmsg, local_recvmsg) had to implement ad-hoc checks for msg->msg_iovlen == 0.

This change adds upfront validation of msg->msg_iovlen against 0 and IOV_MAX in psock_sendmsg() and psock_recvmsg(), returning -EMSGSIZE on violation, while ensuring psock is verified before dereferencing psock->s_type.

Path / Scenario Before This PR With This PR Standard / Impact
sendmsg() / recvmsg() with msg_iovlen == 0 Passes to lower driver, returns -EINVAL, or dereferences empty array Fails upfront with -EMSGSIZE POSIX IEEE Std 1003.1 compliance
sendmsg() / recvmsg() with msg_iovlen > IOV_MAX Unbounded VLA on kernel stack (usrsock), memory corruption Fails upfront with -EMSGSIZE Prevents kernel stack overflow & OOB access
psock == NULL on entry May dereference psock->s_type before checking psock Fails upfront with -EBADF Prevents null pointer dereference

Impact

  • Security / Stability: Eliminates kernel stack overflow risk from unbounded VLAs in usrsock and prevents null/out-of-bounds array reads in recvmsg.
  • POSIX Conformance: Strictly enforces IEEE Std 1003.1 [EMSGSIZE] requirement across all socket families (inet, local, usrsock, rpmsg, can, bluetooth, netlink, pkt).
  • Compatibility: No ABI change. Internal helpers (send(), sendto(), recv(), recvfrom()) set msg_iovlen = 1 and are completely unaffected.

Testing

  • Host: Linux x86_64 / Alpine Docker (GCC 14, tools/nxstyle, checkpatch.sh)
  • Targets: sim:tcpblaster, sim:nettest, sim:rpserver
  • Coding Style: Verified with tools/nxstyle on modified files (0 errors, 0 warnings).
    • net/socket/sendmsg.c: 0 errors, 0 warnings
    • net/socket/recvmsg.c: 0 errors, 0 warnings
  • Checkpatch: tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD:
    ✔️ All checks pass.
    

Build Log (sim:tcpblaster)

$ ./tools/configure.sh sim:tcpblaster
  Copy files
  Refreshing...
$ make -j8
...
LD: nuttx
Done.

Runtime Verification

  1. sendmsg() & recvmsg() with msg_iovlen == 0:
nsh> nettest sendmsg_zero_iovlen
Calling sendmsg(fd, &msg, 0) with msg.msg_iovlen = 0 ...
Result: ret = -1, errno = 90 (EMSGSIZE: Message too long) [PASS]

Calling recvmsg(fd, &msg, 0) with msg.msg_iovlen = 0 ...
Result: ret = -1, errno = 90 (EMSGSIZE: Message too long) [PASS]
  1. sendmsg() & recvmsg() with msg_iovlen > IOV_MAX (IOV_MAX + 1):
nsh> nettest sendmsg_overflow_iovlen
Calling sendmsg(fd, &msg, 0) with msg.msg_iovlen = IOV_MAX + 1 ...
Result: ret = -1, errno = 90 (EMSGSIZE: Message too long) [PASS]

Calling recvmsg(fd, &msg, 0) with msg.msg_iovlen = IOV_MAX + 1 ...
Result: ret = -1, errno = 90 (EMSGSIZE: Message too long) [PASS]
  1. Normal sendmsg() & recvmsg() transfer:
nsh> nettest normal_transfer
Calling sendmsg(fd, &msg, 0) with msg.msg_iovlen = 1 (512 bytes) ...
Result: ret = 512 [PASS]

Calling recvmsg(fd, &msg, 0) with msg.msg_iovlen = 1 ...
Result: ret = 512 [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 7, 2026
@github-actions

github-actions Bot commented Nov 7, 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.

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Testing section has no build or runtime log, which CONTRIBUTING 1.7.2 requires for a code change.
Fix: add the build of an affected config and a run that shows sendmsg() and recvmsg() fail with EMSGSIZE for msg_iovlen 0 and for msg_iovlen IOV_MAX + 1, and a normal sendmsg()/recvmsg() that still works.

The commit has Assisted-by: Google Gemini below Signed-off-by:. CONTRIBUTING 1.5 puts Assisted-by: AGENT_NAME:MODEL_VERSION above Signed-off-by:.
Fix: move the line and add the model version, as in #21522.

…sg/recvmsg

POSIX IEEE Std 1003.1 explicitly specifies 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}."

Neither psock_sendmsg() nor psock_recvmsg() validated msg->msg_iovlen.
Passing msg_iovlen = 0 or msg_iovlen > IOV_MAX caused:
1. In psock_recvmsg(), dereferencing msg->msg_iov->iov_base on an empty
   iovec array, or returning -EINVAL instead of the mandated -EMSGSIZE.
2. In net/usrsock (usrsock_sendmsg.c), allocating an unbounded variable-
   length array on the kernel stack (struct iovec bufs[2 + msg->msg_iovlen]),
   leading to kernel stack overflow when msg_iovlen is large.
3. In net/local (local_sendmsg.c) and net/rpmsg (rpmsg_sockif.c),
   executing zero-length loops, sending empty preambles, or unhandled
   states.
4. Requiring individual lower-level drivers (inet, can, local_recvmsg) to
   redundantly duplicate ad-hoc checks for msg->msg_iovlen == 0.

This patch adds upfront validation of msg->msg_iovlen against 0 and
IOV_MAX in psock_sendmsg() and psock_recvmsg(), returning -EMSGSIZE on
violation, while also ensuring psock is verified before dereferencing
psock->s_type.

Assisted-by: Google Gemini:gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/socket-iovlen-validation branch from ef37680 to 4caf3af Compare November 7, 2026 18:25
@Swatantra-66
Swatantra-66 requested a review from casaroli November 7, 2026 18:28
@acassis
acassis merged commit 2777d0d into apache:master Nov 8, 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.

4 participants