Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 5/5] libnetlink: do not read past datagram when recording dump
Date: Sun,  8 Nov 2026 17:44:02 -0800	[thread overview]
Message-ID: <20261109014434.115922-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20261109014434.115922-1-stephen@networkplumber.org>

With a dump file (ss -D), rtnl_dump_filter_l() wrote NLMSG_ALIGN(status)
bytes, reading up to 3 bytes past the received data, and past the
buffer when the datagram is larger than 32K.

Write the datagram, then explicit zero padding, since rtnl_from_file()
expects padded messages.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/libnetlink.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/libnetlink.c b/lib/libnetlink.c
index de0681ac..f67f2b48 100644
--- a/lib/libnetlink.c
+++ b/lib/libnetlink.c
@@ -928,8 +928,13 @@ static int rtnl_dump_filter_l(struct rtnl_handle *rth,
 		if (status < 0)
 			return status;
 
-		if (rth->dump_fp)
-			fwrite(buf, 1, NLMSG_ALIGN(status), rth->dump_fp);
+		if (rth->dump_fp) {
+			static const char pad[NLMSG_ALIGNTO];
+
+			/* rtnl_from_file() expects padded messages */
+			fwrite(buf, 1, status, rth->dump_fp);
+			fwrite(pad, 1, NLMSG_ALIGN(status) - status, rth->dump_fp);
+		}
 
 		for (a = arg; a->filter; a++) {
 			struct nlmsghdr *h = (struct nlmsghdr *)buf;
-- 
2.53.0


  parent reply	other threads:[~2026-11-09  1:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09  1:43 [PATCH iproute2 0/5] netlink receive path hardening Stephen Hemminger
2026-11-09  1:43 ` [PATCH iproute2 1/5] netlink: use a counter for sequence numbers Stephen Hemminger
2026-11-09  1:43 ` [PATCH iproute2 2/5] netlink: ignore messages not sent by the kernel Stephen Hemminger
2026-11-09  1:44 ` [PATCH iproute2 3/5] netlink: connect sockets to " Stephen Hemminger
2026-11-09  1:44 ` [PATCH iproute2 4/5] netlink: handle unpadded last message in receive loops Stephen Hemminger
2026-11-09  1:44 ` Stephen Hemminger [this message]
2026-11-11 17:30 ` [PATCH iproute2 0/5] netlink receive path hardening patchwork-bot+netdevbpf

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=20261109014434.115922-6-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