binfmt_em86.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * linux/fs/binfmt_em86.c
  3. *
  4. * Based on linux/fs/binfmt_script.c
  5. * Copyright (C) 1996 Martin von Löwis
  6. * original #!-checking implemented by tytso.
  7. *
  8. * em86 changes Copyright (C) 1997 Jim Paradis
  9. */
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/stat.h>
  13. #include <linux/binfmts.h>
  14. #include <linux/elf.h>
  15. #include <linux/init.h>
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/errno.h>
  19. #define EM86_INTERP "/usr/bin/em86"
  20. #define EM86_I_NAME "em86"
  21. static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
  22. {
  23. char *interp, *i_name, *i_arg;
  24. struct file * file;
  25. int retval;
  26. struct elfhdr elf_ex;
  27. /* Make sure this is a Linux/Intel ELF executable... */
  28. elf_ex = *((struct elfhdr *)bprm->buf);
  29. if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
  30. return -ENOEXEC;
  31. /* First of all, some simple consistency checks */
  32. if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
  33. (!((elf_ex.e_machine == EM_386) || (elf_ex.e_machine == EM_486))) ||
  34. (!bprm->file->f_op || !bprm->file->f_op->mmap)) {
  35. return -ENOEXEC;
  36. }
  37. bprm->recursion_depth++; /* Well, the bang-shell is implicit... */
  38. allow_write_access(bprm->file);
  39. fput(bprm->file);
  40. bprm->file = NULL;
  41. /* Unlike in the script case, we don't have to do any hairy
  42. * parsing to find our interpreter... it's hardcoded!
  43. */
  44. interp = EM86_INTERP;
  45. i_name = EM86_I_NAME;
  46. i_arg = NULL; /* We reserve the right to add an arg later */
  47. /*
  48. * Splice in (1) the interpreter's name for argv[0]
  49. * (2) (optional) argument to interpreter
  50. * (3) filename of emulated file (replace argv[0])
  51. *
  52. * This is done in reverse order, because of how the
  53. * user environment and arguments are stored.
  54. */
  55. remove_arg_zero(bprm);
  56. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  57. if (retval < 0) return retval;
  58. bprm->argc++;
  59. if (i_arg) {
  60. retval = copy_strings_kernel(1, &i_arg, bprm);
  61. if (retval < 0) return retval;
  62. bprm->argc++;
  63. }
  64. retval = copy_strings_kernel(1, &i_name, bprm);
  65. if (retval < 0) return retval;
  66. bprm->argc++;
  67. /*
  68. * OK, now restart the process with the interpreter's inode.
  69. * Note that we use open_exec() as the name is now in kernel
  70. * space, and we don't need to copy it.
  71. */
  72. file = open_exec(interp);
  73. if (IS_ERR(file))
  74. return PTR_ERR(file);
  75. bprm->file = file;
  76. retval = prepare_binprm(bprm);
  77. if (retval < 0)
  78. return retval;
  79. return search_binary_handler(bprm, regs);
  80. }
  81. static struct linux_binfmt em86_format = {
  82. .module = THIS_MODULE,
  83. .load_binary = load_em86,
  84. };
  85. static int __init init_em86_binfmt(void)
  86. {
  87. return register_binfmt(&em86_format);
  88. }
  89. static void __exit exit_em86_binfmt(void)
  90. {
  91. unregister_binfmt(&em86_format);
  92. }
  93. core_initcall(init_em86_binfmt);
  94. module_exit(exit_em86_binfmt);
  95. MODULE_LICENSE("GPL");