xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/binfmt-misc.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunKernel Support for miscellaneous Binary Formats (binfmt_misc)
2*4882a593Smuzhiyun=============================================================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunThis Kernel feature allows you to invoke almost (for restrictions see below)
5*4882a593Smuzhiyunevery program by simply typing its name in the shell.
6*4882a593SmuzhiyunThis includes for example compiled Java(TM), Python or Emacs programs.
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunTo achieve this you must tell binfmt_misc which interpreter has to be invoked
9*4882a593Smuzhiyunwith which binary. Binfmt_misc recognises the binary-type by matching some bytes
10*4882a593Smuzhiyunat the beginning of the file with a magic byte sequence (masking out specified
11*4882a593Smuzhiyunbits) you have supplied. Binfmt_misc can also recognise a filename extension
12*4882a593Smuzhiyunaka ``.com`` or ``.exe``.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunFirst you must mount binfmt_misc::
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun	mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunTo actually register a new binary type, you have to set up a string looking like
19*4882a593Smuzhiyun``:name:type:offset:magic:mask:interpreter:flags`` (where you can choose the
20*4882a593Smuzhiyun``:`` upon your needs) and echo it to ``/proc/sys/fs/binfmt_misc/register``.
21*4882a593Smuzhiyun
22*4882a593SmuzhiyunHere is what the fields mean:
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun- ``name``
25*4882a593Smuzhiyun   is an identifier string. A new /proc file will be created with this
26*4882a593Smuzhiyun   ``name below /proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for
27*4882a593Smuzhiyun   obvious reasons.
28*4882a593Smuzhiyun- ``type``
29*4882a593Smuzhiyun   is the type of recognition. Give ``M`` for magic and ``E`` for extension.
30*4882a593Smuzhiyun- ``offset``
31*4882a593Smuzhiyun   is the offset of the magic/mask in the file, counted in bytes. This
32*4882a593Smuzhiyun   defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``).
33*4882a593Smuzhiyun   Ignored when using filename extension matching.
34*4882a593Smuzhiyun- ``magic``
35*4882a593Smuzhiyun   is the byte sequence binfmt_misc is matching for. The magic string
36*4882a593Smuzhiyun   may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you
37*4882a593Smuzhiyun   must escape any NUL bytes; parsing halts at the first one. In a shell
38*4882a593Smuzhiyun   environment you might have to write ``\\x0a`` to prevent the shell from
39*4882a593Smuzhiyun   eating your ``\``.
40*4882a593Smuzhiyun   If you chose filename extension matching, this is the extension to be
41*4882a593Smuzhiyun   recognised (without the ``.``, the ``\x0a`` specials are not allowed).
42*4882a593Smuzhiyun   Extension    matching is case sensitive, and slashes ``/`` are not allowed!
43*4882a593Smuzhiyun- ``mask``
44*4882a593Smuzhiyun   is an (optional, defaults to all 0xff) mask. You can mask out some
45*4882a593Smuzhiyun   bits from matching by supplying a string like magic and as long as magic.
46*4882a593Smuzhiyun   The mask is anded with the byte sequence of the file. Note that you must
47*4882a593Smuzhiyun   escape any NUL bytes; parsing halts at the first one. Ignored when using
48*4882a593Smuzhiyun   filename extension matching.
49*4882a593Smuzhiyun- ``interpreter``
50*4882a593Smuzhiyun   is the program that should be invoked with the binary as first
51*4882a593Smuzhiyun   argument (specify the full path)
52*4882a593Smuzhiyun- ``flags``
53*4882a593Smuzhiyun   is an optional field that controls several aspects of the invocation
54*4882a593Smuzhiyun   of the interpreter. It is a string of capital letters, each controls a
55*4882a593Smuzhiyun   certain aspect. The following flags are supported:
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun      ``P`` - preserve-argv[0]
58*4882a593Smuzhiyun            Legacy behavior of binfmt_misc is to overwrite
59*4882a593Smuzhiyun            the original argv[0] with the full path to the binary. When this
60*4882a593Smuzhiyun            flag is included, binfmt_misc will add an argument to the argument
61*4882a593Smuzhiyun            vector for this purpose, thus preserving the original ``argv[0]``.
62*4882a593Smuzhiyun            e.g. If your interp is set to ``/bin/foo`` and you run ``blah``
63*4882a593Smuzhiyun            (which is in ``/usr/local/bin``), then the kernel will execute
64*4882a593Smuzhiyun            ``/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*4882a593Smuzhiyun            execute ``/usr/local/bin/blah``
66*4882a593Smuzhiyun            with ``argv[]`` set to ``["blah"]``.
67*4882a593Smuzhiyun      ``O`` - open-binary
68*4882a593Smuzhiyun	    Legacy behavior of binfmt_misc is to pass the full path
69*4882a593Smuzhiyun            of the binary to the interpreter as an argument. When this flag is
70*4882a593Smuzhiyun            included, binfmt_misc will open the file for reading and pass its
71*4882a593Smuzhiyun            descriptor as an argument, instead of the full path, thus allowing
72*4882a593Smuzhiyun            the interpreter to execute non-readable binaries. This feature
73*4882a593Smuzhiyun            should be used with care - the interpreter has to be trusted not to
74*4882a593Smuzhiyun            emit the contents of the non-readable binary.
75*4882a593Smuzhiyun      ``C`` - credentials
76*4882a593Smuzhiyun            Currently, the behavior of binfmt_misc is to calculate
77*4882a593Smuzhiyun            the credentials and security token of the new process according to
78*4882a593Smuzhiyun            the interpreter. When this flag is included, these attributes are
79*4882a593Smuzhiyun            calculated according to the binary. It also implies the ``O`` flag.
80*4882a593Smuzhiyun            This feature should be used with care as the interpreter
81*4882a593Smuzhiyun            will run with root permissions when a setuid binary owned by root
82*4882a593Smuzhiyun            is run with binfmt_misc.
83*4882a593Smuzhiyun      ``F`` - fix binary
84*4882a593Smuzhiyun            The usual behaviour of binfmt_misc is to spawn the
85*4882a593Smuzhiyun	    binary lazily when the misc format file is invoked.  However,
86*4882a593Smuzhiyun	    this doesn``t work very well in the face of mount namespaces and
87*4882a593Smuzhiyun	    changeroots, so the ``F`` mode opens the binary as soon as the
88*4882a593Smuzhiyun	    emulation is installed and uses the opened image to spawn the
89*4882a593Smuzhiyun	    emulator, meaning it is always available once installed,
90*4882a593Smuzhiyun	    regardless of how the environment changes.
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunThere are some restrictions:
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun - the whole register string may not exceed 1920 characters
96*4882a593Smuzhiyun - the magic must reside in the first 128 bytes of the file, i.e.
97*4882a593Smuzhiyun   offset+size(magic) has to be less than 128
98*4882a593Smuzhiyun - the interpreter string may not exceed 127 characters
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunTo use binfmt_misc you have to mount it first. You can mount it with
101*4882a593Smuzhiyun``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add
102*4882a593Smuzhiyuna line ``none  /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your
103*4882a593Smuzhiyun``/etc/fstab`` so it auto mounts on boot.
104*4882a593Smuzhiyun
105*4882a593SmuzhiyunYou may want to add the binary formats in one of your ``/etc/rc`` scripts during
106*4882a593Smuzhiyunboot-up. Read the manual of your init program to figure out how to do this
107*4882a593Smuzhiyunright.
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunThink about the order of adding entries! Later added entries are matched first!
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunA few examples (assumed you are in ``/proc/sys/fs/binfmt_misc``):
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun- enable support for em86 (like binfmt_em86, for Alpha AXP only)::
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun    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*4882a593Smuzhiyun    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
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun- enable support for packed DOS applications (pre-configured dosemu hdimages)::
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun    echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun- enable support for Windows executables using wine::
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun    echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
126*4882a593Smuzhiyun
127*4882a593SmuzhiyunFor java support see Documentation/admin-guide/java.rst
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunYou can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
131*4882a593Smuzhiyunor 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or
132*4882a593Smuzhiyun``/proc/.../the_name``.
133*4882a593SmuzhiyunCatting the file tells you the current status of ``binfmt_misc/the_entry``.
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunYou can remove one entry or all entries by echoing -1 to ``/proc/.../the_name``
136*4882a593Smuzhiyunor ``/proc/sys/fs/binfmt_misc/status``.
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunHints
140*4882a593Smuzhiyun-----
141*4882a593Smuzhiyun
142*4882a593SmuzhiyunIf you want to pass special arguments to your interpreter, you can
143*4882a593Smuzhiyunwrite a wrapper script for it.
144*4882a593SmuzhiyunSee :doc:`Documentation/admin-guide/java.rst <./java>` for an example.
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunYour interpreter should NOT look in the PATH for the filename; the kernel
147*4882a593Smuzhiyunpasses it the full filename (or the file descriptor) to use.  Using ``$PATH`` can
148*4882a593Smuzhiyuncause unexpected behaviour and can be a security hazard.
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunRichard Günther <rguenth@tat.physik.uni-tuebingen.de>
152