binfmt_em86.c 2.8 KB

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