binfmt_em86.c 2.8 KB

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