Skip to content

Commit 9cf309c

Browse files
tohojoanakryiko
authored andcommitted
libbpf: Sanitise map names before pinning
When we added sanitising of map names before loading programs to libbpf, we still allowed periods in the name. While the kernel will accept these for the map names themselves, they are not allowed in file names when pinning maps. This means that bpf_object__pin_maps() will fail if called on an object that contains internal maps (such as sections .rodata). Fix this by replacing periods with underscores when constructing map pin paths. This only affects the paths generated by libbpf when bpf_object__pin_maps() is called with a path argument. Any pin paths set by bpf_map__set_pin_path() are unaffected, and it will still be up to the caller to avoid invalid characters in those. Fixes: 113e6b7 ("libbpf: Sanitise internal map names so they are not rejected by the kernel") Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20201203093306.107676-1-toke@redhat.com
1 parent 80b2b5c commit 9cf309c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7659,6 +7659,16 @@ bool bpf_map__is_pinned(const struct bpf_map *map)
76597659
return map->pinned;
76607660
}
76617661

7662+
static void sanitize_pin_path(char *s)
7663+
{
7664+
/* bpffs disallows periods in path names */
7665+
while (*s) {
7666+
if (*s == '.')
7667+
*s = '_';
7668+
s++;
7669+
}
7670+
}
7671+
76627672
int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
76637673
{
76647674
struct bpf_map *map;
@@ -7688,6 +7698,7 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
76887698
err = -ENAMETOOLONG;
76897699
goto err_unpin_maps;
76907700
}
7701+
sanitize_pin_path(buf);
76917702
pin_path = buf;
76927703
} else if (!map->pin_path) {
76937704
continue;
@@ -7732,6 +7743,7 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
77327743
return -EINVAL;
77337744
else if (len >= PATH_MAX)
77347745
return -ENAMETOOLONG;
7746+
sanitize_pin_path(buf);
77357747
pin_path = buf;
77367748
} else if (!map->pin_path) {
77377749
continue;

0 commit comments

Comments
 (0)