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