BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v4 0/2] Fix attaching fentry/fexit/fmod_ret/lsm to modules
@ 2022-12-12 12:59 Viktor Malik
  2022-12-12 12:59 ` [PATCH bpf-next v4 1/2] bpf: " Viktor Malik
  2022-12-12 12:59 ` [PATCH bpf-next v4 2/2] bpf/selftests: Test fentry attachment to shadowed functions Viktor Malik
  0 siblings, 2 replies; 7+ messages in thread
From: Viktor Malik @ 2022-12-12 12:59 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Viktor Malik

While working on bpftrace support for BTF in modules [1], 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.

This patch fixes the above issue 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.

This also adds a new test in test_progs which tries to attach a program
to fentry of two functions of the same name, one located in vmlinux and
the other in bpf_testmod. Prior to the fix, the verifier would always
use the vmlinux function address for the target trampoline, attempting
to create two trampolines for the same address (which is prohibited).

[1] https://github.com/iovisor/bpftrace/pull/2315

---
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

 kernel/bpf/verifier.c                         |  16 ++-
 net/bpf/test_run.c                            |   5 +
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |   6 +
 .../bpf/prog_tests/module_attach_shadow.c     | 131 ++++++++++++++++++
 4 files changed, 157 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c

-- 
2.38.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-12-13 21:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-12 12:59 [PATCH bpf-next v4 0/2] Fix attaching fentry/fexit/fmod_ret/lsm to modules Viktor Malik
2022-12-12 12:59 ` [PATCH bpf-next v4 1/2] bpf: " Viktor Malik
2022-12-12 17:08   ` Yonghong Song
2022-12-13 10:59     ` Viktor Malik
2022-12-13 21:06       ` Yonghong Song
2022-12-13 10:27   ` Viktor Malik
2022-12-12 12:59 ` [PATCH bpf-next v4 2/2] bpf/selftests: Test fentry attachment to shadowed functions Viktor Malik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox