mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-30 22:54:17 -04:00
Exercise the bpf-backed ('B') binfmt_misc handlers end to end. A handler
is a struct binfmt_misc_ops struct_ops map; the test loads and attaches
it (which publishes it by name), activates it with a 'B' entry, and
checks that a matched binary is routed to the interpreter the program
selected via bpf_binprm_set_interp().
Two self-contained cases are covered:
- bpf_interp: the match program matches a synthetic aarch64 ELF header
from the prefetched bprm->buf and the load program routes it to a
fixed interpreter of its choosing.
- nix_origin: the match program parses the program headers to commit
only to a "$ORIGIN/..."-relative PT_INTERP and the load program
resolves it to an interpreter co-located with the binary -- the
relocatable-loader case the kernel ELF loader cannot express. The
relocatable binary is linked with PT_INTERP set to the literal
"$ORIGIN/binfmt_bpf_interp" (-Wl,--dynamic-linker), which the kernel
cannot resolve on its own.
Both route to a small test interpreter that prints a marker, proving the
program-selected interpreter actually ran.
The bpf objects are compiled against the running kernel's BTF: the
Makefile generates vmlinux.h with bpftool and the harness links libbpf.
Override CLANG/BPFTOOL/VMLINUX_BTF/LIBBPF_CFLAGS/LIBBPF_LDLIBS as needed.
The bpf pieces are only built when clang, bpftool, the vmlinux BTF and
libbpf are all present (HAVE_BPF_TOOLCHAIN=y forces them) so the other
exec selftests keep building without a bpf toolchain.
Christian Brauner (Amutable) <brauner@kernel.org> says:
Adapted to the two-op contract: 'B' entries carry the handler name in
the interpreter field, both programs are sleepable, the match programs
decide. nix_origin reads PT_INTERP from the match program and load
returns zero on success. Skip on kernels without binfmt_misc_ops in BTF.
Build the bpf pieces only when the toolchain is present and gitignore
the generated artifacts.
Signed-off-by: Farid Zakaria <farid.m.zakaria@gmail.com>
Link: https://patch.msgid.link/20260714-work-bpf-binfmt_misc-v2-9-57b7529c002c@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
62 lines
1.9 KiB
C
62 lines
1.9 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* binfmt_misc_ops handler for the selftest's fixed-interpreter case: match a
|
|
* 64-bit aarch64 ELF header from the prefetched buffer and route it to a fixed
|
|
* interpreter chosen by the program. This is the portable, self-contained
|
|
* equivalent of routing a foreign binary to an emulator: it matches
|
|
* programmatically and computes the interpreter, but points at a test binary
|
|
* the harness installs rather than a system emulator.
|
|
*/
|
|
#include "vmlinux.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
#define EI_CLASS 4
|
|
#define ELFCLASS64 2
|
|
#define EM_AARCH64 183
|
|
|
|
extern int bpf_binprm_set_interp(struct linux_binprm *bprm, const char *path,
|
|
size_t path__sz) __ksym;
|
|
|
|
/*
|
|
* A magic-style decision needs nothing beyond the prefetched bprm->buf,
|
|
* even though the match program could read the file.
|
|
*/
|
|
SEC("struct_ops.s/match")
|
|
bool BPF_PROG(bpf_interp_match, struct linux_binprm *bprm)
|
|
{
|
|
__u16 machine;
|
|
|
|
if (bprm->buf[0] != 0x7f || bprm->buf[1] != 'E' ||
|
|
bprm->buf[2] != 'L' || bprm->buf[3] != 'F' ||
|
|
bprm->buf[EI_CLASS] != ELFCLASS64)
|
|
return false;
|
|
|
|
/* e_machine is a 16-bit little-endian field at offset 18. */
|
|
machine = (__u8)bprm->buf[18] | ((__u16)(__u8)bprm->buf[19] << 8);
|
|
return machine == EM_AARCH64;
|
|
}
|
|
|
|
SEC("struct_ops.s/load")
|
|
int BPF_PROG(bpf_interp_load, struct linux_binprm *bprm)
|
|
{
|
|
/*
|
|
* Keep the path on the (writable) stack: bpf_binprm_set_interp() takes
|
|
* a sized memory arg and the verifier rejects a read-only .rodata
|
|
* buffer for it. The harness installs the interpreter at this path.
|
|
*/
|
|
char interp[] = "/tmp/binfmt_bpf_interp";
|
|
|
|
/* @path__sz includes the terminating NUL; 0 commits the selection. */
|
|
return bpf_binprm_set_interp(bprm, interp, sizeof(interp));
|
|
}
|
|
|
|
SEC(".struct_ops.link")
|
|
struct binfmt_misc_ops bpf_interp = {
|
|
.match = (void *)bpf_interp_match,
|
|
.load = (void *)bpf_interp_load,
|
|
.name = "bpf_interp",
|
|
};
|