xref: /titanic_51/usr/src/uts/common/exec/java/java.c (revision a4aeef46cda1835da2b19f8f62b4526de6521e6c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a4aeef46SDonghai Qiao  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Launch Java executables via exec(2).
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Java executables are platform-independent executable files
307c478bd9Sstevel@tonic-gate  * based on the JAR file format.  Executable JAR files contain a
317c478bd9Sstevel@tonic-gate  * special 'extra field' header in the first file of the archive
327c478bd9Sstevel@tonic-gate  * that marks the file as a true executable.   The data in that field
337c478bd9Sstevel@tonic-gate  * is used to pass additional run-time information to the Java VM.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * This handler looks for the appropriate magic number on the
367c478bd9Sstevel@tonic-gate  * front of the file, checks that the JAR file is executable, then
377c478bd9Sstevel@tonic-gate  * invokes the Java runtime environment to do the rest of the work.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/exec.h>
447c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * These variables can be tweaked via /etc/system to allow prototyping
507c478bd9Sstevel@tonic-gate  * and debugging.  See PSARC/1997/123.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Modified by PSARC/1999/012 to be Contract Private between Solaris and
537c478bd9Sstevel@tonic-gate  * the Java Technology Group.  It is expected that any future change to
547c478bd9Sstevel@tonic-gate  * these variables be coordinated between the consolidations.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #if defined(__sparc)
577c478bd9Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/sparc/jexec";
587c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__i386_COMPAT)
597c478bd9Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/i386/jexec";
607c478bd9Sstevel@tonic-gate #else
617c478bd9Sstevel@tonic-gate #error "Unknown ISA"
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate char *jexec_arg = "-jar";
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * ZIP/JAR file header information
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	SIGSIZ		4
697c478bd9Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
707c478bd9Sstevel@tonic-gate #define	LOCHDRSIZ	30
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
737c478bd9Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
747c478bd9Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
777c478bd9Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
807c478bd9Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
817c478bd9Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
827c478bd9Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
857c478bd9Sstevel@tonic-gate static int
867c478bd9Sstevel@tonic-gate javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
877c478bd9Sstevel@tonic-gate     struct intpdata *idatap, int level, long *execsz, int setid,
889acbbeafSnn35248     caddr_t execfile, cred_t *cred, int brand_action)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	struct intpdata idata;
917c478bd9Sstevel@tonic-gate 	int error;
927c478bd9Sstevel@tonic-gate 	ssize_t resid;
937c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
947c478bd9Sstevel@tonic-gate 	off_t xoff, xoff_end;
957c478bd9Sstevel@tonic-gate 	char lochdr[LOCHDRSIZ];
967c478bd9Sstevel@tonic-gate 	struct pathname lookpn;
977c478bd9Sstevel@tonic-gate 	struct pathname resolvepn;
987c478bd9Sstevel@tonic-gate 	char *opath;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (level)
1017c478bd9Sstevel@tonic-gate 		return (ENOEXEC);	/* no recursion */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Read in the full local file header, and validate
1057c478bd9Sstevel@tonic-gate 	 * the initial signature.
1067c478bd9Sstevel@tonic-gate 	 */
1077c478bd9Sstevel@tonic-gate 	if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
1087c478bd9Sstevel@tonic-gate 	    0, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1097c478bd9Sstevel@tonic-gate 		return (error);
1107c478bd9Sstevel@tonic-gate 	if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
1117c478bd9Sstevel@tonic-gate 		return (ENOEXEC);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/*
1147c478bd9Sstevel@tonic-gate 	 * Ok, so this -is- a ZIP file, and might even be a JAR file.
1157c478bd9Sstevel@tonic-gate 	 * Is it a Java executable?
1167c478bd9Sstevel@tonic-gate 	 */
1177c478bd9Sstevel@tonic-gate 	xoff = sizeof (lochdr) + LOCNAM(lochdr);
1187c478bd9Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(lochdr);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	while (xoff < xoff_end) {
1217c478bd9Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
1247c478bd9Sstevel@tonic-gate 		    xoff, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1257c478bd9Sstevel@tonic-gate 			return (error);
1267c478bd9Sstevel@tonic-gate 		if (resid != 0)
1277c478bd9Sstevel@tonic-gate 			return (ENOEXEC);
1287c478bd9Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG)
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (xoff >= xoff_end)
1347c478bd9Sstevel@tonic-gate 		return (ENOEXEC);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Note: If we ever make setid execution work, we need to ensure
1387c478bd9Sstevel@tonic-gate 	 * that we use /dev/fd to avoid the classic setuid shell script
1397c478bd9Sstevel@tonic-gate 	 * security hole.
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 	if (setid)
1427c478bd9Sstevel@tonic-gate 		return (EACCES);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Find and invoke the Java runtime environment on the file
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	idata.intp = NULL;
1487c478bd9Sstevel@tonic-gate 	idata.intp_name = jexec;
1497c478bd9Sstevel@tonic-gate 	idata.intp_arg = jexec_arg;
1507c478bd9Sstevel@tonic-gate 	if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &lookpn))
1517c478bd9Sstevel@tonic-gate 		return (error);
1527c478bd9Sstevel@tonic-gate 	pn_alloc(&resolvepn);
1537c478bd9Sstevel@tonic-gate 	if (error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
1547c478bd9Sstevel@tonic-gate 		pn_free(&resolvepn);
1557c478bd9Sstevel@tonic-gate 		pn_free(&lookpn);
1567c478bd9Sstevel@tonic-gate 		return (ENOEXEC);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	opath = args->pathname;
1597c478bd9Sstevel@tonic-gate 	args->pathname = resolvepn.pn_path;
1607c478bd9Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
1617c478bd9Sstevel@tonic-gate 	pn_free(&lookpn);
1629acbbeafSnn35248 	error = gexec(&nvp, uap, args, &idata, level + 1, execsz, execfile,
1639acbbeafSnn35248 	    cred, EBA_NONE);
164*a4aeef46SDonghai Qiao 
165*a4aeef46SDonghai Qiao 	if (!error) {
166*a4aeef46SDonghai Qiao 		/*
167*a4aeef46SDonghai Qiao 		 * Close this Java executable as the interpreter
168*a4aeef46SDonghai Qiao 		 * will open and close it later on.
169*a4aeef46SDonghai Qiao 		 */
170*a4aeef46SDonghai Qiao 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
171*a4aeef46SDonghai Qiao 	}
172*a4aeef46SDonghai Qiao 
1737c478bd9Sstevel@tonic-gate 	VN_RELE(nvp);
1747c478bd9Sstevel@tonic-gate 	args->pathname = opath;
1757c478bd9Sstevel@tonic-gate 	pn_free(&resolvepn);
1767c478bd9Sstevel@tonic-gate 	return (error);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static struct execsw jexecsw = {
1807c478bd9Sstevel@tonic-gate 	javamagicstr,
1817c478bd9Sstevel@tonic-gate 	0,
1827c478bd9Sstevel@tonic-gate 	4,
1837c478bd9Sstevel@tonic-gate 	javaexec,
1847c478bd9Sstevel@tonic-gate 	NULL
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static struct modlexec jmodlexec = {
1887c478bd9Sstevel@tonic-gate 	&mod_execops, "exec for Java", &jexecsw
1897c478bd9Sstevel@tonic-gate };
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static struct modlinkage jmodlinkage = {
1927c478bd9Sstevel@tonic-gate 	MODREV_1, &jmodlexec, NULL
1937c478bd9Sstevel@tonic-gate };
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate int
1967c478bd9Sstevel@tonic-gate _init(void)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	return (mod_install(&jmodlinkage));
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
2027c478bd9Sstevel@tonic-gate _fini(void)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	return (mod_remove(&jmodlinkage));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate int
2087c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	return (mod_info(&jmodlinkage, modinfop));
2117c478bd9Sstevel@tonic-gate }
212