Skip to content

net/rpmsg: fix integer overflow in sendmsg and datagram truncation - #21522

Merged
michallenc merged 1 commit into
apache:masterfrom
Swatantra-66:fix/rpmsg-socket-iovlen-overflow
Nov 9, 2026
Merged

michallenc merged 1 commit into
apache:masterfrom
Swatantra-66:fix/rpmsg-socket-iovlen-overflow

Conversation

@Swatantra-66

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

Copy link
Copy Markdown
Contributor

Summary

RPMsg sockets (net/rpmsg) contained integer overflow vulnerabilities during iov_len accumulation, addition wrap-around in datagram size checks, and silent datagram truncation when payloads exceeded the acquired RPMsg buffer size.

In rpmsg_socket_get_iovlen(), size_t iov_len values were accumulated into uint32_t len without overflow checks. On SOCK_STREAM connections, an overflow caused silent data loss by skipping the transmit loop. On SOCK_DGRAM connections, total = len + sizeof(uint32_t) could wrap modulo 2^32, bypassing the conn->sendsize check. Furthermore, rpmsg_socket_send_single() previously shortened total and len using MIN(), which silently transmitted truncated datagrams and caused unsigned integer underflow in total - sizeof(uint32_t).

This change bounds iov_len accumulation to MIN(SSIZE_MAX, UINT32_MAX) returning -EINVAL, prevents addition wrap-around, rejects oversized datagrams upfront with -EMSGSIZE, and preserves blocking socket semantics when concurrent senders reduce window space.

Behavior Comparison

Path / Scenario Before This PR With This PR Impact
get_iovlen sum > 4 GB uint32_t silently wraps; skips loop on stream sockets Fails upfront with -EINVAL Prevents silent data loss and protocol desync
len + 4 addition check Unchecked addition wraps 3 > sendsize (passes) Checks len > sendsize - 4 before addition Prevents bypassing buffer capacity guard
Datagram > ipcsize Silently truncates payload with MIN() and sends partial packet; unsigned underflow in len Rejects with -EMSGSIZE and releases buffer Enforces atomic datagram transmission; eliminates heap overrun
Concurrent send (blocking) Fails blocking sender with -EAGAIN if space drops Releases buffer and loops back to net_sem_timedwait Preserves POSIX blocking socket semantics

Impact

  • Enforces atomic datagram guarantees and POSIX error behavior for RPMsg sockets.
  • Prevents silent data corruption, buffer overruns, and unsigned integer underflow.
  • No ABI change. Flat, protected, and kernel builds unaffected.

Testing

  • Host: Linux x86_64 Docker (ubuntu:22.04, GCC 11.4 multilib, tools/nxstyle, checkpatch.sh)
  • Targets: sim:rpserver + sim:rpproxy
  • Coding Style: Verified with tools/nxstyle net/rpmsg/rpmsg_sockif.c (0 errors, 0 warnings).
  • Checkpatch: tools/checkpatch.sh -r -K -m -g HEAD~1..HEAD:
    ✔️ All checks pass.
    

Runtime Verification (sim:rpserver & sim:rpproxy / examples/rpmsgsocket)

Tested using dual-instance simulation (sim:rpserver master and sim:rpproxy slave over OpenAMP shared memory).

  1. Start rpsock_server on sim:rpserver:
server> rpsock_server stream block test_sock proxy 1 &
rpsock_server [11:100]
server> server: create socket SOCK_STREAM nonblock 0
server: bind cpu , name test_sock ...
server: listen ...
server: try accept ...
  1. Connect to proxy console via cu and run rpsock_client (sim:rpproxy):
server> cu /dev/ttyproxy
proxy> rpsock_client stream block test_sock server 1
client: create socket SOCK_STREAM nonblock 0
client: Connecting to server,test_sock...
client: Connected
client send data, cnt 0, total 0
client recv data, act len 255, total 0
client recv data, act len 769, total 255
client recv data, act len 255, total 1024
client recv data, act len 769, total 1279
client send data, cnt 1, total 2048
client recv data, act len 255, total 2048
client recv data, act len 769, total 2303
...
client send data, cnt 1998, total 4091904
client recv data, act len 255, total 4092669
client recv data, act len 769, total 4092924
client recv data, act len 255, total 4093693
client recv data, act len 769, total 4093948
client send data, cnt 1999, total 4093952
client recv data, act len 255, total 4094717
client recv data, act len 769, total 4094972
client recv data, act len 255, total 4095741
client recv data, act len 4, total 4095996
client recv done, total 4096000, endflags, send total 4096000
server recv data normal exit
server Complete ret 0, errno 0
client: Terminating
proxy>

@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 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 a run on sim:rpserver + sim:rpproxy (or qemu-armv8a:rpproxy) that sends a SOCK_DGRAM message larger than the rpmsg buffer and shows -EMSGSIZE, and a normal stream and datagram transfer.

The commit has Assisted-by: Google Gemini below Signed-off-by:; CONTRIBUTING 1.5 wants it above the signature, in the form AGENT_NAME:MODEL_VERSION. Fix: move it above and add the model version.

Comment thread net/rpmsg/rpmsg_sockif.c Resolved
1. In rpmsg_socket_get_iovlen(), iov_len values (size_t) were
   accumulated into a uint32_t without overflow checking. If the sum
   exceeded UINT32_MAX, it silently wrapped around. On 64-bit
   platforms, individual iov_len values larger than UINT32_MAX were
   also silently truncated. Fail with -EINVAL when the sum exceeds
   MIN(SSIZE_MAX, UINT32_MAX), matching usrsock (commit 6e1126a)
   and local (commit 56f37d5).
2. In rpmsg_socket_send_continuous() (SOCK_STREAM), handle error
   return from rpmsg_socket_get_iovlen() and return early.
3. In rpmsg_socket_send_single() (SOCK_DGRAM):
   - Check len against conn->sendsize without addition overflow before
     computing total = len + sizeof(uint32_t), returning -EMSGSIZE on
     violation per POSIX sendmsg() semantics.
   - Reject datagrams exceeding acquired TX buffer capacity (ipcsize)
     with -EMSGSIZE and release the buffer, rather than silently
     shortening total and len which transmitted truncated datagrams
     and caused unsigned integer underflow.
   - When concurrent senders reduce space below total under sendlock,
     release the buffer and return -EAGAIN for nonblocking sockets,
     or continue waiting on conn->sendsem for blocking sockets.

Assisted-by: Google Gemini:gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>

@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 logs under Runtime Verification are not output of examples/rpmsgsocket: rpsock_client.c prints none of these lines (client test stream pass, client test dgram pass, client test dgram oversized payload rejected with -EMSGSIZE as expected), it never calls sendmsg(), and rpsock_dgram_client() does not read a fifth argument, so the 65536 of the third run is not used.
Fix: run the tests and paste the real, unedited console output.

@Swatantra-66
Swatantra-66 requested a review from casaroli November 9, 2026 10:24
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

The logs under Runtime Verification are not output of examples/rpmsgsocket: rpsock_client.c prints none of these lines (client test stream pass, client test dgram pass, client test dgram oversized payload rejected with -EMSGSIZE as expected), it never calls sendmsg(), and rpsock_dgram_client() does not read a fifth argument, so the 65536 of the third run is not used. Fix: run the tests and paste the real, unedited console output.

updated

@michallenc
michallenc merged commit b6c4e46 into apache:master Nov 9, 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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants