binfmt_em86.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/binfmt_em86.c
  4. *
  5. * Based on linux/fs/binfmt_script.c
  6. * Copyright (C) 1996 Martin von Löwis
  7. * original #!-checking implemented by tytso.
  8. *
  9. * em86 changes Copyright (C) 1997 Jim Paradis
  10. */
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/stat.h>
  14. #include <linux/binfmts.h>
  15. #include <linux/elf.h>
  16. #include <linux/init.h>
  17. #include <linux/fs.h>
  18. #include <linux/file.h>
  19. #include <linux/errno.h>
  20. #define EM86_INTERP "/usr/bin/em86"
  21. #define EM86_I_NAME "em86"
  22. static int load_em86(struct linux_binprm *bprm)
  23. {
  24. const char *i_name, *i_arg;
  25. char *interp;
  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->mmap) {
  37. return -ENOEXEC;
  38. }
  39. /* Need to be able to load the file after exec */
  40. if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
  41. return -ENOENT;
  42. /* Unlike in the script case, we don't have to do any hairy
  43. * parsing to find our interpreter... it's hardcoded!
  44. */
  45. interp = EM86_INTERP;
  46. i_name = EM86_I_NAME;
  47. i_arg = NULL; /* We reserve the right to add an arg later */
  48. /*
  49. * Splice in (1) the interpreter's name for argv[0]
  50. * (2) (optional) argument to interpreter
  51. * (3) filename of emulated file (replace argv[0])
  52. *
  53. * This is done in reverse order, because of how the
  54. * user environment and arguments are stored.
  55. */
  56. remove_arg_zero(bprm);
  57. retval = copy_string_kernel(bprm->filename, bprm);
  58. if (retval < 0) return retval;
  59. bprm->argc++;
  60. if (i_arg) {
  61. retval = copy_string_kernel(i_arg, bprm);
  62. if (retval < 0) return retval;
  63. bprm->argc++;
  64. }
  65. retval = copy_string_kernel(i_name, bprm);
  66. if (retval < 0) return retval;
  67. bprm->argc++;
  68. /*
  69. * OK, now restart the process with the interpreter's inode.
  70. * Note that we use open_exec() as the name is now in kernel
  71. * space, and we don't need to copy it.
  72. */
  73. file = open_exec(interp);
  74. if (IS_ERR(file))
  75. return PTR_ERR(file);
  76. bprm->interpreter = file;
  77. return 0;
  78. }
  79. static struct linux_binfmt em86_format = {
  80. .module = THIS_MODULE,
  81. .load_binary = load_em86,
  82. };
  83. static int __init init_em86_binfmt(void)
  84. {
  85. register_binfmt(&em86_format);
  86. return 0;
  87. }
  88. static void __exit exit_em86_binfmt(void)
  89. {
  90. unregister_binfmt(&em86_format);
  91. }
  92. core_initcall(init_em86_binfmt);
  93. module_exit(exit_em86_binfmt);
  94. MODULE_LICENSE("GPL");