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 /*
22a4aeef46SDonghai Qiao * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*4e18e297SPatrick Mooney * Copyright 2019 Joyent, Inc.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * Launch Java executables via exec(2).
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * Java executables are platform-independent executable files
317c478bd9Sstevel@tonic-gate * based on the JAR file format. Executable JAR files contain a
327c478bd9Sstevel@tonic-gate * special 'extra field' header in the first file of the archive
337c478bd9Sstevel@tonic-gate * that marks the file as a true executable. The data in that field
347c478bd9Sstevel@tonic-gate * is used to pass additional run-time information to the Java VM.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * This handler looks for the appropriate magic number on the
377c478bd9Sstevel@tonic-gate * front of the file, checks that the JAR file is executable, then
387c478bd9Sstevel@tonic-gate * invokes the Java runtime environment to do the rest of the work.
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.h>
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
447c478bd9Sstevel@tonic-gate #include <sys/exec.h>
457c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * These variables can be tweaked via /etc/system to allow prototyping
517c478bd9Sstevel@tonic-gate * and debugging. See PSARC/1997/123.
527c478bd9Sstevel@tonic-gate *
537c478bd9Sstevel@tonic-gate * Modified by PSARC/1999/012 to be Contract Private between Solaris and
547c478bd9Sstevel@tonic-gate * the Java Technology Group. It is expected that any future change to
557c478bd9Sstevel@tonic-gate * these variables be coordinated between the consolidations.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate #if defined(__sparc)
587c478bd9Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/sparc/jexec";
5986ef0a63SRichard Lowe #elif defined(__x86)
607c478bd9Sstevel@tonic-gate char *jexec = "/usr/java/jre/lib/i386/jexec";
617c478bd9Sstevel@tonic-gate #else
627c478bd9Sstevel@tonic-gate #error "Unknown ISA"
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate char *jexec_arg = "-jar";
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * ZIP/JAR file header information
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate #define SIGSIZ 4
707c478bd9Sstevel@tonic-gate #define LOCSIG "PK\003\004"
717c478bd9Sstevel@tonic-gate #define LOCHDRSIZ 30
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #define CH(b, n) (((unsigned char *)(b))[n])
747c478bd9Sstevel@tonic-gate #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
757c478bd9Sstevel@tonic-gate #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16))
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #define LOCNAM(b) (SH(b, 26)) /* filename size */
787c478bd9Sstevel@tonic-gate #define LOCEXT(b) (SH(b, 28)) /* extra field size */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #define XFHSIZ 4 /* header id, data size */
817c478bd9Sstevel@tonic-gate #define XFHID(b) (SH(b, 0)) /* extract field header id */
827c478bd9Sstevel@tonic-gate #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */
837c478bd9Sstevel@tonic-gate #define XFJAVASIG 0xcafe /* java executables */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
867c478bd9Sstevel@tonic-gate static int
javaexec(vnode_t * vp,struct execa * uap,struct uarg * args,struct intpdata * idatap,int level,size_t * execsz,int setid,caddr_t execfile,cred_t * cred,int brand_action)877c478bd9Sstevel@tonic-gate javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
88*4e18e297SPatrick Mooney struct intpdata *idatap, int level, size_t *execsz, int setid,
899acbbeafSnn35248 caddr_t execfile, cred_t *cred, int brand_action)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate struct intpdata idata;
927c478bd9Sstevel@tonic-gate int error;
937c478bd9Sstevel@tonic-gate ssize_t resid;
947c478bd9Sstevel@tonic-gate vnode_t *nvp;
957c478bd9Sstevel@tonic-gate off_t xoff, xoff_end;
967c478bd9Sstevel@tonic-gate char lochdr[LOCHDRSIZ];
977c478bd9Sstevel@tonic-gate struct pathname lookpn;
987c478bd9Sstevel@tonic-gate struct pathname resolvepn;
997c478bd9Sstevel@tonic-gate char *opath;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (level)
1027c478bd9Sstevel@tonic-gate return (ENOEXEC); /* no recursion */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * Read in the full local file header, and validate
1067c478bd9Sstevel@tonic-gate * the initial signature.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
1097c478bd9Sstevel@tonic-gate 0, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1107c478bd9Sstevel@tonic-gate return (error);
1117c478bd9Sstevel@tonic-gate if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
1127c478bd9Sstevel@tonic-gate return (ENOEXEC);
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * Ok, so this -is- a ZIP file, and might even be a JAR file.
1167c478bd9Sstevel@tonic-gate * Is it a Java executable?
1177c478bd9Sstevel@tonic-gate */
1187c478bd9Sstevel@tonic-gate xoff = sizeof (lochdr) + LOCNAM(lochdr);
1197c478bd9Sstevel@tonic-gate xoff_end = xoff + LOCEXT(lochdr);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate while (xoff < xoff_end) {
1227c478bd9Sstevel@tonic-gate char xfhdr[XFHSIZ];
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
1257c478bd9Sstevel@tonic-gate xoff, UIO_SYSSPACE, 0, (rlim64_t)0, cred, &resid)) != 0)
1267c478bd9Sstevel@tonic-gate return (error);
1277c478bd9Sstevel@tonic-gate if (resid != 0)
1287c478bd9Sstevel@tonic-gate return (ENOEXEC);
1297c478bd9Sstevel@tonic-gate if (XFHID(xfhdr) == XFJAVASIG)
1307c478bd9Sstevel@tonic-gate break;
1317c478bd9Sstevel@tonic-gate xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate if (xoff >= xoff_end)
1357c478bd9Sstevel@tonic-gate return (ENOEXEC);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate * Note: If we ever make setid execution work, we need to ensure
1397c478bd9Sstevel@tonic-gate * that we use /dev/fd to avoid the classic setuid shell script
1407c478bd9Sstevel@tonic-gate * security hole.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate if (setid)
1437c478bd9Sstevel@tonic-gate return (EACCES);
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * Find and invoke the Java runtime environment on the file
1477c478bd9Sstevel@tonic-gate */
1484fd09333SAndy Fiddaman bzero(&idata, sizeof (intpdata_t));
1497c478bd9Sstevel@tonic-gate idata.intp = NULL;
15093cf283aSJerry Jelinek idata.intp_name[0] = jexec;
15193cf283aSJerry Jelinek idata.intp_arg[0] = jexec_arg;
15293cf283aSJerry Jelinek if (error = pn_get(idata.intp_name[0], UIO_SYSSPACE, &lookpn))
1537c478bd9Sstevel@tonic-gate return (error);
1547c478bd9Sstevel@tonic-gate pn_alloc(&resolvepn);
1557c478bd9Sstevel@tonic-gate if (error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
1567c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
1577c478bd9Sstevel@tonic-gate pn_free(&lookpn);
1587c478bd9Sstevel@tonic-gate return (ENOEXEC);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate opath = args->pathname;
1617c478bd9Sstevel@tonic-gate args->pathname = resolvepn.pn_path;
1627c478bd9Sstevel@tonic-gate /* don't free resolvepn until we are done with args */
1637c478bd9Sstevel@tonic-gate pn_free(&lookpn);
1649acbbeafSnn35248 error = gexec(&nvp, uap, args, &idata, level + 1, execsz, execfile,
1659acbbeafSnn35248 cred, EBA_NONE);
166a4aeef46SDonghai Qiao
167a4aeef46SDonghai Qiao if (!error) {
168a4aeef46SDonghai Qiao /*
169a4aeef46SDonghai Qiao * Close this Java executable as the interpreter
170a4aeef46SDonghai Qiao * will open and close it later on.
171a4aeef46SDonghai Qiao */
172a4aeef46SDonghai Qiao (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
173a4aeef46SDonghai Qiao }
174a4aeef46SDonghai Qiao
1757c478bd9Sstevel@tonic-gate VN_RELE(nvp);
1767c478bd9Sstevel@tonic-gate args->pathname = opath;
1777c478bd9Sstevel@tonic-gate pn_free(&resolvepn);
1787c478bd9Sstevel@tonic-gate return (error);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate static struct execsw jexecsw = {
1827c478bd9Sstevel@tonic-gate javamagicstr,
1837c478bd9Sstevel@tonic-gate 0,
1847c478bd9Sstevel@tonic-gate 4,
1857c478bd9Sstevel@tonic-gate javaexec,
1867c478bd9Sstevel@tonic-gate NULL
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate static struct modlexec jmodlexec = {
1907c478bd9Sstevel@tonic-gate &mod_execops, "exec for Java", &jexecsw
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate static struct modlinkage jmodlinkage = {
1947c478bd9Sstevel@tonic-gate MODREV_1, &jmodlexec, NULL
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate int
_init(void)1987c478bd9Sstevel@tonic-gate _init(void)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate return (mod_install(&jmodlinkage));
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate int
_fini(void)2047c478bd9Sstevel@tonic-gate _fini(void)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate return (mod_remove(&jmodlinkage));
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2107c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate return (mod_info(&jmodlinkage, modinfop));
2137c478bd9Sstevel@tonic-gate }
214