From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute] libnetlink: ignore netlink messages not from kernel
Date: Mon, 19 Oct 2026 14:08:29 -0700 [thread overview]
Message-ID: <20261019210829.1019715-1-stephen@networkplumber.org> (raw)
A process with CAP_NET_ADMIN in its own netns can send forged
netlink messages to a monitor, by multicast to an all-nsid listener
in another netns or by unicast to the monitor's port.
Filter on nl_pid in __rtnl_recvmsg() and use it in rtnl_listen().
Dequeue the datagram when peeking, and restore the value-result
msg fields before retrying.
Reported-by: Anthropic OSS scanner
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/libnetlink.c | 51 ++++++++++++++++++++++++++----------------------
1 file changed, 28 insertions(+), 23 deletions(-)
diff --git a/lib/libnetlink.c b/lib/libnetlink.c
index edf3a8ba..a07cbeef 100644
--- a/lib/libnetlink.c
+++ b/lib/libnetlink.c
@@ -825,13 +825,21 @@ static int rtnl_dump_error(const struct rtnl_handle *rth,
static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
{
+ const struct sockaddr_nl *nladdr = msg->msg_name;
+ socklen_t namelen = msg->msg_namelen;
+ size_t controllen = msg->msg_controllen;
int len;
- do {
- len = recvmsg(fd, msg, flags);
- } while (len < 0 && (errno == EINTR || errno == EAGAIN));
+retry:
+ /* value-result fields, restore if discarding a message */
+ msg->msg_namelen = namelen;
+ msg->msg_controllen = controllen;
+ len = recvmsg(fd, msg, flags);
if (len < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ goto retry;
+
fprintf(stderr, "netlink receive error %s (%d)\n",
strerror(errno), errno);
return -errno;
@@ -842,6 +850,19 @@ static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
return -ENODATA;
}
+ if (msg->msg_namelen != sizeof(*nladdr)) {
+ fprintf(stderr, "Sender address length == %u\n",
+ msg->msg_namelen);
+ return -EINVAL;
+ }
+
+ /* Only accept messages from kernel, discard anything else */
+ if (nladdr->nl_pid != 0) {
+ if (flags & MSG_PEEK)
+ recv(fd, NULL, 0, 0);
+ goto retry;
+ }
+
return len;
}
@@ -1248,27 +1269,11 @@ int rtnl_listen(struct rtnl_handle *rtnl,
}
iov.iov_len = sizeof(buf);
- status = recvmsg(rtnl->fd, &msg, 0);
-
- if (status < 0) {
- if (errno == EINTR || errno == EAGAIN)
- continue;
- fprintf(stderr, "netlink receive error %s (%d)\n",
- strerror(errno), errno);
- if (errno == ENOBUFS)
- continue;
- return -1;
- }
- if (status == 0) {
- fprintf(stderr, "EOF on netlink\n");
+ status = __rtnl_recvmsg(rtnl->fd, &msg, 0);
+ if (status == -ENOBUFS)
+ continue;
+ if (status < 0)
return -1;
- }
- if (msg.msg_namelen != sizeof(nladdr)) {
- fprintf(stderr,
- "Sender address length == %d\n",
- msg.msg_namelen);
- exit(1);
- }
if (rtnl->flags & RTNL_HANDLE_F_LISTEN_ALL_NSID) {
memset(&ctrl, 0, sizeof(ctrl));
--
2.53.0
reply other threads:[~2026-10-19 21:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261019210829.1019715-1-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=dsahern@kernel.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox