Skip to content

Commit a2aa95b

Browse files
ytcoodeAlexei Starovoitov
authored andcommitted
bpf: Fix potential array overflow in bpf_trampoline_get_progs()
The cnt value in the 'cnt >= BPF_MAX_TRAMP_PROGS' check does not include BPF_TRAMP_MODIFY_RETURN bpf programs, so the number of the attached BPF_TRAMP_MODIFY_RETURN bpf programs in a trampoline can exceed BPF_MAX_TRAMP_PROGS. When this happens, the assignment '*progs++ = aux->prog' in bpf_trampoline_get_progs() will cause progs array overflow as the progs field in the bpf_tramp_progs struct can only hold at most BPF_MAX_TRAMP_PROGS bpf programs. Fixes: 88fd9e5 ("bpf: Refactor trampoline update code") Signed-off-by: Yuntao Wang <ytcoode@gmail.com> Link: https://lore.kernel.org/r/20220430130803.210624-1-ytcoode@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5790a2f commit a2aa95b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

kernel/bpf/trampoline.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline
415415
enum bpf_tramp_prog_type kind;
416416
struct bpf_tramp_link *link_exiting;
417417
int err = 0;
418-
int cnt;
418+
int cnt = 0, i;
419419

420420
kind = bpf_attach_type_to_tramp(link->link.prog);
421421
mutex_lock(&tr->mutex);
@@ -426,7 +426,10 @@ int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline
426426
err = -EBUSY;
427427
goto out;
428428
}
429-
cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT];
429+
430+
for (i = 0; i < BPF_TRAMP_MAX; i++)
431+
cnt += tr->progs_cnt[i];
432+
430433
if (kind == BPF_TRAMP_REPLACE) {
431434
/* Cannot attach extension if fentry/fexit are in use. */
432435
if (cnt) {
@@ -512,16 +515,19 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
512515

513516
void bpf_trampoline_put(struct bpf_trampoline *tr)
514517
{
518+
int i;
519+
515520
if (!tr)
516521
return;
517522
mutex_lock(&trampoline_mutex);
518523
if (!refcount_dec_and_test(&tr->refcnt))
519524
goto out;
520525
WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
521-
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY])))
522-
goto out;
523-
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
524-
goto out;
526+
527+
for (i = 0; i < BPF_TRAMP_MAX; i++)
528+
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
529+
goto out;
530+
525531
/* This code will be executed even when the last bpf_tramp_image
526532
* is alive. All progs are detached from the trampoline and the
527533
* trampoline image is patched with jmp into epilogue to skip

0 commit comments

Comments
 (0)