Skip to content

Fix attaching fentry/fexit/fmod_ret/lsm to modules

Viktor Malik bpf linux-modules llvm oe-kbuild-all 10 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. v10 Latest2 patches 5 repliesApplied 2/2
  2. v92 patches 3 repliesSuperseded
  3. v82 patches 3 repliesSuperseded
Earlier versions · 7
VersionPostedPatchesRepliesStatus
v72 patches2 repliesSuperseded
v62 patches17 repliesSuperseded
v52 patches2 repliesSuperseded
v42 patches4 repliesSuperseded
v33 patches4 repliesSuperseded
v23 patches2 repliesSuperseded
v12 patches3 repliesSuperseded

Changes in v10 from the cover letter

- added the new test to DENYLIST.aarch64 (suggested by Andrii)
- renamed the test source file to match the test name

Patches in v10 All 2 applied

#Subject and filesSizeRepliesApplied
1/2bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modulesinclude/linux/bpf.h kernel/bpf/syscall.c kernel/bpf/trampoline.c +2 more+30 -295 repliesApplied
2/2bpf/selftests: Test fentry attachment to shadowed functionsnet/bpf/test_run.c tools/testing/selftests/bpf/DENYLIST.aarch64 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +1 more+140 -00 repliesApplied

Reviews trailers per patch

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

PatchJiri OlsaLuis Chamberlain
1/2A-b R-b
2/2A-b

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

Read the v10 thread

Cover letter [PATCH bpf-next v10 0/2] Fix attaching fentry/fexit/fmod_ret/lsm to modules

I noticed that the verifier behaves incorrectly when attaching to fentry of multiple functions of the same name located in different modules (or in vmlinux). The reason for this is that if the target program is not specified, the verifier will search kallsyms for the trampoline address to attach to. The entire kallsyms is always searched, not respecting the module in which the function to attach to is located.

As Yonghong correctly pointed out, there is yet another issue - the trampoline acquires the module reference in register_fentry which means that if the module is unloaded between the place where the address is found in the verifier and register_fentry, it is possible that another module is loaded to the same address in the meantime, which may lead to errors.

This patch fixes the above issues by extracting the module name from the BTF of the attachment target (which must be specified) and by doing the search in kallsyms of the correct module. At the same time, the module reference is acquired right after the address is found and only released right before the program itself is unloaded.

---
Changes in v10:
- added the new test to DENYLIST.aarch64 (suggested by Andrii)
- renamed the test source file to match the test name

Changes in v9:
- two small changes suggested by Jiri Olsa and Jiri's ack

Changes in v8:
- added module_put to error paths in bpf_check_attach_target after the module reference is acquired

Changes in v7:
- refactored the module reference manipulation (comments by Jiri Olsa)
- cleaned up the test (comments by Andrii Nakryiko)

Changes in v6:
- storing the module reference inside bpf_prog_aux instead of bpf_trampoline and releasing it when the program is unloaded (suggested by Jiri Olsa)

Changes in v5:
- fixed acquiring and releasing of module references by trampolines to prevent modules being unloaded between address lookup and trampoline allocation

Changes in v4:
- reworked module kallsyms lookup approach using existing functions, verifier now calls btf_try_get_module to retrieve the module and find_kallsyms_symbol_value to get the symbol address (suggested by Alexei)
- included Jiri Olsa's comments
- improved description of the new test and added it as a comment into the test source

Changes in v3:
- added trivial implementation for kallsyms_lookup_name_in_module() for !CONFIG_MODULES (noticed by test robot, fix suggested by Hao Luo)

Changes in v2:
- introduced and used more space-efficient kallsyms lookup function, suggested by Jiri Olsa
- included Hao Luo's comments

Viktor Malik (2):
bpf: Fix attaching fentry/fexit/fmod_ret/lsm to modules bpf/selftests: Test fentry attachment to shadowed functions

 include/linux/bpf.h                           |   2 +
 kernel/bpf/syscall.c                          |   6 +
 kernel/bpf/trampoline.c                       |  28 ----
 kernel/bpf/verifier.c                         |  18 ++-
 kernel/module/internal.h                      |   5 +
 net/bpf/test_run.c                            |   5 +
 tools/testing/selftests/bpf/DENYLIST.aarch64  |   1 +
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |   6 +
 .../bpf/prog_tests/module_fentry_shadow.c     | 128 ++++++++++++++++++
 9 files changed, 170 insertions(+), 29 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/module_fentry_shadow.c
Signature · 1 line
2.39.2