xref: /onnv-gate/usr/src/uts/common/exec/java/java.c (revision 11736:63a134e1f09c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11736SDonghai.Qiao@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Launch Java executables via exec(2).
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * Java executables are platform-independent executable files
300Sstevel@tonic-gate  * based on the JAR file format.  Executable JAR files contain a
310Sstevel@tonic-gate  * special 'extra field' header in the first file of the archive
320Sstevel@tonic-gate  * that marks the file as a true executable.   The data in that field
330Sstevel@tonic-gate  * is used to pass additional run-time information to the Java VM.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * This handler looks for the appropriate magic number on the
360Sstevel@tonic-gate  * front of the file, checks that the JAR file is executable, then
370Sstevel@tonic-gate  * invokes the Java runtime environment to do the rest of the work.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate #include <sys/exec.h>
440Sstevel@tonic-gate #include <sys/modctl.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/pathname.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * These variables can be tweaked via /etc/system to allow prototyping
500Sstevel@tonic-gate  * and debugging.  See PSARC/1997/123.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Modified by PSARC/1999/012 to be Contract Private between Solaris and
530Sstevel@tonic-gate  * the Java Technology Group.  It is expected that any future change to
540Sstevel@tonic-gate  * these variables be coordinated between the consolidations.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #if defined(__sparc)
570Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/sparc/jexec";
580Sstevel@tonic-gate #elif defined(__i386) || defined(__i386_COMPAT)
590Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/i386/jexec";
600Sstevel@tonic-gate #else
610Sstevel@tonic-gate #error "Unknown ISA"
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate char *jexec_arg = "-jar";
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * ZIP/JAR file header information
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate #define	SIGSIZ		4
690Sstevel@tonic-gate #define	LOCSIG		"PK\003\004"
700Sstevel@tonic-gate #define	LOCHDRSIZ	30
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	CH(b, n)	(((unsigned char *)(b))[n])
730Sstevel@tonic-gate #define	SH(b, n)	(CH(b, n) | (CH(b, n+1) << 8))
740Sstevel@tonic-gate #define	LG(b, n)	(SH(b, n) | (SH(b, n+2) << 16))
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #define	LOCNAM(b)	(SH(b, 26))	/* filename size */
770Sstevel@tonic-gate #define	LOCEXT(b)	(SH(b, 28))	/* extra field size */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	XFHSIZ		4		/* header id, data size */
800Sstevel@tonic-gate #define	XFHID(b)	(SH(b, 0))	/* extract field header id */
810Sstevel@tonic-gate #define	XFDATASIZ(b)	(SH(b, 2))	/* extract field data size */
820Sstevel@tonic-gate #define	XFJAVASIG	0xcafe		/* java executables */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*ARGSUSED3*/
850Sstevel@tonic-gate static int
javaexec(vnode_t * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,long * execsz,int setid,caddr_t execfile,cred_t * cred,int brand_action)860Sstevel@tonic-gate javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
870Sstevel@tonic-gate     struct intpdata *idatap, int level, long *execsz, int setid,
882712Snn35248     caddr_t execfile, cred_t *cred, int brand_action)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	struct intpdata idata;
910Sstevel@tonic-gate 	int error;
920Sstevel@tonic-gate 	ssize_t resid;
930Sstevel@tonic-gate 	vnode_t *nvp;
940Sstevel@tonic-gate 	off_t xoff, xoff_end;
950Sstevel@tonic-gate 	char lochdr[LOCHDRSIZ];
960Sstevel@tonic-gate 	struct pathname lookpn;
970Sstevel@tonic-gate 	struct pathname resolvepn;
980Sstevel@tonic-gate 	char *opath;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (level)
1010Sstevel@tonic-gate 		return (ENOEXEC);	/* no recursion */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/*
1040Sstevel@tonic-gate 	 * Read in the full local file header, and validate
1050Sstevel@tonic-gate 	 * the initial signature.
1060Sstevel@tonic-gate 	 */
1070Sstevel@tonic-gate 	if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
1080Sstevel@tonic-gate 	    0, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1090Sstevel@tonic-gate 		return (error);
1100Sstevel@tonic-gate 	if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
1110Sstevel@tonic-gate 		return (ENOEXEC);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Ok, so this -is- a ZIP file, and might even be a JAR file.
1150Sstevel@tonic-gate 	 * Is it a Java executable?
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	xoff = sizeof (lochdr) + LOCNAM(lochdr);
1180Sstevel@tonic-gate 	xoff_end = xoff + LOCEXT(lochdr);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	while (xoff < xoff_end) {
1210Sstevel@tonic-gate 		char xfhdr[XFHSIZ];
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
1240Sstevel@tonic-gate 		    xoff, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1250Sstevel@tonic-gate 			return (error);
1260Sstevel@tonic-gate 		if (resid != 0)
1270Sstevel@tonic-gate 			return (ENOEXEC);
1280Sstevel@tonic-gate 		if (XFHID(xfhdr) == XFJAVASIG)
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 		xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (xoff >= xoff_end)
1340Sstevel@tonic-gate 		return (ENOEXEC);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Note: If we ever make setid execution work, we need to ensure
1380Sstevel@tonic-gate 	 * that we use /dev/fd to avoid the classic setuid shell script
1390Sstevel@tonic-gate 	 * security hole.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	if (setid)
1420Sstevel@tonic-gate 		return (EACCES);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * Find and invoke the Java runtime environment on the file
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	idata.intp = NULL;
1480Sstevel@tonic-gate 	idata.intp_name = jexec;
1490Sstevel@tonic-gate 	idata.intp_arg = jexec_arg;
1500Sstevel@tonic-gate 	if (error = pn_get(idata.intp_name, UIO_SYSSPACE, &lookpn))
1510Sstevel@tonic-gate 		return (error);
1520Sstevel@tonic-gate 	pn_alloc(&resolvepn);
1530Sstevel@tonic-gate 	if (error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
1540Sstevel@tonic-gate 		pn_free(&resolvepn);
1550Sstevel@tonic-gate 		pn_free(&lookpn);
1560Sstevel@tonic-gate 		return (ENOEXEC);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	opath = args->pathname;
1590Sstevel@tonic-gate 	args->pathname = resolvepn.pn_path;
1600Sstevel@tonic-gate 	/* don't free resolvepn until we are done with args */
1610Sstevel@tonic-gate 	pn_free(&lookpn);
1622712Snn35248 	error = gexec(&nvp, uap, args, &idata, level + 1, execsz, execfile,
1632712Snn35248 	    cred, EBA_NONE);
164*11736SDonghai.Qiao@Sun.COM 
165*11736SDonghai.Qiao@Sun.COM 	if (!error) {
166*11736SDonghai.Qiao@Sun.COM 		/*
167*11736SDonghai.Qiao@Sun.COM 		 * Close this Java executable as the interpreter
168*11736SDonghai.Qiao@Sun.COM 		 * will open and close it later on.
169*11736SDonghai.Qiao@Sun.COM 		 */
170*11736SDonghai.Qiao@Sun.COM 		(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
171*11736SDonghai.Qiao@Sun.COM 	}
172*11736SDonghai.Qiao@Sun.COM 
1730Sstevel@tonic-gate 	VN_RELE(nvp);
1740Sstevel@tonic-gate 	args->pathname = opath;
1750Sstevel@tonic-gate 	pn_free(&resolvepn);
1760Sstevel@tonic-gate 	return (error);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static struct execsw jexecsw = {
1800Sstevel@tonic-gate 	javamagicstr,
1810Sstevel@tonic-gate 	0,
1820Sstevel@tonic-gate 	4,
1830Sstevel@tonic-gate 	javaexec,
1840Sstevel@tonic-gate 	NULL
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static struct modlexec jmodlexec = {
1880Sstevel@tonic-gate 	&mod_execops, "exec for Java", &jexecsw
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct modlinkage jmodlinkage = {
1920Sstevel@tonic-gate 	MODREV_1, &jmodlexec, NULL
1930Sstevel@tonic-gate };
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate int
_init(void)1960Sstevel@tonic-gate _init(void)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	return (mod_install(&jmodlinkage));
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
_fini(void)2020Sstevel@tonic-gate _fini(void)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	return (mod_remove(&jmodlinkage));
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2080Sstevel@tonic-gate _info(struct modinfo *modinfop)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	return (mod_info(&jmodlinkage, modinfop));
2110Sstevel@tonic-gate }
212