Commit 5d01ab7b authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: fix erroneous multi-closing of BTF FD

Libbpf stores associated BTF FD per each instance of bpf_program. When
program is unloaded, that FD is closed. This is wrong, because leads to
a race and possibly closing of unrelated files, if application
simultaneously opens new files while bpf_programs are unloaded.

It's also unnecessary, because struct btf "owns" that FD, and
btf__free(), called from bpf_object__close() will close it. Thus the fix
is to never have per-program BTF FD and fetch it from obj->btf, when
necessary.

Fixes: 2993e051 ("tools/bpf: add support to read .BTF.ext sections")
Reported-by: default avatarAndrey Ignatov <rdna@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 0a062ba7
...@@ -182,7 +182,6 @@ struct bpf_program { ...@@ -182,7 +182,6 @@ struct bpf_program {
bpf_program_clear_priv_t clear_priv; bpf_program_clear_priv_t clear_priv;
enum bpf_attach_type expected_attach_type; enum bpf_attach_type expected_attach_type;
int btf_fd;
void *func_info; void *func_info;
__u32 func_info_rec_size; __u32 func_info_rec_size;
__u32 func_info_cnt; __u32 func_info_cnt;
...@@ -313,7 +312,6 @@ void bpf_program__unload(struct bpf_program *prog) ...@@ -313,7 +312,6 @@ void bpf_program__unload(struct bpf_program *prog)
prog->instances.nr = -1; prog->instances.nr = -1;
zfree(&prog->instances.fds); zfree(&prog->instances.fds);
zclose(prog->btf_fd);
zfree(&prog->func_info); zfree(&prog->func_info);
zfree(&prog->line_info); zfree(&prog->line_info);
} }
...@@ -392,7 +390,6 @@ bpf_program__init(void *data, size_t size, char *section_name, int idx, ...@@ -392,7 +390,6 @@ bpf_program__init(void *data, size_t size, char *section_name, int idx,
prog->instances.fds = NULL; prog->instances.fds = NULL;
prog->instances.nr = -1; prog->instances.nr = -1;
prog->type = BPF_PROG_TYPE_UNSPEC; prog->type = BPF_PROG_TYPE_UNSPEC;
prog->btf_fd = -1;
return 0; return 0;
errout: errout:
...@@ -2288,9 +2285,6 @@ bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj, ...@@ -2288,9 +2285,6 @@ bpf_program_reloc_btf_ext(struct bpf_program *prog, struct bpf_object *obj,
prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext); prog->line_info_rec_size = btf_ext__line_info_rec_size(obj->btf_ext);
} }
if (!insn_offset)
prog->btf_fd = btf__fd(obj->btf);
return 0; return 0;
} }
...@@ -2463,7 +2457,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -2463,7 +2457,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
int log_buf_size = BPF_LOG_BUF_SIZE; int log_buf_size = BPF_LOG_BUF_SIZE;
char *log_buf; char *log_buf;
int ret; int btf_fd, ret;
if (!insns || !insns_cnt) if (!insns || !insns_cnt)
return -EINVAL; return -EINVAL;
...@@ -2478,7 +2472,8 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -2478,7 +2472,8 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
load_attr.license = license; load_attr.license = license;
load_attr.kern_version = kern_version; load_attr.kern_version = kern_version;
load_attr.prog_ifindex = prog->prog_ifindex; load_attr.prog_ifindex = prog->prog_ifindex;
load_attr.prog_btf_fd = prog->btf_fd >= 0 ? prog->btf_fd : 0; btf_fd = bpf_object__btf_fd(prog->obj);
load_attr.prog_btf_fd = btf_fd >= 0 ? btf_fd : 0;
load_attr.func_info = prog->func_info; load_attr.func_info = prog->func_info;
load_attr.func_info_rec_size = prog->func_info_rec_size; load_attr.func_info_rec_size = prog->func_info_rec_size;
load_attr.func_info_cnt = prog->func_info_cnt; load_attr.func_info_cnt = prog->func_info_cnt;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment