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.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/sysmacros.h>
30 #include <sys/signal.h>
33 #include <sys/errno.h>
34 #include <sys/vnode.h>
36 #include <sys/cmn_err.h>
37 #include <sys/debug.h>
38 #include <sys/pathname.h>
45 * This is the loadable module wrapper.
47 #include <sys/modctl.h>
49 /* Local prototypes */
55 struct intpdata
*idatap
,
63 #define SHBIN_CNTL(x) ((x)&037)
64 #define SHBINMAGIC_LEN 4
65 extern char shbinmagicstr
[];
68 * Our list where we may find a copy of ksh93. The ordering is:
69 * 1. 64bit (may not be installed or not supported in hardware)
71 * 3. Use /sbin/ksh93 when /usr is not available
73 * ([1] and [2] explicitly bypass /usr/bin/ksh93 to avoid the
76 static char *shell_list
[] =
78 /* Bypass /usr/bin/ksh93 (which is "isaexec") for performance */
80 "/usr/bin/sparcv9/ksh93",
81 "/usr/bin/sparcv7/ksh93",
82 #elif defined(__amd64)
83 "/usr/bin/amd64/ksh93",
88 #error "Unrecognized platform/CPU (use /usr/bin/ksh93 when in doubt)."
94 static struct execsw esw
= {
103 * Module linkage information for the kernel.
105 extern struct mod_ops mod_execops
;
107 static struct modlexec modlexec
= {
108 &mod_execops
, "exec mod for shell binaries (ksh93)", &esw
111 static struct modlinkage modlinkage
= {
112 MODREV_1
, (void *)&modlexec
, NULL
118 return (mod_install(&modlinkage
));
124 return (mod_remove(&modlinkage
));
128 _info(struct modinfo
*modinfop
)
130 return (mod_info(&modlinkage
, modinfop
));
134 checkshbinmagic(struct vnode
*vp
)
137 char linep
[SHBINMAGIC_LEN
];
141 * Read the entire line and confirm that it starts with the magic
142 * sequence for compiled ksh93 shell scripts.
144 if (error
= vn_rdwr(UIO_READ
, vp
, linep
, sizeof (linep
), (offset_t
)0,
145 UIO_SYSSPACE
, 0, (rlim64_t
)0, CRED(), &resid
))
148 if (memcmp(linep
, shbinmagicstr
, SHBINMAGIC_LEN
) != 0)
159 struct intpdata
*idatap
,
167 _NOTE(ARGUNUSED(brand_action
))
170 struct intpdata idata
;
171 struct pathname intppn
;
172 struct pathname resolvepn
;
174 char devfd
[19]; /* 32-bit int fits in 10 digits + 8 for "/dev/fd/" */
178 if (level
) { /* Can't recurse */
183 ASSERT(idatap
== NULL
);
186 * Check whether the executable has the correct magic value.
188 if (error
= checkshbinmagic(vp
))
191 pn_alloc(&resolvepn
);
194 * Travel the list of shells and look for one which is available...
196 for (i
= 0; shell_list
[i
] != NULL
; i
++) {
197 error
= pn_get(shell_list
[i
], UIO_SYSSPACE
, &intppn
);
202 error
= lookuppn(&intppn
, &resolvepn
, FOLLOW
, NULLVPP
, &nvp
);
208 /* No match found ? Then continue with the next item... */
218 * Setup interpreter data
219 * "--" is passed to mark the end-of-arguments before adding
220 * the scripts file name, preventing problems when a
221 * a script's name starts with a '-' character.
224 idata
.intp_name
[0] = shell_list
[i
];
225 idata
.intp_arg
[0] = "--";
227 opath
= args
->pathname
;
228 args
->pathname
= resolvepn
.pn_path
;
229 /* don't free resolvepn until we are done with args */
233 * When we're executing a set-uid script resulting in uids
234 * mismatching or when we execute with additional privileges,
235 * we close the "replace script between exec and open by shell"
236 * hole by passing the script as /dev/fd parameter.
238 if ((setid
& EXECSETID_PRIVS
) != 0 ||
239 (setid
& (EXECSETID_UGIDS
|EXECSETID_SETID
)) ==
240 (EXECSETID_UGIDS
|EXECSETID_SETID
)) {
241 (void) strcpy(devfd
, "/dev/fd/");
242 if (error
= execopen(&vp
, &fd
))
244 numtos(fd
, &devfd
[8]);
248 error
= gexec(&nvp
, uap
, args
, &idata
, ++level
, execsz
, exec_file
, cred
,
253 * Close this script as the sh interpreter
254 * will open and close it later on.
256 (void) fop_close(vp
, FREAD
, 1, (offset_t
)0, cred
, NULL
);
260 args
->pathname
= opath
;
263 if (error
&& fd
!= -1)
264 (void) execclose(fd
);