Kernel Support for miscellaneous Binary Formats (binfmt_misc)¶
This Kernel feature allows you to invoke almost (for restrictions see below)every program by simply typing its name in the shell.This includes for example compiled Java(TM), Python or Emacs programs.
To achieve this you must tell binfmt_misc which interpreter has to be invokedwith which binary. Binfmt_misc recognises the binary-type by matching some bytesat the beginning of the file with a magic byte sequence (masking out specifiedbits) you have supplied. Binfmt_misc can also recognise a filename extensionaka.com or.exe.
First you must mount binfmt_misc:
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
To actually register a new binary type, you have to set up a string looking like:name:type:offset:magic:mask:interpreter:flags (where you can choose the: upon your needs) and echo it to/proc/sys/fs/binfmt_misc/register.
Here is what the fields mean:
nameis an identifier string. A new /proc file will be created with thisname below
/proc/sys/fs/binfmt_misc; cannot contain slashes/forobvious reasons.
typeis the type of recognition. Give
Mfor magic andEfor extension.
offsetis the offset of the magic/mask in the file, counted in bytes. Thisdefaults to 0 if you omit it (i.e. you write
:name:type::magic...).Ignored when using filename extension matching.
magicis the byte sequence binfmt_misc is matching for. The magic stringmay contain hex-encoded characters like
\x0aor\xA4. Note that youmust escape any NUL bytes; parsing halts at the first one. In a shellenvironment you might have to write\\x0ato prevent the shell fromeating your\.If you chose filename extension matching, this is the extension to berecognised (without the., the\x0aspecials are not allowed).Extension matching is case sensitive, and slashes/are not allowed!
maskis an (optional, defaults to all 0xff) mask. You can mask out somebits from matching by supplying a string like magic and as long as magic.The mask is anded with the byte sequence of the file. Note that you mustescape any NUL bytes; parsing halts at the first one. Ignored when usingfilename extension matching.
interpreteris the program that should be invoked with the binary as firstargument (specify the full path)
flagsis an optional field that controls several aspects of the invocationof the interpreter. It is a string of capital letters, each controls acertain aspect. The following flags are supported:
P- preserve-argv[0]Legacy behavior of binfmt_misc is to overwritethe original argv[0] with the full path to the binary. When thisflag is included, binfmt_misc will add an argument to the argumentvector for this purpose, thus preserving the original
argv[0].e.g. If your interp is set to/bin/fooand you runblah(which is in/usr/local/bin), then the kernel will execute/bin/foowithargv[]set to["/bin/foo","/usr/local/bin/blah","blah"]. The interp has to be aware of this so it canexecute/usr/local/bin/blahwithargv[]set to["blah"].O- open-binaryLegacy behavior of binfmt_misc is to pass the full pathof the binary to the interpreter as an argument. When this flag isincluded, binfmt_misc will open the file for reading and pass itsdescriptor as an argument, instead of the full path, thus allowingthe interpreter to execute non-readable binaries. This featureshould be used with care - the interpreter has to be trusted not toemit the contents of the non-readable binary.
C- credentialsCurrently, the behavior of binfmt_misc is to calculatethe credentials and security token of the new process according tothe interpreter. When this flag is included, these attributes arecalculated according to the binary. It also implies the
Oflag.This feature should be used with care as the interpreterwill run with root permissions when a setuid binary owned by rootis run with binfmt_misc.F- fix binaryThe usual behaviour of binfmt_misc is to spawn thebinary lazily when the misc format file is invoked. However,this doesn’t work very well in the face of mount namespaces andchangeroots, so the
Fmode opens the binary as soon as theemulation is installed and uses the opened image to spawn theemulator, meaning it is always available once installed,regardless of how the environment changes.
There are some restrictions:
the whole register string may not exceed 1920 characters
the magic must reside in the first 128 bytes of the file, i.e.offset+size(magic) has to be less than 128
the interpreter string may not exceed 127 characters
To use binfmt_misc you have to mount it first. You can mount it withmount-tbinfmt_miscnone/proc/sys/fs/binfmt_misc command, or you can adda linenone /proc/sys/fs/binfmt_miscbinfmt_miscdefaults00 to your/etc/fstab so it auto mounts on boot.
You may want to add the binary formats in one of your/etc/rc scripts duringboot-up. Read the manual of your init program to figure out how to do thisright.
Think about the order of adding entries! Later added entries are matched first!
A few examples (assumed you are in/proc/sys/fs/binfmt_misc):
enable support for em86 (like binfmt_em86, for Alpha AXP only):
echo ':i386:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:' > registerecho ':i486:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:' > register
enable support for packed DOS applications (pre-configured dosemu hdimages):
echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
enable support for Windows executables using wine:
echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
For java support seeJava(tm) Binary Kernel Support for Linux v1.03
You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)or 1 (to enable) to/proc/sys/fs/binfmt_misc/status or/proc/.../the_name.Catting the file tells you the current status ofbinfmt_misc/the_entry.
You can remove one entry or all entries by echoing -1 to/proc/.../the_nameor/proc/sys/fs/binfmt_misc/status.
Hints¶
If you want to pass special arguments to your interpreter, you canwrite a wrapper script for it.SeeDocumentation/admin-guide/java.rst for an example.
Your interpreter should NOT look in the PATH for the filename; the kernelpasses it the full filename (or the file descriptor) to use. Using$PATH cancause unexpected behaviour and can be a security hazard.
Richard Günther <rguenth@tat.physik.uni-tuebingen.de>