Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/binfmt_em86.c |
| 3 | * |
| 4 | * Based on linux/fs/binfmt_script.c |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 5 | * Copyright (C) 1996 Martin von Löwis |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * original #!-checking implemented by tytso. |
| 7 | * |
| 8 | * em86 changes Copyright (C) 1997 Jim Paradis |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 21 | |
| 22 | #define EM86_INTERP "/usr/bin/em86" |
| 23 | #define EM86_I_NAME "em86" |
| 24 | |
Al Viro | 71613c3 | 2012-10-20 22:00:48 -0400 | [diff] [blame] | 25 | static int load_em86(struct linux_binprm *bprm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | char *interp, *i_name, *i_arg; |
| 28 | struct file * file; |
| 29 | int retval; |
| 30 | struct elfhdr elf_ex; |
| 31 | |
| 32 | /* Make sure this is a Linux/Intel ELF executable... */ |
| 33 | elf_ex = *((struct elfhdr *)bprm->buf); |
| 34 | |
| 35 | if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0) |
| 36 | return -ENOEXEC; |
| 37 | |
| 38 | /* First of all, some simple consistency checks */ |
| 39 | if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) || |
| 40 | (!((elf_ex.e_machine == EM_386) || (elf_ex.e_machine == EM_486))) || |
Al Viro | 72c2d53 | 2013-09-22 16:27:52 -0400 | [diff] [blame] | 41 | !bprm->file->f_op->mmap) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | return -ENOEXEC; |
| 43 | } |
| 44 | |
David Drysdale | 51f39a1 | 2014-12-12 16:57:29 -0800 | [diff] [blame] | 45 | /* Need to be able to load the file after exec */ |
| 46 | if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE) |
| 47 | return -ENOENT; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | allow_write_access(bprm->file); |
| 50 | fput(bprm->file); |
| 51 | bprm->file = NULL; |
| 52 | |
| 53 | /* Unlike in the script case, we don't have to do any hairy |
| 54 | * parsing to find our interpreter... it's hardcoded! |
| 55 | */ |
| 56 | interp = EM86_INTERP; |
| 57 | i_name = EM86_I_NAME; |
| 58 | i_arg = NULL; /* We reserve the right to add an arg later */ |
| 59 | |
| 60 | /* |
| 61 | * Splice in (1) the interpreter's name for argv[0] |
| 62 | * (2) (optional) argument to interpreter |
| 63 | * (3) filename of emulated file (replace argv[0]) |
| 64 | * |
| 65 | * This is done in reverse order, because of how the |
| 66 | * user environment and arguments are stored. |
| 67 | */ |
| 68 | remove_arg_zero(bprm); |
| 69 | retval = copy_strings_kernel(1, &bprm->filename, bprm); |
| 70 | if (retval < 0) return retval; |
| 71 | bprm->argc++; |
| 72 | if (i_arg) { |
| 73 | retval = copy_strings_kernel(1, &i_arg, bprm); |
| 74 | if (retval < 0) return retval; |
| 75 | bprm->argc++; |
| 76 | } |
| 77 | retval = copy_strings_kernel(1, &i_name, bprm); |
| 78 | if (retval < 0) return retval; |
| 79 | bprm->argc++; |
| 80 | |
| 81 | /* |
| 82 | * OK, now restart the process with the interpreter's inode. |
| 83 | * Note that we use open_exec() as the name is now in kernel |
| 84 | * space, and we don't need to copy it. |
| 85 | */ |
| 86 | file = open_exec(interp); |
| 87 | if (IS_ERR(file)) |
| 88 | return PTR_ERR(file); |
| 89 | |
| 90 | bprm->file = file; |
| 91 | |
| 92 | retval = prepare_binprm(bprm); |
| 93 | if (retval < 0) |
| 94 | return retval; |
| 95 | |
Al Viro | 3c456bf | 2012-10-20 21:53:31 -0400 | [diff] [blame] | 96 | return search_binary_handler(bprm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static struct linux_binfmt em86_format = { |
| 100 | .module = THIS_MODULE, |
| 101 | .load_binary = load_em86, |
| 102 | }; |
| 103 | |
| 104 | static int __init init_em86_binfmt(void) |
| 105 | { |
Al Viro | 8fc3dc5 | 2012-03-17 03:05:16 -0400 | [diff] [blame] | 106 | register_binfmt(&em86_format); |
| 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void __exit exit_em86_binfmt(void) |
| 111 | { |
| 112 | unregister_binfmt(&em86_format); |
| 113 | } |
| 114 | |
| 115 | core_initcall(init_em86_binfmt); |
| 116 | module_exit(exit_em86_binfmt); |
| 117 | MODULE_LICENSE("GPL"); |