Repository navigation
net/socket: validate msg_iovlen against IOV_MAX and non-zero in sendmsg/recvmsg - #21538
Merged
Merged
Conversation
Swatantra-66
requested review from
jerpelea and
xiaoxiang781216
as code owners
November 7, 2026 08:52
|
casaroli
reviewed
Nov 7, 2026
casaroli
left a comment
Contributor
There was a problem hiding this comment.
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 and a run that shows sendmsg() and recvmsg() fail with EMSGSIZE for msg_iovlen 0 and for msg_iovlen IOV_MAX + 1, and a normal sendmsg()/recvmsg() that still works.
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.
…sg/recvmsg
POSIX IEEE Std 1003.1 explicitly specifies 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}."
Neither psock_sendmsg() nor psock_recvmsg() validated msg->msg_iovlen.
Passing msg_iovlen = 0 or msg_iovlen > IOV_MAX caused:
1. In psock_recvmsg(), dereferencing msg->msg_iov->iov_base on an empty
iovec array, or returning -EINVAL instead of the mandated -EMSGSIZE.
2. In net/usrsock (usrsock_sendmsg.c), allocating an unbounded variable-
length array on the kernel stack (struct iovec bufs[2 + msg->msg_iovlen]),
leading to kernel stack overflow when msg_iovlen is large.
3. In net/local (local_sendmsg.c) and net/rpmsg (rpmsg_sockif.c),
executing zero-length loops, sending empty preambles, or unhandled
states.
4. Requiring individual lower-level drivers (inet, can, local_recvmsg) to
redundantly duplicate ad-hoc checks for msg->msg_iovlen == 0.
This patch adds upfront validation of msg->msg_iovlen against 0 and
IOV_MAX in psock_sendmsg() and psock_recvmsg(), returning -EMSGSIZE on
violation, while also ensuring psock is verified before dereferencing
psock->s_type.
Assisted-by: Google Gemini:gemini-3.8-flash
Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/socket-iovlen-validation
branch
from
November 7, 2026 18:25
ef37680 to
4caf3af
Compare
xiaoxiang781216
approved these changes
Nov 8, 2026
acassis
approved these changes
Nov 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX IEEE Std 1003.1 explicitly specifies for both
sendmsg()andrecvmsg():In NuttX,
{IOV_MAX}is defined in<limits.h>asINT_MAX. Becausemsg_iovlenis anunsigned longin NuttX'sstruct msghdr, a value of<= 0corresponds tomsg_iovlen == 0.Neither
psock_sendmsg()norpsock_recvmsg()previously validatedmsg->msg_iovlen:psock_recvmsg(), passingmsg_iovlen == 0caused an out-of-bounds dereference onmsg->msg_iov->iov_basebefore returning-EINVALinstead of the mandated-EMSGSIZE.net/usrsock(usrsock_sendmsg.c),struct iovec bufs[2 + msg->msg_iovlen]allocates a Variable-Length Array (VLA) directly on the kernel stack, leading to kernel stack overflow ifmsg_iovlenis large.net/local(local_sendmsg.c) andnet/rpmsg(rpmsg_sockif.c), missing top-level validation caused lower-level drivers to run 0-iteration loops, send empty packet preambles, or reach undefined states.inet_sendmsg,inet_recvmsg,can_sendmsg,can_recvmsg,local_recvmsg) had to implement ad-hoc checks formsg->msg_iovlen == 0.This change adds upfront validation of
msg->msg_iovlenagainst0andIOV_MAXinpsock_sendmsg()andpsock_recvmsg(), returning-EMSGSIZEon violation, while ensuringpsockis verified before dereferencingpsock->s_type.sendmsg()/recvmsg()withmsg_iovlen == 0-EINVAL, or dereferences empty array-EMSGSIZEsendmsg()/recvmsg()withmsg_iovlen > IOV_MAXusrsock), memory corruption-EMSGSIZEpsock == NULLon entrypsock->s_typebefore checkingpsock-EBADFImpact
usrsockand prevents null/out-of-bounds array reads inrecvmsg.[EMSGSIZE]requirement across all socket families (inet,local,usrsock,rpmsg,can,bluetooth,netlink,pkt).send(),sendto(),recv(),recvfrom()) setmsg_iovlen = 1and are completely unaffected.Testing
sim:tcpblaster,sim:nettest,sim:rpservertools/nxstyleon modified files (0 errors, 0 warnings).net/socket/sendmsg.c: 0 errors, 0 warningsnet/socket/recvmsg.c: 0 errors, 0 warningstools/checkpatch.sh -r -K -m -g HEAD~1..HEAD:Build Log (
sim:tcpblaster)Runtime Verification
sendmsg()&recvmsg()withmsg_iovlen == 0:sendmsg()&recvmsg()withmsg_iovlen > IOV_MAX(IOV_MAX + 1):sendmsg()&recvmsg()transfer: