bpf: Refuse the vmlinux prototype for a module function
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v2 Latest4 patches 1 replyIn review
- v14 patches 0 repliesSuperseded
Changes in v2 from the cover letter
* Refuse the ID in bpf_check_attach_target(), which logs why, and fix the Fixes tag (patch 1)
* Don't search the module twice when it has no function of that name (patch 3)
Patches in v2 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/4 | bpf: Refuse the vmlinux prototype for a module functioninclude/ kernel/ kernel/ | +45 | 0 replies | |
| 2/4 | bpf: Refuse the vmlinux prototype for a module function in multi linkskernel/ | +6 | 0 replies | |
| 3/4 | libbpf: Look up the functions of a module firsttools/ | +51 | 0 replies | |
| 4/4 | selftests/bpf: Test a vmlinux function ID with the BTF of a moduletools/ tools/ tools/ +1 more | +126 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.
Cover letter [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
A tracing program can be loaded with the BTF of a module and the ID of a vmlinux function. The kernel then attaches it to the module function but with the vmlinux prototype. If the two prototypes differ the program reads the arguments as wrong types. Even worse, if the module function takes more arguments the trampoline doesn't save them. A tracing multi link has the same problem with its IDs. And libbpf makes such pairs itself for "fentry/mod:func" and for module globs, since it searches the types of vmlinux first.
Patches 1 and 2 refuse the ID of a vmlinux function with the BTF of a module if the module has its own function of that name. This is checked at load and at the creation of a tracing multi link. Patch 3 makes libbpf look up the functions of a module first. Patch 4 tests the kernel and libbpf.
A simpler check "with the BTF of a module the ID must be a module type" doesn't work, because pahole and resolve_btfids dedup the BTF of a module against vmlinux and a module function with the name and the prototype of a vmlinux function is merged into the vmlinux FUNC. The vmlinux ID is then the only way to attach to it. Some selftests rely on this.
One case stays open: a module function which pahole leaves out of BTF and which has the name of a vmlinux function of another prototype. pahole drops a function whose prototype doesn't match how it is compiled or whose name has several prototypes in the module. Its symbol is still in kallsyms and the module has no FUNC to tell it by, so the kernel takes the vmlinux ID and uses the wrong prototype.
And one case which worked before is now refused: a caller which passes the vmlinux ID while the module has its own FUNC of a compatible prototype, e.g. one with other parameter names, which dedup doesn't merge. libbpf with patch 3 picks the module's own ID. Other loaders have to do the same. Such functions are rare. In a stock Ubuntu 7.0 arm64 kernel six module functions have their own FUNC with the name of a vmlinux function. Four of them have another prototype and are the bug which patches 1 and 2 fix. Only two, ghash_setkey() and ghash_update() of ghash_ce, differ just in parameter names.
A proper longer-term (to deploy) fix: both cases go away if btf__dedup() stops merging the FUNCs of a module into the FUNCs of vmlinux. A FUNC stands for a symbol of one object, so the two aren't the same type even when they look equal. Then the kernel can refuse any vmlinux function ID with the BTF of a module. This needs a change in btf__dedup() for split BTF and a pahole which picks it up. I'm looking for comments on whether this follow-up is ok.
Patches 1-3 have Fixes tags, however the bug is old and needs CAP_BPF and CAP_PERFMON, so I am sending this to bpf-next.
v1 -> v2:
* Refuse the ID in bpf_check_attach_target(), which logs why, and fix the Fixes tag (patch 1)
* Don't search the module twice when it has no function of that name (patch 3)
v1: https://lore.kernel.org/bpf/[email protected]
Anton Protopopov (4):
bpf: Refuse the vmlinux prototype for a module function
bpf: Refuse the vmlinux prototype for a module function in multi links
libbpf: Look up the functions of a module first selftests/bpf: Test a vmlinux function ID with the BTF of a module
include/linux/btf.h | 2 + kernel/bpf/btf.c | 40 ++++++-- kernel/bpf/verifier.c | 17 ++++ tools/lib/bpf/libbpf.c | 75 ++++++++++----- .../selftests/bpf/prog_tests/module_attach.c | 11 +++ .../bpf/prog_tests/module_fentry_shadow.c | 96 +++++++++++++++++-- .../selftests/bpf/progs/test_module_attach.c | 16 ++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 10 ++ 8 files changed, 228 insertions(+), 39 deletions(-)
Signature · 1 line
2.43.0