Ejemplo n.º 1
0
/* ARGSUSED */
int
sys_setauid(struct thread *td, struct setauid_args *uap)
{
    struct ucred *newcred, *oldcred;
    au_id_t id;
    int error;

    if (jailed(td->td_ucred))
        return (ENOSYS);
    error = copyin(uap->auid, &id, sizeof(id));
    if (error)
        return (error);
    audit_arg_auid(id);
    newcred = crget();
    PROC_LOCK(td->td_proc);
    oldcred = td->td_proc->p_ucred;
    crcopy(newcred, oldcred);
#ifdef MAC
    error = mac_cred_check_setauid(oldcred, id);
    if (error)
        goto fail;
#endif
    error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
    if (error)
        goto fail;
    newcred->cr_audit.ai_auid = id;
    proc_set_cred(td->td_proc, newcred);
    PROC_UNLOCK(td->td_proc);
    crfree(oldcred);
    return (0);
fail:
    PROC_UNLOCK(td->td_proc);
    crfree(newcred);
    return (error);
}
Ejemplo n.º 2
0
/* ARGSUSED */
int
sys_setloginclass(struct thread *td, struct setloginclass_args *uap)
{
	struct proc *p = td->td_proc;
	int error;
	char lcname[MAXLOGNAME];
	struct loginclass *newlc;
	struct ucred *newcred, *oldcred;

	error = priv_check(td, PRIV_PROC_SETLOGINCLASS);
	if (error != 0)
		return (error);
	error = copyinstr(uap->namebuf, lcname, sizeof(lcname), NULL);
	if (error != 0)
		return (error);

	newlc = loginclass_find(lcname);
	if (newlc == NULL)
		return (EINVAL);
	newcred = crget();

	PROC_LOCK(p);
	oldcred = crcopysafe(p, newcred);
	newcred->cr_loginclass = newlc;
	proc_set_cred(p, newcred);
	PROC_UNLOCK(p);
#ifdef RACCT
	racct_proc_ucred_changed(p, oldcred, newcred);
#endif
	loginclass_free(oldcred->cr_loginclass);
	crfree(oldcred);

	return (0);
}
Ejemplo n.º 3
0
/*
 * System call to enter capability mode for the process.
 */
int
sys_cap_enter(struct thread *td, struct cap_enter_args *uap)
{
	struct ucred *newcred, *oldcred;
	struct proc *p;

	if (IN_CAPABILITY_MODE(td))
		return (0);

	newcred = crget();
	p = td->td_proc;
	PROC_LOCK(p);
	oldcred = crcopysafe(p, newcred);
	newcred->cr_flags |= CRED_FLAG_CAPMODE;
	proc_set_cred(p, newcred);
	PROC_UNLOCK(p);
	crfree(oldcred);
	return (0);
}
Ejemplo n.º 4
0
/* ARGSUSED */
int
sys_setaudit(struct thread *td, struct setaudit_args *uap)
{
    struct ucred *newcred, *oldcred;
    struct auditinfo ai;
    int error;

    if (jailed(td->td_ucred))
        return (ENOSYS);
    error = copyin(uap->auditinfo, &ai, sizeof(ai));
    if (error)
        return (error);
    audit_arg_auditinfo(&ai);
    newcred = crget();
    PROC_LOCK(td->td_proc);
    oldcred = td->td_proc->p_ucred;
    crcopy(newcred, oldcred);
#ifdef MAC
    error = mac_cred_check_setaudit(oldcred, &ai);
    if (error)
        goto fail;
#endif
    error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
    if (error)
        goto fail;
    bzero(&newcred->cr_audit, sizeof(newcred->cr_audit));
    newcred->cr_audit.ai_auid = ai.ai_auid;
    newcred->cr_audit.ai_mask = ai.ai_mask;
    newcred->cr_audit.ai_asid = ai.ai_asid;
    newcred->cr_audit.ai_termid.at_addr[0] = ai.ai_termid.machine;
    newcred->cr_audit.ai_termid.at_port = ai.ai_termid.port;
    newcred->cr_audit.ai_termid.at_type = AU_IPv4;
    proc_set_cred(td->td_proc, newcred);
    PROC_UNLOCK(td->td_proc);
    crfree(oldcred);
    return (0);
fail:
    PROC_UNLOCK(td->td_proc);
    crfree(newcred);
    return (error);
}
Ejemplo n.º 5
0
/* ARGSUSED */
int
sys_setaudit_addr(struct thread *td, struct setaudit_addr_args *uap)
{
    struct ucred *newcred, *oldcred;
    struct auditinfo_addr aia;
    int error;

    if (jailed(td->td_ucred))
        return (ENOSYS);
    error = copyin(uap->auditinfo_addr, &aia, sizeof(aia));
    if (error)
        return (error);
    audit_arg_auditinfo_addr(&aia);
    if (aia.ai_termid.at_type != AU_IPv6 &&
            aia.ai_termid.at_type != AU_IPv4)
        return (EINVAL);
    newcred = crget();
    PROC_LOCK(td->td_proc);
    oldcred = td->td_proc->p_ucred;
    crcopy(newcred, oldcred);
#ifdef MAC
    error = mac_cred_check_setaudit_addr(oldcred, &aia);
    if (error)
        goto fail;
#endif
    error = priv_check_cred(oldcred, PRIV_AUDIT_SETAUDIT, 0);
    if (error)
        goto fail;
    newcred->cr_audit = aia;
    proc_set_cred(td->td_proc, newcred);
    PROC_UNLOCK(td->td_proc);
    crfree(oldcred);
    return (0);
fail:
    PROC_UNLOCK(td->td_proc);
    crfree(newcred);
    return (error);
}
Ejemplo n.º 6
0
/*
 * In-kernel implementation of execve().  All arguments are assumed to be
 * userspace pointers from the passed thread.
 */
static int
do_execve(struct thread *td, struct image_args *args, struct mac *mac_p)
{
	struct proc *p = td->td_proc;
	struct nameidata nd;
	struct ucred *oldcred;
	struct uidinfo *euip = NULL;
	register_t *stack_base;
	int error, i;
	struct image_params image_params, *imgp;
	struct vattr attr;
	int (*img_first)(struct image_params *);
	struct pargs *oldargs = NULL, *newargs = NULL;
	struct sigacts *oldsigacts = NULL, *newsigacts = NULL;
#ifdef KTRACE
	struct vnode *tracevp = NULL;
	struct ucred *tracecred = NULL;
#endif
	struct vnode *oldtextvp = NULL, *newtextvp;
	int credential_changing;
	int textset;
#ifdef MAC
	struct label *interpvplabel = NULL;
	int will_transition;
#endif
#ifdef HWPMC_HOOKS
	struct pmckern_procexec pe;
#endif
	static const char fexecv_proc_title[] = "(fexecv)";

	imgp = &image_params;

	/*
	 * Lock the process and set the P_INEXEC flag to indicate that
	 * it should be left alone until we're done here.  This is
	 * necessary to avoid race conditions - e.g. in ptrace() -
	 * that might allow a local user to illicitly obtain elevated
	 * privileges.
	 */
	PROC_LOCK(p);
	KASSERT((p->p_flag & P_INEXEC) == 0,
	    ("%s(): process already has P_INEXEC flag", __func__));
	p->p_flag |= P_INEXEC;
	PROC_UNLOCK(p);

	/*
	 * Initialize part of the common data
	 */
	bzero(imgp, sizeof(*imgp));
	imgp->proc = p;
	imgp->attr = &attr;
	imgp->args = args;
	oldcred = p->p_ucred;

#ifdef MAC
	error = mac_execve_enter(imgp, mac_p);
	if (error)
		goto exec_fail;
#endif

	/*
	 * Translate the file name. namei() returns a vnode pointer
	 *	in ni_vp among other things.
	 *
	 * XXXAUDIT: It would be desirable to also audit the name of the
	 * interpreter if this is an interpreted binary.
	 */
	if (args->fname != NULL) {
		NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME
		    | AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
	}

	SDT_PROBE1(proc, , , exec, args->fname);

interpret:
	if (args->fname != NULL) {
#ifdef CAPABILITY_MODE
		/*
		 * While capability mode can't reach this point via direct
		 * path arguments to execve(), we also don't allow
		 * interpreters to be used in capability mode (for now).
		 * Catch indirect lookups and return a permissions error.
		 */
		if (IN_CAPABILITY_MODE(td)) {
			error = ECAPMODE;
			goto exec_fail;
		}
#endif
		error = namei(&nd);
		if (error)
			goto exec_fail;

		newtextvp = nd.ni_vp;
		imgp->vp = newtextvp;
	} else {
		AUDIT_ARG_FD(args->fd);
		/*
		 * Descriptors opened only with O_EXEC or O_RDONLY are allowed.
		 */
		error = fgetvp_exec(td, args->fd, &cap_fexecve_rights, &newtextvp);
		if (error)
			goto exec_fail;
		vn_lock(newtextvp, LK_EXCLUSIVE | LK_RETRY);
		AUDIT_ARG_VNODE1(newtextvp);
		imgp->vp = newtextvp;
	}

	/*
	 * Check file permissions (also 'opens' file)
	 */
	error = exec_check_permissions(imgp);
	if (error)
		goto exec_fail_dealloc;

	imgp->object = imgp->vp->v_object;
	if (imgp->object != NULL)
		vm_object_reference(imgp->object);

	/*
	 * Set VV_TEXT now so no one can write to the executable while we're
	 * activating it.
	 *
	 * Remember if this was set before and unset it in case this is not
	 * actually an executable image.
	 */
	textset = VOP_IS_TEXT(imgp->vp);
	VOP_SET_TEXT(imgp->vp);

	error = exec_map_first_page(imgp);
	if (error)
		goto exec_fail_dealloc;

	imgp->proc->p_osrel = 0;
	imgp->proc->p_fctl0 = 0;

	/*
	 * Implement image setuid/setgid.
	 *
	 * Determine new credentials before attempting image activators
	 * so that it can be used by process_exec handlers to determine
	 * credential/setid changes.
	 *
	 * Don't honor setuid/setgid if the filesystem prohibits it or if
	 * the process is being traced.
	 *
	 * We disable setuid/setgid/etc in capability mode on the basis
	 * that most setugid applications are not written with that
	 * environment in mind, and will therefore almost certainly operate
	 * incorrectly. In principle there's no reason that setugid
	 * applications might not be useful in capability mode, so we may want
	 * to reconsider this conservative design choice in the future.
	 *
	 * XXXMAC: For the time being, use NOSUID to also prohibit
	 * transitions on the file system.
	 */
	credential_changing = 0;
	credential_changing |= (attr.va_mode & S_ISUID) &&
	    oldcred->cr_uid != attr.va_uid;
	credential_changing |= (attr.va_mode & S_ISGID) &&
	    oldcred->cr_gid != attr.va_gid;
#ifdef MAC
	will_transition = mac_vnode_execve_will_transition(oldcred, imgp->vp,
	    interpvplabel, imgp);
	credential_changing |= will_transition;
#endif

	/* Don't inherit PROC_PDEATHSIG_CTL value if setuid/setgid. */
	if (credential_changing)
		imgp->proc->p_pdeathsig = 0;

	if (credential_changing &&
#ifdef CAPABILITY_MODE
	    ((oldcred->cr_flags & CRED_FLAG_CAPMODE) == 0) &&
#endif
	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
	    (p->p_flag & P_TRACED) == 0) {
		imgp->credential_setid = true;
		VOP_UNLOCK(imgp->vp, 0);
		imgp->newcred = crdup(oldcred);
		if (attr.va_mode & S_ISUID) {
			euip = uifind(attr.va_uid);
			change_euid(imgp->newcred, euip);
		}
		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
		if (attr.va_mode & S_ISGID)
			change_egid(imgp->newcred, attr.va_gid);
		/*
		 * Implement correct POSIX saved-id behavior.
		 *
		 * XXXMAC: Note that the current logic will save the
		 * uid and gid if a MAC domain transition occurs, even
		 * though maybe it shouldn't.
		 */
		change_svuid(imgp->newcred, imgp->newcred->cr_uid);
		change_svgid(imgp->newcred, imgp->newcred->cr_gid);
	} else {
		/*
		 * Implement correct POSIX saved-id behavior.
		 *
		 * XXX: It's not clear that the existing behavior is
		 * POSIX-compliant.  A number of sources indicate that the
		 * saved uid/gid should only be updated if the new ruid is
		 * not equal to the old ruid, or the new euid is not equal
		 * to the old euid and the new euid is not equal to the old
		 * ruid.  The FreeBSD code always updates the saved uid/gid.
		 * Also, this code uses the new (replaced) euid and egid as
		 * the source, which may or may not be the right ones to use.
		 */
		if (oldcred->cr_svuid != oldcred->cr_uid ||
		    oldcred->cr_svgid != oldcred->cr_gid) {
			VOP_UNLOCK(imgp->vp, 0);
			imgp->newcred = crdup(oldcred);
			vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
			change_svuid(imgp->newcred, imgp->newcred->cr_uid);
			change_svgid(imgp->newcred, imgp->newcred->cr_gid);
		}
	}
	/* The new credentials are installed into the process later. */

	/*
	 * Do the best to calculate the full path to the image file.
	 */
	if (args->fname != NULL && args->fname[0] == '/')
		imgp->execpath = args->fname;
	else {
		VOP_UNLOCK(imgp->vp, 0);
		if (vn_fullpath(td, imgp->vp, &imgp->execpath,
		    &imgp->freepath) != 0)
			imgp->execpath = args->fname;
		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
	}

	/*
	 *	If the current process has a special image activator it
	 *	wants to try first, call it.   For example, emulating shell
	 *	scripts differently.
	 */
	error = -1;
	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
		error = img_first(imgp);

	/*
	 *	Loop through the list of image activators, calling each one.
	 *	An activator returns -1 if there is no match, 0 on success,
	 *	and an error otherwise.
	 */
	for (i = 0; error == -1 && execsw[i]; ++i) {
		if (execsw[i]->ex_imgact == NULL ||
		    execsw[i]->ex_imgact == img_first) {
			continue;
		}
		error = (*execsw[i]->ex_imgact)(imgp);
	}

	if (error) {
		if (error == -1) {
			if (textset == 0)
				VOP_UNSET_TEXT(imgp->vp);
			error = ENOEXEC;
		}
		goto exec_fail_dealloc;
	}

	/*
	 * Special interpreter operation, cleanup and loop up to try to
	 * activate the interpreter.
	 */
	if (imgp->interpreted) {
		exec_unmap_first_page(imgp);
		/*
		 * VV_TEXT needs to be unset for scripts.  There is a short
		 * period before we determine that something is a script where
		 * VV_TEXT will be set. The vnode lock is held over this
		 * entire period so nothing should illegitimately be blocked.
		 */
		VOP_UNSET_TEXT(imgp->vp);
		/* free name buffer and old vnode */
		if (args->fname != NULL)
			NDFREE(&nd, NDF_ONLY_PNBUF);
#ifdef MAC
		mac_execve_interpreter_enter(newtextvp, &interpvplabel);
#endif
		if (imgp->opened) {
			VOP_CLOSE(newtextvp, FREAD, td->td_ucred, td);
			imgp->opened = 0;
		}
		vput(newtextvp);
		vm_object_deallocate(imgp->object);
		imgp->object = NULL;
		imgp->credential_setid = false;
		if (imgp->newcred != NULL) {
			crfree(imgp->newcred);
			imgp->newcred = NULL;
		}
		imgp->execpath = NULL;
		free(imgp->freepath, M_TEMP);
		imgp->freepath = NULL;
		/* set new name to that of the interpreter */
		NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
		    UIO_SYSSPACE, imgp->interpreter_name, td);
		args->fname = imgp->interpreter_name;
		goto interpret;
	}

	/*
	 * NB: We unlock the vnode here because it is believed that none
	 * of the sv_copyout_strings/sv_fixup operations require the vnode.
	 */
	VOP_UNLOCK(imgp->vp, 0);

	if (disallow_high_osrel &&
	    P_OSREL_MAJOR(p->p_osrel) > P_OSREL_MAJOR(__FreeBSD_version)) {
		error = ENOEXEC;
		uprintf("Osrel %d for image %s too high\n", p->p_osrel,
		    imgp->execpath != NULL ? imgp->execpath : "<unresolved>");
		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
		goto exec_fail_dealloc;
	}

	/* ABI enforces the use of Capsicum. Switch into capabilities mode. */
	if (SV_PROC_FLAG(p, SV_CAPSICUM))
		sys_cap_enter(td, NULL);

	/*
	 * Copy out strings (args and env) and initialize stack base.
	 */
	stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);

	/*
	 * Stack setup.
	 */
	error = (*p->p_sysent->sv_fixup)(&stack_base, imgp);
	if (error != 0) {
		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
		goto exec_fail_dealloc;
	}

	if (args->fdp != NULL) {
		/* Install a brand new file descriptor table. */
		fdinstall_remapped(td, args->fdp);
		args->fdp = NULL;
	} else {
		/*
		 * Keep on using the existing file descriptor table. For
		 * security and other reasons, the file descriptor table
		 * cannot be shared after an exec.
		 */
		fdunshare(td);
		/* close files on exec */
		fdcloseexec(td);
	}

	/*
	 * Malloc things before we need locks.
	 */
	i = exec_args_get_begin_envv(imgp->args) - imgp->args->begin_argv;
	/* Cache arguments if they fit inside our allowance */
	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
		newargs = pargs_alloc(i);
		bcopy(imgp->args->begin_argv, newargs->ar_args, i);
	}

	/*
	 * For security and other reasons, signal handlers cannot
	 * be shared after an exec. The new process gets a copy of the old
	 * handlers. In execsigs(), the new process will have its signals
	 * reset.
	 */
	if (sigacts_shared(p->p_sigacts)) {
		oldsigacts = p->p_sigacts;
		newsigacts = sigacts_alloc();
		sigacts_copy(newsigacts, oldsigacts);
	}

	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);

	PROC_LOCK(p);
	if (oldsigacts)
		p->p_sigacts = newsigacts;
	/* Stop profiling */
	stopprofclock(p);

	/* reset caught signals */
	execsigs(p);

	/* name this process - nameiexec(p, ndp) */
	bzero(p->p_comm, sizeof(p->p_comm));
	if (args->fname)
		bcopy(nd.ni_cnd.cn_nameptr, p->p_comm,
		    min(nd.ni_cnd.cn_namelen, MAXCOMLEN));
	else if (vn_commname(newtextvp, p->p_comm, sizeof(p->p_comm)) != 0)
		bcopy(fexecv_proc_title, p->p_comm, sizeof(fexecv_proc_title));
	bcopy(p->p_comm, td->td_name, sizeof(td->td_name));
#ifdef KTR
	sched_clear_tdname(td);
#endif

	/*
	 * mark as execed, wakeup the process that vforked (if any) and tell
	 * it that it now has its own resources back
	 */
	p->p_flag |= P_EXEC;
	if ((p->p_flag2 & P2_NOTRACE_EXEC) == 0)
		p->p_flag2 &= ~P2_NOTRACE;
	if (p->p_flag & P_PPWAIT) {
		p->p_flag &= ~(P_PPWAIT | P_PPTRACE);
		cv_broadcast(&p->p_pwait);
		/* STOPs are no longer ignored, arrange for AST */
		signotify(td);
	}

	/*
	 * Implement image setuid/setgid installation.
	 */
	if (imgp->credential_setid) {
		/*
		 * Turn off syscall tracing for set-id programs, except for
		 * root.  Record any set-id flags first to make sure that
		 * we do not regain any tracing during a possible block.
		 */
		setsugid(p);

#ifdef KTRACE
		if (p->p_tracecred != NULL &&
		    priv_check_cred(p->p_tracecred, PRIV_DEBUG_DIFFCRED))
			ktrprocexec(p, &tracecred, &tracevp);
#endif
		/*
		 * Close any file descriptors 0..2 that reference procfs,
		 * then make sure file descriptors 0..2 are in use.
		 *
		 * Both fdsetugidsafety() and fdcheckstd() may call functions
		 * taking sleepable locks, so temporarily drop our locks.
		 */
		PROC_UNLOCK(p);
		VOP_UNLOCK(imgp->vp, 0);
		fdsetugidsafety(td);
		error = fdcheckstd(td);
		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
		if (error != 0)
			goto exec_fail_dealloc;
		PROC_LOCK(p);
#ifdef MAC
		if (will_transition) {
			mac_vnode_execve_transition(oldcred, imgp->newcred,
			    imgp->vp, interpvplabel, imgp);
		}
#endif
	} else {
		if (oldcred->cr_uid == oldcred->cr_ruid &&
		    oldcred->cr_gid == oldcred->cr_rgid)
			p->p_flag &= ~P_SUGID;
	}
	/*
	 * Set the new credentials.
	 */
	if (imgp->newcred != NULL) {
		proc_set_cred(p, imgp->newcred);
		crfree(oldcred);
		oldcred = NULL;
	}

	/*
	 * Store the vp for use in procfs.  This vnode was referenced by namei
	 * or fgetvp_exec.
	 */
	oldtextvp = p->p_textvp;
	p->p_textvp = newtextvp;

#ifdef KDTRACE_HOOKS
	/*
	 * Tell the DTrace fasttrap provider about the exec if it
	 * has declared an interest.
	 */
	if (dtrace_fasttrap_exec)
		dtrace_fasttrap_exec(p);
#endif

	/*
	 * Notify others that we exec'd, and clear the P_INEXEC flag
	 * as we're now a bona fide freshly-execed process.
	 */
	KNOTE_LOCKED(p->p_klist, NOTE_EXEC);
	p->p_flag &= ~P_INEXEC;

	/* clear "fork but no exec" flag, as we _are_ execing */
	p->p_acflag &= ~AFORK;

	/*
	 * Free any previous argument cache and replace it with
	 * the new argument cache, if any.
	 */
	oldargs = p->p_args;
	p->p_args = newargs;
	newargs = NULL;

	PROC_UNLOCK(p);

#ifdef	HWPMC_HOOKS
	/*
	 * Check if system-wide sampling is in effect or if the
	 * current process is using PMCs.  If so, do exec() time
	 * processing.  This processing needs to happen AFTER the
	 * P_INEXEC flag is cleared.
	 */
	if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
		VOP_UNLOCK(imgp->vp, 0);
		pe.pm_credentialschanged = credential_changing;
		pe.pm_entryaddr = imgp->entry_addr;

		PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
	}
#endif

	/* Set values passed into the program in registers. */
	(*p->p_sysent->sv_setregs)(td, imgp, (u_long)(uintptr_t)stack_base);

	vfs_mark_atime(imgp->vp, td->td_ucred);

	SDT_PROBE1(proc, , , exec__success, args->fname);

exec_fail_dealloc:
	if (imgp->firstpage != NULL)
		exec_unmap_first_page(imgp);

	if (imgp->vp != NULL) {
		if (args->fname)
			NDFREE(&nd, NDF_ONLY_PNBUF);
		if (imgp->opened)
			VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td);
		if (error != 0)
			vput(imgp->vp);
		else
			VOP_UNLOCK(imgp->vp, 0);
	}

	if (imgp->object != NULL)
		vm_object_deallocate(imgp->object);

	free(imgp->freepath, M_TEMP);

	if (error == 0) {
		if (p->p_ptevents & PTRACE_EXEC) {
			PROC_LOCK(p);
			if (p->p_ptevents & PTRACE_EXEC)
				td->td_dbgflags |= TDB_EXEC;
			PROC_UNLOCK(p);
		}

		/*
		 * Stop the process here if its stop event mask has
		 * the S_EXEC bit set.
		 */
		STOPEVENT(p, S_EXEC, 0);
	} else {
exec_fail:
		/* we're done here, clear P_INEXEC */
		PROC_LOCK(p);
		p->p_flag &= ~P_INEXEC;
		PROC_UNLOCK(p);

		SDT_PROBE1(proc, , , exec__failure, error);
	}

	if (imgp->newcred != NULL && oldcred != NULL)
		crfree(imgp->newcred);

#ifdef MAC
	mac_execve_exit(imgp);
	mac_execve_interpreter_exit(interpvplabel);
#endif
	exec_free_args(args);

	/*
	 * Handle deferred decrement of ref counts.
	 */
	if (oldtextvp != NULL)
		vrele(oldtextvp);
#ifdef KTRACE
	if (tracevp != NULL)
		vrele(tracevp);
	if (tracecred != NULL)
		crfree(tracecred);
#endif
	pargs_drop(oldargs);
	pargs_drop(newargs);
	if (oldsigacts != NULL)
		sigacts_free(oldsigacts);
	if (euip != NULL)
		uifree(euip);

	if (error && imgp->vmspace_destroyed) {
		/* sorry, no more process anymore. exit gracefully */
		exit1(td, 0, SIGABRT);
		/* NOT REACHED */
	}

#ifdef KTRACE
	if (error == 0)
		ktrprocctor(p);
#endif

	/*
	 * We don't want cpu_set_syscall_retval() to overwrite any of
	 * the register values put in place by exec_setregs().
	 * Implementations of cpu_set_syscall_retval() will leave
	 * registers unmodified when returning EJUSTRETURN.
	 */
	return (error == 0 ? EJUSTRETURN : error);
}
Ejemplo n.º 7
0
/* ARGSUSED */
int
sys_auditon(struct thread *td, struct auditon_args *uap)
{
    struct ucred *cred, *newcred, *oldcred;
    int error;
    union auditon_udata udata;
    struct proc *tp;

    if (jailed(td->td_ucred))
        return (ENOSYS);
    AUDIT_ARG_CMD(uap->cmd);

#ifdef MAC
    error = mac_system_check_auditon(td->td_ucred, uap->cmd);
    if (error)
        return (error);
#endif

    error = priv_check(td, PRIV_AUDIT_CONTROL);
    if (error)
        return (error);

    if ((uap->length <= 0) || (uap->length > sizeof(union auditon_udata)))
        return (EINVAL);

    memset((void *)&udata, 0, sizeof(udata));

    /*
     * Some of the GET commands use the arguments too.
     */
    switch (uap->cmd) {
    case A_SETPOLICY:
    case A_OLDSETPOLICY:
    case A_SETKMASK:
    case A_SETQCTRL:
    case A_OLDSETQCTRL:
    case A_SETSTAT:
    case A_SETUMASK:
    case A_SETSMASK:
    case A_SETCOND:
    case A_OLDSETCOND:
    case A_SETCLASS:
    case A_SETPMASK:
    case A_SETFSIZE:
    case A_SETKAUDIT:
    case A_GETCLASS:
    case A_GETPINFO:
    case A_GETPINFO_ADDR:
    case A_SENDTRIGGER:
        error = copyin(uap->data, (void *)&udata, uap->length);
        if (error)
            return (error);
        AUDIT_ARG_AUDITON(&udata);
        break;
    }

    /*
     * XXXAUDIT: Locking?
     */
    switch (uap->cmd) {
    case A_OLDGETPOLICY:
    case A_GETPOLICY:
        if (uap->length == sizeof(udata.au_policy64)) {
            if (!audit_fail_stop)
                udata.au_policy64 |= AUDIT_CNT;
            if (audit_panic_on_write_fail)
                udata.au_policy64 |= AUDIT_AHLT;
            if (audit_argv)
                udata.au_policy64 |= AUDIT_ARGV;
            if (audit_arge)
                udata.au_policy64 |= AUDIT_ARGE;
            break;
        }
        if (uap->length != sizeof(udata.au_policy))
            return (EINVAL);
        if (!audit_fail_stop)
            udata.au_policy |= AUDIT_CNT;
        if (audit_panic_on_write_fail)
            udata.au_policy |= AUDIT_AHLT;
        if (audit_argv)
            udata.au_policy |= AUDIT_ARGV;
        if (audit_arge)
            udata.au_policy |= AUDIT_ARGE;
        break;

    case A_OLDSETPOLICY:
    case A_SETPOLICY:
        if (uap->length == sizeof(udata.au_policy64)) {
            if (udata.au_policy & (~AUDIT_CNT|AUDIT_AHLT|
                                   AUDIT_ARGV|AUDIT_ARGE))
                return (EINVAL);
            audit_fail_stop = ((udata.au_policy64 & AUDIT_CNT) ==
                               0);
            audit_panic_on_write_fail = (udata.au_policy64 &
                                         AUDIT_AHLT);
            audit_argv = (udata.au_policy64 & AUDIT_ARGV);
            audit_arge = (udata.au_policy64 & AUDIT_ARGE);
            break;
        }
        if (uap->length != sizeof(udata.au_policy))
            return (EINVAL);
        if (udata.au_policy & ~(AUDIT_CNT|AUDIT_AHLT|AUDIT_ARGV|
                                AUDIT_ARGE))
            return (EINVAL);
        /*
         * XXX - Need to wake up waiters if the policy relaxes?
         */
        audit_fail_stop = ((udata.au_policy & AUDIT_CNT) == 0);
        audit_panic_on_write_fail = (udata.au_policy & AUDIT_AHLT);
        audit_argv = (udata.au_policy & AUDIT_ARGV);
        audit_arge = (udata.au_policy & AUDIT_ARGE);
        break;

    case A_GETKMASK:
        if (uap->length != sizeof(udata.au_mask))
            return (EINVAL);
        udata.au_mask = audit_nae_mask;
        break;

    case A_SETKMASK:
        if (uap->length != sizeof(udata.au_mask))
            return (EINVAL);
        audit_nae_mask = udata.au_mask;
        break;

    case A_OLDGETQCTRL:
    case A_GETQCTRL:
        if (uap->length == sizeof(udata.au_qctrl64)) {
            udata.au_qctrl64.aq64_hiwater =
                (u_int64_t)audit_qctrl.aq_hiwater;
            udata.au_qctrl64.aq64_lowater =
                (u_int64_t)audit_qctrl.aq_lowater;
            udata.au_qctrl64.aq64_bufsz =
                (u_int64_t)audit_qctrl.aq_bufsz;
            udata.au_qctrl64.aq64_minfree =
                (u_int64_t)audit_qctrl.aq_minfree;
            break;
        }
        if (uap->length != sizeof(udata.au_qctrl))
            return (EINVAL);
        udata.au_qctrl = audit_qctrl;
        break;

    case A_OLDSETQCTRL:
    case A_SETQCTRL:
        if (uap->length == sizeof(udata.au_qctrl64)) {
            if ((udata.au_qctrl64.aq64_hiwater > AQ_MAXHIGH) ||
                    (udata.au_qctrl64.aq64_lowater >=
                     udata.au_qctrl.aq_hiwater) ||
                    (udata.au_qctrl64.aq64_bufsz > AQ_MAXBUFSZ) ||
                    (udata.au_qctrl64.aq64_minfree < 0) ||
                    (udata.au_qctrl64.aq64_minfree > 100))
                return (EINVAL);
            audit_qctrl.aq_hiwater =
                (int)udata.au_qctrl64.aq64_hiwater;
            audit_qctrl.aq_lowater =
                (int)udata.au_qctrl64.aq64_lowater;
            audit_qctrl.aq_bufsz =
                (int)udata.au_qctrl64.aq64_bufsz;
            audit_qctrl.aq_minfree =
                (int)udata.au_qctrl64.aq64_minfree;
            audit_qctrl.aq_delay = -1;	/* Not used. */
            break;
        }
        if (uap->length != sizeof(udata.au_qctrl))
            return (EINVAL);
        if ((udata.au_qctrl.aq_hiwater > AQ_MAXHIGH) ||
                (udata.au_qctrl.aq_lowater >= udata.au_qctrl.aq_hiwater) ||
                (udata.au_qctrl.aq_bufsz > AQ_MAXBUFSZ) ||
                (udata.au_qctrl.aq_minfree < 0) ||
                (udata.au_qctrl.aq_minfree > 100))
            return (EINVAL);

        audit_qctrl = udata.au_qctrl;
        /* XXX The queue delay value isn't used with the kernel. */
        audit_qctrl.aq_delay = -1;
        break;

    case A_GETCWD:
        return (ENOSYS);
        break;

    case A_GETCAR:
        return (ENOSYS);
        break;

    case A_GETSTAT:
        return (ENOSYS);
        break;

    case A_SETSTAT:
        return (ENOSYS);
        break;

    case A_SETUMASK:
        return (ENOSYS);
        break;

    case A_SETSMASK:
        return (ENOSYS);
        break;

    case A_OLDGETCOND:
    case A_GETCOND:
        if (uap->length == sizeof(udata.au_cond64)) {
            if (audit_enabled && !audit_suspended)
                udata.au_cond64 = AUC_AUDITING;
            else
                udata.au_cond64 = AUC_NOAUDIT;
            break;
        }
        if (uap->length != sizeof(udata.au_cond))
            return (EINVAL);
        if (audit_enabled && !audit_suspended)
            udata.au_cond = AUC_AUDITING;
        else
            udata.au_cond = AUC_NOAUDIT;
        break;

    case A_OLDSETCOND:
    case A_SETCOND:
        if (uap->length == sizeof(udata.au_cond64)) {
            if (udata.au_cond64 == AUC_NOAUDIT)
                audit_suspended = 1;
            if (udata.au_cond64 == AUC_AUDITING)
                audit_suspended = 0;
            if (udata.au_cond64 == AUC_DISABLED) {
                audit_suspended = 1;
                audit_shutdown(NULL, 0);
            }
            break;
        }
        if (uap->length != sizeof(udata.au_cond))
            return (EINVAL);
        if (udata.au_cond == AUC_NOAUDIT)
            audit_suspended = 1;
        if (udata.au_cond == AUC_AUDITING)
            audit_suspended = 0;
        if (udata.au_cond == AUC_DISABLED) {
            audit_suspended = 1;
            audit_shutdown(NULL, 0);
        }
        break;

    case A_GETCLASS:
        if (uap->length != sizeof(udata.au_evclass))
            return (EINVAL);
        udata.au_evclass.ec_class = au_event_class(
                                        udata.au_evclass.ec_number);
        break;

    case A_SETCLASS:
        if (uap->length != sizeof(udata.au_evclass))
            return (EINVAL);
        au_evclassmap_insert(udata.au_evclass.ec_number,
                             udata.au_evclass.ec_class);
        break;

    case A_GETPINFO:
        if (uap->length != sizeof(udata.au_aupinfo))
            return (EINVAL);
        if (udata.au_aupinfo.ap_pid < 1)
            return (ESRCH);
        if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL)
            return (ESRCH);
        if ((error = p_cansee(td, tp)) != 0) {
            PROC_UNLOCK(tp);
            return (error);
        }
        cred = tp->p_ucred;
        if (cred->cr_audit.ai_termid.at_type == AU_IPv6) {
            PROC_UNLOCK(tp);
            return (EINVAL);
        }
        udata.au_aupinfo.ap_auid = cred->cr_audit.ai_auid;
        udata.au_aupinfo.ap_mask.am_success =
            cred->cr_audit.ai_mask.am_success;
        udata.au_aupinfo.ap_mask.am_failure =
            cred->cr_audit.ai_mask.am_failure;
        udata.au_aupinfo.ap_termid.machine =
            cred->cr_audit.ai_termid.at_addr[0];
        udata.au_aupinfo.ap_termid.port =
            (dev_t)cred->cr_audit.ai_termid.at_port;
        udata.au_aupinfo.ap_asid = cred->cr_audit.ai_asid;
        PROC_UNLOCK(tp);
        break;

    case A_SETPMASK:
        if (uap->length != sizeof(udata.au_aupinfo))
            return (EINVAL);
        if (udata.au_aupinfo.ap_pid < 1)
            return (ESRCH);
        newcred = crget();
        if ((tp = pfind(udata.au_aupinfo.ap_pid)) == NULL) {
            crfree(newcred);
            return (ESRCH);
        }
        if ((error = p_cansee(td, tp)) != 0) {
            PROC_UNLOCK(tp);
            crfree(newcred);
            return (error);
        }
        oldcred = tp->p_ucred;
        crcopy(newcred, oldcred);
        newcred->cr_audit.ai_mask.am_success =
            udata.au_aupinfo.ap_mask.am_success;
        newcred->cr_audit.ai_mask.am_failure =
            udata.au_aupinfo.ap_mask.am_failure;
        proc_set_cred(tp, newcred);
        PROC_UNLOCK(tp);
        crfree(oldcred);
        break;

    case A_SETFSIZE:
        if (uap->length != sizeof(udata.au_fstat))
            return (EINVAL);
        if ((udata.au_fstat.af_filesz != 0) &&
                (udata.au_fstat.af_filesz < MIN_AUDIT_FILE_SIZE))
            return (EINVAL);
        audit_fstat.af_filesz = udata.au_fstat.af_filesz;
        break;

    case A_GETFSIZE:
        if (uap->length != sizeof(udata.au_fstat))
            return (EINVAL);
        udata.au_fstat.af_filesz = audit_fstat.af_filesz;
        udata.au_fstat.af_currsz = audit_fstat.af_currsz;
        break;

    case A_GETPINFO_ADDR:
        if (uap->length != sizeof(udata.au_aupinfo_addr))
            return (EINVAL);
        if (udata.au_aupinfo_addr.ap_pid < 1)
            return (ESRCH);
        if ((tp = pfind(udata.au_aupinfo_addr.ap_pid)) == NULL)
            return (ESRCH);
        cred = tp->p_ucred;
        udata.au_aupinfo_addr.ap_auid = cred->cr_audit.ai_auid;
        udata.au_aupinfo_addr.ap_mask.am_success =
            cred->cr_audit.ai_mask.am_success;
        udata.au_aupinfo_addr.ap_mask.am_failure =
            cred->cr_audit.ai_mask.am_failure;
        udata.au_aupinfo_addr.ap_termid = cred->cr_audit.ai_termid;
        udata.au_aupinfo_addr.ap_asid = cred->cr_audit.ai_asid;
        PROC_UNLOCK(tp);
        break;

    case A_GETKAUDIT:
        if (uap->length != sizeof(udata.au_kau_info))
            return (EINVAL);
        audit_get_kinfo(&udata.au_kau_info);
        break;

    case A_SETKAUDIT:
        if (uap->length != sizeof(udata.au_kau_info))
            return (EINVAL);
        if (udata.au_kau_info.ai_termid.at_type != AU_IPv4 &&
                udata.au_kau_info.ai_termid.at_type != AU_IPv6)
            return (EINVAL);
        audit_set_kinfo(&udata.au_kau_info);
        break;

    case A_SENDTRIGGER:
        if (uap->length != sizeof(udata.au_trigger))
            return (EINVAL);
        if ((udata.au_trigger < AUDIT_TRIGGER_MIN) ||
                (udata.au_trigger > AUDIT_TRIGGER_MAX))
            return (EINVAL);
        return (audit_send_trigger(udata.au_trigger));

    default:
        return (EINVAL);
    }

    /*
     * Copy data back to userspace for the GET comands.
     */
    switch (uap->cmd) {
    case A_GETPOLICY:
    case A_OLDGETPOLICY:
    case A_GETKMASK:
    case A_GETQCTRL:
    case A_OLDGETQCTRL:
    case A_GETCWD:
    case A_GETCAR:
    case A_GETSTAT:
    case A_GETCOND:
    case A_OLDGETCOND:
    case A_GETCLASS:
    case A_GETPINFO:
    case A_GETFSIZE:
    case A_GETPINFO_ADDR:
    case A_GETKAUDIT:
        error = copyout((void *)&udata, uap->data, uap->length);
        if (error)
            return (error);
        break;
    }

    return (0);
}