summaryrefslogtreecommitdiff
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorAndrii Nakryiko <andriin@fb.com>2020-07-09 18:10:23 -0700
committerDaniel Borkmann <daniel@iogearbox.net>2020-07-10 16:24:35 +0200
commit5c3320d7fece4612d4a413aa3c8e82cdb5b49fcb (patch)
treef3a1592c19c8d4b1502b06a352d598a24b498b82 /tools/lib/bpf/libbpf.c
parent2977282b63c3b6f112145ecf0bcefff0c65bd3ac (diff)
libbpf: Fix memory leak and optimize BTF sanitization
Coverity's static analysis helpfully reported a memory leak introduced by 0f0e55d8247c ("libbpf: Improve BTF sanitization handling"). While fixing it, I realized that btf__new() already creates a memory copy, so there is no need to do this. So this patch also fixes misleading btf__new() signature to make data into a `const void *` input parameter. And it avoids unnecessary memory allocation and copy in BTF sanitization code altogether. Fixes: 0f0e55d8247c ("libbpf: Improve BTF sanitization handling") Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200710011023.1655008-1-andriin@fb.com
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r--tools/lib/bpf/libbpf.c11
1 files changed, 3 insertions, 8 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6602eb479596..25e4f77be8d7 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2533,17 +2533,12 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
sanitize = btf_needs_sanitization(obj);
if (sanitize) {
- const void *orig_data;
- void *san_data;
+ const void *raw_data;
__u32 sz;
/* clone BTF to sanitize a copy and leave the original intact */
- orig_data = btf__get_raw_data(obj->btf, &sz);
- san_data = malloc(sz);
- if (!san_data)
- return -ENOMEM;
- memcpy(san_data, orig_data, sz);
- kern_btf = btf__new(san_data, sz);
+ raw_data = btf__get_raw_data(obj->btf, &sz);
+ kern_btf = btf__new(raw_data, sz);
if (IS_ERR(kern_btf))
return PTR_ERR(kern_btf);