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