/* $NetBSD: kern_prot.c,v 1.122 2020/05/23 23:42:43 ad Exp $ */
/*
* Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_prot.c 8.9 (Berkeley) 2/14/95
*/
/*
* System calls related to processes and protection
*/
/* Get process group ID; note that POSIX getpgrp takes no parameter */
int
sys_getpgrp(struct lwp *l, const void *v, register_t *retval)
{
struct proc *p = l->l_proc;
/*
* Return the process group ID of the session leader (session ID)
* for the specified process.
*/
int
sys_getsid(struct lwp *l, const struct sys_getsid_args *uap, register_t *retval)
{
/* {
syscalldarg(pid_t) pid;
} */
pid_t pid = SCARG(uap, pid);
struct proc *p;
int error = 0;
/*
* Get effective group ID. The "egid" is groups[0], and could be obtained
* via getgroups. This syscall exists because it is somewhat painful to do
* correctly in a library function.
*/
/* ARGSUSED */
int
sys_getegid(struct lwp *l, const void *v, register_t *retval)
{
/*
* set process group (setpgid/old setpgrp)
*
* caller does setpgid(targpid, targpgid)
*
* pgid must be in valid range (EINVAL)
* pid must be caller or child of caller (ESRCH)
* if a child
* pid must be in same session (EPERM)
* pid can't have done an exec (EACCES)
* if pgid != pid
* there must exist some pid in same session having pgid (EPERM)
* pid must not be session leader (EPERM)
*
* Permission checks now in proc_enterpgrp()
*/
int
sys_setpgid(struct lwp *l, const struct sys_setpgid_args *uap,
register_t *retval)
{
/* {
syscallarg(int) pid;
syscallarg(int) pgid;
} */
struct proc *p = l->l_proc;
pid_t targp, pgid;
if (SCARG(uap, pgid) < 0)
return EINVAL;
if ((targp = SCARG(uap, pid)) == 0)
targp = p->p_pid;
if ((pgid = SCARG(uap, pgid)) == 0)
pgid = targp;
return proc_enterpgrp(p, targp, pgid, false);
}
/*
* Set real, effective and saved uids to the requested values.
* non-root callers can only ever change uids to values that match
* one of the processes current uid values.
* This is further restricted by the flags argument.
*/
/* If nothing has changed, short circuit the request */
if ((r == -1 || r == kauth_cred_getuid(cred))
&& (e == -1 || e == kauth_cred_geteuid(cred))
&& (sv == -1 || sv == kauth_cred_getsvuid(cred))) {
proc_crmod_leave(cred, ncred, false);
return 0;
}
kauth_cred_clone(cred, ncred);
if (r != -1 && r != kauth_cred_getuid(ncred)) {
u_long nlwps;
/* Update count of processes for this user. */
(void)chgproccnt(kauth_cred_getuid(ncred), -1);
(void)chgproccnt(r, 1);
/* The first LWP of a process is excluded. */
KASSERT(mutex_owned(p->p_lock));
nlwps = p->p_nlwps - 1;
(void)chglwpcnt(kauth_cred_getuid(ncred), -nlwps);
(void)chglwpcnt(r, nlwps);
kauth_cred_setuid(ncred, r);
}
if (sv != -1)
kauth_cred_setsvuid(ncred, sv);
if (e != -1)
kauth_cred_seteuid(ncred, e);
/* Broadcast our credentials to the process and other LWPs. */
proc_crmod_leave(ncred, cred, true);
return 0;
}
/*
* Set real, effective and saved gids to the requested values.
* non-root callers can only ever change gids to values that match
* one of the processes current gid values.
* This is further restricted by the flags argument.
*/
/*
* Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
* we use PK_SUGID because we consider changing the owners as
* "tainting" as well.
* This is significant for procs that start as root and "become"
* a user without an exec - programs cannot know *everything*
* that libc *might* have put in their data segment.
*/
*retval = (p->p_flag & PK_SUGID) != 0;
return (0);
}