Skip to contentSkip to conversation

tools: bpftool: fix truncated netlink dumps

PATCH Superseded Jakub Kicinski 3 participants 3 messages Feb 15 – Feb 16

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools: bpftool: fix truncated netlink dumps

Netlink requires that the recv buffer used during dumps is at least min(PAGE_SIZE, 8k) (see the man page). Otherwise the messages will get truncated. Make sure bpftool follows this requirement, avoid missing information on systems with large pages.

Fixes 7084566a236f ("tools/bpftool: Remove libbpf_internal.h usage in bpftool")

---
Sorry for the long delay! I haven't CCed netdev@ on v1, and it appears that CC: bpf@ + kuba@ gets misfiled in my inbox.

v2:
- make the same change in libbpf_netlink_recv()
note that libbpf_netlink_recv() does a PEEK and then realloc if the message is larger, so it's technically already safe but probably better if we are consistent
- fprintf(stderr) -> p_err()
- adjust msg
v1: https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/net.c | 5 ++++-
 tools/lib/bpf/netlink.c | 4 +++-
 2 files changed, 7 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +4 -1
index f25d66c8395e..974189da8a91 100644
@@ -156,7 +156,7 @@ static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
 	bool multipart = true;
 	struct nlmsgerr *err;
 	struct nlmsghdr *nh;
-	char buf[4096];
+	char buf[8192];
 	int len, ret;
 
 	while (multipart) {
@@ -201,6 +201,9 @@ static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
 					return ret;
 			}
 		}
+
+		if (len)
+			p_err("Invalid message or trailing data in Netlink response: %d bytes left", len);
 	}
 	ret = 0;
 done:
tools/lib/bpf/netlink.c +3 -1
index c997e69d507f..96c6b09c433e 100644
@@ -143,7 +143,7 @@ static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
 	struct nlmsghdr *nh;
 	int len, ret;
 
-	ret = alloc_iov(&iov, 4096);
+	ret = alloc_iov(&iov, 8192);
 	if (ret)
 		goto done;
 
@@ -212,6 +212,8 @@ static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
 				}
 			}
 		}
+		if (len)
+			pr_warn("Invalid message or trailing data in Netlink response: %d bytes left", len);
 	}
 	ret = 0;
 done:
Signature · 1 line
2.53.0
#1 lore raw
Should this have a trailing \n in the format string? The bpftool version uses p_err() which appends a newline automatically, but libbpf's pr_warn() passes the format string through as-is. Every…
Quoted 3 lines
tools/lib/bpf/netlink.c +0 -0

[ ... ]

tools/lib/bpf/netlink.c struct msghdr mhdr = { · line 216

Quoted 6 lines
tools/lib/bpf/netlink.c +2 -0
@@ -212,6 +212,8 @@ struct msghdr mhdr = {
 				}
 			}
 		}
+		if (len)
+			pr_warn("Invalid message or trailing data in Netlink response: %d bytes left", len);

Should this have a trailing \n in the format string? The bpftool version uses p_err() which appends a newline automatically, but libbpf's pr_warn() passes the format string through as-is. Every other pr_warn() call in this file includes \n, for example:

tools/lib/bpf/netlink.c:netlink_open() {
    ...
    pr_warn("Netlink error reporting not supported\n");
    ...
}

Without it the next libbpf log message will be concatenated onto the same line as this warning.

Quoted 3 lines
tools/lib/bpf/netlink.c +0 -0
 	}
 	ret = 0;

done:

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/22026600932

AI-authorship-score: low
AI-authorship-explanation: The patch shows human review interaction patterns across two versions with natural review feedback incorporation, and the missing newline is a typical human oversight during code adaptation.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Missing trailing newline in pr_warn() format string causes garbled log output but does not affect functionality or system stability.

maintainer Acked-by on v2 1/1
#2 lore raw
In reply to Jakub Kicinski

2026-02-14 16:06 UTC-0800 ~ Jakub Kicinski <[email protected]>

Quoted 76 lines

Netlink requires that the recv buffer used during dumps is at least min(PAGE_SIZE, 8k) (see the man page). Otherwise the messages will get truncated. Make sure bpftool follows this requirement, avoid missing information on systems with large pages.

Fixes 7084566a236f ("tools/bpftool: Remove libbpf_internal.h usage in bpftool")

---
Sorry for the long delay! I haven't CCed netdev@ on v1, and it appears that CC: bpf@ + kuba@ gets misfiled in my inbox.

v2:
- make the same change in libbpf_netlink_recv()
note that libbpf_netlink_recv() does a PEEK and then realloc if the message is larger, so it's technically already safe but probably better if we are consistent
- fprintf(stderr) -> p_err()
- adjust msg
v1: https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/net.c | 5 ++++-
 tools/lib/bpf/netlink.c | 4 +++-
 2 files changed, 7 insertions(+), 2 deletions(-)
tools/bpf/bpftool/net.c +4 -1
index f25d66c8395e..974189da8a91 100644
@@ -156,7 +156,7 @@ static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
 	bool multipart = true;
 	struct nlmsgerr *err;
 	struct nlmsghdr *nh;
-	char buf[4096];
+	char buf[8192];
 	int len, ret;
 
 	while (multipart) {
@@ -201,6 +201,9 @@ static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
 					return ret;
 			}
 		}
+
+		if (len)
+			p_err("Invalid message or trailing data in Netlink response: %d bytes left", len);
 	}
 	ret = 0;
 done:
tools/lib/bpf/netlink.c +1 -1
index c997e69d507f..96c6b09c433e 100644
@@ -143,7 +143,7 @@ static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
 	struct nlmsghdr *nh;
 	int len, ret;
 
-	ret = alloc_iov(&iov, 4096);
+	ret = alloc_iov(&iov, 8192);
 	if (ret)
 		goto done;
 

tools/lib/bpf/netlink.c static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq, · line 216

Quoted 5 lines
tools/lib/bpf/netlink.c +2 -0
@@ -212,6 +212,8 @@ static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq,
 				}
 			}
 		}
+		if (len)
+			pr_warn("Invalid message or trailing data in Netlink response: %d bytes left", len);

CI bot is right about libbpf's pr_warn() not adding the line break automatically. Other than that, it looks good to me, thanks!