#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: exec_script.c,v 1.85 2024/12/06 16:19:41 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_script.h"
#endif
#if defined(SETUIDSCRIPTS) && !defined(FDSCRIPTS)
#define FDSCRIPTS
#endif
#include <sys/param.h>
#include <sys/types.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <sys/exec_script.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/sdt.h>
#ifdef SETUIDSCRIPTS
#include <sys/stat.h>
#endif
#include <sys/systm.h>
#include <sys/vnode.h>
MODULE(MODULE_CLASS_EXEC, exec_script, NULL);
static struct execsw exec_script_execsw = {
.es_hdrsz = SCRIPT_HDR_SIZE,
.es_makecmds = exec_script_makecmds,
.u = {
.elf_probe_func = NULL,
},
.es_emul = NULL,
.es_prio = EXECSW_PRIO_ANY,
.es_arglen = 0,
.es_copyargs = NULL,
.es_setregs = NULL,
.es_coredump = NULL,
.es_setup_stack = exec_setup_stack,
};
static int
exec_script_modcmd(modcmd_t cmd, void *arg)
{
switch (cmd) {
case MODULE_CMD_INIT:
return exec_add(&exec_script_execsw, 1);
case MODULE_CMD_FINI:
return exec_remove(&exec_script_execsw, 1);
case MODULE_CMD_AUTOUNLOAD:
return SET_ERROR(EBUSY);
default:
return SET_ERROR(ENOTTY);
}
}
int
exec_script_makecmds(struct lwp *l, struct exec_package *epp)
{
int error, hdrlinelen, shellnamelen, shellarglen;
char *hdrstr = epp->ep_hdr;
char *cp, *shellname, *shellarg;
size_t shellargp_len;
struct exec_fakearg *shellargp;
struct exec_fakearg *tmpsap;
struct pathbuf *shell_pathbuf;
struct vnode *scriptvp;
#ifdef SETUIDSCRIPTS
uid_t script_uid = (uid_t) -1;
gid_t script_gid = NOGROUP;
u_short script_sbits;
#endif
if ((epp->ep_flags & EXEC_INDIR) != 0 ||
epp->ep_hdrvalid < EXEC_SCRIPT_MAGICLEN ||
strncmp(hdrstr, EXEC_SCRIPT_MAGIC, EXEC_SCRIPT_MAGICLEN))
return SET_ERROR(ENOEXEC);
hdrlinelen = uimin(epp->ep_hdrvalid, SCRIPT_HDR_SIZE);
for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; cp < hdrstr + hdrlinelen;
cp++) {
if (*cp == '\n') {
*cp = '\0';
break;
}
}
if (cp >= hdrstr + hdrlinelen)
return SET_ERROR(ENOEXEC);
for (cp = hdrstr + EXEC_SCRIPT_MAGICLEN; *cp == ' ' || *cp == '\t';
cp++)
;
if (*cp == '\0')
return SET_ERROR(ENOEXEC);
shellarg = NULL;
shellarglen = 0;
shellname = cp;
shellnamelen = 0;
for ( ; *cp != '\0' && *cp != ' ' && *cp != '\t'; cp++)
shellnamelen++;
if (*cp == '\0')
goto check_shell;
*cp++ = '\0';
for ( ; *cp == ' ' || *cp == '\t'; cp++)
;
if (*cp == '\0')
goto check_shell;
shellarg = cp;
for ( ; *cp != '\0'; cp++)
shellarglen++;
*cp++ = '\0';
check_shell:
#ifdef SETUIDSCRIPTS
script_sbits = epp->ep_vap->va_mode & (S_ISUID | S_ISGID);
if (script_sbits != 0) {
script_uid = epp->ep_vap->va_uid;
script_gid = epp->ep_vap->va_gid;
}
#endif
#ifdef FDSCRIPTS
vn_lock(epp->ep_vp, LK_SHARED | LK_RETRY);
error = VOP_ACCESS(epp->ep_vp, VREAD, l->l_cred);
VOP_UNLOCK(epp->ep_vp);
if (error == EACCES
#ifdef SETUIDSCRIPTS
|| script_sbits
#endif
) {
struct file *fp;
KASSERT(!(epp->ep_flags & EXEC_HASFD));
if ((error = fd_allocfile(&fp, &epp->ep_fd)) != 0) {
scriptvp = NULL;
shellargp = NULL;
goto fail;
}
epp->ep_flags |= EXEC_HASFD;
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
fp->f_vnode = epp->ep_vp;
fp->f_flag = FREAD;
fd_affix(curproc, fp, epp->ep_fd);
}
#endif
shellargp_len = 4 * sizeof(*shellargp);
shellargp = kmem_alloc(shellargp_len, KM_SLEEP);
tmpsap = shellargp;
tmpsap->fa_len = shellnamelen + 1;
tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
strlcpy(tmpsap->fa_arg, shellname, tmpsap->fa_len);
tmpsap++;
if (shellarg != NULL) {
tmpsap->fa_len = shellarglen + 1;
tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
strlcpy(tmpsap->fa_arg, shellarg, tmpsap->fa_len);
tmpsap++;
}
tmpsap->fa_len = MAXPATHLEN;
tmpsap->fa_arg = kmem_alloc(tmpsap->fa_len, KM_SLEEP);
#ifdef FDSCRIPTS
if ((epp->ep_flags & EXEC_HASFD) == 0) {
#endif
error = copystr(epp->ep_kname, tmpsap->fa_arg, MAXPATHLEN,
NULL);
KASSERT(error == 0);
tmpsap++;
#ifdef FDSCRIPTS
} else {
snprintf(tmpsap->fa_arg, MAXPATHLEN, "/dev/fd/%d", epp->ep_fd);
tmpsap++;
}
#endif
tmpsap->fa_arg = NULL;
scriptvp = epp->ep_vp;
epp->ep_vp = NULL;
epp->ep_flags |= EXEC_INDIR;
epp->ep_hdrvalid = 0;
if ((error = exec_makepathbuf(l, shellname, UIO_SYSSPACE,
&shell_pathbuf, NULL)) == 0) {
error = check_exec(l, epp, shell_pathbuf, NULL);
pathbuf_destroy(shell_pathbuf);
}
epp->ep_flags |= EXEC_DESTR;
if (error == 0) {
if ((epp->ep_flags & EXEC_HASFD) == 0) {
vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
VOP_CLOSE(scriptvp, FREAD, l->l_cred);
vput(scriptvp);
}
epp->ep_flags |= (EXEC_HASARGL | EXEC_SKIPARG);
epp->ep_fa = shellargp;
epp->ep_fa_len = shellargp_len;
#ifdef SETUIDSCRIPTS
epp->ep_vap->va_mode |= script_sbits;
if (script_sbits & S_ISUID)
epp->ep_vap->va_uid = script_uid;
if (script_sbits & S_ISGID)
epp->ep_vap->va_gid = script_gid;
#endif
return (0);
}
#ifdef FDSCRIPTS
fail:
#endif
if (epp->ep_flags & EXEC_HASFD) {
epp->ep_flags &= ~EXEC_HASFD;
fd_close(epp->ep_fd);
} else if (scriptvp) {
vn_lock(scriptvp, LK_EXCLUSIVE | LK_RETRY);
VOP_CLOSE(scriptvp, FREAD, l->l_cred);
vput(scriptvp);
}
if ((tmpsap = shellargp) != NULL) {
while (tmpsap->fa_arg != NULL) {
kmem_free(tmpsap->fa_arg, tmpsap->fa_len);
tmpsap++;
}
kmem_free(shellargp, shellargp_len);
}
kill_vmcmds(&epp->ep_vmcmds);
return error;
}