binfmt_misc.txt 7.2 KB
Newer Older
1 2
Kernel Support for miscellaneous (your favourite) Binary Formats v1.1
=====================================================================
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11

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 invoked
with which binary. Binfmt_misc recognises the binary-type by matching some bytes
at the beginning of the file with a magic byte sequence (masking out specified
bits) you have supplied. Binfmt_misc can also recognise a filename extension
12
aka ``.com`` or ``.exe``.
L
Linus Torvalds 已提交
13

14 15 16
First you must mount binfmt_misc::

	mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
L
Linus Torvalds 已提交
17 18

To actually register a new binary type, you have to set up a string looking like
19 20
``: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``.
21

L
Linus Torvalds 已提交
22
Here is what the fields mean:
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

- ``name``
   is an identifier string. A new /proc file will be created with this
   ``name below /proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for
   obvious reasons.
- ``type``
   is the type of recognition. Give ``M`` for magic and ``E`` for extension.
- ``offset``
   is the offset of the magic/mask in the file, counted in bytes. This
   defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``).
   Ignored when using filename extension matching.
- ``magic``
   is the byte sequence binfmt_misc is matching for. The magic string
   may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you
   must escape any NUL bytes; parsing halts at the first one. In a shell
   environment you might have to write ``\\x0a`` to prevent the shell from
   eating your ``\``.
L
Linus Torvalds 已提交
40
   If you chose filename extension matching, this is the extension to be
41 42 43 44
   recognised (without the ``.``, the ``\x0a`` specials are not allowed).
   Extension    matching is case sensitive, and slashes ``/`` are not allowed!
- ``mask``
   is an (optional, defaults to all 0xff) mask. You can mask out some
L
Linus Torvalds 已提交
45
   bits from matching by supplying a string like magic and as long as magic.
46 47 48
   The mask is anded with the byte sequence of the file. Note that you must
   escape any NUL bytes; parsing halts at the first one. Ignored when using
   filename extension matching.
49 50
- ``interpreter``
   is the program that should be invoked with the binary as first
L
Linus Torvalds 已提交
51
   argument (specify the full path)
52 53
- ``flags``
   is an optional field that controls several aspects of the invocation
54
   of the interpreter. It is a string of capital letters, each controls a
55 56 57 58
   certain aspect. The following flags are supported:

      ``P`` - preserve-argv[0]
            Legacy behavior of binfmt_misc is to overwrite
59 60
            the original argv[0] with the full path to the binary. When this
            flag is included, binfmt_misc will add an argument to the argument
61 62 63 64 65 66 67 68
            vector for this purpose, thus preserving the original ``argv[0]``.
            e.g. If your interp is set to ``/bin/foo`` and you run ``blah``
            (which is in ``/usr/local/bin``), then the kernel will execute
            ``/bin/foo`` with ``argv[]`` set to ``["/bin/foo", "/usr/local/bin/blah", "blah"]``.  The interp has to be aware of this so it can
            execute ``/usr/local/bin/blah``
            with ``argv[]`` set to ``["blah"]``.
      ``O`` - open-binary
	    Legacy behavior of binfmt_misc is to pass the full path
L
Linus Torvalds 已提交
69 70 71
            of the binary to the interpreter as an argument. When this flag is
            included, binfmt_misc will open the file for reading and pass its
            descriptor as an argument, instead of the full path, thus allowing
72 73 74
            the interpreter to execute non-readable binaries. This feature
            should be used with care - the interpreter has to be trusted not to
            emit the contents of the non-readable binary.
75 76
      ``C`` - credentials
            Currently, the behavior of binfmt_misc is to calculate
L
Linus Torvalds 已提交
77 78
            the credentials and security token of the new process according to
            the interpreter. When this flag is included, these attributes are
79
            calculated according to the binary. It also implies the ``O`` flag.
L
Linus Torvalds 已提交
80 81 82
            This feature should be used with care as the interpreter
            will run with root permissions when a setuid binary owned by root
            is run with binfmt_misc.
83 84 85 86 87
      ``F`` - fix binary
            The usual behaviour of binfmt_misc is to spawn the
	    binary lazily when the misc format file is invoked.  However,
	    this doesn``t work very well in the face of mount namespaces and
	    changeroots, so the ``F`` mode opens the binary as soon as the
88 89 90
	    emulation is installed and uses the opened image to spawn the
	    emulator, meaning it is always available once installed,
	    regardless of how the environment changes.
L
Linus Torvalds 已提交
91 92 93


There are some restrictions:
94

95
 - the whole register string may not exceed 1920 characters
L
Linus Torvalds 已提交
96 97 98 99 100
 - 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 with
101 102 103
``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
a line ``none  /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
``/etc/fstab`` so it auto mounts on boot.
L
Linus Torvalds 已提交
104

105
You may want to add the binary formats in one of your ``/etc/rc`` scripts during
L
Linus Torvalds 已提交
106 107 108 109 110 111
boot-up. Read the manual of your init program to figure out how to do this
right.

Think about the order of adding entries! Later added entries are matched first!


112 113 114 115 116 117 118 119
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:' > register
    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

- enable support for packed DOS applications (pre-configured dosemu hdimages)::
L
Linus Torvalds 已提交
120

121
    echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
L
Linus Torvalds 已提交
122

123
- enable support for Windows executables using wine::
L
Linus Torvalds 已提交
124

125
    echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
L
Linus Torvalds 已提交
126 127 128 129 130

For java support see Documentation/java.txt


You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
131 132 133
or 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or
``/proc/.../the_name``.
Catting the file tells you the current status of ``binfmt_misc/the_entry``.
L
Linus Torvalds 已提交
134

135 136
You can remove one entry or all entries by echoing -1 to ``/proc/.../the_name``
or ``/proc/sys/fs/binfmt_misc/status``.
L
Linus Torvalds 已提交
137 138


139 140
Hints
-----
L
Linus Torvalds 已提交
141 142 143 144 145 146

If you want to pass special arguments to your interpreter, you can
write a wrapper script for it. See Documentation/java.txt for an
example.

Your interpreter should NOT look in the PATH for the filename; the kernel
147
passes it the full filename (or the file descriptor) to use.  Using ``$PATH`` can
L
Linus Torvalds 已提交
148 149 150
cause unexpected behaviour and can be a security hazard.


151
Richard Günther <rguenth@tat.physik.uni-tuebingen.de>