Skip to content

Commit 60b1305

Browse files
Rtoaxqmonnet
authored andcommitted
bpftool: Check map name length when map create
The size of struct bpf_map::name is BPF_OBJ_NAME_LEN (16). bpf(2) { map_create() { bpf_obj_name_cpy(map->name, attr->map_name, sizeof(attr->map_name)); } } When specifying a map name using bpftool map create name, no error is reported if the name length is greater than 15. $ sudo bpftool map create /sys/fs/bpf/12345678901234567890 \ type array key 4 value 4 entries 5 name 12345678901234567890 Users will think that 12345678901234567890 is legal, but this name cannot be used to index a map. $ sudo bpftool map show name 12345678901234567890 Error: can't parse name $ sudo bpftool map show ... 1249: array name 123456789012345 flags 0x0 key 4B value 4B max_entries 5 memlock 304B $ sudo bpftool map show name 123456789012345 1249: array name 123456789012345 flags 0x0 key 4B value 4B max_entries 5 memlock 304B The map name provided in the command line is truncated, but no warning is reported. This submission checks the length of the map name. Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Rong Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b2bf2bb commit 60b1305

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/map.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,10 @@ static int do_create(int argc, char **argv)
12701270
} else if (is_prefix(*argv, "name")) {
12711271
NEXT_ARG();
12721272
map_name = GET_ARG();
1273+
if (strlen(map_name) > BPF_OBJ_NAME_LEN - 1) {
1274+
p_info("Warning: map name is longer than %u characters, it will be truncated.",
1275+
BPF_OBJ_NAME_LEN - 1);
1276+
}
12731277
} else if (is_prefix(*argv, "key")) {
12741278
if (parse_u32_arg(&argc, &argv, &key_size,
12751279
"key size"))

0 commit comments

Comments
 (0)