Skip to content

net/netlink: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21785

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/netlink-iovlen-bounds
Nov 15, 2026
Merged

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

  1. msg->msg_iov->iov_base was dereferenced at the beginning of netlink_sendmsg() and netlink_recvmsg() before validating msg_iovlen or checking msg_iov != NULL. Passing msg_iovlen == 0 or msg_iov == NULL resulted in immediate NULL pointer / out-of-bounds dereferences (CWE-476 / CWE-125).
  2. msg_iovlen was not validated against <= 0 || > IOV_MAX (returning -EMSGSIZE per POSIX).
  3. If msg->msg_iov[0].iov_len < sizeof(struct nlmsghdr), or if nlmsg->nlmsg_len < sizeof(struct nlmsghdr) || nlmsg->nlmsg_len > msg->msg_iov[0].iov_len, netlink_sendmsg() would trigger assertion panics or allow protocol handlers to read out-of-bounds memory.
  4. netlink_recvmsg() did not clear msg->msg_flags or set MSG_TRUNC when the received Netlink response exceeded the user-provided buffer capacity, nor did it guard memcpy() against calling with a NULL buffer when len == 0.

This patch addresses these issues:

  • Defers dereferencing msg->msg_iov until after validating msg_iovlen, msg_iov != NULL, and buffer pointers.
  • Validates msg_iovlen == 0 || msg_iovlen > IOV_MAX returning -EMSGSIZE.
  • Preserves -ENOTSUP when msg_iovlen != 1.
  • Validates msg_iov == NULL and iov_len > 0 && iov_base == NULL returning -EINVAL.
  • Validates that msg_iov[0].iov_len >= sizeof(struct nlmsghdr) and that nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) && nlmsg->nlmsg_len <= msg_iov[0].iov_len before parsing in netlink_sendmsg(), returning -EINVAL on malformed headers.
  • Clears msg->msg_flags = 0 and sets MSG_TRUNC when len < entry->msg.nlmsg_len in netlink_recvmsg().
  • Guards memcpy() with if (len > 0) to prevent undefined behavior with 0-length NULL user buffers.
  • Includes <limits.h> for IOV_MAX.

Before vs After

Scenario Before After
msg_iovlen == 0 or msg_iov == NULL Immediate NULL/OOB dereference in local variable initialization (CWE-476 / CWE-125) Safe validation; returns -EMSGSIZE or -EINVAL before any dereference
msg_iovlen > IOV_MAX Returned -ENOTSUP Returns -EMSGSIZE per POSIX IEEE Std 1003.1
iov_len > 0 && iov_base == NULL Unchecked, crashes later during buffer access Rejects upfront with -EINVAL
sendmsg with truncated Netlink header (iov_len < sizeof(nlmsghdr)) Dereferences nlmsg->nlmsg_len on uninitialized memory; assertion failure or panic Rejects upfront with -EINVAL
sendmsg with invalid nlmsg_len (nlmsg_len > iov_len) Assertion failure in debug mode, heap out-of-bounds read in release mode Rejects upfront with -EINVAL
recvmsg user buffer smaller than response Truncated silently without notifying caller; msg_flags untouched Sets MSG_TRUNC in msg->msg_flags per POSIX
recvmsg with zero-length buffer (len == 0) Called memcpy(NULL, ...) leading to undefined behavior Guarded with if (len > 0); safely frees entry and returns 0

Impact

  • Security: Eliminates potential CWE-476 NULL pointer dereferences and CWE-125 out-of-bounds reads.
  • POSIX Compliance: Conforms to POSIX IEEE Std 1003.1 [EMSGSIZE] and MSG_TRUNC specifications.
  • Compatibility: Preserves full backward compatibility for valid Netlink sockets.

Testing

1. Code Style Check (nxstyle)

$ ./tools/nxstyle net/netlink/netlink_sockif.c

Output:

(0 errors, 0 warnings)

2. Full Build (sim:dynconns from tools/ci/testlist/sim-02.dat)

sim:dynconns enables CONFIG_NET_NETLINK=y and CONFIG_NETLINK_ROUTE=y.

$ ./tools/configure.sh sim:dynconns
$ make -j8

Output:

Create version.h
...
CC:  netlink/netlink_sockif.c
...
IN: libs/libc/libc.a -> staging/libc.a
CC:  symtab_apps.c
IN: /repos/apps/libapps.a -> staging/libapps.a
CC:  sim/sim_head.c
...
LD:  nuttx
Pac SIM with dynamic libs..
'/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

Binary size:

$ size nuttx
   text	   data	    bss	    dec	    hex	filename
 599545	   5244	  46752	 651541	  9f115	nuttx

3. Runtime Boot & Execution in Simulator

$ ./nuttx
NuttShell (NSH) NuttX-12.8.0
nsh> help
help usage:  help [-v] [<cmd>]

    .            break        exec         mkdir        rm           true         
    [            cat          exit         mkfifo       rmdir        truncate     
    ?            cd           expr         mkrd         rmmod        uname        
    addroute     cp           false        mount        set          umount       
    alias        cmp          help         mv           kill         unset        
    unalias      dirname      hexdump      nslookup     sleep        uptime       
    arp          date         insmod       poweroff     usleep       watch        
    base64dec    delroute     losetup      quit         source       xd           
    base64enc    dmesg        ls           printf       test         
    basename     echo         du           pwd          time         

Builtin Apps:
    dd      init    nsh     sh      
nsh> exit

@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 12, 2026
@github-actions

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

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

ci fixed by #21771.

…/recvmsg

Defer dereferencing msg->msg_iov in netlink_sendmsg() and netlink_recvmsg()
until after validating msg_iovlen and checking msg_iov != NULL, avoiding
null pointer dereferences (CWE-476) and out-of-bounds reads (CWE-125)
when msg_iovlen == 0 or msg_iov == NULL.

Enforce POSIX IEEE Std 1003.1 conformance across Netlink sockets:
- Return -EMSGSIZE when msg_iovlen == 0 or msg_iovlen > IOV_MAX.
- Return -ENOTSUP when msg_iovlen > 1 (scatter/gather unsupported).
- Return -EINVAL when msg_iov is NULL or when an element has iov_len > 0
  with a NULL iov_base.
- Validate that msg_iov[0].iov_len is at least sizeof(struct nlmsghdr)
  and that nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) and
  nlmsg->nlmsg_len <= msg_iov[0].iov_len before parsing, returning
  -EINVAL on malformed netlink headers.
- Initialize msg->msg_flags to 0 in netlink_recvmsg().
- Set MSG_TRUNC in msg->msg_flags when incoming response data exceeds
  user buffer capacity (len < entry->msg.nlmsg_len).
- Include <limits.h> for IOV_MAX.

Assisted-by: gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/netlink-iovlen-bounds branch from 44d7272 to 60b8990 Compare November 14, 2026 07:26
@acassis
acassis merged commit 23e3cfa into apache:master Nov 15, 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