binfmt_misc.txt 6.5 KB

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