binfmt_misc.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Kernel Support for miscellaneous (your favourite) Binary Formats v1.1
  2. =====================================================================
  3. This Kernel feature allows you to invoke almost (for restrictions see below)
  4. every program by simply typing its name in the shell.
  5. This includes for example compiled Java(TM), Python or Emacs programs.
  6. To achieve this you must tell binfmt_misc which interpreter has to be invoked
  7. with which binary. Binfmt_misc recognises the binary-type by matching some bytes
  8. at the beginning of the file with a magic byte sequence (masking out specified
  9. bits) you have supplied. Binfmt_misc can also recognise a filename extension
  10. aka '.com' or '.exe'.
  11. First you must mount binfmt_misc:
  12. mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
  13. To actually register a new binary type, you have to set up a string looking like
  14. :name:type:offset:magic:mask:interpreter:flags (where you can choose the ':' upon
  15. your needs) and echo it to /proc/sys/fs/binfmt_misc/register.
  16. Here is what the fields mean:
  17. - 'name' is an identifier string. A new /proc file will be created with this
  18. name below /proc/sys/fs/binfmt_misc
  19. - 'type' is the type of recognition. Give 'M' for magic and 'E' for extension.
  20. - 'offset' is the offset of the magic/mask in the file, counted in bytes. This
  21. defaults to 0 if you omit it (i.e. you write ':name:type::magic...')
  22. - 'magic' is the byte sequence binfmt_misc is matching for. The magic string
  23. may contain hex-encoded characters like \x0a or \xA4. In a shell environment
  24. you will have to write \\x0a to prevent the shell from eating your \.
  25. If you chose filename extension matching, this is the extension to be
  26. recognised (without the '.', the \x0a specials are not allowed). Extension
  27. matching is case sensitive!
  28. - 'mask' is an (optional, defaults to all 0xff) mask. You can mask out some
  29. bits from matching by supplying a string like magic and as long as magic.
  30. The mask is anded with the byte sequence of the file.
  31. - 'interpreter' is the program that should be invoked with the binary as first
  32. argument (specify the full path)
  33. - 'flags' is an optional field that controls several aspects of the invocation
  34. of the interpreter. It is a string of capital letters, each controls a certain
  35. aspect. The following flags are supported -
  36. 'P' - preserve-argv[0]. Legacy behavior of binfmt_misc is to overwrite the
  37. original argv[0] with the full path to the binary. When this flag is
  38. included, binfmt_misc will add an argument to the argument vector for
  39. this purpose, thus preserving the original argv[0].
  40. 'O' - open-binary. Legacy behavior of binfmt_misc is to pass the full path
  41. of the binary to the interpreter as an argument. When this flag is
  42. included, binfmt_misc will open the file for reading and pass its
  43. descriptor as an argument, instead of the full path, thus allowing
  44. the interpreter to execute non-readable binaries. This feature should
  45. be used with care - the interpreter has to be trusted not to emit
  46. the contents of the non-readable binary.
  47. 'C' - credentials. Currently, the behavior of binfmt_misc is to calculate
  48. the credentials and security token of the new process according to
  49. the interpreter. When this flag is included, these attributes are
  50. calculated according to the binary. It also implies the 'O' flag.
  51. This feature should be used with care as the interpreter
  52. will run with root permissions when a setuid binary owned by root
  53. is run with binfmt_misc.
  54. There are some restrictions:
  55. - the whole register string may not exceed 255 characters
  56. - the magic must reside in the first 128 bytes of the file, i.e.
  57. offset+size(magic) has to be less than 128
  58. - the interpreter string may not exceed 127 characters
  59. To use binfmt_misc you have to mount it first. You can mount it with
  60. "mount -t binfmt_misc none /proc/sys/fs/binfmt_misc" command, or you can add
  61. a line "none /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0" to your
  62. /etc/fstab so it auto mounts on boot.
  63. You may want to add the binary formats in one of your /etc/rc scripts during
  64. boot-up. Read the manual of your init program to figure out how to do this
  65. right.
  66. Think about the order of adding entries! Later added entries are matched first!
  67. A few examples (assumed you are in /proc/sys/fs/binfmt_misc):
  68. - enable support for em86 (like binfmt_em86, for Alpha AXP only):
  69. 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
  70. 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
  71. - enable support for packed DOS applications (pre-configured dosemu hdimages):
  72. echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register
  73. - enable support for Windows executables using wine:
  74. echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register
  75. For java support see Documentation/java.txt
  76. You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
  77. or 1 (to enable) to /proc/sys/fs/binfmt_misc/status or /proc/.../the_name.
  78. Catting the file tells you the current status of binfmt_misc/the entry.
  79. You can remove one entry or all entries by echoing -1 to /proc/.../the_name
  80. or /proc/sys/fs/binfmt_misc/status.
  81. HINTS:
  82. ======
  83. If you want to pass special arguments to your interpreter, you can
  84. write a wrapper script for it. See Documentation/java.txt for an
  85. example.
  86. Your interpreter should NOT look in the PATH for the filename; the kernel
  87. passes it the full filename (or the file descriptor) to use. Using $PATH can
  88. cause unexpected behaviour and can be a security hazard.
  89. There is a web page about binfmt_misc at
  90. http://www.tat.physik.uni-tuebingen.de
  91. Richard Günther <rguenth@tat.physik.uni-tuebingen.de>