Skip to content

binfmt_misc: transparent interpreters and PT_INTERP loader substitution

Christian Brauner bpf linux-fsdevel linux-mm sashiko-reviews 2 versions – In review

Get series with b4
b4 am 20260721-work-bpf-binfmt_misc-ptinterp-v2-0-e57866e4ae0f@kernel.org change-id 20260719-work-bpf-binfmt_misc-ptinterp-e6ee99ff6ba8

Versions newest first

  1. v2 Latest21 patches 13 repliesIn review
  2. v121 patches 3 repliesSuperseded

Changes in v2 from the cover letter

- Move "exec: do not act on a stale execfd request without an executable" into vfs.fixes.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/21binfmt_misc: require an absolute interpreter path with 'C'Documentation/admin-guide/binfmt-misc.rst fs/binfmt_misc.c+10 -01 reply
2/21docs, binfmt_misc: keep general usage out of the handler sectionsDocumentation/admin-guide/binfmt-misc.rst+40 -391 reply
3/21binfmt_misc: table-drive the register string flagsfs/binfmt_misc.c+52 -401 reply
4/21binfmt_misc: normalize the per-exec invocation flagsfs/binfmt_misc.c+39 -231 reply
5/21binfmt_misc: split out entry_open_interpreter() and build_interp_argv()fs/binfmt_misc.c+76 -341 reply
6/21exec: release the replaced file with do_close_execat()fs/exec.c+6 -41 reply
7/21selftests/exec: convert the binfmt_misc bpf test to the kselftest harnesstools/testing/selftests/exec/Makefile tools/testing/selftests/exec/binfmt_misc_bpf.c tools/testing/selftests/exec/binfmt_misc_common.h+168 -1310 replies
8/21exec: add AT_FLAGS_TRANSPARENT_INTERPfs/binfmt_elf.c fs/binfmt_elf_fdpic.c include/linux/binfmts.h +1 more+31 -80 replies
9/21exec: label mm->exe_file with the binary for a transparent dispatchfs/exec.c+27 -90 replies
10/21binfmt_misc: add transparent interpreter dispatchfs/binfmt_misc.c+19 -81 reply
11/21binfmt_misc: add a static transparent flag 'T'Documentation/admin-guide/binfmt-misc.rst fs/binfmt_misc.c+16 -01 reply
12/21binfmt_misc: let a bpf handler run the interpreter transparentlyDocumentation/admin-guide/binfmt-misc.rst fs/binfmt_misc.c fs/binfmt_misc_bpf.c +1 more+31 -70 replies
13/21selftests/exec: test the transparent binfmt_misc modetools/testing/selftests/exec/.gitignore tools/testing/selftests/exec/Makefile tools/testing/selftests/exec/binfmt_misc_bpf.c +3 more+345 -41 reply
14/21binfmt_misc: document the transparent identity contractDocumentation/admin-guide/binfmt-misc.rst+30 -01 reply
15/21exec: carry a PT_INTERP substitute in struct linux_binprmfs/exec.c include/linux/binfmts.h+45 -00 replies
16/21binfmt_elf: consume a stashed PT_INTERP substitutefs/binfmt_elf.c+4 -11 reply
17/21binfmt_elf_fdpic: consume a stashed PT_INTERP substitutefs/binfmt_elf_fdpic.c+5 -10 replies
18/21binfmt_misc: add the 'L' loader substitution flagDocumentation/admin-guide/binfmt-misc.rst fs/binfmt_misc.c+42 -60 replies
19/21binfmt_misc: let a bpf handler request loader substitutionDocumentation/admin-guide/binfmt-misc.rst fs/binfmt_misc.c fs/binfmt_misc_bpf.c +1 more+24 -80 replies
20/21selftests/exec: test binfmt_misc loader substitutiontools/testing/selftests/exec/.gitignore tools/testing/selftests/exec/Makefile tools/testing/selftests/exec/binfmt_loader_payload.c +4 more+808 -220 replies
21/21binfmt_misc: document loader substitutionDocumentation/admin-guide/binfmt-misc.rst+47 -00 replies

Reviews trailers per patch

6 of 21 patches have a Reviewed-by or Acked-by.

PatchFarid Zakaria
1/21R-b
2/21R-b
3/21R-b
4/21R-b
5/21R-b
6/21R-b
7/21
8/21
9/21
10/21
11/21
12/21
13/21
14/21
15/21
16/21
17/21
18/21
19/21
20/21
21/21

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 00/21] binfmt_misc: transparent interpreters and PT_INTERP loader substitution

binfmt_misc has exactly one execution model where the registered interpreter becomes the executed program and the matched binary is handed to it as an argument. For wine or qemu-user that is the point. For a per-binary loader it is backwards. The interpreter is an implementation detail of running the binary, yet it owns the entire process identity:

- argv[0] and /proc/pid/cmdline show the interpreter invocation, not what the caller executed.

- /proc/self/exe names the interpreter. Relocatable programs commonly locate themselves through it and find the dynamic linker instead.

- A binary passed to execveat() as an inaccessible O_CLOEXEC fd cannot run at all as the interpreter has no path to open it by.

- gdb cross-validates AT_ENTRY/AT_PHDR against the exe file and discards the load displacement on mismatch leaving PIE symbols unrelocated.

This series adds two dispatch modes that close the gap from opposite ends:

(1) transparent dispatch

Registered with the 'T' flag or chosen per exec with BPF_BINPRM_TRANSPARENT. The binary is sent to the interpreter through AT_EXECFD, the argument vector stays exactly as the caller built it, and the kernel labels mm->exe_file and comm with the binary. A new AT_FLAGS_TRANSPARENT_INTERP aux vector bit is raised indicating that nothing was spliced, argv belongs to the program, and to load it from the descriptor.

The interpreter keeps control of mapping the binary, so the mode covers foreign architectures and non-ELF payloads.

The exe label is not a new privilege. It names precisely the file the caller passed to execve(), not a file of the process's choosing. That file is permission-checked, write-denied while the process runs and recorded by audit. Credential derivation does not change exactly as today.

(2) loader substitution

The kernel executes the matched binary natively as the main image and substitutes the registered interpreter for the binary's PT_INTERP. binfmt_misc functions as a PT_INTERP override. There is no contract and no identity to reconstruct. So a stock dynamic loader works unchanged. Hence, 'L' is for native-arch ELF with PT_INTERP.

The two modes compose. A bpf handler reads the ELF header from bprm->buf and grades per binary, picking 'L' where it applies and 'T' or classic dispatch for the rest. If userspace control over relocation is wanted 'T' is the way to go.

---
Changes in v2:
- Move "exec: do not act on a stale execfd request without an executable" into vfs.fixes.

- support 'L' for binfmt_elf_fdpic
- Share the substitution between the two ELF loaders instead of
open-coding it twice.
- Reject a relative interpreter path when registering an 'L' or 'C'
entry without 'F'.
- Map -ENOEXEC from the loader-substitution interpreter open to -EACCES.
- Keep the write denial on bprm->executable unconditionally until
begin_new_exec() hands the file over.
- Reject a staged interpreter argument for a transparent dispatch
instead of silently discarding it.
- WARN_ON_ONCE() a loader staged by an earlier format in
load_misc_binary().
- Fix various selftests issues.
- Link to v1: https://patch.msgid.link/20260720-work-bpf-binfmt_misc-ptinterp-v1-0-ddb76c9a508e@kernel.org

---
Christian Brauner (21):
binfmt_misc: require an absolute interpreter path with 'C' docs, binfmt_misc: keep general usage out of the handler sections
binfmt_misc: table-drive the register string flags
binfmt_misc: normalize the per-exec invocation flags
binfmt_misc: split out entry_open_interpreter() and build_interp_argv()
exec: release the replaced file with do_close_execat() selftests/exec: convert the binfmt_misc bpf test to the kselftest harness
exec: add AT_FLAGS_TRANSPARENT_INTERP
exec: label mm->exe_file with the binary for a transparent dispatch
binfmt_misc: add transparent interpreter dispatch
binfmt_misc: add a static transparent flag 'T'
binfmt_misc: let a bpf handler run the interpreter transparently selftests/exec: test the transparent binfmt_misc mode
binfmt_misc: document the transparent identity contract
exec: carry a PT_INTERP substitute in struct linux_binprm
binfmt_elf: consume a stashed PT_INTERP substitute
binfmt_elf_fdpic: consume a stashed PT_INTERP substitute
binfmt_misc: add the 'L' loader substitution flag
binfmt_misc: let a bpf handler request loader substitution selftests/exec: test binfmt_misc loader substitution
binfmt_misc: document loader substitution

 Documentation/admin-guide/binfmt-misc.rst          | 177 ++++++++--
 fs/binfmt_elf.c                                    |  10 +-
 fs/binfmt_elf_fdpic.c                              |  11 +-
 fs/binfmt_misc.c                                   | 314 +++++++++++------
 fs/binfmt_misc_bpf.c                               |  22 +-
 fs/exec.c                                          |  84 ++++-
 include/linux/binfmt_misc.h                        |   8 +
 include/linux/binfmts.h                            |  25 ++
 include/uapi/linux/binfmts.h                       |   7 +
 tools/testing/selftests/exec/.gitignore            |   5 +
 tools/testing/selftests/exec/Makefile              |  27 +-
 .../testing/selftests/exec/binfmt_loader_payload.c | 167 ++++++++++
 tools/testing/selftests/exec/binfmt_misc_bpf.c     | 342 ++++++++++---------
 tools/testing/selftests/exec/binfmt_misc_common.h  | 205 ++++++++++++
 tools/testing/selftests/exec/binfmt_misc_loader.c  | 371 +++++++++++++++++++++
 .../selftests/exec/binfmt_misc_transparent.c       | 108 ++++++
 .../selftests/exec/binfmt_transparent_interp.c     | 130 ++++++++
 tools/testing/selftests/exec/loader.bpf.c          |  56 ++++
 tools/testing/selftests/exec/transparent.bpf.c     |  57 ++++
 19 files changed, 1823 insertions(+), 303 deletions(-)

---
base-commit: 6cea93ff6fe97df34b63fe94487473e16c73b74b
change-id: 20260719-work-bpf-binfmt_misc-ptinterp-e6ee99ff6ba8