summaryrefslogtreecommitdiffstats
path: root/fs/binfmt_java.c
blob: 41e66a21a28576b4b1da713a68bb23985b93d91a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  linux/fs/binfmt_java.c
 *
 *  Copyright (C) 1996  Brian A. Lantz
 *  derived from binfmt_script.c
 *
 *  Simplified and modified to support binary java interpreters
 *  by Tom May <ftom@netcom.com>.
 */

#include <linux/module.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/malloc.h>
#include <linux/binfmts.h>
#include <linux/init.h>

#define _PATH_JAVA	"/usr/bin/java"
#define _PATH_APPLET	"/usr/bin/appletviewer"

/*  These paths can be modified with sysctl().  */

char binfmt_java_interpreter[65] = _PATH_JAVA;
char binfmt_java_appletviewer[65] = _PATH_APPLET;

static int do_load_java(struct linux_binprm *bprm,struct pt_regs *regs)
{
	char *i_name;
	int len;
	int retval;
	struct dentry * dentry;
	unsigned char *ucp = (unsigned char *) bprm->buf;

	if ((ucp[0] != 0xca) || (ucp[1] != 0xfe) || (ucp[2] != 0xba) || (ucp[3] != 0xbe)) 
		return -ENOEXEC;

	/*
	 * Fail if we're called recursively, e.g., the Java interpreter
	 * is a java binary.
	 */

	if (bprm->java)
		return -ENOEXEC;

	bprm->java = 1;

	dput(bprm->dentry);
	bprm->dentry = NULL;

	/*
	 * Set args: [0] the name of the java interpreter
	 *           [1] name of java class to execute, which is the
	 *		 filename without the path and without trailing
	 *		 ".class".  Note that the interpreter will use
	 *		 its own way to found the class file (typically using
	 *		 environment variable CLASSPATH), and may in fact
	 *		 execute a different file from the one we want.
	 *
	 * This is done in reverse order, because of how the
	 * user environment and arguments are stored.
	 */
	remove_arg_zero(bprm);
	len = strlen (bprm->filename);
	if (len >= 6 && !strcmp (bprm->filename + len - 6, ".class"))
		bprm->filename[len - 6] = 0;
	if ((i_name = strrchr (bprm->filename, '/')) != NULL)
		i_name++;
	else
		i_name = bprm->filename;
	bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
	bprm->argc++;

	i_name = binfmt_java_interpreter;
	bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
	bprm->argc++;

	if (!bprm->p) 
		return -E2BIG;
	/*
	 * OK, now restart the process with the interpreter's dentry.
	 */
	bprm->filename = binfmt_java_interpreter;
	dentry = open_namei(binfmt_java_interpreter, 0, 0);
	retval = PTR_ERR(dentry);
	if (IS_ERR(dentry))
		return retval;

	bprm->dentry = dentry;
	retval = prepare_binprm(bprm);
	if (retval < 0)
		return retval;

	return search_binary_handler(bprm,regs);
}

static int do_load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
{
	char *i_name;
	struct dentry * dentry;
	int retval;

	if (strncmp (bprm->buf, "<!--applet", 10))
		return -ENOEXEC;

	dput(bprm->dentry);
	bprm->dentry = NULL;

	/*
	 * Set args: [0] the name of the appletviewer
	 *           [1] filename of html file
	 *
	 * This is done in reverse order, because of how the
	 * user environment and arguments are stored.
	 */
	remove_arg_zero(bprm);
	i_name = bprm->filename;
	bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
	bprm->argc++;

	i_name = binfmt_java_appletviewer;
	bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
	bprm->argc++;

	if (!bprm->p) 
		return -E2BIG;
	/*
	 * OK, now restart the process with the interpreter's dentry.
	 */
	bprm->filename = binfmt_java_appletviewer;
	dentry = open_namei(binfmt_java_appletviewer, 0, 0);
	retval = PTR_ERR(dentry);
	if (IS_ERR(dentry))
		return retval;

	bprm->dentry = dentry;
	retval = prepare_binprm(bprm);
	if (retval < 0)
		return retval;

	return search_binary_handler(bprm,regs);
}

static int load_java(struct linux_binprm *bprm,struct pt_regs *regs)
{
	int retval;
	MOD_INC_USE_COUNT;
	retval = do_load_java(bprm,regs);
	MOD_DEC_USE_COUNT;
	return retval;
}

static struct linux_binfmt java_format = {
#ifndef MODULE
	NULL, 0, load_java, NULL, NULL
#else
	NULL, &__this_module, load_java, NULL, NULL
#endif
};

static int load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
{
	int retval;
	MOD_INC_USE_COUNT;
	retval = do_load_applet(bprm,regs);
	MOD_DEC_USE_COUNT;
	return retval;
}

static struct linux_binfmt applet_format = {
#ifndef MODULE
	NULL, 0, load_applet, NULL, NULL
#else
	NULL, &__this_module, load_applet, NULL, NULL
#endif
};

__initfunc(int init_java_binfmt(void))
{
	register_binfmt(&java_format);
	return register_binfmt(&applet_format);
}

#ifdef MODULE
int init_module(void)
{
	return init_java_binfmt();
}

void cleanup_module( void) {
	unregister_binfmt(&java_format);
	unregister_binfmt(&applet_format);
}
#endif