* [PATCH bpf-next] libbpf: fix memory leak and optimize BTF sanitization
@ 2020-07-10 1:10 Andrii Nakryiko
2020-07-10 14:29 ` Daniel Borkmann
0 siblings, 1 reply; 2+ messages in thread
From: Andrii Nakryiko @ 2020-07-10 1:10 UTC (permalink / raw)
To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko
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>
---
tools/lib/bpf/btf.c | 2 +-
tools/lib/bpf/btf.h | 2 +-
tools/lib/bpf/libbpf.c | 11 +++--------
3 files changed, 5 insertions(+), 10 deletions(-)
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index c8861c9e3635..c9e760e120dc 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -397,7 +397,7 @@ void btf__free(struct btf *btf)
free(btf);
}
-struct btf *btf__new(__u8 *data, __u32 size)
+struct btf *btf__new(const void *data, __u32 size)
{
struct btf *btf;
int err;
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 173eff23c472..a3b7ef9b737f 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -63,7 +63,7 @@ struct btf_ext_header {
};
LIBBPF_API void btf__free(struct btf *btf);
-LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size);
+LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
LIBBPF_API struct btf *btf__parse_elf(const char *path,
struct btf_ext **btf_ext);
LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
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);
--
2.24.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH bpf-next] libbpf: fix memory leak and optimize BTF sanitization
2020-07-10 1:10 [PATCH bpf-next] libbpf: fix memory leak and optimize BTF sanitization Andrii Nakryiko
@ 2020-07-10 14:29 ` Daniel Borkmann
0 siblings, 0 replies; 2+ messages in thread
From: Daniel Borkmann @ 2020-07-10 14:29 UTC (permalink / raw)
To: Andrii Nakryiko, bpf, netdev, ast; +Cc: andrii.nakryiko, kernel-team
On 7/10/20 3:10 AM, Andrii Nakryiko wrote:
> 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>
LGTM, applied, thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-07-10 14:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-10 1:10 [PATCH bpf-next] libbpf: fix memory leak and optimize BTF sanitization Andrii Nakryiko
2020-07-10 14:29 ` Daniel Borkmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).