162306a36Sopenharmony_ciKernel Support for miscellaneous Binary Formats (binfmt_misc)
262306a36Sopenharmony_ci=============================================================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciThis Kernel feature allows you to invoke almost (for restrictions see below)
562306a36Sopenharmony_cievery program by simply typing its name in the shell.
662306a36Sopenharmony_ciThis includes for example compiled Java(TM), Python or Emacs programs.
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciTo achieve this you must tell binfmt_misc which interpreter has to be invoked
962306a36Sopenharmony_ciwith which binary. Binfmt_misc recognises the binary-type by matching some bytes
1062306a36Sopenharmony_ciat the beginning of the file with a magic byte sequence (masking out specified
1162306a36Sopenharmony_cibits) you have supplied. Binfmt_misc can also recognise a filename extension
1262306a36Sopenharmony_ciaka ``.com`` or ``.exe``.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciFirst you must mount binfmt_misc::
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciTo actually register a new binary type, you have to set up a string looking like
1962306a36Sopenharmony_ci``:name:type:offset:magic:mask:interpreter:flags`` (where you can choose the
2062306a36Sopenharmony_ci``:`` upon your needs) and echo it to ``/proc/sys/fs/binfmt_misc/register``.
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciHere is what the fields mean:
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci- ``name``
2562306a36Sopenharmony_ci   is an identifier string. A new /proc file will be created with this
2662306a36Sopenharmony_ci   name below ``/proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for
2762306a36Sopenharmony_ci   obvious reasons.
2862306a36Sopenharmony_ci- ``type``
2962306a36Sopenharmony_ci   is the type of recognition. Give ``M`` for magic and ``E`` for extension.
3062306a36Sopenharmony_ci- ``offset``
3162306a36Sopenharmony_ci   is the offset of the magic/mask in the file, counted in bytes. This
3262306a36Sopenharmony_ci   defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``).
3362306a36Sopenharmony_ci   Ignored when using filename extension matching.
3462306a36Sopenharmony_ci- ``magic``
3562306a36Sopenharmony_ci   is the byte sequence binfmt_misc is matching for. The magic string
3662306a36Sopenharmony_ci   may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you
3762306a36Sopenharmony_ci   must escape any NUL bytes; parsing halts at the first one. In a shell
3862306a36Sopenharmony_ci   environment you might have to write ``\\x0a`` to prevent the shell from
3962306a36Sopenharmony_ci   eating your ``\``.
4062306a36Sopenharmony_ci   If you chose filename extension matching, this is the extension to be
4162306a36Sopenharmony_ci   recognised (without the ``.``, the ``\x0a`` specials are not allowed).
4262306a36Sopenharmony_ci   Extension    matching is case sensitive, and slashes ``/`` are not allowed!
4362306a36Sopenharmony_ci- ``mask``
4462306a36Sopenharmony_ci   is an (optional, defaults to all 0xff) mask. You can mask out some
4562306a36Sopenharmony_ci   bits from matching by supplying a string like magic and as long as magic.
4662306a36Sopenharmony_ci   The mask is anded with the byte sequence of the file. Note that you must
4762306a36Sopenharmony_ci   escape any NUL bytes; parsing halts at the first one. Ignored when using
4862306a36Sopenharmony_ci   filename extension matching.
4962306a36Sopenharmony_ci- ``interpreter``
5062306a36Sopenharmony_ci   is the program that should be invoked with the binary as first
5162306a36Sopenharmony_ci   argument (specify the full path)
5262306a36Sopenharmony_ci- ``flags``
5362306a36Sopenharmony_ci   is an optional field that controls several aspects of the invocation
5462306a36Sopenharmony_ci   of the interpreter. It is a string of capital letters, each controls a
5562306a36Sopenharmony_ci   certain aspect. The following flags are supported:
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci      ``P`` - preserve-argv[0]
5862306a36Sopenharmony_ci            Legacy behavior of binfmt_misc is to overwrite
5962306a36Sopenharmony_ci            the original argv[0] with the full path to the binary. When this
6062306a36Sopenharmony_ci            flag is included, binfmt_misc will add an argument to the argument
6162306a36Sopenharmony_ci            vector for this purpose, thus preserving the original ``argv[0]``.
6262306a36Sopenharmony_ci            e.g. If your interp is set to ``/bin/foo`` and you run ``blah``
6362306a36Sopenharmony_ci            (which is in ``/usr/local/bin``), then the kernel will execute
6462306a36Sopenharmony_ci            ``/bin/foo`` with ``argv[]`` set to ``["/bin/foo", "/usr/local/bin/blah", "blah"]``.  The interp has to be aware of this so it can
6562306a36Sopenharmony_ci            execute ``/usr/local/bin/blah``
6662306a36Sopenharmony_ci            with ``argv[]`` set to ``["blah"]``.
6762306a36Sopenharmony_ci      ``O`` - open-binary
6862306a36Sopenharmony_ci	    Legacy behavior of binfmt_misc is to pass the full path
6962306a36Sopenharmony_ci            of the binary to the interpreter as an argument. When this flag is
7062306a36Sopenharmony_ci            included, binfmt_misc will open the file for reading and pass its
7162306a36Sopenharmony_ci            descriptor as an argument, instead of the full path, thus allowing
7262306a36Sopenharmony_ci            the interpreter to execute non-readable binaries. This feature
7362306a36Sopenharmony_ci            should be used with care - the interpreter has to be trusted not to
7462306a36Sopenharmony_ci            emit the contents of the non-readable binary.
7562306a36Sopenharmony_ci      ``C`` - credentials
7662306a36Sopenharmony_ci            Currently, the behavior of binfmt_misc is to calculate
7762306a36Sopenharmony_ci            the credentials and security token of the new process according to
7862306a36Sopenharmony_ci            the interpreter. When this flag is included, these attributes are
7962306a36Sopenharmony_ci            calculated according to the binary. It also implies the ``O`` flag.
8062306a36Sopenharmony_ci            This feature should be used with care as the interpreter
8162306a36Sopenharmony_ci            will run with root permissions when a setuid binary owned by root
8262306a36Sopenharmony_ci            is run with binfmt_misc.
8362306a36Sopenharmony_ci      ``F`` - fix binary
8462306a36Sopenharmony_ci            The usual behaviour of binfmt_misc is to spawn the
8562306a36Sopenharmony_ci	    binary lazily when the misc format file is invoked.  However,
8662306a36Sopenharmony_ci	    this doesn't work very well in the face of mount namespaces and
8762306a36Sopenharmony_ci	    changeroots, so the ``F`` mode opens the binary as soon as the
8862306a36Sopenharmony_ci	    emulation is installed and uses the opened image to spawn the
8962306a36Sopenharmony_ci	    emulator, meaning it is always available once installed,
9062306a36Sopenharmony_ci	    regardless of how the environment changes.
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciThere are some restrictions:
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci - the whole register string may not exceed 1920 characters
9662306a36Sopenharmony_ci - the magic must reside in the first 128 bytes of the file, i.e.
9762306a36Sopenharmony_ci   offset+size(magic) has to be less than 128
9862306a36Sopenharmony_ci - the interpreter string may not exceed 127 characters
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciTo use binfmt_misc you have to mount it first. You can mount it with
10162306a36Sopenharmony_ci``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
10262306a36Sopenharmony_cia line ``none  /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
10362306a36Sopenharmony_ci``/etc/fstab`` so it auto mounts on boot.
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciYou may want to add the binary formats in one of your ``/etc/rc`` scripts during
10662306a36Sopenharmony_ciboot-up. Read the manual of your init program to figure out how to do this
10762306a36Sopenharmony_ciright.
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciThink about the order of adding entries! Later added entries are matched first!
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciA few examples (assumed you are in ``/proc/sys/fs/binfmt_misc``):
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci- enable support for em86 (like binfmt_em86, for Alpha AXP only)::
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci    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
11762306a36Sopenharmony_ci    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
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci- enable support for packed DOS applications (pre-configured dosemu hdimages)::
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci    echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci- enable support for Windows executables using wine::
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci    echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciFor java support see Documentation/admin-guide/java.rst
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciYou can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
13162306a36Sopenharmony_cior 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or
13262306a36Sopenharmony_ci``/proc/.../the_name``.
13362306a36Sopenharmony_ciCatting the file tells you the current status of ``binfmt_misc/the_entry``.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciYou can remove one entry or all entries by echoing -1 to ``/proc/.../the_name``
13662306a36Sopenharmony_cior ``/proc/sys/fs/binfmt_misc/status``.
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciHints
14062306a36Sopenharmony_ci-----
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ciIf you want to pass special arguments to your interpreter, you can
14362306a36Sopenharmony_ciwrite a wrapper script for it.
14462306a36Sopenharmony_ciSee :doc:`Documentation/admin-guide/java.rst <./java>` for an example.
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciYour interpreter should NOT look in the PATH for the filename; the kernel
14762306a36Sopenharmony_cipasses it the full filename (or the file descriptor) to use.  Using ``$PATH`` can
14862306a36Sopenharmony_cicause unexpected behaviour and can be a security hazard.
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciRichard Günther <rguenth@tat.physik.uni-tuebingen.de>
152