Skip to content

net/icmp: validate msg_iovlen and defer iov dereference - #21698

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/icmp-iovlen-bounds
Nov 11, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/icmp-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/icmp (icmp_sendmsg.c and icmp_recvmsg.c), several vulnerabilities and POSIX non-compliances existed:

  1. msg->msg_iov->iov_base and msg->msg_iov->iov_len were dereferenced in local variable initializations before validating msg->msg_iovlen or msg->msg_iov. Passing msg_iovlen == 0 or msg_iov == NULL resulted in immediate NULL pointer / out-of-bounds dereferences (CWE-476 / CWE-125).
  2. Passing msg_iovlen <= 0 or msg_iovlen > IOV_MAX returned -ENOTSUP instead of POSIX-mandated -EMSGSIZE.
  3. Datagram truncation was not signaled to user space; msg_flags was never updated with MSG_TRUNC.
  4. In struct icmp_recvfrom_s, recv_buflen was typed as uint16_t and recv_result as int16_t. Providing a standard 64 KiB (65,536 bytes) buffer wrapped recv_buflen to 0, prematurely discarding valid replies.
  5. In icmp_recvmsg(), an artificial check if (len < ICMP_HDRLEN) return -EINVAL; prevented callers from providing short or zero-length buffers to receive truncated prefixes or probe datagram arrival.
  6. In recvfrom_eventhandler(), large replies spanning chained I/O buffers (dev->d_iob) were copied using memcpy(pstate->recv_buf, IPBUF(offset), recvsize);. Because IPBUF(offset) points only into the first IOB buffer, copying past the first IOB resulted in heap out-of-bounds reads (CWE-125).

Before & After Comparison

Condition / Scenario Before (master) After (this PR)
msg->msg_iovlen == 0 Immediate OOB dereference / crash in local declaration Fails early with -EMSGSIZE per POSIX
msg->msg_iovlen > IOV_MAX Returns -ENOTSUP Fails early with -EMSGSIZE per POSIX
msg->msg_iov == NULL Immediate NULL pointer dereference Validated, returns -EINVAL
len > 0 && buf == NULL Unchecked, crashes during copy Validated, returns -EINVAL
Buffer $\ge 65,536$ bytes Wraps to 0 (uint16_t), packet discarded Stored as size_t, accepted properly
Reply larger than user buffer Excess silently truncated, no flag set Sets msg->msg_flags |= MSG_TRUNC in both readahead & eventhandler
Short buffer (< ICMP_HDRLEN) Artificially fails with -EINVAL Reaches truncation path, returns prefix with MSG_TRUNC
Reply spanning IOB chain Raw memcpy(..., IPBUF(offset), ...) reads past 1st IOB (Heap OOB read) iob_copyout(...) safely traverses the chained IOB segments

Impact

  • Is new feature added? No.
  • User impact? POSIX-compliant error codes (-EMSGSIZE) and truncation reporting (MSG_TRUNC) for IPPROTO_ICMP sockets; support for $\ge 64$ KiB receive buffers.
  • Security impact? Eliminates CWE-476 (NULL dereference), CWE-125 (Heap out-of-bounds read on chained IOB packets), and integer overflow wraparound.
  • Build impact? None.
  • Hardware impact? None.
  • Compatibility impact? Backward compatible; internal to net/icmp.

Testing

Host & Configuration

  • Build Host: Linux x86_64 (Ubuntu inside Docker container nuttx-builder) on Windows 11
  • Target Configuration: sim:bridge (CONFIG_NET_ICMP=y, CONFIG_NET_ICMP_SOCKET=y)

1. Coding Style Verification (nxstyle)

$ ./tools/nxstyle net/icmp/icmp_sendmsg.c
$ ./tools/nxstyle net/icmp/icmp_recvmsg.c
# 0 errors, 0 warnings

2. Patch Check (checkpatch.sh)

$ ./tools/checkpatch.sh -u -r -K -m
✔️ All checks pass.

3. Compilation & Link Log (sim:bridge)

$ make -j8
CC:  icmp/icmp_sendmsg.c
CC:  icmp/icmp_recvmsg.c
IN:  net/libnet.a -> staging/libnet.a
CC:  misc/lib_utsname.c
IN:  libs/libc/libc.a -> staging/libc.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

Clean build and link with 0 compiler warnings and 0 errors.

What Was Not Tested

Physical hardware target architectures (ARM, RISC-V, Xtensa boards) or protected/kernel build modes; verified on simulator (sim:bridge).

POSIX IEEE Std 1003.1 requires sendmsg() and recvmsg() to validate
msg_iovlen and fail with -EMSGSIZE when msg_iovlen is 0 or greater
than IOV_MAX. Furthermore, dereferencing msg->msg_iov in local variable
initializations prior to validating msg_iovlen and msg_iov != NULL risks
NULL pointer dereference or out-of-bounds access.

This patch addresses these issues in net/icmp:
1. Defer dereferencing msg->msg_iov until after validating msg_iovlen
   and confirming msg->msg_iov != NULL.
2. Return -EMSGSIZE when msg_iovlen == 0 or msg_iovlen > IOV_MAX.
3. Reject invalid buffers where len > 0 && buf == NULL with -EINVAL.
4. Set MSG_TRUNC in msg->msg_flags when received data exceeds the
   user buffer in both icmp_readahead() and recvfrom_eventhandler().
5. Widen recv_buflen to size_t and recv_result to ssize_t in
   struct icmp_recvfrom_s to prevent 16-bit integer wraparound on
   >=64KiB buffers.
6. Remove artificial len < ICMP_HDRLEN rejection to allow short and
   zero-length buffers to reach truncation paths per POSIX.
7. Use iob_copyout() in recvfrom_eventhandler() to safely copy across
   chained I/O buffers (dev->d_iob) without out-of-bounds heap read.

Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@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

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.

@xiaoxiang781216
xiaoxiang781216 merged commit 5a65a44 into apache:master Nov 11, 2026
49 of 52 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