History

bpf: Fix use after free in bpf_get_prog_name

There is one more problematic case I noticed while recently fixing BPF kallsyms handling in cd7455f1013e ("bpf: Fix use after free in subprog's jited symbol removal") and that is bpf_get_prog_name(). If BTF has been attached to the prog, then we may be able to fetch the function signature type id in kallsyms through prog->aux->func_info[prog->aux->func_idx].type_id. However, while the BTF object itself is torn down via RCU callback, the prog's aux->func_info is immediately freed via kvfree(prog->aux->func_info) once the prog's refcount either hit zero or when subprograms were already exposed via kallsyms and we hit the error path added in 5482e9a93c83 ("bpf: Fix memleak in aux->func_info and aux->btf"). This violates RCU as well since kallsyms could be walked in parallel where we could access aux->func_info. Hence, defer kvfree() to after RCU grace period. Looking at ba64e7d85252 ("bpf: btf: support proper non-jit func info") there is no reason/dependency where we couldn't defer the kvfree(aux->func_info) into the RCU callback.

Daniel Borkmann committed 7 years ago

Commit 3b4d9eb1 parent cd7455f1 file · +1 −1Browse files

Unified · Split

kernel/bpf/syscall.c+1−1
@@ -1326,6 +1326,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu) · 1325 unmodified lines
1326{1326{
1327 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);1327 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1328 1328
1329 kvfree(aux->func_info);
1329 free_used_maps(aux);1330 free_used_maps(aux);
1330 bpf_prog_uncharge_memlock(aux->prog);1331 bpf_prog_uncharge_memlock(aux->prog);
1331 security_bpf_prog_free(aux);1332 security_bpf_prog_free(aux);
@@ -1336,7 +1337,6 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) · 4 unmodified lines
1336{1337{
1337 bpf_prog_kallsyms_del_all(prog);1338 bpf_prog_kallsyms_del_all(prog);
1338 btf_put(prog->aux->btf);1339 btf_put(prog->aux->btf);
1339 kvfree(prog->aux->func_info);
1340 bpf_prog_free_linfo(prog);1340 bpf_prog_free_linfo(prog);
1341 1341
1342 if (deferred)1342 if (deferred)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t