blob: 06b9b9fddf7083720b03b117fcbf142461c6940e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/binfmt_em86.c
4 *
5 * Based on linux/fs/binfmt_script.c
Jan Engelhardt96de0e22007-10-19 23:21:04 +02006 * Copyright (C) 1996 Martin von Löwis
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * original #!-checking implemented by tytso.
8 *
9 * em86 changes Copyright (C) 1997 Jim Paradis
10 */
11
12#include <linux/module.h>
13#include <linux/string.h>
14#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/binfmts.h>
16#include <linux/elf.h>
17#include <linux/init.h>
18#include <linux/fs.h>
19#include <linux/file.h>
20#include <linux/errno.h>
21
22
23#define EM86_INTERP "/usr/bin/em86"
24#define EM86_I_NAME "em86"
25
Al Viro71613c32012-10-20 22:00:48 -040026static int load_em86(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Daniel Wagnera310dcb2016-08-02 14:04:57 -070028 const char *i_name, *i_arg;
29 char *interp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct file * file;
31 int retval;
32 struct elfhdr elf_ex;
33
34 /* Make sure this is a Linux/Intel ELF executable... */
35 elf_ex = *((struct elfhdr *)bprm->buf);
36
37 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
38 return -ENOEXEC;
39
40 /* First of all, some simple consistency checks */
41 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
42 (!((elf_ex.e_machine == EM_386) || (elf_ex.e_machine == EM_486))) ||
Al Viro72c2d532013-09-22 16:27:52 -040043 !bprm->file->f_op->mmap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return -ENOEXEC;
45 }
46
David Drysdale51f39a12014-12-12 16:57:29 -080047 /* Need to be able to load the file after exec */
48 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
49 return -ENOENT;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 /* Unlike in the script case, we don't have to do any hairy
52 * parsing to find our interpreter... it's hardcoded!
53 */
54 interp = EM86_INTERP;
55 i_name = EM86_I_NAME;
56 i_arg = NULL; /* We reserve the right to add an arg later */
57
58 /*
59 * Splice in (1) the interpreter's name for argv[0]
60 * (2) (optional) argument to interpreter
61 * (3) filename of emulated file (replace argv[0])
62 *
63 * This is done in reverse order, because of how the
64 * user environment and arguments are stored.
65 */
66 remove_arg_zero(bprm);
Christoph Hellwig986db2d2020-06-04 16:51:14 -070067 retval = copy_string_kernel(bprm->filename, bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (retval < 0) return retval;
69 bprm->argc++;
70 if (i_arg) {
Christoph Hellwig986db2d2020-06-04 16:51:14 -070071 retval = copy_string_kernel(i_arg, bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (retval < 0) return retval;
73 bprm->argc++;
74 }
Christoph Hellwig986db2d2020-06-04 16:51:14 -070075 retval = copy_string_kernel(i_name, bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (retval < 0) return retval;
77 bprm->argc++;
78
79 /*
80 * OK, now restart the process with the interpreter's inode.
81 * Note that we use open_exec() as the name is now in kernel
82 * space, and we don't need to copy it.
83 */
84 file = open_exec(interp);
85 if (IS_ERR(file))
86 return PTR_ERR(file);
87
Eric W. Biedermanbc2bf332020-05-18 18:43:20 -050088 bprm->interpreter = file;
89 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static struct linux_binfmt em86_format = {
93 .module = THIS_MODULE,
94 .load_binary = load_em86,
95};
96
97static int __init init_em86_binfmt(void)
98{
Al Viro8fc3dc52012-03-17 03:05:16 -040099 register_binfmt(&em86_format);
100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static void __exit exit_em86_binfmt(void)
104{
105 unregister_binfmt(&em86_format);
106}
107
108core_initcall(init_em86_binfmt);
109module_exit(exit_em86_binfmt);
110MODULE_LICENSE("GPL");