Skip to content

Commit 621d81c

Browse files
danobigregkh
authored andcommitted
libbpf: Do not close un-owned FD 0 on errors
[ Upstream commit c34c338 ] 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: 3289959 ("libbpf: Support BTF loading and raw data output in both endianness") Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/5969bb991adedb03c6ae93e051fd2a00d293cf25.1627513670.git.dxu@dxuuu.xyz Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 51f1958 commit 621d81c

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

‎tools/lib/bpf/btf.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
805805
btf->nr_types = 0;
806806
btf->start_id = 1;
807807
btf->start_str_off = 0;
808+
btf->fd = -1;
808809

809810
if (base_btf) {
810811
btf->base_btf = base_btf;
@@ -833,8 +834,6 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
833834
if (err)
834835
goto done;
835836

836-
btf->fd = -1;
837-
838837
done:
839838
if (err) {
840839
btf__free(btf);

0 commit comments

Comments
 (0)