#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysmacros.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/pathname.h>
#include <sys/disp.h>
#include <sys/exec.h>
#include <sys/kmem.h>
#include <sys/note.h>
#include <sys/sdt.h>
#include <sys/modctl.h>
extern int intpexec(struct vnode *, struct execa *, struct uarg *,
struct intpdata *, int, size_t *, int, caddr_t, struct cred *, int);
static struct execsw esw = {
intpmagicstr,
0,
2,
intpexec,
NULL
};
extern struct mod_ops mod_execops;
static struct modlexec modlexec = {
&mod_execops, "exec mod for interp", &esw
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlexec, NULL
};
int
_init()
{
return (mod_install(&modlinkage));
}
int
_fini()
{
return (mod_remove(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
getintphead(struct vnode *vp, struct intpdata *idatap)
{
int error;
char *cp, *linep = idatap->intp;
ssize_t resid;
if (error = vn_rdwr(UIO_READ, vp, linep, INTPSZ, (offset_t)0,
UIO_SYSSPACE, 0, (rlim64_t)0, CRED(), &resid))
return (error);
if (resid > INTPSZ-2 || linep[0] != '#' || linep[1] != '!')
return (ENOEXEC);
for (cp = &linep[2]; cp < &linep[INTPSZ] && *cp != '\n'; cp++)
if (*cp == '\t')
*cp = ' ';
if (cp >= &linep[INTPSZ])
return (ENOEXEC);
ASSERT(*cp == '\n');
*cp = '\0';
for (cp = &linep[2]; *cp == ' '; cp++)
;
if (*cp == '\0')
return (ENOEXEC);
idatap->intp_name[0] = cp;
while (*cp && *cp != ' ')
cp++;
if (*cp == '\0') {
idatap->intp_arg[0] = NULL;
} else {
*cp++ = '\0';
while (*cp == ' ')
cp++;
if (*cp == '\0')
idatap->intp_arg[0] = NULL;
else {
idatap->intp_arg[0] = cp;
while (*cp && *cp != ' ')
cp++;
*cp = '\0';
}
}
return (0);
}
int
intpexec(
struct vnode *vp,
struct execa *uap,
struct uarg *args,
struct intpdata *idatap,
int level,
size_t *execsz,
int setid,
caddr_t exec_file,
struct cred *cred,
int brand_action)
{
_NOTE(ARGUNUSED(brand_action))
vnode_t *nvp;
int error = 0;
struct intpdata idata;
struct pathname intppn;
struct pathname resolvepn;
char *opath;
char devfd[19];
int fd = -1;
if (level >= INTP_MAXDEPTH) {
error = ELOOP;
goto bad;
}
if (level == 0)
ASSERT(idatap == (struct intpdata *)NULL);
bzero(&idata, sizeof (intpdata_t));
idata.intp = kmem_alloc(INTPSZ, KM_SLEEP);
if (error = getintphead(vp, &idata))
goto fail;
if (error = pn_get(idata.intp_name[0], UIO_SYSSPACE, &intppn))
goto fail;
pn_alloc(&resolvepn);
if (error = lookuppn(&intppn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
pn_free(&resolvepn);
pn_free(&intppn);
goto fail;
}
if (level > 0) {
int i;
atomic_inc_32(&curproc->p_zone->zone_nested_intp);
ASSERT(idatap != NULL);
for (i = 0; i < (INTP_MAXDEPTH - 1); i++) {
idata.intp_name[i + 1] = idatap->intp_name[i];
idata.intp_arg[i + 1] = idatap->intp_arg[i];
}
DTRACE_PROBE3(nested__intp, int, level, void *, &idata,
void *, nvp);
}
opath = args->pathname;
args->pathname = resolvepn.pn_path;
pn_free(&intppn);
if (level > 0 && args->fname != NULL) {
error = ENOEXEC;
goto done;
}
if ((setid & EXECSETID_PRIVS) != 0 ||
(setid & (EXECSETID_UGIDS|EXECSETID_SETID)) ==
(EXECSETID_UGIDS|EXECSETID_SETID)) {
(void) strcpy(devfd, "/dev/fd/");
if (error = execopen(&vp, &fd))
goto done;
numtos(fd, &devfd[8]);
args->fname = devfd;
}
error = gexec(&nvp, uap, args, &idata, ++level, execsz, exec_file, cred,
EBA_NONE);
if (!error) {
(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, cred, NULL);
}
done:
VN_RELE(nvp);
args->pathname = opath;
pn_free(&resolvepn);
fail:
kmem_free(idata.intp, INTPSZ);
if (error && fd != -1)
(void) execclose(fd);
bad:
return (error);
}