blob: 97b0d792707825b2d43ef221eca4861604b8ccd1 [file] [log] [blame]
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -03001Kernel Support for miscellaneous (your favourite) Binary Formats v1.1
2=====================================================================
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4This Kernel feature allows you to invoke almost (for restrictions see below)
5every program by simply typing its name in the shell.
6This includes for example compiled Java(TM), Python or Emacs programs.
7
8To achieve this you must tell binfmt_misc which interpreter has to be invoked
9with which binary. Binfmt_misc recognises the binary-type by matching some bytes
10at the beginning of the file with a magic byte sequence (masking out specified
11bits) you have supplied. Binfmt_misc can also recognise a filename extension
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030012aka ``.com`` or ``.exe``.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030014First you must mount binfmt_misc::
15
16 mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18To actually register a new binary type, you have to set up a string looking like
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030019``:name:type:offset:magic:mask:interpreter:flags`` (where you can choose the
20``:`` upon your needs) and echo it to ``/proc/sys/fs/binfmt_misc/register``.
Mike Frysinger43bd40e2014-10-13 15:52:05 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022Here is what the fields mean:
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030023
24- ``name``
25 is an identifier string. A new /proc file will be created with this
26 ``name below /proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for
27 obvious reasons.
28- ``type``
29 is the type of recognition. Give ``M`` for magic and ``E`` for extension.
30- ``offset``
31 is the offset of the magic/mask in the file, counted in bytes. This
32 defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``).
33 Ignored when using filename extension matching.
34- ``magic``
35 is the byte sequence binfmt_misc is matching for. The magic string
36 may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you
37 must escape any NUL bytes; parsing halts at the first one. In a shell
38 environment you might have to write ``\\x0a`` to prevent the shell from
39 eating your ``\``.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 If you chose filename extension matching, this is the extension to be
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030041 recognised (without the ``.``, the ``\x0a`` specials are not allowed).
42 Extension matching is case sensitive, and slashes ``/`` are not allowed!
43- ``mask``
44 is an (optional, defaults to all 0xff) mask. You can mask out some
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 bits from matching by supplying a string like magic and as long as magic.
Mike Frysinger43bd40e2014-10-13 15:52:05 -070046 The mask is anded with the byte sequence of the file. Note that you must
47 escape any NUL bytes; parsing halts at the first one. Ignored when using
48 filename extension matching.
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030049- ``interpreter``
50 is the program that should be invoked with the binary as first
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 argument (specify the full path)
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030052- ``flags``
53 is an optional field that controls several aspects of the invocation
Mike Frysinger43bd40e2014-10-13 15:52:05 -070054 of the interpreter. It is a string of capital letters, each controls a
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030055 certain aspect. The following flags are supported:
56
57 ``P`` - preserve-argv[0]
58 Legacy behavior of binfmt_misc is to overwrite
Mike Frysinger43bd40e2014-10-13 15:52:05 -070059 the original argv[0] with the full path to the binary. When this
60 flag is included, binfmt_misc will add an argument to the argument
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030061 vector for this purpose, thus preserving the original ``argv[0]``.
62 e.g. If your interp is set to ``/bin/foo`` and you run ``blah``
63 (which is in ``/usr/local/bin``), then the kernel will execute
64 ``/bin/foo`` with ``argv[]`` set to ``["/bin/foo", "/usr/local/bin/blah", "blah"]``. The interp has to be aware of this so it can
65 execute ``/usr/local/bin/blah``
66 with ``argv[]`` set to ``["blah"]``.
67 ``O`` - open-binary
68 Legacy behavior of binfmt_misc is to pass the full path
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 of the binary to the interpreter as an argument. When this flag is
70 included, binfmt_misc will open the file for reading and pass its
71 descriptor as an argument, instead of the full path, thus allowing
Mike Frysinger43bd40e2014-10-13 15:52:05 -070072 the interpreter to execute non-readable binaries. This feature
73 should be used with care - the interpreter has to be trusted not to
74 emit the contents of the non-readable binary.
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030075 ``C`` - credentials
76 Currently, the behavior of binfmt_misc is to calculate
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 the credentials and security token of the new process according to
78 the interpreter. When this flag is included, these attributes are
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030079 calculated according to the binary. It also implies the ``O`` flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 This feature should be used with care as the interpreter
81 will run with root permissions when a setuid binary owned by root
82 is run with binfmt_misc.
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030083 ``F`` - fix binary
84 The usual behaviour of binfmt_misc is to spawn the
85 binary lazily when the misc format file is invoked. However,
86 this doesn``t work very well in the face of mount namespaces and
87 changeroots, so the ``F`` mode opens the binary as soon as the
James Bottomley4af75df2016-02-25 08:32:51 -080088 emulation is installed and uses the opened image to spawn the
89 emulator, meaning it is always available once installed,
90 regardless of how the environment changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92
93There are some restrictions:
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -030094
Mike Frysingerbbaecc02014-10-13 15:52:03 -070095 - the whole register string may not exceed 1920 characters
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 - the magic must reside in the first 128 bytes of the file, i.e.
97 offset+size(magic) has to be less than 128
98 - the interpreter string may not exceed 127 characters
99
100To use binfmt_misc you have to mount it first. You can mount it with
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300101``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
102a line ``none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
103``/etc/fstab`` so it auto mounts on boot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300105You may want to add the binary formats in one of your ``/etc/rc`` scripts during
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106boot-up. Read the manual of your init program to figure out how to do this
107right.
108
109Think about the order of adding entries! Later added entries are matched first!
110
111
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300112A few examples (assumed you are in ``/proc/sys/fs/binfmt_misc``):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300114- enable support for em86 (like binfmt_em86, for Alpha AXP only)::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300116 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
117 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300119- enable support for packed DOS applications (pre-configured dosemu hdimages)::
120
121 echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
122
123- enable support for Windows executables using wine::
124
125 echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Mauro Carvalho Chehab8c27cef2016-10-18 10:12:27 -0200127For java support see Documentation/admin-guide/java.rst
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129
130You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300131or 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or
132``/proc/.../the_name``.
133Catting the file tells you the current status of ``binfmt_misc/the_entry``.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300135You can remove one entry or all entries by echoing -1 to ``/proc/.../the_name``
136or ``/proc/sys/fs/binfmt_misc/status``.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300139Hints
140-----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142If you want to pass special arguments to your interpreter, you can
Mauro Carvalho Chehab8c27cef2016-10-18 10:12:27 -0200143write a wrapper script for it. See Documentation/admin-guide/java.rst for an
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144example.
145
146Your interpreter should NOT look in the PATH for the filename; the kernel
Mauro Carvalho Chehab59029812016-09-21 13:56:19 -0300147passes it the full filename (or the file descriptor) to use. Using ``$PATH`` can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148cause unexpected behaviour and can be a security hazard.
149
150
John Anthony Kazos Jrbe2a6082007-05-09 08:50:42 +0200151Richard Günther <rguenth@tat.physik.uni-tuebingen.de>