提交 8a7a2903 编写于 作者: D Daniel Xu 提交者: Zheng Zengkai

libbpf: Do not close un-owned FD 0 on errors

mainline inclusion
from mainline-5.14-rc6
commit c34c338a
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c34c338a40e4f3b6f80889cd17fd9281784d1c32

-------------------------------------------------

Before this patch, btf_new() was liable to close an arbitrary FD 0 if
BTF parsing failed. This was because:

* btf->fd was initialized to 0 through the calloc()
* btf__free() (in the `done` label) closed any FDs >= 0
* btf->fd is left at 0 if parsing fails

This issue was discovered on a system using libbpf v0.3 (without
BTF_KIND_FLOAT support) but with a kernel that had BTF_KIND_FLOAT types
in BTF. Thus, parsing fails.

While this patch technically doesn't fix any issues b/c upstream libbpf
has BTF_KIND_FLOAT support, it'll help prevent issues in the future if
more BTF types are added. It also allow the fix to be backported to
older libbpf's.

Fixes: 3289959b ("libbpf: Support BTF loading and raw data output in both endianness")
Signed-off-by: NDaniel Xu <dxu@dxuuu.xyz>
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Acked-by: NYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/5969bb991adedb03c6ae93e051fd2a00d293cf25.1627513670.git.dxu@dxuuu.xyz
(cherry picked from commit c34c338a)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 3ccf0901
...@@ -810,6 +810,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf) ...@@ -810,6 +810,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
btf->nr_types = 0; btf->nr_types = 0;
btf->start_id = 1; btf->start_id = 1;
btf->start_str_off = 0; btf->start_str_off = 0;
btf->fd = -1;
if (base_btf) { if (base_btf) {
btf->base_btf = base_btf; btf->base_btf = base_btf;
...@@ -838,8 +839,6 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf) ...@@ -838,8 +839,6 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
if (err) if (err)
goto done; goto done;
btf->fd = -1;
done: done:
if (err) { if (err) {
btf__free(btf); btf__free(btf);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册