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 --- 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 #include #include +#include #include @@ -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