18c2ecf20Sopenharmony_ciKernel Support for miscellaneous Binary Formats (binfmt_misc) 28c2ecf20Sopenharmony_ci============================================================= 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ciThis Kernel feature allows you to invoke almost (for restrictions see below) 58c2ecf20Sopenharmony_cievery program by simply typing its name in the shell. 68c2ecf20Sopenharmony_ciThis includes for example compiled Java(TM), Python or Emacs programs. 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ciTo achieve this you must tell binfmt_misc which interpreter has to be invoked 98c2ecf20Sopenharmony_ciwith which binary. Binfmt_misc recognises the binary-type by matching some bytes 108c2ecf20Sopenharmony_ciat the beginning of the file with a magic byte sequence (masking out specified 118c2ecf20Sopenharmony_cibits) you have supplied. Binfmt_misc can also recognise a filename extension 128c2ecf20Sopenharmony_ciaka ``.com`` or ``.exe``. 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ciFirst you must mount binfmt_misc:: 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ciTo actually register a new binary type, you have to set up a string looking like 198c2ecf20Sopenharmony_ci``:name:type:offset:magic:mask:interpreter:flags`` (where you can choose the 208c2ecf20Sopenharmony_ci``:`` upon your needs) and echo it to ``/proc/sys/fs/binfmt_misc/register``. 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciHere is what the fields mean: 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci- ``name`` 258c2ecf20Sopenharmony_ci is an identifier string. A new /proc file will be created with this 268c2ecf20Sopenharmony_ci ``name below /proc/sys/fs/binfmt_misc``; cannot contain slashes ``/`` for 278c2ecf20Sopenharmony_ci obvious reasons. 288c2ecf20Sopenharmony_ci- ``type`` 298c2ecf20Sopenharmony_ci is the type of recognition. Give ``M`` for magic and ``E`` for extension. 308c2ecf20Sopenharmony_ci- ``offset`` 318c2ecf20Sopenharmony_ci is the offset of the magic/mask in the file, counted in bytes. This 328c2ecf20Sopenharmony_ci defaults to 0 if you omit it (i.e. you write ``:name:type::magic...``). 338c2ecf20Sopenharmony_ci Ignored when using filename extension matching. 348c2ecf20Sopenharmony_ci- ``magic`` 358c2ecf20Sopenharmony_ci is the byte sequence binfmt_misc is matching for. The magic string 368c2ecf20Sopenharmony_ci may contain hex-encoded characters like ``\x0a`` or ``\xA4``. Note that you 378c2ecf20Sopenharmony_ci must escape any NUL bytes; parsing halts at the first one. In a shell 388c2ecf20Sopenharmony_ci environment you might have to write ``\\x0a`` to prevent the shell from 398c2ecf20Sopenharmony_ci eating your ``\``. 408c2ecf20Sopenharmony_ci If you chose filename extension matching, this is the extension to be 418c2ecf20Sopenharmony_ci recognised (without the ``.``, the ``\x0a`` specials are not allowed). 428c2ecf20Sopenharmony_ci Extension matching is case sensitive, and slashes ``/`` are not allowed! 438c2ecf20Sopenharmony_ci- ``mask`` 448c2ecf20Sopenharmony_ci is an (optional, defaults to all 0xff) mask. You can mask out some 458c2ecf20Sopenharmony_ci bits from matching by supplying a string like magic and as long as magic. 468c2ecf20Sopenharmony_ci The mask is anded with the byte sequence of the file. Note that you must 478c2ecf20Sopenharmony_ci escape any NUL bytes; parsing halts at the first one. Ignored when using 488c2ecf20Sopenharmony_ci filename extension matching. 498c2ecf20Sopenharmony_ci- ``interpreter`` 508c2ecf20Sopenharmony_ci is the program that should be invoked with the binary as first 518c2ecf20Sopenharmony_ci argument (specify the full path) 528c2ecf20Sopenharmony_ci- ``flags`` 538c2ecf20Sopenharmony_ci is an optional field that controls several aspects of the invocation 548c2ecf20Sopenharmony_ci of the interpreter. It is a string of capital letters, each controls a 558c2ecf20Sopenharmony_ci certain aspect. The following flags are supported: 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci ``P`` - preserve-argv[0] 588c2ecf20Sopenharmony_ci Legacy behavior of binfmt_misc is to overwrite 598c2ecf20Sopenharmony_ci the original argv[0] with the full path to the binary. When this 608c2ecf20Sopenharmony_ci flag is included, binfmt_misc will add an argument to the argument 618c2ecf20Sopenharmony_ci vector for this purpose, thus preserving the original ``argv[0]``. 628c2ecf20Sopenharmony_ci e.g. If your interp is set to ``/bin/foo`` and you run ``blah`` 638c2ecf20Sopenharmony_ci (which is in ``/usr/local/bin``), then the kernel will execute 648c2ecf20Sopenharmony_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 658c2ecf20Sopenharmony_ci execute ``/usr/local/bin/blah`` 668c2ecf20Sopenharmony_ci with ``argv[]`` set to ``["blah"]``. 678c2ecf20Sopenharmony_ci ``O`` - open-binary 688c2ecf20Sopenharmony_ci Legacy behavior of binfmt_misc is to pass the full path 698c2ecf20Sopenharmony_ci of the binary to the interpreter as an argument. When this flag is 708c2ecf20Sopenharmony_ci included, binfmt_misc will open the file for reading and pass its 718c2ecf20Sopenharmony_ci descriptor as an argument, instead of the full path, thus allowing 728c2ecf20Sopenharmony_ci the interpreter to execute non-readable binaries. This feature 738c2ecf20Sopenharmony_ci should be used with care - the interpreter has to be trusted not to 748c2ecf20Sopenharmony_ci emit the contents of the non-readable binary. 758c2ecf20Sopenharmony_ci ``C`` - credentials 768c2ecf20Sopenharmony_ci Currently, the behavior of binfmt_misc is to calculate 778c2ecf20Sopenharmony_ci the credentials and security token of the new process according to 788c2ecf20Sopenharmony_ci the interpreter. When this flag is included, these attributes are 798c2ecf20Sopenharmony_ci calculated according to the binary. It also implies the ``O`` flag. 808c2ecf20Sopenharmony_ci This feature should be used with care as the interpreter 818c2ecf20Sopenharmony_ci will run with root permissions when a setuid binary owned by root 828c2ecf20Sopenharmony_ci is run with binfmt_misc. 838c2ecf20Sopenharmony_ci ``F`` - fix binary 848c2ecf20Sopenharmony_ci The usual behaviour of binfmt_misc is to spawn the 858c2ecf20Sopenharmony_ci binary lazily when the misc format file is invoked. However, 868c2ecf20Sopenharmony_ci this doesn``t work very well in the face of mount namespaces and 878c2ecf20Sopenharmony_ci changeroots, so the ``F`` mode opens the binary as soon as the 888c2ecf20Sopenharmony_ci emulation is installed and uses the opened image to spawn the 898c2ecf20Sopenharmony_ci emulator, meaning it is always available once installed, 908c2ecf20Sopenharmony_ci regardless of how the environment changes. 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciThere are some restrictions: 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci - the whole register string may not exceed 1920 characters 968c2ecf20Sopenharmony_ci - the magic must reside in the first 128 bytes of the file, i.e. 978c2ecf20Sopenharmony_ci offset+size(magic) has to be less than 128 988c2ecf20Sopenharmony_ci - the interpreter string may not exceed 127 characters 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ciTo use binfmt_misc you have to mount it first. You can mount it with 1018c2ecf20Sopenharmony_ci``mount -t binfmt_misc none /proc/sys/fs/binfmt_misc`` command, or you can add 1028c2ecf20Sopenharmony_cia line ``none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0`` to your 1038c2ecf20Sopenharmony_ci``/etc/fstab`` so it auto mounts on boot. 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ciYou may want to add the binary formats in one of your ``/etc/rc`` scripts during 1068c2ecf20Sopenharmony_ciboot-up. Read the manual of your init program to figure out how to do this 1078c2ecf20Sopenharmony_ciright. 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ciThink about the order of adding entries! Later added entries are matched first! 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ciA few examples (assumed you are in ``/proc/sys/fs/binfmt_misc``): 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci- enable support for em86 (like binfmt_em86, for Alpha AXP only):: 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_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 1178c2ecf20Sopenharmony_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 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci- enable support for packed DOS applications (pre-configured dosemu hdimages):: 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci- enable support for Windows executables using wine:: 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ciFor java support see Documentation/admin-guide/java.rst 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciYou can enable/disable binfmt_misc or one binary type by echoing 0 (to disable) 1318c2ecf20Sopenharmony_cior 1 (to enable) to ``/proc/sys/fs/binfmt_misc/status`` or 1328c2ecf20Sopenharmony_ci``/proc/.../the_name``. 1338c2ecf20Sopenharmony_ciCatting the file tells you the current status of ``binfmt_misc/the_entry``. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciYou can remove one entry or all entries by echoing -1 to ``/proc/.../the_name`` 1368c2ecf20Sopenharmony_cior ``/proc/sys/fs/binfmt_misc/status``. 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ciHints 1408c2ecf20Sopenharmony_ci----- 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciIf you want to pass special arguments to your interpreter, you can 1438c2ecf20Sopenharmony_ciwrite a wrapper script for it. 1448c2ecf20Sopenharmony_ciSee :doc:`Documentation/admin-guide/java.rst <./java>` for an example. 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ciYour interpreter should NOT look in the PATH for the filename; the kernel 1478c2ecf20Sopenharmony_cipasses it the full filename (or the file descriptor) to use. Using ``$PATH`` can 1488c2ecf20Sopenharmony_cicause unexpected behaviour and can be a security hazard. 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ciRichard Günther <rguenth@tat.physik.uni-tuebingen.de> 152