]> git.baikalelectronics.ru Git - kernel.git/commit
libbpf: Fix possible NULL pointer dereference when destroying skeleton
authorYafang Shao <laoar.shao@gmail.com>
Sat, 8 Jan 2022 13:47:39 +0000 (13:47 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 13 Jan 2022 01:01:37 +0000 (17:01 -0800)
commitba5d27009d9490f8f7a4275b57e367ebc4c2a21e
treeb4f8407de2ef8ed55864a27d308189ea23f6cf6e
parent54abe705e6e63c7946bf0cdf34406e78ba5dc8d5
libbpf: Fix possible NULL pointer dereference when destroying skeleton

When I checked the code in skeleton header file generated with my own
bpf prog, I found there may be possible NULL pointer dereference when
destroying skeleton. Then I checked the in-tree bpf progs, finding that is
a common issue. Let's take the generated samples/bpf/xdp_redirect_cpu.skel.h
for example. Below is the generated code in
xdp_redirect_cpu__create_skeleton():

xdp_redirect_cpu__create_skeleton
struct bpf_object_skeleton *s;
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));
if (!s)
goto error;
...
error:
bpf_object__destroy_skeleton(s);
return  -ENOMEM;

After goto error, the NULL 's' will be deferenced in
bpf_object__destroy_skeleton().

We can simply fix this issue by just adding a NULL check in
bpf_object__destroy_skeleton().

Fixes: d6cb421f5419 ("libbpf: Add BPF object skeleton support")
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220108134739.32541-1-laoar.shao@gmail.com
tools/lib/bpf/libbpf.c