0001 Kernel Support for miscellaneous Binary Formats (binfmt_misc)
0002 =============================================================
0003
0004 This Kernel feature allows you to invoke almost (for restrictions see below)
0005 every program by simply typing its name in the shell.
0006 This includes for example compiled Java(TM), Python or Emacs programs.
0007
0008 To achieve this you must tell binfmt_misc which interpreter has to be invoked
0009 with which binary. Binfmt_misc recognises the binary-type by matching some bytes
0010 at the beginning of the file with a magic byte sequence (masking out specified
0011 bits) you have supplied. Binfmt_misc can also recognise a filename extension
0012 aka ``.com`` or ``.exe``.
0013
0014 First you must mount binfmt_misc::
0015
0016 mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
0017
0018 To actually register a new binary type, you have to set up a string looking like
0019 ``:name:type:offset:magic:mask:interpreter:flags`` (where you can choose the
0020 ``:`` upon your needs) and echo it to ``/proc/sys/fs/binfmt_misc/register``.
0021
0022 Here is what the fields mean:
0023
0024 - ``name``
0025 is an identifier string. A new /proc file will be created with this
0026 name below ``/proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for
0027 obvious reasons.
0028 - ``type``
0029 is the type of recognition. Give ``M`` for magic and ``E`` for extension.
0030 - ``offset``
0031 is the offset of the magic/mask in the file, counted in bytes. This
0032 defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``).
0033 Ignored when using filename extension matching.
0034 - ``magic``
0035 is the byte sequence binfmt_misc is matching for. The magic string
0036 may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you
0037 must escape any NUL bytes; parsing halts at the first one. In a shell
0038 environment you might have to write ``\\x0a`` to prevent the shell from
0039 eating your ``\``.
0040 If you chose filename extension matching, this is the extension to be
0041 recognised (without the ``.``, the ``\x0a`` specials are not allowed).
0042 Extension matching is case sensitive, and slashes ``/`` are not allowed!
0043 - ``mask``
0044 is an (optional, defaults to all 0xff) mask. You can mask out some
0045 bits from matching by supplying a string like magic and as long as magic.
0046 The mask is anded with the byte sequence of the file. Note that you must
0047 escape any NUL bytes; parsing halts at the first one. Ignored when using
0048 filename extension matching.
0049 - ``interpreter``
0050 is the program that should be invoked with the binary as first
0051 argument (specify the full path)
0052 - ``flags``
0053 is an optional field that controls several aspects of the invocation
0054 of the interpreter. It is a string of capital letters, each controls a
0055 certain aspect. The following flags are supported:
0056
0057 ``P`` - preserve-argv[0]
0058 Legacy behavior of binfmt_misc is to overwrite
0059 the original argv[0] with the full path to the binary. When this
0060 flag is included, binfmt_misc will add an argument to the argument
0061 vector for this purpose, thus preserving the original ``argv[0]``.
0062 e.g. If your interp is set to ``/bin/foo`` and you run ``blah``
0063 (which is in ``/usr/local/bin``), then the kernel will execute
0064 ``/bin/foo`` with ``argv[]`` set to ``["/bin/foo", "/usr/local/bin/blah", "blah"]``. The interp has to be aware of this so it can
0065 execute ``/usr/local/bin/blah``
0066 with ``argv[]`` set to ``["blah"]``.
0067 ``O`` - open-binary
0068 Legacy behavior of binfmt_misc is to pass the full path
0069 of the binary to the interpreter as an argument. When this flag is
0070 included, binfmt_misc will open the file for reading and pass its
0071 descriptor as an argument, instead of the full path, thus allowing
0072 the interpreter to execute non-readable binaries. This feature
0073 should be used with care - the interpreter has to be trusted not to
0074 emit the contents of the non-readable binary.
0075 ``C`` - credentials
0076 Currently, the behavior of binfmt_misc is to calculate
0077 the credentials and security token of the new process according to
0078 the interpreter. When this flag is included, these attributes are
0079 calculated according to the binary. It also implies the ``O`` flag.
0080 This feature should be used with care as the interpreter
0081 will run with root permissions when a setuid binary owned by root
0082 is run with binfmt_misc.
0083 ``F`` - fix binary
0084 The usual behaviour of binfmt_misc is to spawn the
0085 binary lazily when the misc format file is invoked. However,
0086 this doesn't work very well in the face of mount namespaces and
0087 changeroots, so the ``F`` mode opens the binary as soon as the
0088 emulation is installed and uses the opened image to spawn the
0089 emulator, meaning it is always available once installed,
0090 regardless of how the environment changes.
0091
0092
0093 There are some restrictions:
0094
0095 - the whole register string may not exceed 1920 characters
0096 - the magic must reside in the first 128 bytes of the file, i.e.
0097 offset+size(magic) has to be less than 128
0098 - the interpreter string may not exceed 127 characters
0099
0100 To use binfmt_misc you have to mount it first. You can mount it with
0101 ``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
0102 a line ``none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
0103 ``/etc/fstab`` so it auto mounts on boot.
0104
0105 You may want to add the binary formats in one of your ``/etc/rc`` scripts during
0106 boot-up. Read the manual of your init program to figure out how to do this
0107 right.
0108
0109 Think about the order of adding entries! Later added entries are matched first!
0110
0111
0112 A few examples (assumed you are in ``/proc/sys/fs/binfmt_misc``):
0113
0114 - enable support for em86 (like binfmt_em86, for Alpha AXP only)::
0115
0116 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:' > register
0117 echo ':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
0118
0119 - enable support for packed DOS applications (pre-configured dosemu hdimages)::
0120
0121 echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
0122
0123 - enable support for Windows executables using wine::
0124
0125 echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
0126
0127 For java support see Documentation/admin-guide/java.rst
0128
0129
0130 You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
0131 or 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or
0132 ``/proc/.../the_name``.
0133 Catting the file tells you the current status of ``binfmt_misc/the_entry``.
0134
0135 You can remove one entry or all entries by echoing -1 to ``/proc/.../the_name``
0136 or ``/proc/sys/fs/binfmt_misc/status``.
0137
0138
0139 Hints
0140 -----
0141
0142 If you want to pass special arguments to your interpreter, you can
0143 write a wrapper script for it.
0144 See :doc:`Documentation/admin-guide/java.rst <./java>` for an example.
0145
0146 Your interpreter should NOT look in the PATH for the filename; the kernel
0147 passes it the full filename (or the file descriptor) to use. Using ``$PATH`` can
0148 cause unexpected behaviour and can be a security hazard.
0149
0150
0151 Richard Günther <rguenth@tat.physik.uni-tuebingen.de>