xref: /titanic_52/usr/src/uts/common/exec/java/java.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Launch Java executables via exec(2).
30  *
31  * Java executables are platform-independent executable files
32  * based on the JAR file format.  Executable JAR files contain a
33  * special 'extra field' header in the first file of the archive
34  * that marks the file as a true executable.   The data in that field
35  * is used to pass additional run-time information to the Java VM.
36  *
37  * This handler looks for the appropriate magic number on the
38  * front of the file, checks that the JAR file is executable, then
39  * invokes the Java runtime environment to do the rest of the work.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/exec.h>
46 #include <sys/modctl.h>
47 #include <sys/cmn_err.h>
48 #include <sys/pathname.h>
49 
50 /*
51  * These variables can be tweaked via /etc/system to allow prototyping
52  * and debugging.  See PSARC/1997/123.
53  *
54  * Modified by PSARC/1999/012 to be Contract Private between Solaris and
55  * the Java Technology Group.  It is expected that any future change to
56  * these variables be coordinated between the consolidations.
57  */
58 #if defined(__sparc)
59 char *jexec = "/usr/java/jre/lib/sparc/jexec";
60 #elif defined(__i386) || defined(__i386_COMPAT)
61 char *jexec = "/usr/java/jre/lib/i386/jexec";
62 #else
63 #error "Unknown ISA"
64 #endif
65 char *jexec_arg = "-jar";
66 
67 /*
68  * ZIP/JAR file header information
69  */
70 #define	SIGSIZ		4
71 #define	LOCSIG		"PK\003\004"
72 #define	LOCHDRSIZ	30
73 
74 #define	CH(b, n)	(((unsigned char *)(b))[n])
75 #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
76 #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
77 
78 #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
79 #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
80 
81 #define	XFHSIZ		4		/* header id, data size */
82 #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
83 #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
84 #define	XFJAVASIG	0xcafe		/* java executables */
85 
86 /*ARGSUSED3*/
87 static int
88 javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
89     struct intpdata *idatap, int level, long *execsz, int setid,
90     caddr_t execfile, cred_t *cred, int brand_action)
91 {
92 	struct intpdata idata;
93 	int error;
94 	ssize_t resid;
95 	vnode_t *nvp;
96 	off_t xoff, xoff_end;
97 	char lochdr[LOCHDRSIZ];
98 	struct pathname lookpn;
99 	struct pathname resolvepn;
100 	char *opath;
101 
102 	if (level)
103 		return (ENOEXEC);	/* no recursion */
104 
105 	/*
106 	 * Read in the full local file header, and validate
107 	 * the initial signature.
108 	 */
109 	if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
110 	    0, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
111 		return (error);
112 	if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
113 		return (ENOEXEC);
114 
115 	/*
116 	 * Ok, so this -is- a ZIP file, and might even be a JAR file.
117 	 * Is it a Java executable?
118 	 */
119 	xoff = sizeof (lochdr) + LOCNAM(lochdr);
120 	xoff_end = xoff + LOCEXT(lochdr);
121 
122 	while (xoff < xoff_end) {
123 		char xfhdr[XFHSIZ];
124 
125 		if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
126 		    xoff, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
127 			return (error);
128 		if (resid != 0)
129 			return (ENOEXEC);
130 		if (XFHID(xfhdr) == XFJAVASIG)
131 			break;
132 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
133 	}
134 
135 	if (xoff >= xoff_end)
136 		return (ENOEXEC);
137 
138 	/*
139 	 * Note: If we ever make setid execution work, we need to ensure
140 	 * that we use /dev/fd to avoid the classic setuid shell script
141 	 * security hole.
142 	 */
143 	if (setid)
144 		return (EACCES);
145 
146 	/*
147 	 * Find and invoke the Java runtime environment on the file
148 	 */
149 	idata.intp = NULL;
150 	idata.intp_name = jexec;
151 	idata.intp_arg = jexec_arg;
152 	if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &lookpn))
153 		return (error);
154 	pn_alloc(&resolvepn);
155 	if (error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
156 		pn_free(&resolvepn);
157 		pn_free(&lookpn);
158 		return (ENOEXEC);
159 	}
160 	opath = args->pathname;
161 	args->pathname = resolvepn.pn_path;
162 	/* don't free resolvepn until we are done with args */
163 	pn_free(&lookpn);
164 	error = gexec(&nvp, uap, args, &idata, level + 1, execsz, execfile,
165 	    cred, EBA_NONE);
166 	VN_RELE(nvp);
167 	args->pathname = opath;
168 	pn_free(&resolvepn);
169 	return (error);
170 }
171 
172 static struct execsw jexecsw = {
173 	javamagicstr,
174 	0,
175 	4,
176 	javaexec,
177 	NULL
178 };
179 
180 static struct modlexec jmodlexec = {
181 	&mod_execops, "exec for Java", &jexecsw
182 };
183 
184 static struct modlinkage jmodlinkage = {
185 	MODREV_1, &jmodlexec, NULL
186 };
187 
188 int
189 _init(void)
190 {
191 	return (mod_install(&jmodlinkage));
192 }
193 
194 int
195 _fini(void)
196 {
197 	return (mod_remove(&jmodlinkage));
198 }
199 
200 int
201 _info(struct modinfo *modinfop)
202 {
203 	return (mod_info(&jmodlinkage, modinfop));
204 }
205