Skip to content

Commit a9f36bf

Browse files
jrfastabgregkh
authored andcommitted
bpf: Track subprog poke descriptors correctly and fix use-after-free
commit f263a81 upstream. Subprograms are calling map_poke_track(), but on program release there is no hook to call map_poke_untrack(). However, on program release, the aux memory (and poke descriptor table) is freed even though we still have a reference to it in the element list of the map aux data. When we run map_poke_run(), we then end up accessing free'd memory, triggering KASAN in prog_array_map_poke_run(): [...] [ 402.824689] BUG: KASAN: use-after-free in prog_array_map_poke_run+0xc2/0x34e [ 402.824698] Read of size 4 at addr ffff8881905a7940 by task hubble-fgs/4337 [ 402.824705] CPU: 1 PID: 4337 Comm: hubble-fgs Tainted: G I 5.12.0+ raspberrypi#399 [ 402.824715] Call Trace: [ 402.824719] dump_stack+0x93/0xc2 [ 402.824727] print_address_description.constprop.0+0x1a/0x140 [ 402.824736] ? prog_array_map_poke_run+0xc2/0x34e [ 402.824740] ? prog_array_map_poke_run+0xc2/0x34e [ 402.824744] kasan_report.cold+0x7c/0xd8 [ 402.824752] ? prog_array_map_poke_run+0xc2/0x34e [ 402.824757] prog_array_map_poke_run+0xc2/0x34e [ 402.824765] bpf_fd_array_map_update_elem+0x124/0x1a0 [...] The elements concerned are walked as follows: for (i = 0; i < elem->aux->size_poke_tab; i++) { poke = &elem->aux->poke_tab[i]; [...] The access to size_poke_tab is a 4 byte read, verified by checking offsets in the KASAN dump: [ 402.825004] The buggy address belongs to the object at ffff8881905a7800 which belongs to the cache kmalloc-1k of size 1024 [ 402.825008] The buggy address is located 320 bytes inside of 1024-byte region [ffff8881905a7800, ffff8881905a7c00) The pahole output of bpf_prog_aux: struct bpf_prog_aux { [...] /* --- cacheline 5 boundary (320 bytes) --- */ u32 size_poke_tab; /* 320 4 */ [...] In general, subprograms do not necessarily manage their own data structures. For example, BTF func_info and linfo are just pointers to the main program structure. This allows reference counting and cleanup to be done on the latter which simplifies their management a bit. The aux->poke_tab struct, however, did not follow this logic. The initial proposed fix for this use-after-free bug further embedded poke data tracking into the subprogram with proper reference counting. However, Daniel and Alexei questioned why we were treating these objects special; I agree, its unnecessary. The fix here removes the per subprogram poke table allocation and map tracking and instead simply points the aux->poke_tab pointer at the main programs poke table. This way, map tracking is simplified to the main program and we do not need to manage them per subprogram. This also means, bpf_prog_free_deferred(), which unwinds the program reference counting and kfrees objects, needs to ensure that we don't try to double free the poke_tab when free'ing the subprog structures. This is easily solved by NULL'ing the poke_tab pointer. The second detail is to ensure that per subprogram JIT logic only does fixups on poke_tab[] entries it owns. To do this, we add a pointer in the poke structure to point at the subprogram value so JITs can easily check while walking the poke_tab structure if the current entry belongs to the current program. The aux pointer is stable and therefore suitable for such comparison. On the jit_subprogs() error path, we omit cleaning up the poke->aux field because these are only ever referenced from the JIT side, but on error we will never make it to the JIT, so its fine to leave them dangling. Removing these pointers would complicate the error path for no reason. However, we do need to untrack all poke descriptors from the main program as otherwise they could race with the freeing of JIT memory from the subprograms. Lastly, a748c69 ("bpf: propagate poke descriptors to subprograms") had an off-by-one on the subprogram instruction index range check as it was testing 'insn_idx >= subprog_start && insn_idx <= subprog_end'. However, subprog_end is the next subprogram's start instruction. Fixes: a748c69 ("bpf: propagate poke descriptors to subprograms") Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Co-developed-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 782d71e commit a9f36bf

File tree

4 files changed

+32
-40
lines changed

4 files changed

+32
-40
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ static void bpf_tail_call_direct_fixup(struct bpf_prog *prog)
564564

565565
for (i = 0; i < prog->aux->size_poke_tab; i++) {
566566
poke = &prog->aux->poke_tab[i];
567+
if (poke->aux && poke->aux != prog->aux)
568+
continue;
569+
567570
WARN_ON_ONCE(READ_ONCE(poke->tailcall_target_stable));
568571

569572
if (poke->reason != BPF_POKE_REASON_TAIL_CALL)

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ struct bpf_jit_poke_descriptor {
752752
void *tailcall_target;
753753
void *tailcall_bypass;
754754
void *bypass_addr;
755+
void *aux;
755756
union {
756757
struct {
757758
struct bpf_map *map;

kernel/bpf/core.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,14 @@ static void bpf_prog_free_deferred(struct work_struct *work)
21672167
#endif
21682168
if (aux->dst_trampoline)
21692169
bpf_trampoline_put(aux->dst_trampoline);
2170-
for (i = 0; i < aux->func_cnt; i++)
2170+
for (i = 0; i < aux->func_cnt; i++) {
2171+
/* We can just unlink the subprog poke descriptor table as
2172+
* it was originally linked to the main program and is also
2173+
* released along with it.
2174+
*/
2175+
aux->func[i]->aux->poke_tab = NULL;
21712176
bpf_jit_free(aux->func[i]);
2177+
}
21722178
if (aux->func_cnt) {
21732179
kfree(aux->func);
21742180
bpf_prog_unlock_free(aux->prog);

kernel/bpf/verifier.c

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11157,33 +11157,19 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1115711157
goto out_free;
1115811158
func[i]->is_func = 1;
1115911159
func[i]->aux->func_idx = i;
11160-
/* the btf and func_info will be freed only at prog->aux */
11160+
/* Below members will be freed only at prog->aux */
1116111161
func[i]->aux->btf = prog->aux->btf;
1116211162
func[i]->aux->func_info = prog->aux->func_info;
11163+
func[i]->aux->poke_tab = prog->aux->poke_tab;
11164+
func[i]->aux->size_poke_tab = prog->aux->size_poke_tab;
1116311165

1116411166
for (j = 0; j < prog->aux->size_poke_tab; j++) {
11165-
u32 insn_idx = prog->aux->poke_tab[j].insn_idx;
11166-
int ret;
11167+
struct bpf_jit_poke_descriptor *poke;
1116711168

11168-
if (!(insn_idx >= subprog_start &&
11169-
insn_idx <= subprog_end))
11170-
continue;
11171-
11172-
ret = bpf_jit_add_poke_descriptor(func[i],
11173-
&prog->aux->poke_tab[j]);
11174-
if (ret < 0) {
11175-
verbose(env, "adding tail call poke descriptor failed\n");
11176-
goto out_free;
11177-
}
11178-
11179-
func[i]->insnsi[insn_idx - subprog_start].imm = ret + 1;
11180-
11181-
map_ptr = func[i]->aux->poke_tab[ret].tail_call.map;
11182-
ret = map_ptr->ops->map_poke_track(map_ptr, func[i]->aux);
11183-
if (ret < 0) {
11184-
verbose(env, "tracking tail call prog failed\n");
11185-
goto out_free;
11186-
}
11169+
poke = &prog->aux->poke_tab[j];
11170+
if (poke->insn_idx < subprog_end &&
11171+
poke->insn_idx >= subprog_start)
11172+
poke->aux = func[i]->aux;
1118711173
}
1118811174

1118911175
/* Use bpf_prog_F_tag to indicate functions in stack traces.
@@ -11213,18 +11199,6 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1121311199
cond_resched();
1121411200
}
1121511201

11216-
/* Untrack main program's aux structs so that during map_poke_run()
11217-
* we will not stumble upon the unfilled poke descriptors; each
11218-
* of the main program's poke descs got distributed across subprogs
11219-
* and got tracked onto map, so we are sure that none of them will
11220-
* be missed after the operation below
11221-
*/
11222-
for (i = 0; i < prog->aux->size_poke_tab; i++) {
11223-
map_ptr = prog->aux->poke_tab[i].tail_call.map;
11224-
11225-
map_ptr->ops->map_poke_untrack(map_ptr, prog->aux);
11226-
}
11227-
1122811202
/* at this point all bpf functions were successfully JITed
1122911203
* now populate all bpf_calls with correct addresses and
1123011204
* run last pass of JIT
@@ -11293,14 +11267,22 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1129311267
bpf_prog_free_unused_jited_linfo(prog);
1129411268
return 0;
1129511269
out_free:
11270+
/* We failed JIT'ing, so at this point we need to unregister poke
11271+
* descriptors from subprogs, so that kernel is not attempting to
11272+
* patch it anymore as we're freeing the subprog JIT memory.
11273+
*/
11274+
for (i = 0; i < prog->aux->size_poke_tab; i++) {
11275+
map_ptr = prog->aux->poke_tab[i].tail_call.map;
11276+
map_ptr->ops->map_poke_untrack(map_ptr, prog->aux);
11277+
}
11278+
/* At this point we're guaranteed that poke descriptors are not
11279+
* live anymore. We can just unlink its descriptor table as it's
11280+
* released with the main prog.
11281+
*/
1129611282
for (i = 0; i < env->subprog_cnt; i++) {
1129711283
if (!func[i])
1129811284
continue;
11299-
11300-
for (j = 0; j < func[i]->aux->size_poke_tab; j++) {
11301-
map_ptr = func[i]->aux->poke_tab[j].tail_call.map;
11302-
map_ptr->ops->map_poke_untrack(map_ptr, func[i]->aux);
11303-
}
11285+
func[i]->aux->poke_tab = NULL;
1130411286
bpf_jit_free(func[i]);
1130511287
}
1130611288
kfree(func);

0 commit comments

Comments
 (0)