Home | History | Annotate | Line # | Download | only in kern
kern_prot.c revision 1.16
      1 /*	$NetBSD: kern_prot.c,v 1.16 1994/06/29 06:32:38 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)kern_prot.c	8.6 (Berkeley) 1/21/94
     41  */
     42 
     43 /*
     44  * System calls related to processes and protection
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/acct.h>
     49 #include <sys/systm.h>
     50 #include <sys/ucred.h>
     51 #include <sys/proc.h>
     52 #include <sys/timeb.h>
     53 #include <sys/times.h>
     54 #include <sys/malloc.h>
     55 
     56 /* ARGSUSED */
     57 getpid(p, uap, retval)
     58 	struct proc *p;
     59 	void *uap;
     60 	int *retval;
     61 {
     62 
     63 	*retval = p->p_pid;
     64 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
     65 	retval[1] = p->p_pptr->p_pid;
     66 #endif
     67 	return (0);
     68 }
     69 
     70 /* ARGSUSED */
     71 getppid(p, uap, retval)
     72 	struct proc *p;
     73 	void *uap;
     74 	int *retval;
     75 {
     76 
     77 	*retval = p->p_pptr->p_pid;
     78 	return (0);
     79 }
     80 
     81 /* Get process group ID; note that POSIX getpgrp takes no parameter */
     82 getpgrp(p, uap, retval)
     83 	struct proc *p;
     84 	void *uap;
     85 	int *retval;
     86 {
     87 
     88 	*retval = p->p_pgrp->pg_id;
     89 	return (0);
     90 }
     91 
     92 /* ARGSUSED */
     93 getuid(p, uap, retval)
     94 	struct proc *p;
     95 	void *uap;
     96 	int *retval;
     97 {
     98 
     99 	*retval = p->p_cred->p_ruid;
    100 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    101 	retval[1] = p->p_ucred->cr_uid;
    102 #endif
    103 	return (0);
    104 }
    105 
    106 /* ARGSUSED */
    107 geteuid(p, uap, retval)
    108 	struct proc *p;
    109 	void *uap;
    110 	int *retval;
    111 {
    112 
    113 	*retval = p->p_ucred->cr_uid;
    114 	return (0);
    115 }
    116 
    117 /* ARGSUSED */
    118 getgid(p, uap, retval)
    119 	struct proc *p;
    120 	void *uap;
    121 	int *retval;
    122 {
    123 
    124 	*retval = p->p_cred->p_rgid;
    125 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    126 	retval[1] = p->p_ucred->cr_groups[0];
    127 #endif
    128 	return (0);
    129 }
    130 
    131 /*
    132  * Get effective group ID.  The "egid" is groups[0], and could be obtained
    133  * via getgroups.  This syscall exists because it is somewhat painful to do
    134  * correctly in a library function.
    135  */
    136 /* ARGSUSED */
    137 getegid(p, uap, retval)
    138 	struct proc *p;
    139 	void *uap;
    140 	int *retval;
    141 {
    142 
    143 	*retval = p->p_ucred->cr_groups[0];
    144 	return (0);
    145 }
    146 
    147 struct getgroups_args {
    148 	u_int	gidsetsize;
    149 	gid_t	*gidset;
    150 };
    151 getgroups(p, uap, retval)
    152 	struct proc *p;
    153 	register struct	getgroups_args *uap;
    154 	int *retval;
    155 {
    156 	register struct pcred *pc = p->p_cred;
    157 	register u_int ngrp;
    158 	int error;
    159 
    160 	if ((ngrp = uap->gidsetsize) == 0) {
    161 		*retval = pc->pc_ucred->cr_ngroups;
    162 		return (0);
    163 	}
    164 	if (ngrp < pc->pc_ucred->cr_ngroups)
    165 		return (EINVAL);
    166 	ngrp = pc->pc_ucred->cr_ngroups;
    167 	if (error = copyout((caddr_t)pc->pc_ucred->cr_groups,
    168 	    (caddr_t)uap->gidset, ngrp * sizeof(gid_t)))
    169 		return (error);
    170 	*retval = ngrp;
    171 	return (0);
    172 }
    173 
    174 /* ARGSUSED */
    175 setsid(p, uap, retval)
    176 	register struct proc *p;
    177 	void *uap;
    178 	int *retval;
    179 {
    180 
    181 	if (p->p_pgid == p->p_pid || pgfind(p->p_pid)) {
    182 		return (EPERM);
    183 	} else {
    184 		(void)enterpgrp(p, p->p_pid, 1);
    185 		*retval = p->p_pid;
    186 		return (0);
    187 	}
    188 }
    189 
    190 /*
    191  * set process group (setpgid/old setpgrp)
    192  *
    193  * caller does setpgid(targpid, targpgid)
    194  *
    195  * pid must be caller or child of caller (ESRCH)
    196  * if a child
    197  *	pid must be in same session (EPERM)
    198  *	pid can't have done an exec (EACCES)
    199  * if pgid != pid
    200  * 	there must exist some pid in same session having pgid (EPERM)
    201  * pid must not be session leader (EPERM)
    202  */
    203 struct setpgid_args {
    204 	int	pid;	/* target process id */
    205 	int	pgid;	/* target pgrp id */
    206 };
    207 /* ARGSUSED */
    208 setpgid(curp, uap, retval)
    209 	struct proc *curp;
    210 	register struct setpgid_args *uap;
    211 	int *retval;
    212 {
    213 	register struct proc *targp;		/* target process */
    214 	register struct pgrp *pgrp;		/* target pgrp */
    215 
    216 #ifdef COMPAT_09
    217 	uap->pid  = (short) uap->pid;		/* XXX */
    218 	uap->pgid = (short) uap->pgid;		/* XXX */
    219 #endif
    220 
    221 	if (uap->pid != 0 && uap->pid != curp->p_pid) {
    222 		if ((targp = pfind(uap->pid)) == 0 || !inferior(targp))
    223 			return (ESRCH);
    224 		if (targp->p_session != curp->p_session)
    225 			return (EPERM);
    226 		if (targp->p_flag & P_EXEC)
    227 			return (EACCES);
    228 	} else
    229 		targp = curp;
    230 	if (SESS_LEADER(targp))
    231 		return (EPERM);
    232 	if (uap->pgid == 0)
    233 		uap->pgid = targp->p_pid;
    234 	else if (uap->pgid != targp->p_pid)
    235 		if ((pgrp = pgfind(uap->pgid)) == 0 ||
    236 	            pgrp->pg_session != curp->p_session)
    237 			return (EPERM);
    238 	return (enterpgrp(targp, uap->pgid, 0));
    239 }
    240 
    241 struct setuid_args {
    242 	uid_t	uid;
    243 };
    244 /* ARGSUSED */
    245 setuid(p, uap, retval)
    246 	struct proc *p;
    247 	struct setuid_args *uap;
    248 	int *retval;
    249 {
    250 	register struct pcred *pc = p->p_cred;
    251 	register uid_t uid;
    252 	int error;
    253 
    254 #ifdef COMPAT_09				/* XXX */
    255 	uid = (u_short)uap->uid;
    256 #else
    257 	uid = uap->uid;
    258 #endif
    259 	if (uid != pc->p_ruid &&
    260 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    261 		return (error);
    262 	/*
    263 	 * Everything's okay, do it.
    264 	 * Transfer proc count to new user.
    265 	 * Copy credentials so other references do not see our changes.
    266 	 */
    267 	(void)chgproccnt(pc->p_ruid, -1);
    268 	(void)chgproccnt(uid, 1);
    269 	pc->pc_ucred = crcopy(pc->pc_ucred);
    270 	pc->pc_ucred->cr_uid = uid;
    271 	pc->p_ruid = uid;
    272 	pc->p_svuid = uid;
    273 	p->p_flag |= P_SUGID;
    274 	return (0);
    275 }
    276 
    277 struct seteuid_args {
    278 	uid_t	euid;
    279 };
    280 /* ARGSUSED */
    281 seteuid(p, uap, retval)
    282 	struct proc *p;
    283 	struct seteuid_args *uap;
    284 	int *retval;
    285 {
    286 	register struct pcred *pc = p->p_cred;
    287 	register uid_t euid;
    288 	int error;
    289 
    290 #ifdef COMPAT_09				/* XXX */
    291 	euid = (u_short)uap->euid;
    292 #else
    293 	euid = uap->euid;
    294 #endif
    295 	if (euid != pc->p_ruid && euid != pc->p_svuid &&
    296 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    297 		return (error);
    298 	/*
    299 	 * Everything's okay, do it.  Copy credentials so other references do
    300 	 * not see our changes.
    301 	 */
    302 	pc->pc_ucred = crcopy(pc->pc_ucred);
    303 	pc->pc_ucred->cr_uid = euid;
    304 	p->p_flag |= P_SUGID;
    305 	return (0);
    306 }
    307 
    308 struct setgid_args {
    309 	gid_t	gid;
    310 };
    311 /* ARGSUSED */
    312 setgid(p, uap, retval)
    313 	struct proc *p;
    314 	struct setgid_args *uap;
    315 	int *retval;
    316 {
    317 	register struct pcred *pc = p->p_cred;
    318 	register gid_t gid;
    319 	int error;
    320 
    321 #ifdef COMPAT_09				/* XXX */
    322 	gid = (u_short)uap->gid;
    323 #else
    324 	gid = uap->gid;
    325 #endif
    326 	if (gid != pc->p_rgid && (error = suser(pc->pc_ucred, &p->p_acflag)))
    327 		return (error);
    328 	pc->pc_ucred = crcopy(pc->pc_ucred);
    329 	pc->pc_ucred->cr_groups[0] = gid;
    330 	pc->p_rgid = gid;
    331 	pc->p_svgid = gid;		/* ??? */
    332 	p->p_flag |= P_SUGID;
    333 	return (0);
    334 }
    335 
    336 struct setegid_args {
    337 	gid_t	egid;
    338 };
    339 /* ARGSUSED */
    340 setegid(p, uap, retval)
    341 	struct proc *p;
    342 	struct setegid_args *uap;
    343 	int *retval;
    344 {
    345 	register struct pcred *pc = p->p_cred;
    346 	register gid_t egid;
    347 	int error;
    348 
    349 #ifdef COMPAT_09				/* XXX */
    350 	egid = (u_short)uap->egid;
    351 #else
    352 	egid = uap->egid;
    353 #endif
    354 	if (egid != pc->p_rgid && egid != pc->p_svgid &&
    355 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
    356 		return (error);
    357 	pc->pc_ucred = crcopy(pc->pc_ucred);
    358 	pc->pc_ucred->cr_groups[0] = egid;
    359 	p->p_flag |= P_SUGID;
    360 	return (0);
    361 }
    362 
    363 struct setgroups_args {
    364 	u_int	gidsetsize;
    365 	gid_t	*gidset;
    366 };
    367 /* ARGSUSED */
    368 setgroups(p, uap, retval)
    369 	struct proc *p;
    370 	struct setgroups_args *uap;
    371 	int *retval;
    372 {
    373 	register struct pcred *pc = p->p_cred;
    374 	register u_int ngrp;
    375 	int error;
    376 
    377 	if (error = suser(pc->pc_ucred, &p->p_acflag))
    378 		return (error);
    379 	if ((ngrp = uap->gidsetsize) > NGROUPS)
    380 		return (EINVAL);
    381 	pc->pc_ucred = crcopy(pc->pc_ucred);
    382 	if (error = copyin((caddr_t)uap->gidset,
    383 	    (caddr_t)pc->pc_ucred->cr_groups, ngrp * sizeof(gid_t)))
    384 		return (error);
    385 	pc->pc_ucred->cr_ngroups = ngrp;
    386 	p->p_flag |= P_SUGID;
    387 	return (0);
    388 }
    389 
    390 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    391 struct setreuid_args {
    392 	int	ruid;
    393 	int	euid;
    394 };
    395 /* ARGSUSED */
    396 osetreuid(p, uap, retval)
    397 	register struct proc *p;
    398 	struct setreuid_args *uap;
    399 	int *retval;
    400 {
    401 	register struct pcred *pc = p->p_cred;
    402 	struct seteuid_args args;
    403 
    404 	/*
    405 	 * we assume that the intent of setting ruid is to be able to get
    406 	 * back ruid priviledge. So we make sure that we will be able to
    407 	 * do so, but do not actually set the ruid.
    408 	 */
    409 	if (uap->ruid != (uid_t)-1 && uap->ruid != pc->p_ruid &&
    410 	    uap->ruid != pc->p_svuid)
    411 		return (EPERM);
    412 	if (uap->euid == (uid_t)-1)
    413 		return (0);
    414 	args.euid = uap->euid;
    415 	return (seteuid(p, &args, retval));
    416 }
    417 
    418 struct setregid_args {
    419 	int	rgid;
    420 	int	egid;
    421 };
    422 /* ARGSUSED */
    423 osetregid(p, uap, retval)
    424 	register struct proc *p;
    425 	struct setregid_args *uap;
    426 	int *retval;
    427 {
    428 	register struct pcred *pc = p->p_cred;
    429 	struct setegid_args args;
    430 
    431 	/*
    432 	 * we assume that the intent of setting rgid is to be able to get
    433 	 * back rgid priviledge. So we make sure that we will be able to
    434 	 * do so, but do not actually set the rgid.
    435 	 */
    436 	if (uap->rgid != (gid_t)-1 && uap->rgid != pc->p_rgid &&
    437 	    uap->rgid != pc->p_svgid)
    438 		return (EPERM);
    439 	if (uap->egid == (gid_t)-1)
    440 		return (0);
    441 	args.egid = uap->egid;
    442 	return (setegid(p, &args, retval));
    443 }
    444 #endif /* defined(COMPAT_43) || defined(COMPAT_SUNOS) */
    445 
    446 /*
    447  * Check if gid is a member of the group set.
    448  */
    449 groupmember(gid, cred)
    450 	gid_t gid;
    451 	register struct ucred *cred;
    452 {
    453 	register gid_t *gp;
    454 	gid_t *egp;
    455 
    456 	egp = &(cred->cr_groups[cred->cr_ngroups]);
    457 	for (gp = cred->cr_groups; gp < egp; gp++)
    458 		if (*gp == gid)
    459 			return (1);
    460 	return (0);
    461 }
    462 
    463 /*
    464  * Test whether the specified credentials imply "super-user"
    465  * privilege; if so, and we have accounting info, set the flag
    466  * indicating use of super-powers.
    467  * Returns 0 or error.
    468  */
    469 suser(cred, acflag)
    470 	struct ucred *cred;
    471 	u_short *acflag;
    472 {
    473 	if (cred->cr_uid == 0) {
    474 		if (acflag)
    475 			*acflag |= ASU;
    476 		return (0);
    477 	}
    478 	return (EPERM);
    479 }
    480 
    481 /*
    482  * Allocate a zeroed cred structure.
    483  */
    484 struct ucred *
    485 crget()
    486 {
    487 	register struct ucred *cr;
    488 
    489 	MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK);
    490 	bzero((caddr_t)cr, sizeof(*cr));
    491 	cr->cr_ref = 1;
    492 	return (cr);
    493 }
    494 
    495 /*
    496  * Free a cred structure.
    497  * Throws away space when ref count gets to 0.
    498  */
    499 crfree(cr)
    500 	struct ucred *cr;
    501 {
    502 	int s;
    503 
    504 	s = splimp();				/* ??? */
    505 	if (--cr->cr_ref == 0)
    506 		FREE((caddr_t)cr, M_CRED);
    507 	(void) splx(s);
    508 }
    509 
    510 /*
    511  * Copy cred structure to a new one and free the old one.
    512  */
    513 struct ucred *
    514 crcopy(cr)
    515 	struct ucred *cr;
    516 {
    517 	struct ucred *newcr;
    518 
    519 	if (cr->cr_ref == 1)
    520 		return (cr);
    521 	newcr = crget();
    522 	*newcr = *cr;
    523 	crfree(cr);
    524 	newcr->cr_ref = 1;
    525 	return (newcr);
    526 }
    527 
    528 /*
    529  * Dup cred struct to a new held one.
    530  */
    531 struct ucred *
    532 crdup(cr)
    533 	struct ucred *cr;
    534 {
    535 	struct ucred *newcr;
    536 
    537 	newcr = crget();
    538 	*newcr = *cr;
    539 	newcr->cr_ref = 1;
    540 	return (newcr);
    541 }
    542 
    543 /*
    544  * Get login name, if available.
    545  */
    546 struct getlogin_args {
    547 	char	*namebuf;
    548 	u_int	namelen;
    549 };
    550 /* ARGSUSED */
    551 getlogin(p, uap, retval)
    552 	struct proc *p;
    553 	struct getlogin_args *uap;
    554 	int *retval;
    555 {
    556 
    557 	if (uap->namelen > sizeof (p->p_pgrp->pg_session->s_login))
    558 		uap->namelen = sizeof (p->p_pgrp->pg_session->s_login);
    559 	return (copyout((caddr_t) p->p_pgrp->pg_session->s_login,
    560 	    (caddr_t) uap->namebuf, uap->namelen));
    561 }
    562 
    563 /*
    564  * Set login name.
    565  */
    566 struct setlogin_args {
    567 	char	*namebuf;
    568 };
    569 /* ARGSUSED */
    570 setlogin(p, uap, retval)
    571 	struct proc *p;
    572 	struct setlogin_args *uap;
    573 	int *retval;
    574 {
    575 	int error;
    576 
    577 	if (error = suser(p->p_ucred, &p->p_acflag))
    578 		return (error);
    579 	error = copyinstr((caddr_t) uap->namebuf,
    580 	    (caddr_t) p->p_pgrp->pg_session->s_login,
    581 	    sizeof (p->p_pgrp->pg_session->s_login) - 1, (u_int *)0);
    582 	if (error == ENAMETOOLONG)
    583 		error = EINVAL;
    584 	return (error);
    585 }
    586