Skip to contentSkip to conversation

libbpf: Check if map names are not NULL

PATCH In review Michal Rostecki 2 participants 2 messages Mar 20, 2019 – Mar 21, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Check if map names are not NULL

BPF maps do not have to be named, so map names can be NULL. Map name pointers should be checked before doing any operations which cannot be done on NULL pointers.

This issue was detected by the following errors coming from bpftool built with AddressSanitizer:

bpf.c:256:2: runtime error: null pointer passed as argument 2, which is declared to never be null bpf.c:92:2: runtime error: null pointer passed as argument 2, which is declared to never be null bpf.c:169:2: runtime error: null pointer passed as argument 2, which is declared to never be null

Fixes 88cda1c9da02 ("bpf: libbpf: Provide basic API support to specify BPF obj name")
Fixes d7be143b67c2 ("libbpf: Support expected_attach_type at prog load")
Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")
---
 tools/lib/bpf/bpf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/lib/bpf/bpf.c +9 -5
index 9cd015574e83..21484ff1cecb 100644
@@ -89,8 +89,9 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
 	attr.value_size = create_attr->value_size;
 	attr.max_entries = create_attr->max_entries;
 	attr.map_flags = create_attr->map_flags;
-	memcpy(attr.map_name, create_attr->name,
-	       min(name_len, BPF_OBJ_NAME_LEN - 1));
+	if (create_attr->name)
+		memcpy(attr.map_name, create_attr->name,
+		       min(name_len, BPF_OBJ_NAME_LEN - 1));
 	attr.numa_node = create_attr->numa_node;
 	attr.btf_fd = create_attr->btf_fd;
 	attr.btf_key_type_id = create_attr->btf_key_type_id;
@@ -166,7 +167,9 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
 	attr.inner_map_fd = inner_map_fd;
 	attr.max_entries = max_entries;
 	attr.map_flags = map_flags;
-	memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
+	if (name)
+		memcpy(attr.map_name, name,
+		       min(name_len, BPF_OBJ_NAME_LEN - 1));
 
 	if (node >= 0) {
 		attr.map_flags |= BPF_F_NUMA_NODE;
@@ -253,8 +256,9 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
 	attr.line_info_rec_size = load_attr->line_info_rec_size;
 	attr.line_info_cnt = load_attr->line_info_cnt;
 	attr.line_info = ptr_to_u64(load_attr->line_info);
-	memcpy(attr.prog_name, load_attr->name,
-	       min(name_len, BPF_OBJ_NAME_LEN - 1));
+	if (load_attr->name)
+		memcpy(attr.prog_name, load_attr->name,
+		       min(name_len, BPF_OBJ_NAME_LEN - 1));
 
 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
 	if (fd >= 0)
Signature · 1 line
2.16.4
#1 lore raw
Quoted 7 lines · Michal Rostecki

BPF maps do not have to be named, so map names can be NULL. Map name pointers should be checked before doing any operations which cannot be done on NULL pointers.

This issue was detected by the following errors coming from bpftool built with AddressSanitizer:

Quoted 8 lines

bpf.c:256:2: runtime error: null pointer passed as argument 2, which is declared to never be null bpf.c:92:2: runtime error: null pointer passed as argument 2, which is declared to never be null bpf.c:169:2: runtime error: null pointer passed as argument 2, which is declared to never be null

Fixes 88cda1c9da02 ("bpf: libbpf: Provide basic API support to specify BPF obj name")
Fixes d7be143b67c2 ("libbpf: Support expected_attach_type at prog load")
Fixes 8a138aed4a80 ("bpf: btf: Add BTF support to libbpf")

I don't think that's necessary. memcpy is fine with src==null and len==0