BPF List
 help / color / mirror / Atom feed
* [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
@ 2026-12-03 17:26 Anton Protopopov
  2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Anton Protopopov @ 2026-12-03 17:26 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend, Viktor Malik
  Cc: Anton Protopopov

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.

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.

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/syscall.c                          | 14 +++
 kernel/bpf/verifier.c                         |  6 ++
 tools/lib/bpf/libbpf.c                        | 73 +++++++++-----
 .../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 ++
 9 files changed, 230 insertions(+), 38 deletions(-)

-- 
2.43.0


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

end of thread, other threads:[~2026-12-03 18:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 17:26 [PATCH v1 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
2026-12-03 17:26 ` [PATCH v1 bpf-next 1/4] " Anton Protopopov
2026-12-03 17:54   ` bot+bpf-ci
2026-12-03 17:26 ` [PATCH v1 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
2026-12-03 17:26 ` [PATCH v1 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
2026-12-03 18:57   ` sashiko-bot
2026-12-03 17:26 ` [PATCH v1 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov

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