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>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH iproute2-next 1/5] lib/bpf: parse /proc/mounts with getmntent and check fs magic
Date: Wed, 18 Nov 2026 16:15:05 -0800	[thread overview]
Message-ID: <20261119001539.2868072-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20261119001539.2868072-1-stephen@networkplumber.org>

bpf_find_mntpt() parsed /proc/mounts with fscanf("%s"). The kernel
does not escape \r, \v or \f in mount paths, so a mount path
containing them can be misparsed as a bpf mount at some other path.
That path was used without the statfs() check done for the well
known mount points, and tc/ip then created the tc/globals hierarchy
and symlinks there.

The scan is done whenever /sys/fs/bpf is not bpffs, which is always
the case under ip -n, tc -n and ip netns exec.

Also, the %4096s conversion into a PATH_MAX buffer could write the
terminating NUL one byte past the end.

Use getmntent() and apply the statfs() magic check to every
candidate. Reject paths that are too long rather than truncate them.

Fixes: 32e93fb7f66d ("{f,m}_bpf: allow for sharing maps")
Reported-by: Anthropic OSS scanner (ANT-2026-SJ93QIGZ)
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/bpf_legacy.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c
index 9d5fcf5f..5b8ac47d 100644
--- a/lib/bpf_legacy.c
+++ b/lib/bpf_legacy.c
@@ -32,6 +32,7 @@
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/resource.h>
+#include <mntent.h>
 
 #include <arpa/inet.h>
 
@@ -543,12 +544,13 @@ static const char *bpf_find_mntpt_single(unsigned long magic, char *mnt,
 	int ret;
 
 	ret = bpf_valid_mntpt(mntpt, magic);
-	if (!ret) {
-		strlcpy(mnt, mntpt, len);
-		return mnt;
-	}
+	if (ret)
+		return NULL;
 
-	return NULL;
+	if (strlcpy(mnt, mntpt, len) >= (size_t)len)
+		return NULL;
+
+	return mnt;
 }
 
 static const char *bpf_find_mntpt(const char *fstype, unsigned long magic,
@@ -556,7 +558,8 @@ static const char *bpf_find_mntpt(const char *fstype, unsigned long magic,
 				  const char * const *known_mnts)
 {
 	const char * const *ptr;
-	char type[100];
+	const char *found = NULL;
+	struct mntent *ent;
 	FILE *fp;
 
 	if (known_mnts) {
@@ -568,24 +571,21 @@ static const char *bpf_find_mntpt(const char *fstype, unsigned long magic,
 		}
 	}
 
-	if (len != PATH_MAX)
-		return NULL;
-
-	fp = fopen("/proc/mounts", "r");
+	fp = setmntent("/proc/mounts", "r");
 	if (fp == NULL)
 		return NULL;
 
-	while (fscanf(fp, "%*s %" textify(PATH_MAX) "s %99s %*s %*d %*d\n",
-		      mnt, type) == 2) {
-		if (strcmp(type, fstype) == 0)
+	while ((ent = getmntent(fp)) != NULL) {
+		if (strcmp(ent->mnt_type, fstype) != 0)
+			continue;
+
+		found = bpf_find_mntpt_single(magic, mnt, len, ent->mnt_dir);
+		if (found)
 			break;
 	}
 
-	fclose(fp);
-	if (strcmp(type, fstype) != 0)
-		return NULL;
-
-	return mnt;
+	endmntent(fp);
+	return found;
 }
 
 int bpf_trace_pipe(void)
-- 
2.53.0


  reply	other threads:[~2026-11-19  0:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  0:15 [PATCH iproute2-next 0/5] bpf: fix mount point parsing and drop the legacy loader Stephen Hemminger
2026-11-19  0:15 ` Stephen Hemminger [this message]
2026-11-19  0:15 ` [PATCH iproute2-next 2/5] lib/fs: parse /proc/mounts with getmntent and check fs magic Stephen Hemminger
2026-11-19  0:15 ` [PATCH iproute2-next 3/5] tc: remove eBPF map export and exec bpf import Stephen Hemminger
2026-11-19  0:15 ` [PATCH iproute2-next 4/5] lib/bpf: require libbpf and remove legacy ELF loader Stephen Hemminger
2026-11-19  0:15 ` [PATCH iproute2-next 5/5] lib/bpf: check for missing bpf fs mount point Stephen Hemminger

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=20261119001539.2868072-2-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=daniel@iogearbox.net \
    --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