Skip to content

bpf: Fix tailcall infinite loop caused by freplace

Leon Hwang bpf 8 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest2 patches 0 repliesApplied 2/2
  2. v72 patches 5 repliesSuperseded
  3. v63 patches 13 repliesSuperseded
Earlier versions · 5
VersionPostedPatchesRepliesStatus
v53 patches1 replySuperseded
v44 patches9 repliesSuperseded
v34 patches6 repliesSuperseded
v24 patches9 repliesSuperseded
v14 patches19 repliesSuperseded

Changes in v8 from the cover letter

* Address comment from Alexei:
* guard(mutex) should not hold range all the way through bpf_arch_text_poke().
* Address suggestion from Xu Kuohai:
* Extension prog should not be tailcalled independently.

Patches in v8 All 2 applied

#Subject and filesSizeRepliesApplied
1/2bpf: Prevent tailcall infinite loop caused by freplaceinclude/linux/bpf.h kernel/bpf/arraymap.c kernel/bpf/core.c +2 more+81 -170 repliesApplied
2/2selftests/bpf: Add test to verify tailcall and freplace restrictionstools/testing/selftests/bpf/prog_tests/tailcalls.c tools/testing/selftests/bpf/progs/tc_bpf2bpf.c+109 -160 repliesApplied

Reviews trailers per patch

All 2 patches have a Reviewed-by or Acked-by.

PatchEduard Zingerman
1/2R-b
2/2A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v8 thread

Cover letter [PATCH bpf-next v8 0/2] bpf: Fix tailcall infinite loop caused by freplace

Previously, I addressed a tailcall infinite loop issue related to trampolines[0].

In this patchset, I resolve a similar issue where a tailcall infinite loop can occur due to the combination of tailcalls and freplace programs. The fix prevents adding extended programs to the prog_array map and blocks the extension of a tail callee program with freplace.

Key changes:

1. If a program or its subprogram has been extended by an freplace program, it can no longer be updated to a prog_array map.
2. If a program has been added to a prog_array map, neither it nor its subprograms can be extended by an freplace program.

Additionally, an extension program should not be tailcalled. As a result, return -EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a prog_array map.

Changes:
v7 -> v8:
* Address comment from Alexei:
* guard(mutex) should not hold range all the way through bpf_arch_text_poke().
* Address suggestion from Xu Kuohai:
* Extension prog should not be tailcalled independently.

v6 -> v7:
* Address comments from Alexei:
* Rewrite commit message more imperative and consice with AI.
* Extend bpf_trampoline_link_prog() and bpf_trampoline_unlink_prog() to link and unlink target prog for freplace prog.
* Use guard(mutex)(&tgt_prog->aux->ext_mutex) instead of mutex_lock()&mutex_unlock() pair.
* Address comment from Eduard:
* Remove misplaced "Reported-by" and "Closes" tags.

v5 -> v6:
* Fix a build warning reported by kernel test robot.

v4 -> v5:
* Move code of linking/unlinking target prog of freplace to trampoline.c.
* Address comments from Alexei:
* Change type of prog_array_member_cnt to u64.
* Combine two patches to one.

v3 -> v4:
* Address comments from Eduard:
* Rename 'tail_callee_cnt' to 'prog_array_member_cnt'.
* Add comment to 'prog_array_member_cnt'.
* Use a mutex to protect 'is_extended' and 'prog_array_member_cnt'.

v2 -> v3:
* Address comments from Alexei:
* Stop hacking JIT.
* Prevent the specific use case at attach/update time.

v1 -> v2:
* Address comment from Eduard:
* Explain why nop5 and xor/nop3 are swapped at prologue.
* Address comment from Alexei:
* Disallow attaching tail_call_reachable freplace prog to not-tail_call_reachable target in verifier.
* Update "bpf, arm64: Fix tailcall infinite loop caused by freplace" with latest arm64 JIT code.

Links:
[0] https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (2):
bpf: Prevent tailcall infinite loop caused by freplace selftests/bpf: Add test to verify tailcall and freplace restrictions

 include/linux/bpf.h                           |  17 ++-
 kernel/bpf/arraymap.c                         |  26 +++-
 kernel/bpf/core.c                             |   1 +
 kernel/bpf/syscall.c                          |   7 +-
 kernel/bpf/trampoline.c                       |  47 +++++--
 .../selftests/bpf/prog_tests/tailcalls.c      | 120 ++++++++++++++++--
 .../testing/selftests/bpf/progs/tc_bpf2bpf.c  |   5 +-
 7 files changed, 190 insertions(+), 33 deletions(-)
Signature · 1 line
2.44.0