diff options
author | Yonghong Song <yhs@fb.com> | 2019-04-09 20:37:41 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2019-04-10 03:46:51 -0400 |
commit | 69a0f9ecef22131982ba328e6b74ebb082bc0992 (patch) | |
tree | 75665c934bb3b1449a290394d33f2b435d16b6f9 | |
parent | 6316f78306c171f5a857a2442dbeebc7baab3566 (diff) |
bpf, bpftool: fix a few ubsan warnings
The issue is reported at https://github.com/libbpf/libbpf/issues/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>
-rw-r--r-- | tools/lib/bpf/bpf.c | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index c039094ad3aa..dababce68f0b 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c | |||
@@ -79,7 +79,6 @@ static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size) | |||
79 | 79 | ||
80 | int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) | 80 | int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) |
81 | { | 81 | { |
82 | __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0; | ||
83 | union bpf_attr attr; | 82 | union bpf_attr attr; |
84 | 83 | ||
85 | memset(&attr, '\0', sizeof(attr)); | 84 | memset(&attr, '\0', sizeof(attr)); |
@@ -89,8 +88,9 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) | |||
89 | attr.value_size = create_attr->value_size; | 88 | attr.value_size = create_attr->value_size; |
90 | attr.max_entries = create_attr->max_entries; | 89 | attr.max_entries = create_attr->max_entries; |
91 | attr.map_flags = create_attr->map_flags; | 90 | attr.map_flags = create_attr->map_flags; |
92 | memcpy(attr.map_name, create_attr->name, | 91 | if (create_attr->name) |
93 | min(name_len, BPF_OBJ_NAME_LEN - 1)); | 92 | memcpy(attr.map_name, create_attr->name, |
93 | min(strlen(create_attr->name), BPF_OBJ_NAME_LEN - 1)); | ||
94 | attr.numa_node = create_attr->numa_node; | 94 | attr.numa_node = create_attr->numa_node; |
95 | attr.btf_fd = create_attr->btf_fd; | 95 | attr.btf_fd = create_attr->btf_fd; |
96 | attr.btf_key_type_id = create_attr->btf_key_type_id; | 96 | 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, | |||
155 | int key_size, int inner_map_fd, int max_entries, | 155 | int key_size, int inner_map_fd, int max_entries, |
156 | __u32 map_flags, int node) | 156 | __u32 map_flags, int node) |
157 | { | 157 | { |
158 | __u32 name_len = name ? strlen(name) : 0; | ||
159 | union bpf_attr attr; | 158 | union bpf_attr attr; |
160 | 159 | ||
161 | memset(&attr, '\0', sizeof(attr)); | 160 | 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, | |||
166 | attr.inner_map_fd = inner_map_fd; | 165 | attr.inner_map_fd = inner_map_fd; |
167 | attr.max_entries = max_entries; | 166 | attr.max_entries = max_entries; |
168 | attr.map_flags = map_flags; | 167 | 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)); | ||
170 | 171 | ||
171 | if (node >= 0) { | 172 | if (node >= 0) { |
172 | attr.map_flags |= BPF_F_NUMA_NODE; | 173 | attr.map_flags |= BPF_F_NUMA_NODE; |
@@ -216,7 +217,6 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, | |||
216 | void *finfo = NULL, *linfo = NULL; | 217 | void *finfo = NULL, *linfo = NULL; |
217 | union bpf_attr attr; | 218 | union bpf_attr attr; |
218 | __u32 log_level; | 219 | __u32 log_level; |
219 | __u32 name_len; | ||
220 | int fd; | 220 | int fd; |
221 | 221 | ||
222 | if (!load_attr || !log_buf != !log_buf_sz) | 222 | 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, | |||
226 | if (log_level > (4 | 2 | 1) || (log_level && !log_buf)) | 226 | if (log_level > (4 | 2 | 1) || (log_level && !log_buf)) |
227 | return -EINVAL; | 227 | return -EINVAL; |
228 | 228 | ||
229 | name_len = load_attr->name ? strlen(load_attr->name) : 0; | ||
230 | |||
231 | memset(&attr, 0, sizeof(attr)); | 229 | memset(&attr, 0, sizeof(attr)); |
232 | attr.prog_type = load_attr->prog_type; | 230 | attr.prog_type = load_attr->prog_type; |
233 | attr.expected_attach_type = load_attr->expected_attach_type; | 231 | 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, | |||
253 | attr.line_info_rec_size = load_attr->line_info_rec_size; | 251 | attr.line_info_rec_size = load_attr->line_info_rec_size; |
254 | attr.line_info_cnt = load_attr->line_info_cnt; | 252 | attr.line_info_cnt = load_attr->line_info_cnt; |
255 | attr.line_info = ptr_to_u64(load_attr->line_info); | 253 | attr.line_info = ptr_to_u64(load_attr->line_info); |
256 | memcpy(attr.prog_name, load_attr->name, | 254 | if (load_attr->name) |
257 | min(name_len, BPF_OBJ_NAME_LEN - 1)); | 255 | memcpy(attr.prog_name, load_attr->name, |
256 | min(strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1)); | ||
258 | 257 | ||
259 | fd = sys_bpf_prog_load(&attr, sizeof(attr)); | 258 | fd = sys_bpf_prog_load(&attr, sizeof(attr)); |
260 | if (fd >= 0) | 259 | if (fd >= 0) |