Repository navigation
net/netlink: validate msg_iovlen and defer iov dereference in sendmsg/recvmsg - #21785
Merged
Merged
Conversation
Swatantra-66
requested review from
jerpelea and
xiaoxiang781216
as code owners
November 12, 2026 10:51
|
xiaoxiang781216
approved these changes
Nov 14, 2026
Contributor
|
ci fixed by #21771. |
…/recvmsg Defer dereferencing msg->msg_iov in netlink_sendmsg() and netlink_recvmsg() until after validating msg_iovlen and checking msg_iov != NULL, avoiding null pointer dereferences (CWE-476) and out-of-bounds reads (CWE-125) when msg_iovlen == 0 or msg_iov == NULL. Enforce POSIX IEEE Std 1003.1 conformance across Netlink sockets: - 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. - Validate that msg_iov[0].iov_len is at least sizeof(struct nlmsghdr) and that nlmsg->nlmsg_len >= sizeof(struct nlmsghdr) and nlmsg->nlmsg_len <= msg_iov[0].iov_len before parsing, returning -EINVAL on malformed netlink headers. - Initialize msg->msg_flags to 0 in netlink_recvmsg(). - Set MSG_TRUNC in msg->msg_flags when incoming response data exceeds user buffer capacity (len < entry->msg.nlmsg_len). - Include <limits.h> for IOV_MAX. Assisted-by: gemini-3.8-flash Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/netlink-iovlen-bounds
branch
from
November 14, 2026 07:26
44d7272 to
60b8990
Compare
acassis
approved these changes
Nov 15, 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 mandates for both
sendmsg()andrecvmsg():In
net/netlink(netlink_sockif.c), several vulnerabilities and POSIX non-compliances existed:msg->msg_iov->iov_basewas dereferenced at the beginning ofnetlink_sendmsg()andnetlink_recvmsg()before validatingmsg_iovlenor checkingmsg_iov != NULL. Passingmsg_iovlen == 0ormsg_iov == NULLresulted in immediate NULL pointer / out-of-bounds dereferences (CWE-476 / CWE-125).msg_iovlenwas not validated against<= 0 || > IOV_MAX(returning-EMSGSIZEper POSIX).msg->msg_iov[0].iov_len < sizeof(struct nlmsghdr), or ifnlmsg->nlmsg_len < sizeof(struct nlmsghdr) || nlmsg->nlmsg_len > msg->msg_iov[0].iov_len,netlink_sendmsg()would trigger assertion panics or allow protocol handlers to read out-of-bounds memory.netlink_recvmsg()did not clearmsg->msg_flagsor setMSG_TRUNCwhen the received Netlink response exceeded the user-provided buffer capacity, nor did it guardmemcpy()against calling with a NULL buffer whenlen == 0.This patch addresses these issues:
msg->msg_iovuntil after validatingmsg_iovlen,msg_iov != NULL, and buffer pointers.msg_iovlen == 0 || msg_iovlen > IOV_MAXreturning-EMSGSIZE.-ENOTSUPwhenmsg_iovlen != 1.msg_iov == NULLandiov_len > 0 && iov_base == NULLreturning-EINVAL.msg_iov[0].iov_len >= sizeof(struct nlmsghdr)and thatnlmsg->nlmsg_len >= sizeof(struct nlmsghdr) && nlmsg->nlmsg_len <= msg_iov[0].iov_lenbefore parsing innetlink_sendmsg(), returning-EINVALon malformed headers.msg->msg_flags = 0and setsMSG_TRUNCwhenlen < entry->msg.nlmsg_leninnetlink_recvmsg().memcpy()withif (len > 0)to prevent undefined behavior with 0-length NULL user buffers.<limits.h>forIOV_MAX.Before vs After
msg_iovlen == 0ormsg_iov == NULL-EMSGSIZEor-EINVALbefore any dereferencemsg_iovlen > IOV_MAX-ENOTSUP-EMSGSIZEper POSIX IEEE Std 1003.1iov_len > 0 && iov_base == NULL-EINVALsendmsgwith truncated Netlink header (iov_len < sizeof(nlmsghdr))nlmsg->nlmsg_lenon uninitialized memory; assertion failure or panic-EINVALsendmsgwith invalidnlmsg_len(nlmsg_len > iov_len)-EINVALrecvmsguser buffer smaller than responsemsg_flagsuntouchedMSG_TRUNCinmsg->msg_flagsper POSIXrecvmsgwith zero-length buffer (len == 0)memcpy(NULL, ...)leading to undefined behaviorif (len > 0); safely frees entry and returns 0Impact
[EMSGSIZE]andMSG_TRUNCspecifications.Testing
1. Code Style Check (
nxstyle)Output:
2. Full Build (
sim:dynconnsfromtools/ci/testlist/sim-02.dat)sim:dynconnsenablesCONFIG_NET_NETLINK=yandCONFIG_NETLINK_ROUTE=y.Output:
Binary size:
3. Runtime Boot & Execution in Simulator