Skip to content

Commit d82e50e

Browse files
amscanneKernel Patches Daemon
authored andcommitted
libbpf: fix possible use-after-free for externs
The `name` field in `obj->externs` points into the BTF data at load time. However, some functions may invalidate this after loading (e.g. `bpf_map__set_value_size`), which results in pointers into freed memory and undefined behavior. The simplest solution is to simply `strdup` these strings, similar to the `essent_name`, and free them at the same time. Signed-off-by: Adin Scannell <[email protected]>
1 parent a994d4a commit d82e50e

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ struct extern_desc {
597597
int sym_idx;
598598
int btf_id;
599599
int sec_btf_id;
600-
const char *name;
600+
char *name;
601601
char *essent_name;
602602
bool is_set;
603603
bool is_weak;
@@ -4259,7 +4259,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
42594259
return ext->btf_id;
42604260
}
42614261
t = btf__type_by_id(obj->btf, ext->btf_id);
4262-
ext->name = btf__name_by_offset(obj->btf, t->name_off);
4262+
ext->name = strdup(btf__name_by_offset(obj->btf, t->name_off));
42634263
ext->sym_idx = i;
42644264
ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
42654265

@@ -9138,8 +9138,10 @@ void bpf_object__close(struct bpf_object *obj)
91389138
zfree(&obj->btf_custom_path);
91399139
zfree(&obj->kconfig);
91409140

9141-
for (i = 0; i < obj->nr_extern; i++)
9141+
for (i = 0; i < obj->nr_extern; i++) {
9142+
zfree(&obj->externs[i].name);
91429143
zfree(&obj->externs[i].essent_name);
9144+
}
91439145

91449146
zfree(&obj->externs);
91459147
obj->nr_extern = 0;

0 commit comments

Comments
 (0)