All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: Check if map names are not NULL
@ 2019-03-20 12:27 Michal Rostecki
  2019-03-21  1:33 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: Michal Rostecki @ 2019-03-20 12:27 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf
  Cc: 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:

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")
Signed-off-by: Michal Rostecki <mrostecki@opensuse.org>
---
 tools/lib/bpf/bpf.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 9cd015574e83..21484ff1cecb 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -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)
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf] libbpf: Check if map names are not NULL
  2019-03-20 12:27 [PATCH bpf] libbpf: Check if map names are not NULL Michal Rostecki
@ 2019-03-21  1:33 ` Alexei Starovoitov
  0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2019-03-21  1:33 UTC (permalink / raw)
  To: Michal Rostecki
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf

On Wed, Mar 20, 2019 at 01:27:42PM +0100, Michal Rostecki wrote:
> 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")
> Signed-off-by: Michal Rostecki <mrostecki@opensuse.org>

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-21  1:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20 12:27 [PATCH bpf] libbpf: Check if map names are not NULL Michal Rostecki
2019-03-21  1:33 ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.