This is a continuation of Christian's bpf-backed binfmt_misc POC [1], which he offered to hand off to me. I am carrying it forward. The kernel design is his and is unchanged. This series rebases it onto his binfmt_misc locking/cleanup series [2] and adds selftests (+ fixed from the one's shared), and therefore does not apply to mainline alone.
As for motivation for this whole change, Christian did a great VL;MR; write-up [1]. TL;DR: binfmt_misc can match a binary and hand it to a fixed interpreter, but it can't match programmatically or compute the interpreter per binary. The Nix community would love to support relocatable binaries, where the correct loader can only be found relative to the binary itself. This adds a 'B' handler type: a binfmt_misc_ops struct_ops program that inspects the binary and picks the interpreter with one new kfunc, bpf_binprm_set_interp(). bpftool struct_ops register nix_origin.bpf.o /sys/fs/bpf echo ':nix-origin:B:nix_origin::::' > /proc/sys/fs/binfmt_misc/register Patch 5's nix_origin.bpf.c is Christian's example program with a small fix so it passes the verifier. Christian, happy to add a Co-developed-by: and your Signed-off-by: if you would like the credit in the trailer. For those that want to validate this functionality on a NixOS machine, you may find my flake.nix handy [3]. It builds the kernel, compiles the bpf objects against its BTF, and runs the selftest on boot, # be sure to change virtualisation.sharedDirectories in flake.nix to your checkout $ nix build .#nixosConfigurations.micro-vm.config.system.build.vm -o /tmp/vm $ NIXPKGS_QEMU_KERNEL_micro_vm=$PWD/arch/x86/boot/bzImage \ $ NIX_DISK_IMAGE=/tmp/vm.qcow2 \ $ QEMU_KERNEL_PARAMS="binfmt_autotest console=ttyS0" \ $ QEMU_OPTS="-nographic -no-reboot" \ /tmp/vm/bin/run-micro-vm-vm [1] https://lore.kernel.org/r/[email protected] [2] https://lore.kernel.org/all/[email protected]/ [3] https://gist.github.com/fzakaria/0155e11a0882bd3d6e63f4070e7fac0c To: Christian Brauner <[email protected]> To: Alexei Starovoitov <[email protected]> To: Daniel Borkmann <[email protected]> To: Martin KaFai Lau <[email protected]> To: Shuah Khan <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Kees Cook <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Jan Kara <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Jann Horn <[email protected]> Cc: John Ericson <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Signed-off-by: Farid Zakaria <[email protected]> --- Changes in v2: - Patch 5 adds new tests that validate the functionality: - a bpf handler matches a synthetic aarch64 header and routes it to a fixed interpreter the program chooses; - a handler resolves a "$ORIGIN/..."-relative PT_INTERP to an interpreter co-located with the binary (i.e, Nix usecase); - Patch 3 is rebased to fit the new binfmt_misc code style: the 'B' field parsing is now a parse_bpf_fields() helper matching the new parse_{magic,extension}_fields() split, and load_misc_binary() keeps the bpf retry loop in the __free() cleanup style. - Link to v1: https://lore.kernel.org/r/[email protected] --- Christian Brauner (4): exec: stash a bpf-selected interpreter in struct linux_binprm binfmt_misc: add binfmt_misc_ops bpf struct_ops binfmt_misc: wire up bpf-backed 'B' entries bpf: allow fs kfuncs for binfmt_misc_ops programs Farid Zakaria (1): selftests/exec: add binfmt_misc bpf-backed handler test Documentation/admin-guide/binfmt-misc.rst | 40 +++- fs/Kconfig.binfmt | 14 ++ fs/Makefile | 1 + fs/binfmt_misc.c | 149 +++++++++++- fs/binfmt_misc_bpf.c | 275 +++++++++++++++++++++++ fs/bpf_fs_kfuncs.c | 23 +- fs/exec.c | 1 + include/linux/binfmt_misc.h | 49 ++++ include/linux/binfmts.h | 1 + tools/testing/selftests/exec/Makefile | 37 +++ tools/testing/selftests/exec/binfmt_bpf_app.c | 12 + tools/testing/selftests/exec/binfmt_bpf_interp.c | 15 ++ tools/testing/selftests/exec/binfmt_misc_bpf.c | 260 +++++++++++++++++++++ tools/testing/selftests/exec/bpf_interp.bpf.c | 52 +++++ tools/testing/selftests/exec/nix_origin.bpf.c | 179 +++++++++++++++ 15 files changed, 1091 insertions(+), 17 deletions(-) --- base-commit: 6203a47c1d78f948cd5e9ad1a4c089745e466870 change-id: 20260711-binfmt-misc-bpf-v2-0f5c1f99b9ed Best regards, -- Farid Zakaria <[email protected]>

