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 
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 
21 
22 #define EM86_INTERP	"/usr/bin/em86"
23 #define EM86_I_NAME	"em86"
24 
load_em86(struct linux_binprm * bprm,struct pt_regs * regs)25 static int load_em86(struct linux_binprm *bprm,struct pt_regs *regs)
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))) ||
41 		(!bprm->file->f_op || !bprm->file->f_op->mmap)) {
42 			return -ENOEXEC;
43 	}
44 
45 	allow_write_access(bprm->file);
46 	fput(bprm->file);
47 	bprm->file = NULL;
48 
49 	/* Unlike in the script case, we don't have to do any hairy
50 	 * parsing to find our interpreter... it's hardcoded!
51 	 */
52 	interp = EM86_INTERP;
53 	i_name = EM86_I_NAME;
54 	i_arg = NULL;		/* We reserve the right to add an arg later */
55 
56 	/*
57 	 * Splice in (1) the interpreter's name for argv[0]
58 	 *           (2) (optional) argument to interpreter
59 	 *           (3) filename of emulated file (replace argv[0])
60 	 *
61 	 * This is done in reverse order, because of how the
62 	 * user environment and arguments are stored.
63 	 */
64 	remove_arg_zero(bprm);
65 	retval = copy_strings_kernel(1, &bprm->filename, bprm);
66 	if (retval < 0) return retval;
67 	bprm->argc++;
68 	if (i_arg) {
69 		retval = copy_strings_kernel(1, &i_arg, bprm);
70 		if (retval < 0) return retval;
71 		bprm->argc++;
72 	}
73 	retval = copy_strings_kernel(1, &i_name, bprm);
74 	if (retval < 0)	return retval;
75 	bprm->argc++;
76 
77 	/*
78 	 * OK, now restart the process with the interpreter's inode.
79 	 * Note that we use open_exec() as the name is now in kernel
80 	 * space, and we don't need to copy it.
81 	 */
82 	file = open_exec(interp);
83 	if (IS_ERR(file))
84 		return PTR_ERR(file);
85 
86 	bprm->file = file;
87 
88 	retval = prepare_binprm(bprm);
89 	if (retval < 0)
90 		return retval;
91 
92 	return search_binary_handler(bprm, regs);
93 }
94 
95 static struct linux_binfmt em86_format = {
96 	.module		= THIS_MODULE,
97 	.load_binary	= load_em86,
98 };
99 
init_em86_binfmt(void)100 static int __init init_em86_binfmt(void)
101 {
102 	register_binfmt(&em86_format);
103 	return 0;
104 }
105 
exit_em86_binfmt(void)106 static void __exit exit_em86_binfmt(void)
107 {
108 	unregister_binfmt(&em86_format);
109 }
110 
111 core_initcall(init_em86_binfmt);
112 module_exit(exit_em86_binfmt);
113 MODULE_LICENSE("GPL");
114