Skip to content

net/local: fix integer overflow in sendpacket and SCM leak in sendmsg - #21492

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/local-sendpacket-overflow
Nov 6, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/local-sendpacket-overflow

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes two integer overflow bugs and two SCM (Socket Control Message) descriptor handling bugs in Unix domain sockets (net/local):

  1. Datagram preamble integer wrap-around (local_send_preamble):
    pktlen was accumulated into lc_size_t (uint16_t when CONFIG_DEV_PIPE_MAXSIZE <= 65535). If the sum of iov_len values exceeded 65535, pktlen silently wrapped around modulo 65536 before checking against rcvsize, allowing oversized datagrams to pass validation, write a truncated packet length header, and corrupt FIFO packet framing. Now accumulates the total in size_t and validates payload headroom before adding each buffer.

  2. Stream send 16-bit truncation (local_send_packet):
    sendlen was typed lc_size_t (uint16_t). Stream socket writes (SOCK_STREAM) larger than 64 KB wrapped around modulo 65536, returning a bogus truncated byte count to userspace. Changed sendlen and function return types to ssize_t.

  3. Zero-length send handling (local_send_packet):
    Explicitly returns 0 when len == 0 so empty datagrams (e.g. heartbeat/ping) succeed and return 0 bytes instead of falling through to -EINVAL.

  4. Dead error cleanup & descriptor leak (local_sendmsg):
    The return value of local_sendto() / local_send() was assigned to unsigned size_t len, rendering if (len < 0 && count > 0) dead code. Any send failure leaked the control message file descriptors. Changed to signed ssize_t ret.

  5. Orphaned SCM descriptors on zero-payload stream sends (local_sendmsg):
    On stream sockets (SOCK_STREAM), sending SCM_RIGHTS with 0 bytes of payload queued descriptors, but wrote 0 bytes to the stream FIFO. The receiver never woke up, leaving descriptors permanently orphaned in lc_cfps until lc_cfpcount reached LOCAL_NCONTROLFDS (4) and broke future sends with -EMFILE. Now rejects zero-data stream SCM sends with -EINVAL, strictly conforming to POSIX / Linux unix(7) behavior (if (scm && !total_len) return -EINVAL;).

Impact

  • Impact on user: Fixes silent packet corruption for datagrams > 64 KB, fixes return value truncation for stream writes > 64 KB, and prevents SCM file descriptor leaks in Unix domain sockets.
  • Impact on build: None.
  • Impact on hardware: None.

Testing

  • Linters: Verified locally against full CI checkpatch: ./tools/checkpatch.sh -u -f net/local/local_sendpacket.c net/local/local_sendmsg.c net/local/local.h (exit code 0, all checks passed).
  • nxstyle: Verified 0 errors and 0 warnings across all modified files.

1. In local_send_preamble(), packet length was accumulated into
   lc_size_t pktlen (uint16_t when CONFIG_DEV_PIPE_MAXSIZE <= 65535).
   If the sum of iov_len values exceeded 65535, pktlen silently wrapped
   around before checking against rcvsize, allowing oversized datagrams
   to corrupt FIFO packet framing. Accumulate total into size_t and
   validate against payload space before adding.
2. In local_send_packet(), sendlen was typed lc_size_t. Stream socket
   writes (SOCK_STREAM) larger than 64 KB wrapped around modulo 65536.
   Change sendlen and function return type to ssize_t, and handle zero
   iovec count by returning 0.
3. In local_sendmsg(), the return value of local_sendto() / local_send()
   was assigned to size_t len, making if (len < 0 && count > 0) dead
   code and leaking SCM file descriptors on error. Use ssize_t ret.
4. In local_sendmsg(), reject stream socket sends (SOCK_STREAM) with
   control messages (SCM_RIGHTS) when total payload length is zero with
   -EINVAL (matching Linux unix(7)) to avoid queuing orphaned descriptors
   when no data is written to wake up the receiver.

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
@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 5, 2026
@github-actions

github-actions Bot commented Nov 5, 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
xiaoxiang781216 merged commit 56f37d5 into apache:master Nov 6, 2026
53 checks passed
Comment thread net/local/local_sendmsg.c Resolved
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.

5 participants