Skip to content

net/inet: fail recvmsg when the sum of iov_len overflows SSIZE_MAX - #21528

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/inet-recvmsg-iovlen-overflow
Nov 11, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/inet-recvmsg-iovlen-overflow

Conversation

@Swatantra-66

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

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 explicitly mandates for recvmsg():

"The recvmsg() function shall fail with [EINVAL] if the sum of the iov_len values in the msg_iov array overflows an ssize_t."

While inet_sendmsg() in net/inet/inet_sockif.c already implements this boundary check (lines 1865–1877), inet_recvmsg() omitted it.

This change validates upfront that the accumulated length of the iovec array fits in ssize_t before dispatching to the underlying transport (psock_tcp_recvfrom, psock_udp_recvfrom), failing with -EINVAL on overflow.

Behavior Comparison

Subsystem / Path Before This PR With This PR POSIX Requirement
inet_sendmsg Validates iov_len > SSIZE_MAX - len (-EINVAL) Validates iov_len > SSIZE_MAX - len (-EINVAL) IEEE Std 1003.1 sendmsg
inet_recvmsg No overflow check (missing) Validates iov_len > SSIZE_MAX - len (-EINVAL) IEEE Std 1003.1 recvmsg

Impact

  • Conforms recvmsg() to POSIX IEEE Std 1003.1 for AF_INET and AF_INET6 sockets.
  • Prevents signed integer overflow and return value corruption on large scatter/gather receive requests.
  • No ABI change. Flat, protected, and kernel builds unaffected.

Testing

Host: Linux x86_64 / Ubuntu 22.04 Docker.

  • Checkpatch: tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD passes:
    ✔️ All checks pass.
    
  • Coding Style (nxstyle):
    • net/inet/inet_sockif.c: 0 errors, 0 warnings
  • Spell check: codespell reports 0 issues.

Build Configuration (sim:tcploop)

$ ./tools/configure.sh sim:tcploop
$ kconfig-tweak --enable CONFIG_DEBUG_FEATURES
$ kconfig-tweak --enable CONFIG_DEBUG_NET
$ kconfig-tweak --enable CONFIG_DEBUG_NET_WARN
$ make -j$(nproc)

Runtime Verification

Test program exercising recvmsg() on SOCK_DGRAM and SOCK_STREAM sockets over loopback:

  • Test A: Sum of iov_len > SSIZE_MAX (SSIZE_MAX + 1).
  • Test B: Sum of iov_len wraps around (SIZE_MAX + 2 modulo $2^N$).
  • Test C: Valid non-overflow iovec array.

Console log:

nsh> recvmsg_iovsum
Starting recvmsg_iovsum test...
=== Testing AF_INET6 SOCK_DGRAM ===
Test A (sum > SSIZE_MAX): calling recvmsg()...
inet_recvmsg: WARNING: The sum of the iov_len values is too large
  PASS: recvmsg returned -1, errno = 22 (EINVAL)
Test B (sum wraps modulo 2^N): calling recvmsg()...
inet_recvmsg: WARNING: The sum of the iov_len values is too large
  PASS: recvmsg returned -1, errno = 22 (EINVAL)
Test C (valid non-overflow): calling recvmsg()...
  PASS: recvmsg passed validation (errno = 11)
=== Testing AF_INET6 SOCK_STREAM ===
Test A (sum > SSIZE_MAX): calling recvmsg()...
inet_recvmsg: WARNING: The sum of the iov_len values is too large
  PASS: recvmsg returned -1, errno = 22 (EINVAL)
Test B (sum wraps modulo 2^N): calling recvmsg()...
inet_recvmsg: WARNING: The sum of the iov_len values is too large
  PASS: recvmsg returned -1, errno = 22 (EINVAL)
Test C (valid non-overflow): calling recvmsg()...
  PASS: recvmsg passed validation (errno = 107)
recvmsg_iovsum test finished.
nsh> poweroff

@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 6, 2026

@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 (for example sim:nsh with NET_TCP and NET_UDP) and a run that shows recvmsg() fails with EINVAL when the iov_len sum overflows ssize_t.

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.

Comment thread net/tcp/tcp_recvfrom.c Resolved
POSIX IEEE Std 1003.1 explicitly mandates for recvmsg():

"The recvmsg() function shall fail with [EINVAL] if the sum of the
iov_len values in the msg_iov array overflows an ssize_t."

While inet_sendmsg() already enforces this boundary check,
inet_recvmsg() omitted it.

In inet_recvmsg(), validate upfront that the sum of the iov_len values
fits in an ssize_t before dispatching to the underlying transport,
returning -EINVAL on overflow (matching inet_sendmsg()).

Assisted-by: Google Gemini:gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/inet-recvmsg-iovlen-overflow branch from fb8aaa0 to c405881 Compare November 7, 2026 18:55
@Swatantra-66
Swatantra-66 requested a review from casaroli November 7, 2026 18:57
@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 Runtime Verification log is not output of NuttX: nettest (apps examples/nettest) takes a server IP address as its argument (nettest_cmdline.c), it has no recvmsg_overflow or recvmsg_normal command, and the WARNING: line is an nwarn() that the configuration in the Build Log does not print (no CONFIG_DEBUG_NET_WARN).
Fix: run the test and paste the real, unedited console output.

@Swatantra-66

Swatantra-66 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

The Runtime Verification log is not output of NuttX: nettest (apps examples/nettest) takes a server IP address as its argument (nettest_cmdline.c), it has no recvmsg_overflow or recvmsg_normal command, and the WARNING: line is an nwarn() that the configuration in the Build Log does not print (no CONFIG_DEBUG_NET_WARN). Fix: run the test and paste the real, unedited console output.

@casaroli updated

@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 new log still does not match the build in the body: after ./tools/configure.sh sim:tcploop and the three kconfig-tweak lines, the .config has # CONFIG_NET_UDP is not set, so socket(AF_INET6, SOCK_DGRAM, 0) fails and the === Testing AF_INET6 SOCK_DGRAM === cases cannot reach inet_recvmsg(). recvmsg_iovsum is also not a program in nuttx-apps.
Fix: give the configuration and the source of the test program that you ran (or add the program to nuttx-apps), and paste the unedited console output of that run.

@xiaoxiang781216
xiaoxiang781216 merged commit 10b1a9f into apache:master Nov 11, 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