Repository navigation
net/pkt: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21735
Merged
Merged
Conversation
|
xiaoxiang781216
approved these changes
Nov 12, 2026
Contributor
|
ci fixed by #21771. |
…vmsg Defer dereferencing msg->msg_iov in both buffered and unbuffered pkt_sendmsg() implementations until after pkt_sendmsg_is_valid() validates input arguments, avoiding null pointer dereferences and out-of-bounds reads when msg_iovlen == 0 or msg_iov == NULL. Enforce POSIX IEEE Std 1003.1 conformance across pkt_sendmsg_is_valid() and pkt_recvmsg(): - 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. - Initialize msg->msg_flags to 0 in pkt_recvmsg(). - Set MSG_TRUNC in msg->msg_flags when incoming packet data exceeds the user buffer capacity, accounting for link-layer header offset on SOCK_DGRAM sockets in pkt_recvfrom_newdata() and in pkt_readdata(). Assisted-by: gemini-3.8-flash Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/pkt-iovlen-bounds
branch
from
November 14, 2026 07:26
35cd899 to
91fe450
Compare
GUIDINGLI
approved these changes
Nov 17, 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 mandates that
sendmsg()andrecvmsg()validatemsg_iovlenand return-EMSGSIZEwhenmsg_iovlen <= 0 || msg_iovlen > IOV_MAX. Furthermore, dereferencingmsg->msg_iovbefore validatingmsg_iovlenand checkingmsg_iov != NULLrisks kernel panics due to null-pointer dereferences (CWE-476) or out-of-bounds reads (CWE-125).This patch hardens
net/pkt(packet sockets):msg->msg_iovinpkt_sendmsg_unbuffered.candpkt_sendmsg_buffered.cuntil afterpkt_sendmsg_is_valid()validates input arguments.[EMSGSIZE]Compliance: Returns-EMSGSIZEwhenmsg_iovlen == 0 || msg_iovlen > IOV_MAXin bothpkt_sendmsg_is_valid()andpkt_recvmsg(), while preserving-ENOTSUPformsg_iovlen > 1.msg_iov != NULLand rejects buffers withiov_len > 0 && iov_base == NULLwith-EINVAL.MSG_TRUNC: Initializesmsg->msg_flags = 0inpkt_recvmsg(), setsMSG_TRUNCwhen payload exceeds buffer capacity inpkt_readdata(), and correctly accounts for link-layer header stripping onSOCK_DGRAMsockets inpkt_recvfrom_newdata()to avoid spurious truncation flags.<limits.h>: Provides standardIOV_MAX.Before vs After
pkt_sendmsg()msg->msg_iov->iov_baseandiov_lenbeforepkt_sendmsg_is_valid()is called whenmsg_iovlen == 0ormsg_iov == NULL.pkt_sendmsg_is_valid()passes.[EMSGSIZE]onmsg_iovlen <= 0 || > IOV_MAX-ENOTSUP, violating POSIX specification.-EMSGSIZEwhenmsg_iovlen == 0 || msg_iovlen > IOV_MAX. Preserves-ENOTSUPonly formsg_iovlen > 1.[EMSGSIZE]msg_iov == NULLoriov_len > 0 && iov_base == NULLwas unchecked, risking segfault during buffer copy.-EINVALimmediately.[EINVAL]MSG_TRUNCinrecvmsg()MSG_TRUNCinmsg->msg_flags.MSG_TRUNCinmsg->msg_flagswhen buffer is smaller than payload.MSG_TRUNCSOCK_DGRAMTruncation Calculationdev->d_len(which includes link-layer header), causing spuriousMSG_TRUNCeven when payload fit.dev->d_len - NET_LL_HDRLEN(dev)) before checking truncation forSOCK_DGRAM.Impact
msghdrinputs innet/pkt.Testing
1. Coding Style Verification (
nxstyle)All modified files verified with
nxstylewith 0 errors and 0 warnings:2. Build Verification (
sim:dynconns)A. Unbuffered Packet Sendmsg (
CONFIG_NET_PKT=y)Target build compiling
net/libnet.a:Result: Build succeeded with 0 compiler errors and 0 warnings.
B. Buffered Packet Sendmsg (
CONFIG_NET_PKT_WRITE_BUFFERS=y)Target build compiling
net/libnet.awith write buffering enabled:Result: Build succeeded with 0 compiler errors and 0 warnings.