libbpf: improve handling of unresolved kfuncs
Andrii Nakryiko authored

Currently, libbpf leaves `call #0` instruction for __weak unresolved
kfuncs, which might lead to a confusing verifier log situations, where
invalid `call #0` will be treated as successfully validated.

We can do better. Libbpf already has an established mechanism of
poisoning instructions that failed some form of resolution (e.g., CO-RE
relocation and BPF map set to not be auto-created). Libbpf doesn't fail
them outright to allow users to guard them through other means, and as
long as BPF verifier can prove that such poisoned instructions cannot be
ever reached, this doesn't consistute an invalid BPF program. If user
didn't guard such code, libbpf will extract few pieces of information to
tie such poisoned instructions back to additional information about what
entitity wasn't resolved (e.g., BPF map name, or CO-RE relocation
information).

__weak unresolved kfuncs fit this model well, so this patch extends
libbpf with poisioning and log fixup logic for kfunc calls.

Note, this poisoning is done only for kfunc *calls*, not kfunc address
resolution (ldimm64 instructions). The former cannot be ever valid, if
reached, so it's safe to poison them. The latter is a valid mechanism to
check if __weak kfunc ksym was resolved, and do necessary guarding and
work arounds based on this result, supported in most recent kernels. As
such, libbpf keeps such ldimm64 instructions as loading zero, never
poisoning them.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230418002148.3255690-4-andrii@kernel.org

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
05b6f766
Name Last commit Last update
Documentation bpf,docs: Remove KF_KPTR_GET from documentation
LICENSES LICENSES: Add the copyleft-next-0.3.1 license
arch bpf: Support 64-bit pointers to kfuncs
block block: don't set GD_NEED_PART_SCAN if scan partition failed
certs Merge tag 'kbuild-v6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild
crypto asymmetric_keys: log on fatal failures in PE/pkcs7
drivers Daniel Borkmann says:
fs Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
include bpf: Set skb redirect and from_ingress info in __bpf_tx_skb
init bootconfig: Change message if no bootconfig with CONFIG_BOOT_CONFIG_FORCE=y
io_uring Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
ipc Merge branch 'work.namespace' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
kernel bpf: Improve verifier u32 scalar equality checking
lib Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
mm Daniel Borkmann says:
net bpf: Set skb redirect and from_ingress info in __bpf_tx_skb
rust Merge tag 'rust-fixes-6.3-rc1' of https://github.com/Rust-for-Linux/linux
samples samples/bpf: sampleip: Replace PAGE_OFFSET with _text address
scripts Daniel Borkmann says:
security Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
sound ALSA: hda/hdmi: Preserve the previous PCM device upon re-enablement
tools libbpf: improve handling of unresolved kfuncs
usr usr/gen_init_cpio.c: remove unnecessary -1 values from int file
virt KVM: PPC: Make KVM_CAP_IRQFD_RESAMPLE platform dependent
.clang-format cpumask: re-introduce constant-sized cpumask optimizations
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README