binfmt_em86.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 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->mmap) {
  35. return -ENOEXEC;
  36. }
  37. /* Need to be able to load the file after exec */
  38. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  39. return -ENOENT;
  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);
  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. register_binfmt(&em86_format);
  90. return 0;
  91. }
  92. static void __exit exit_em86_binfmt(void)
  93. {
  94. unregister_binfmt(&em86_format);
  95. }
  96. core_initcall(init_em86_binfmt);
  97. module_exit(exit_em86_binfmt);
  98. MODULE_LICENSE("GPL");