xref: /illumos-gate/usr/src/uts/common/exec/java/java.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright 2019 Joyent, Inc.
25  */
26 
27 /*
28  * Launch Java executables via exec(2).
29  *
30  * Java executables are platform-independent executable files
31  * based on the JAR file format.  Executable JAR files contain a
32  * special 'extra field' header in the first file of the archive
33  * that marks the file as a true executable.   The data in that field
34  * is used to pass additional run-time information to the Java VM.
35  *
36  * This handler looks for the appropriate magic number on the
37  * front of the file, checks that the JAR file is executable, then
38  * invokes the Java runtime environment to do the rest of the work.
39  */
40 
41 #include <sys/types.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/exec.h>
45 #include <sys/modctl.h>
46 #include <sys/cmn_err.h>
47 #include <sys/pathname.h>
48 
49 /*
50  * These variables can be tweaked via /etc/system to allow prototyping
51  * and debugging.  See PSARC/1997/123.
52  *
53  * Modified by PSARC/1999/012 to be Contract Private between Solaris and
54  * the Java Technology Group.  It is expected that any future change to
55  * these variables be coordinated between the consolidations.
56  */
57 #if defined(__sparc)
58 char *jexec = "/usr/java/jre/lib/sparc/jexec";
59 #elif defined(__x86)
60 char *jexec = "/usr/java/jre/lib/i386/jexec";
61 #else
62 #error "Unknown ISA"
63 #endif
64 char *jexec_arg = "-jar";
65 
66 /*
67  * ZIP/JAR file header information
68  */
69 #define	SIGSIZ		4
70 #define	LOCSIG		"PK\003\004"
71 #define	LOCHDRSIZ	30
72 
73 #define	CH(b, n)	(((unsigned char *)(b))[n])
74 #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
75 #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
76 
77 #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
78 #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
79 
80 #define	XFHSIZ		4		/* header id, data size */
81 #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
82 #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
83 #define	XFJAVASIG	0xcafe		/* java executables */
84 
85 /*ARGSUSED3*/
86 static int
87 javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
88     struct intpdata *idatap, int level, size_t *execsz, int setid,
89     caddr_t execfile, cred_t *cred, int brand_action)
90 {
91 	struct intpdata idata;
92 	int error;
93 	ssize_t resid;
94 	vnode_t *nvp;
95 	off_t xoff, xoff_end;
96 	char lochdr[LOCHDRSIZ];
97 	struct pathname lookpn;
98 	struct pathname resolvepn;
99 	char *opath;
100 
101 	if (level)
102 		return (ENOEXEC);	/* no recursion */
103 
104 	/*
105 	 * Read in the full local file header, and validate
106 	 * the initial signature.
107 	 */
108 	if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
109 	    0, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
110 		return (error);
111 	if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
112 		return (ENOEXEC);
113 
114 	/*
115 	 * Ok, so this -is- a ZIP file, and might even be a JAR file.
116 	 * Is it a Java executable?
117 	 */
118 	xoff = sizeof (lochdr) + LOCNAM(lochdr);
119 	xoff_end = xoff + LOCEXT(lochdr);
120 
121 	while (xoff < xoff_end) {
122 		char xfhdr[XFHSIZ];
123 
124 		if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
125 		    xoff, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
126 			return (error);
127 		if (resid != 0)
128 			return (ENOEXEC);
129 		if (XFHID(xfhdr) == XFJAVASIG)
130 			break;
131 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
132 	}
133 
134 	if (xoff >= xoff_end)
135 		return (ENOEXEC);
136 
137 	/*
138 	 * Note: If we ever make setid execution work, we need to ensure
139 	 * that we use /dev/fd to avoid the classic setuid shell script
140 	 * security hole.
141 	 */
142 	if (setid)
143 		return (EACCES);
144 
145 	/*
146 	 * Find and invoke the Java runtime environment on the file
147 	 */
148 	bzero(&idata, sizeof (intpdata_t));
149 	idata.intp = NULL;
150 	idata.intp_name[0] = jexec;
151 	idata.intp_arg[0] = jexec_arg;
152 	if (error = pn_get(idata.intp_name[0], 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 
167 	if (!error) {
168 		/*
169 		 * Close this Java executable as the interpreter
170 		 * will open and close it later on.
171 		 */
172 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
173 	}
174 
175 	VN_RELE(nvp);
176 	args->pathname = opath;
177 	pn_free(&resolvepn);
178 	return (error);
179 }
180 
181 static struct execsw jexecsw = {
182 	javamagicstr,
183 	0,
184 	4,
185 	javaexec,
186 	NULL
187 };
188 
189 static struct modlexec jmodlexec = {
190 	&mod_execops, "exec for Java", &jexecsw
191 };
192 
193 static struct modlinkage jmodlinkage = {
194 	MODREV_1, &jmodlexec, NULL
195 };
196 
197 int
198 _init(void)
199 {
200 	return (mod_install(&jmodlinkage));
201 }
202 
203 int
204 _fini(void)
205 {
206 	return (mod_remove(&jmodlinkage));
207 }
208 
209 int
210 _info(struct modinfo *modinfop)
211 {
212 	return (mod_info(&jmodlinkage, modinfop));
213 }
214