Skip to content

Commit 69a0f9e

Browse files
yonghong-songborkmann
authored andcommitted
bpf, bpftool: fix a few ubsan warnings
The issue is reported at libbpf/libbpf#28. Basically, per C standard, for void *memcpy(void *dest, const void *src, size_t n) if "dest" or "src" is NULL, regardless of whether "n" is 0 or not, the result of memcpy is undefined. clang ubsan reported three such instances in bpf.c with the following pattern: memcpy(dest, 0, 0). Although in practice, no known compiler will cause issues when copy size is 0. Let us still fix the issue to silence ubsan warnings. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 6316f78 commit 69a0f9e

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

‎tools/lib/bpf/bpf.c‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
7979

8080
int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
8181
{
82-
__u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
8382
union bpf_attr attr;
8483

8584
memset(&attr, '\0', sizeof(attr));
@@ -89,8 +88,9 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
8988
attr.value_size = create_attr->value_size;
9089
attr.max_entries = create_attr->max_entries;
9190
attr.map_flags = create_attr->map_flags;
92-
memcpy(attr.map_name, create_attr->name,
93-
min(name_len, BPF_OBJ_NAME_LEN - 1));
91+
if (create_attr->name)
92+
memcpy(attr.map_name, create_attr->name,
93+
min(strlen(create_attr->name), BPF_OBJ_NAME_LEN - 1));
9494
attr.numa_node = create_attr->numa_node;
9595
attr.btf_fd = create_attr->btf_fd;
9696
attr.btf_key_type_id = create_attr->btf_key_type_id;
@@ -155,7 +155,6 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
155155
int key_size, int inner_map_fd, int max_entries,
156156
__u32 map_flags, int node)
157157
{
158-
__u32 name_len = name ? strlen(name) : 0;
159158
union bpf_attr attr;
160159

161160
memset(&attr, '\0', sizeof(attr));
@@ -166,7 +165,9 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
166165
attr.inner_map_fd = inner_map_fd;
167166
attr.max_entries = max_entries;
168167
attr.map_flags = map_flags;
169-
memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
168+
if (name)
169+
memcpy(attr.map_name, name,
170+
min(strlen(name), BPF_OBJ_NAME_LEN - 1));
170171

171172
if (node >= 0) {
172173
attr.map_flags |= BPF_F_NUMA_NODE;
@@ -216,7 +217,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
216217
void *finfo = NULL, *linfo = NULL;
217218
union bpf_attr attr;
218219
__u32 log_level;
219-
__u32 name_len;
220220
int fd;
221221

222222
if (!load_attr || !log_buf != !log_buf_sz)
@@ -226,8 +226,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
226226
if (log_level > (4 | 2 | 1) || (log_level && !log_buf))
227227
return -EINVAL;
228228

229-
name_len = load_attr->name ? strlen(load_attr->name) : 0;
230-
231229
memset(&attr, 0, sizeof(attr));
232230
attr.prog_type = load_attr->prog_type;
233231
attr.expected_attach_type = load_attr->expected_attach_type;
@@ -253,8 +251,9 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
253251
attr.line_info_rec_size = load_attr->line_info_rec_size;
254252
attr.line_info_cnt = load_attr->line_info_cnt;
255253
attr.line_info = ptr_to_u64(load_attr->line_info);
256-
memcpy(attr.prog_name, load_attr->name,
257-
min(name_len, BPF_OBJ_NAME_LEN - 1));
254+
if (load_attr->name)
255+
memcpy(attr.prog_name, load_attr->name,
256+
min(strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1));
258257

259258
fd = sys_bpf_prog_load(&attr, sizeof(attr));
260259
if (fd >= 0)

0 commit comments

Comments
 (0)