Skip to content

bpf: Fix two trampoline image UAFs in bpf_trampoline_multi_detach()

Hui Zhu bpf lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2bpf: Fix stale old_image UAF on trampoline update failurekernel/bpf/trampoline.c+11 -10 replies
2/2bpf: Fix UAF in bpf_trampoline_multi_detach when ftrace update failskernel/bpf/trampoline.c+35 -71 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next 0/2] bpf: Fix two trampoline image UAFs in bpf_trampoline_multi_detach()

From: Hui Zhu <[email protected]>

While auditing the error paths added by commit aef4dfa790b2 ("bpf: Add bpf_trampoline_multi_attach/detach functions"), I found two independent ways bpf_trampoline_multi_detach() can free a trampoline image that ftrace is still actively calling into.

Patch 1 fixes the case where __bpf_trampoline_unlink_prog() itself fails before touching ftrace (e.g. -ENOMEM building the new image):
cur_image is left equal to old_image, but bpf_trampoline_multi_attach_free() frees old_image unconditionally.

Patch 2 fixes the narrower case where the per-trampoline unlink succeeds (cur_image has already been speculatively advanced) but the later batched update_ftrace_direct_del()/update_ftrace_direct_mod() call fails. Patch 1's old_image != cur_image check alone isn't enough here since ftrace was never actually updated; the affected trampolines need to be rolled back to old_image instead of freed.

Both paths are already WARN_ON_ONCE()'d and need an allocation or ftrace failure to trigger, so they're unlikely to fire in practice, but when they do, the current code frees a live image out from under ftrace.

Hui Zhu (2):
bpf: Fix stale old_image UAF on trampoline update failure
bpf: Fix UAF in bpf_trampoline_multi_detach when ftrace update fails

 kernel/bpf/trampoline.c | 54 +++++++++++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0