Binfmt misc
binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines. It is one of a number of binary format handlers in the kernel that are involved in preparing a user-space program to run.
The executable formats are registered through the special purpose file system
binfmt_misc file-system interface. This is either done directly by sending special sequences to the register procfs file or using a wrapper like Debian-based distributions binfmt-support package or systemd's systemd-binfmt.service.Registration
According to the a executable file type can be added by writing a line to theregister file, of the form::name:type:offset:magic:mask:interpreter:flags, name is the name of the binary format.type is either E or M- * If it is
E, the executable file format is identified by its filename extension:magicis the file extension to be associated with the binary format;offsetandmaskare ignored. - * If it is
M, the format is identified bymagicnumber at an absoluteoffsetin the file andmaskis a bitmask indicating which bits in the number are significant.interpreteris a program that is to be run with the matching file as an argument.flagsis a string of letters, each controlling a certain aspect ofinterpreterinvocation: - *
Pto preserve the original program name typed by user in command line — by adding that name toargv; theinterpreterhas to be aware of this so it can correctly pass that additional parameter to the interpreted program as itsargv. - *
Oto open the program file and pass its file descriptor to theinterpreterso it could read an otherwise unreadable file. - *
Cto determine new process credentials based on program file rather thaninterpreterfile ; impliesOflag. - *
Fto make the kernel open the binary at configuration time instead of lazily at startup time, so that it is available inside other mount namespaces and chroots as well.
/proc/sys/fs/binfmt_misc directory which can be read to get information about a given file format, or written to disable, enable or deregister it.Common usage
- binfmt_misc allows Java programs to be passed directly to the Java virtual machine.
- binfmt_misc allows identification of PE executables using the magic number "MZ". Examples:
- *
:DOSWin:M::MZ::/usr/bin/wine:will hand all such files to Wine - *
:CLR:M::MZ::/usr/bin/mono:will hand such files to Mono - * Microsoft's WSL1 uses binfmt to allow for calling Windows programs from Linux. The line is equivalent to
:WSLInterOP:M::MZ::/init:P. - binfmt_misc can also be combined with QEMU's user-mode emulation or Box86 to execute programs for other processor architectures as if they were native binaries.
- binfmt can be used to turn some compiled languages such as Go into scripting languages, acting as a substitute for the shebang line.
- binfmt_misc can be used on NixOS to run AppImage files with an interpreter.