ifq: Switch to drop-head for default enqueue method.
[dragonfly.git] / sys / kern / kern_prot.c
bloba8350841e9527f6e8db49c894182910b91855399
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.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $
39 * System calls related to processes and protection
42 #include <sys/param.h>
43 #include <sys/acct.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/proc.h>
49 #include <sys/priv.h>
50 #include <sys/malloc.h>
51 #include <sys/pioctl.h>
52 #include <sys/resourcevar.h>
53 #include <sys/jail.h>
54 #include <sys/lockf.h>
55 #include <sys/spinlock.h>
57 #include <sys/thread2.h>
58 #include <sys/spinlock2.h>
60 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
62 int
63 sys_getpid(struct getpid_args *uap)
65 struct proc *p = curproc;
67 uap->sysmsg_fds[0] = p->p_pid;
68 return (0);
71 int
72 sys_getppid(struct getppid_args *uap)
74 struct proc *p = curproc;
76 lwkt_gettoken_shared(&p->p_token);
77 uap->sysmsg_result = p->p_pptr->p_pid;
78 lwkt_reltoken(&p->p_token);
80 return (0);
83 int
84 sys_lwp_gettid(struct lwp_gettid_args *uap)
86 struct lwp *lp = curthread->td_lwp;
87 uap->sysmsg_result = lp->lwp_tid;
88 return (0);
91 /*
92 * Get process group ID; note that POSIX getpgrp takes no parameter
94 int
95 sys_getpgrp(struct getpgrp_args *uap)
97 struct proc *p = curproc;
99 lwkt_gettoken_shared(&p->p_token);
100 uap->sysmsg_result = p->p_pgrp->pg_id;
101 lwkt_reltoken(&p->p_token);
103 return (0);
107 * Get an arbitrary pid's process group id
110 sys_getpgid(struct getpgid_args *uap)
112 struct proc *p = curproc;
113 struct proc *pt;
114 int error;
116 error = 0;
118 if (uap->pid == 0) {
119 pt = p;
120 PHOLD(pt);
121 } else {
122 pt = pfind(uap->pid);
123 if (pt == NULL)
124 error = ESRCH;
126 if (error == 0) {
127 lwkt_gettoken_shared(&pt->p_token);
128 uap->sysmsg_result = pt->p_pgrp->pg_id;
129 lwkt_reltoken(&pt->p_token);
131 if (pt)
132 PRELE(pt);
133 return (error);
137 * Get an arbitrary pid's session id.
140 sys_getsid(struct getsid_args *uap)
142 struct proc *p = curproc;
143 struct proc *pt;
144 int error;
146 error = 0;
148 if (uap->pid == 0) {
149 pt = p;
150 PHOLD(pt);
151 } else {
152 pt = pfind(uap->pid);
153 if (pt == NULL)
154 error = ESRCH;
156 if (error == 0)
157 uap->sysmsg_result = pt->p_session->s_sid;
158 if (pt)
159 PRELE(pt);
160 return (error);
165 * getuid()
168 sys_getuid(struct getuid_args *uap)
170 struct ucred *cred = curthread->td_ucred;
172 uap->sysmsg_fds[0] = cred->cr_ruid;
173 return (0);
177 * geteuid()
180 sys_geteuid(struct geteuid_args *uap)
182 struct ucred *cred = curthread->td_ucred;
184 uap->sysmsg_result = cred->cr_uid;
185 return (0);
189 * getgid()
192 sys_getgid(struct getgid_args *uap)
194 struct ucred *cred = curthread->td_ucred;
196 uap->sysmsg_fds[0] = cred->cr_rgid;
197 return (0);
201 * Get effective group ID. The "egid" is groups[0], and could be obtained
202 * via getgroups. This syscall exists because it is somewhat painful to do
203 * correctly in a library function.
206 sys_getegid(struct getegid_args *uap)
208 struct ucred *cred = curthread->td_ucred;
210 uap->sysmsg_result = cred->cr_groups[0];
211 return (0);
215 sys_getgroups(struct getgroups_args *uap)
217 struct ucred *cr;
218 u_int ngrp;
219 int error;
221 cr = curthread->td_ucred;
222 if ((ngrp = uap->gidsetsize) == 0) {
223 uap->sysmsg_result = cr->cr_ngroups;
224 return (0);
226 if (ngrp < cr->cr_ngroups)
227 return (EINVAL);
228 ngrp = cr->cr_ngroups;
229 error = copyout((caddr_t)cr->cr_groups,
230 (caddr_t)uap->gidset, ngrp * sizeof(gid_t));
231 if (error == 0)
232 uap->sysmsg_result = ngrp;
233 return (error);
237 sys_lwp_setname(struct lwp_setname_args *uap)
239 struct proc *p = curproc;
240 char comm0[MAXCOMLEN + 1];
241 const char *comm = NULL;
242 struct lwp *lp;
243 int error;
245 if (uap->name != NULL) {
246 error = copyinstr(uap->name, comm0, sizeof(comm0), NULL);
247 if (error) {
248 if (error != ENAMETOOLONG)
249 return error;
250 /* Truncate */
251 comm0[MAXCOMLEN] = '\0';
253 comm = comm0;
254 } else {
255 /* Restore to the default name, i.e. process name. */
256 comm = p->p_comm;
259 lwkt_gettoken(&p->p_token);
261 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid);
262 if (lp != NULL) {
263 strlcpy(lp->lwp_thread->td_comm, comm,
264 sizeof(lp->lwp_thread->td_comm));
265 error = 0;
266 } else {
267 error = ESRCH;
270 lwkt_reltoken(&p->p_token);
271 return error;
275 sys_setsid(struct setsid_args *uap)
277 struct proc *p = curproc;
278 struct pgrp *pg = NULL;
279 int error;
281 lwkt_gettoken(&p->p_token);
282 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) {
283 error = EPERM;
284 if (pg)
285 pgrel(pg);
286 } else {
287 enterpgrp(p, p->p_pid, 1);
288 uap->sysmsg_result = p->p_pid;
289 error = 0;
291 lwkt_reltoken(&p->p_token);
292 return (error);
296 * set process group (setpgid/old setpgrp)
298 * caller does setpgid(targpid, targpgid)
300 * pid must be caller or child of caller (ESRCH)
301 * if a child
302 * pid must be in same session (EPERM)
303 * pid can't have done an exec (EACCES)
304 * if pgid != pid
305 * there must exist some pid in same session having pgid (EPERM)
306 * pid must not be session leader (EPERM)
309 sys_setpgid(struct setpgid_args *uap)
311 struct proc *curp = curproc;
312 struct proc *targp; /* target process */
313 struct pgrp *pgrp = NULL; /* target pgrp */
314 int error;
316 if (uap->pgid < 0)
317 return (EINVAL);
319 if (uap->pid != 0 && uap->pid != curp->p_pid) {
320 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) {
321 if (targp)
322 PRELE(targp);
323 error = ESRCH;
324 targp = NULL;
325 goto done;
327 lwkt_gettoken(&targp->p_token);
328 /* targp now referenced and its token is held */
330 if (targp->p_pgrp == NULL ||
331 targp->p_session != curp->p_session) {
332 error = EPERM;
333 goto done;
335 if (targp->p_flags & P_EXEC) {
336 error = EACCES;
337 goto done;
339 } else {
340 targp = curp;
341 PHOLD(targp);
342 lwkt_gettoken(&targp->p_token);
344 if (SESS_LEADER(targp)) {
345 error = EPERM;
346 goto done;
348 if (uap->pgid == 0) {
349 uap->pgid = targp->p_pid;
350 } else if (uap->pgid != targp->p_pid) {
351 if ((pgrp = pgfind(uap->pgid)) == NULL ||
352 pgrp->pg_session != curp->p_session) {
353 error = EPERM;
354 goto done;
357 error = enterpgrp(targp, uap->pgid, 0);
358 done:
359 if (pgrp)
360 pgrel(pgrp);
361 if (targp) {
362 lwkt_reltoken(&targp->p_token);
363 PRELE(targp);
365 return (error);
369 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
370 * compatible. It says that setting the uid/gid to euid/egid is a special
371 * case of "appropriate privilege". Once the rules are expanded out, this
372 * basically means that setuid(nnn) sets all three id's, in all permitted
373 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
374 * does not set the saved id - this is dangerous for traditional BSD
375 * programs. For this reason, we *really* do not want to set
376 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
378 #define POSIX_APPENDIX_B_4_2_2
381 sys_setuid(struct setuid_args *uap)
383 struct proc *p = curproc;
384 struct ucred *cr;
385 uid_t uid;
386 int error;
388 lwkt_gettoken(&p->p_token);
389 cr = p->p_ucred;
392 * See if we have "permission" by POSIX 1003.1 rules.
394 * Note that setuid(geteuid()) is a special case of
395 * "appropriate privileges" in appendix B.4.2.2. We need
396 * to use this clause to be compatible with traditional BSD
397 * semantics. Basically, it means that "setuid(xx)" sets all
398 * three id's (assuming you have privs).
400 * Notes on the logic. We do things in three steps.
401 * 1: We determine if the euid is going to change, and do EPERM
402 * right away. We unconditionally change the euid later if this
403 * test is satisfied, simplifying that part of the logic.
404 * 2: We determine if the real and/or saved uid's are going to
405 * change. Determined by compile options.
406 * 3: Change euid last. (after tests in #2 for "appropriate privs")
408 uid = uap->uid;
409 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */
410 #ifdef _POSIX_SAVED_IDS
411 uid != crc->cr_svuid && /* allow setuid(saved gid) */
412 #endif
413 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
414 uid != cr->cr_uid && /* allow setuid(geteuid()) */
415 #endif
416 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0)))
417 goto done;
419 #ifdef _POSIX_SAVED_IDS
421 * Do we have "appropriate privileges" (are we root or uid == euid)
422 * If so, we are changing the real uid and/or saved uid.
424 if (
425 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
426 uid == cr->cr_uid ||
427 #endif
428 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */
429 #endif
432 * Set the real uid and transfer proc count to new user.
434 if (uid != cr->cr_ruid) {
435 cr = change_ruid(uid);
436 setsugid();
439 * Set saved uid
441 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
442 * the security of seteuid() depends on it. B.4.2.2 says it
443 * is important that we should do this.
445 if (cr->cr_svuid != uid) {
446 cr = cratom_proc(p);
447 cr->cr_svuid = uid;
448 setsugid();
453 * In all permitted cases, we are changing the euid.
454 * Copy credentials so other references do not see our changes.
456 if (cr->cr_uid != uid) {
457 change_euid(uid);
458 setsugid();
460 error = 0;
461 done:
462 lwkt_reltoken(&p->p_token);
463 return (error);
467 sys_seteuid(struct seteuid_args *uap)
469 struct proc *p = curproc;
470 struct ucred *cr;
471 uid_t euid;
472 int error;
474 lwkt_gettoken(&p->p_token);
475 cr = p->p_ucred;
476 euid = uap->euid;
477 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */
478 euid != cr->cr_svuid && /* allow seteuid(saved uid) */
479 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) {
480 lwkt_reltoken(&p->p_token);
481 return (error);
485 * Everything's okay, do it. Copy credentials so other references do
486 * not see our changes.
488 if (cr->cr_uid != euid) {
489 change_euid(euid);
490 setsugid();
492 lwkt_reltoken(&p->p_token);
493 return (0);
497 sys_setgid(struct setgid_args *uap)
499 struct proc *p = curproc;
500 struct ucred *cr;
501 gid_t gid;
502 int error;
504 lwkt_gettoken(&p->p_token);
505 cr = p->p_ucred;
508 * See if we have "permission" by POSIX 1003.1 rules.
510 * Note that setgid(getegid()) is a special case of
511 * "appropriate privileges" in appendix B.4.2.2. We need
512 * to use this clause to be compatible with traditional BSD
513 * semantics. Basically, it means that "setgid(xx)" sets all
514 * three id's (assuming you have privs).
516 * For notes on the logic here, see setuid() above.
518 gid = uap->gid;
519 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */
520 #ifdef _POSIX_SAVED_IDS
521 gid != cr->cr_svgid && /* allow setgid(saved gid) */
522 #endif
523 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
524 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */
525 #endif
526 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) {
527 goto done;
530 #ifdef _POSIX_SAVED_IDS
532 * Do we have "appropriate privileges" (are we root or gid == egid)
533 * If so, we are changing the real uid and saved gid.
535 if (
536 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
537 gid == cr->cr_groups[0] ||
538 #endif
539 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */
540 #endif
543 * Set real gid
545 if (cr->cr_rgid != gid) {
546 cr = cratom_proc(p);
547 cr->cr_rgid = gid;
548 setsugid();
551 * Set saved gid
553 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
554 * the security of setegid() depends on it. B.4.2.2 says it
555 * is important that we should do this.
557 if (cr->cr_svgid != gid) {
558 cr = cratom_proc(p);
559 cr->cr_svgid = gid;
560 setsugid();
564 * In all cases permitted cases, we are changing the egid.
565 * Copy credentials so other references do not see our changes.
567 if (cr->cr_groups[0] != gid) {
568 cr = cratom_proc(p);
569 cr->cr_groups[0] = gid;
570 setsugid();
572 error = 0;
573 done:
574 lwkt_reltoken(&p->p_token);
575 return (error);
579 sys_setegid(struct setegid_args *uap)
581 struct proc *p = curproc;
582 struct ucred *cr;
583 gid_t egid;
584 int error;
586 lwkt_gettoken(&p->p_token);
587 cr = p->p_ucred;
588 egid = uap->egid;
589 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */
590 egid != cr->cr_svgid && /* allow setegid(saved gid) */
591 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) {
592 goto done;
594 if (cr->cr_groups[0] != egid) {
595 cr = cratom_proc(p);
596 cr->cr_groups[0] = egid;
597 setsugid();
599 error = 0;
600 done:
601 lwkt_reltoken(&p->p_token);
602 return (error);
606 sys_setgroups(struct setgroups_args *uap)
608 struct proc *p = curproc;
609 struct ucred *cr;
610 u_int ngrp;
611 int error;
613 lwkt_gettoken(&p->p_token);
614 cr = p->p_ucred;
616 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0)))
617 goto done;
618 ngrp = uap->gidsetsize;
619 if (ngrp > NGROUPS) {
620 error = EINVAL;
621 goto done;
624 * XXX A little bit lazy here. We could test if anything has
625 * changed before cratom() and setting P_SUGID.
627 cr = cratom_proc(p);
628 if (ngrp < 1) {
630 * setgroups(0, NULL) is a legitimate way of clearing the
631 * groups vector on non-BSD systems (which generally do not
632 * have the egid in the groups[0]). We risk security holes
633 * when running non-BSD software if we do not do the same.
635 cr->cr_ngroups = 1;
636 } else {
637 error = copyin(uap->gidset, cr->cr_groups,
638 ngrp * sizeof(gid_t));
639 if (error)
640 goto done;
641 cr->cr_ngroups = ngrp;
643 setsugid();
644 error = 0;
645 done:
646 lwkt_reltoken(&p->p_token);
647 return (error);
651 sys_setreuid(struct setreuid_args *uap)
653 struct proc *p = curproc;
654 struct ucred *cr;
655 uid_t ruid, euid;
656 int error;
658 lwkt_gettoken(&p->p_token);
659 cr = p->p_ucred;
661 ruid = uap->ruid;
662 euid = uap->euid;
663 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
664 ruid != cr->cr_svuid) ||
665 (euid != (uid_t)-1 && euid != cr->cr_uid &&
666 euid != cr->cr_ruid && euid != cr->cr_svuid)) &&
667 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) {
668 goto done;
671 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
672 cr = change_euid(euid);
673 setsugid();
675 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
676 cr = change_ruid(ruid);
677 setsugid();
679 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) &&
680 cr->cr_svuid != cr->cr_uid) {
681 cr = cratom_proc(p);
682 cr->cr_svuid = cr->cr_uid;
683 setsugid();
685 error = 0;
686 done:
687 lwkt_reltoken(&p->p_token);
688 return (error);
692 sys_setregid(struct setregid_args *uap)
694 struct proc *p = curproc;
695 struct ucred *cr;
696 gid_t rgid, egid;
697 int error;
699 lwkt_gettoken(&p->p_token);
700 cr = p->p_ucred;
702 rgid = uap->rgid;
703 egid = uap->egid;
704 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
705 rgid != cr->cr_svgid) ||
706 (egid != (gid_t)-1 && egid != cr->cr_groups[0] &&
707 egid != cr->cr_rgid && egid != cr->cr_svgid)) &&
708 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) {
709 goto done;
712 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
713 cr = cratom_proc(p);
714 cr->cr_groups[0] = egid;
715 setsugid();
717 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
718 cr = cratom_proc(p);
719 cr->cr_rgid = rgid;
720 setsugid();
722 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) &&
723 cr->cr_svgid != cr->cr_groups[0]) {
724 cr = cratom_proc(p);
725 cr->cr_svgid = cr->cr_groups[0];
726 setsugid();
728 error = 0;
729 done:
730 lwkt_reltoken(&p->p_token);
731 return (error);
735 * setresuid(ruid, euid, suid) is like setreuid except control over the
736 * saved uid is explicit.
739 sys_setresuid(struct setresuid_args *uap)
741 struct proc *p = curproc;
742 struct ucred *cr;
743 uid_t ruid, euid, suid;
744 int error;
746 lwkt_gettoken(&p->p_token);
747 cr = p->p_ucred;
749 ruid = uap->ruid;
750 euid = uap->euid;
751 suid = uap->suid;
752 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid &&
753 ruid != cr->cr_svuid && ruid != cr->cr_uid) ||
754 (euid != (uid_t)-1 && euid != cr->cr_ruid &&
755 euid != cr->cr_svuid && euid != cr->cr_uid) ||
756 (suid != (uid_t)-1 && suid != cr->cr_ruid &&
757 suid != cr->cr_svuid && suid != cr->cr_uid)) &&
758 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) {
759 goto done;
761 if (euid != (uid_t)-1 && cr->cr_uid != euid) {
762 cr = change_euid(euid);
763 setsugid();
765 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) {
766 cr = change_ruid(ruid);
767 setsugid();
769 if (suid != (uid_t)-1 && cr->cr_svuid != suid) {
770 cr = cratom_proc(p);
771 cr->cr_svuid = suid;
772 setsugid();
774 error = 0;
775 done:
776 lwkt_reltoken(&p->p_token);
777 return (error);
781 * setresgid(rgid, egid, sgid) is like setregid except control over the
782 * saved gid is explicit.
785 sys_setresgid(struct setresgid_args *uap)
787 struct proc *p = curproc;
788 struct ucred *cr;
789 gid_t rgid, egid, sgid;
790 int error;
792 lwkt_gettoken(&p->p_token);
793 cr = p->p_ucred;
794 rgid = uap->rgid;
795 egid = uap->egid;
796 sgid = uap->sgid;
797 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid &&
798 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) ||
799 (egid != (gid_t)-1 && egid != cr->cr_rgid &&
800 egid != cr->cr_svgid && egid != cr->cr_groups[0]) ||
801 (sgid != (gid_t)-1 && sgid != cr->cr_rgid &&
802 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) &&
803 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) {
804 goto done;
807 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) {
808 cr = cratom_proc(p);
809 cr->cr_groups[0] = egid;
810 setsugid();
812 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) {
813 cr = cratom_proc(p);
814 cr->cr_rgid = rgid;
815 setsugid();
817 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) {
818 cr = cratom_proc(p);
819 cr->cr_svgid = sgid;
820 setsugid();
822 error = 0;
823 done:
824 lwkt_reltoken(&p->p_token);
825 return (error);
829 sys_getresuid(struct getresuid_args *uap)
831 struct ucred *cr;
832 int error1 = 0, error2 = 0, error3 = 0;
835 * copyout's can fault synchronously so we cannot use a shared
836 * token here.
838 cr = curthread->td_ucred;
839 if (uap->ruid)
840 error1 = copyout((caddr_t)&cr->cr_ruid,
841 (caddr_t)uap->ruid, sizeof(cr->cr_ruid));
842 if (uap->euid)
843 error2 = copyout((caddr_t)&cr->cr_uid,
844 (caddr_t)uap->euid, sizeof(cr->cr_uid));
845 if (uap->suid)
846 error3 = copyout((caddr_t)&cr->cr_svuid,
847 (caddr_t)uap->suid, sizeof(cr->cr_svuid));
848 return error1 ? error1 : (error2 ? error2 : error3);
852 sys_getresgid(struct getresgid_args *uap)
854 struct ucred *cr;
855 int error1 = 0, error2 = 0, error3 = 0;
857 cr = curthread->td_ucred;
858 if (uap->rgid)
859 error1 = copyout(&cr->cr_rgid, uap->rgid,
860 sizeof(cr->cr_rgid));
861 if (uap->egid)
862 error2 = copyout(&cr->cr_groups[0], uap->egid,
863 sizeof(cr->cr_groups[0]));
864 if (uap->sgid)
865 error3 = copyout(&cr->cr_svgid, uap->sgid,
866 sizeof(cr->cr_svgid));
867 return error1 ? error1 : (error2 ? error2 : error3);
872 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
873 * we use P_SUGID because we consider changing the owners as
874 * "tainting" as well.
875 * This is significant for procs that start as root and "become"
876 * a user without an exec - programs cannot know *everything*
877 * that libc *might* have put in their data segment.
880 sys_issetugid(struct issetugid_args *uap)
882 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0;
883 return (0);
887 * Check if gid is a member of the group set.
890 groupmember(gid_t gid, struct ucred *cred)
892 gid_t *gp;
893 gid_t *egp;
895 egp = &(cred->cr_groups[cred->cr_ngroups]);
896 for (gp = cred->cr_groups; gp < egp; gp++) {
897 if (*gp == gid)
898 return (1);
900 return (0);
904 * Test whether the specified credentials have the privilege
905 * in question.
907 * A kernel thread without a process context is assumed to have
908 * the privilege in question. In situations where the caller always
909 * expect a cred to exist, the cred should be passed separately and
910 * priv_check_cred() should be used instead of priv_check().
912 * Returns 0 or error.
915 priv_check(struct thread *td, int priv)
917 if (td->td_lwp != NULL)
918 return priv_check_cred(td->td_ucred, priv, 0);
919 return (0);
923 * Check a credential for privilege.
925 * A non-null credential is expected unless NULL_CRED_OKAY is set.
928 priv_check_cred(struct ucred *cred, int priv, int flags)
930 int error;
932 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege"));
934 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY),
935 ("priv_check_cred: NULL cred!"));
937 if (cred == NULL) {
938 if (flags & NULL_CRED_OKAY)
939 return (0);
940 else
941 return (EPERM);
943 if (cred->cr_uid != 0)
944 return (EPERM);
946 error = prison_priv_check(cred, priv);
947 if (error)
948 return (error);
950 /* NOTE: accounting for suser access (p_acflag/ASU) removed */
951 return (0);
955 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise.
958 p_trespass(struct ucred *cr1, struct ucred *cr2)
960 if (cr1 == cr2)
961 return (0);
962 if (!PRISON_CHECK(cr1, cr2))
963 return (ESRCH);
964 if (cr1->cr_ruid == cr2->cr_ruid)
965 return (0);
966 if (cr1->cr_uid == cr2->cr_ruid)
967 return (0);
968 if (cr1->cr_ruid == cr2->cr_uid)
969 return (0);
970 if (cr1->cr_uid == cr2->cr_uid)
971 return (0);
972 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0)
973 return (0);
974 return (EPERM);
977 static __inline void
978 _crinit(struct ucred *cr)
980 cr->cr_ref = 1;
983 void
984 crinit(struct ucred *cr)
986 bzero(cr, sizeof(*cr));
987 _crinit(cr);
991 * Allocate a zeroed cred structure.
993 struct ucred *
994 crget(void)
996 struct ucred *cr;
998 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO);
999 _crinit(cr);
1000 return (cr);
1004 * Claim another reference to a ucred structure. Can be used with special
1005 * creds.
1007 * It must be possible to call this routine with spinlocks held, meaning
1008 * that this routine itself cannot obtain a spinlock.
1010 struct ucred *
1011 crhold(struct ucred *cr)
1013 if (cr != NOCRED && cr != FSCRED)
1014 atomic_add_int(&cr->cr_ref, 1);
1015 return(cr);
1019 * Drop a reference from the cred structure, free it if the reference count
1020 * reaches 0.
1022 * NOTE: because we used atomic_add_int() above, without a spinlock, we
1023 * must also use atomic_subtract_int() below. A spinlock is required
1024 * in crfree() to handle multiple callers racing the refcount to 0.
1026 void
1027 crfree(struct ucred *cr)
1029 if (cr->cr_ref <= 0)
1030 panic("Freeing already free credential! %p", cr);
1031 if (atomic_fetchadd_int(&cr->cr_ref, -1) == 1) {
1033 * Some callers of crget(), such as nfs_statfs(),
1034 * allocate a temporary credential, but don't
1035 * allocate a uidinfo structure.
1037 if (cr->cr_uidinfo != NULL) {
1038 uidrop(cr->cr_uidinfo);
1039 cr->cr_uidinfo = NULL;
1041 if (cr->cr_ruidinfo != NULL) {
1042 uidrop(cr->cr_ruidinfo);
1043 cr->cr_ruidinfo = NULL;
1047 * Destroy empty prisons
1049 if (jailed(cr))
1050 prison_free(cr->cr_prison);
1051 cr->cr_prison = NULL; /* safety */
1053 kfree((caddr_t)cr, M_CRED);
1058 * Atomize a cred structure so it can be modified without polluting
1059 * other references to it.
1061 * MPSAFE (however, *pcr must be stable)
1063 struct ucred *
1064 cratom(struct ucred **pcr)
1066 struct ucred *oldcr;
1067 struct ucred *newcr;
1069 oldcr = *pcr;
1070 if (oldcr->cr_ref == 1)
1071 return (oldcr);
1072 newcr = crget(); /* this might block */
1073 oldcr = *pcr; /* re-cache after potentially blocking */
1074 *newcr = *oldcr;
1075 if (newcr->cr_uidinfo)
1076 uihold(newcr->cr_uidinfo);
1077 if (newcr->cr_ruidinfo)
1078 uihold(newcr->cr_ruidinfo);
1079 if (jailed(newcr))
1080 prison_hold(newcr->cr_prison);
1081 newcr->cr_ref = 1;
1082 crfree(oldcr);
1083 *pcr = newcr;
1085 return (newcr);
1089 * Called with a modifying token held, but must still obtain p_spin to
1090 * actually replace p_ucred to handle races against syscall entry from
1091 * other threads which cache p_ucred->td_ucred.
1093 * (the threads will only get the spin-lock, and they only need to in
1094 * the case where td_ucred != p_ucred so this is optimal).
1096 struct ucred *
1097 cratom_proc(struct proc *p)
1099 struct ucred *oldcr;
1100 struct ucred *newcr;
1102 oldcr = p->p_ucred;
1103 if (oldcr->cr_ref == 1)
1104 return(oldcr);
1106 newcr = crget(); /* this might block */
1107 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */
1108 *newcr = *oldcr;
1109 if (newcr->cr_uidinfo)
1110 uihold(newcr->cr_uidinfo);
1111 if (newcr->cr_ruidinfo)
1112 uihold(newcr->cr_ruidinfo);
1113 if (jailed(newcr))
1114 prison_hold(newcr->cr_prison);
1115 newcr->cr_ref = 1;
1117 spin_lock(&p->p_spin);
1118 p->p_ucred = newcr;
1119 spin_unlock(&p->p_spin);
1120 crfree(oldcr);
1122 return newcr;
1126 * Dup cred struct to a new held one.
1128 struct ucred *
1129 crdup(struct ucred *cr)
1131 struct ucred *newcr;
1133 newcr = crget();
1134 *newcr = *cr;
1135 if (newcr->cr_uidinfo)
1136 uihold(newcr->cr_uidinfo);
1137 if (newcr->cr_ruidinfo)
1138 uihold(newcr->cr_ruidinfo);
1139 if (jailed(newcr))
1140 prison_hold(newcr->cr_prison);
1141 newcr->cr_ref = 1;
1142 return (newcr);
1146 * Fill in a struct xucred based on a struct ucred.
1148 void
1149 cru2x(struct ucred *cr, struct xucred *xcr)
1152 bzero(xcr, sizeof(*xcr));
1153 xcr->cr_version = XUCRED_VERSION;
1154 xcr->cr_uid = cr->cr_uid;
1155 xcr->cr_ngroups = cr->cr_ngroups;
1156 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
1160 * Get login name, if available.
1163 sys_getlogin(struct getlogin_args *uap)
1165 struct proc *p = curproc;
1166 char buf[MAXLOGNAME];
1167 int error;
1169 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */
1170 uap->namelen = MAXLOGNAME;
1171 bzero(buf, sizeof(buf));
1172 lwkt_gettoken_shared(&p->p_token);
1173 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen);
1174 lwkt_reltoken(&p->p_token);
1176 error = copyout(buf, uap->namebuf, uap->namelen);
1177 return (error);
1181 * Set login name.
1184 sys_setlogin(struct setlogin_args *uap)
1186 struct thread *td = curthread;
1187 struct proc *p;
1188 struct ucred *cred;
1189 char buf[MAXLOGNAME];
1190 int error;
1192 cred = td->td_ucred;
1193 p = td->td_proc;
1195 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0)))
1196 return (error);
1197 bzero(buf, sizeof(buf));
1198 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL);
1199 if (error == ENAMETOOLONG)
1200 error = EINVAL;
1201 if (error == 0) {
1202 lwkt_gettoken_shared(&p->p_token);
1203 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf));
1204 lwkt_reltoken(&p->p_token);
1206 return (error);
1209 void
1210 setsugid(void)
1212 struct proc *p = curproc;
1214 KKASSERT(p != NULL);
1215 lwkt_gettoken(&p->p_token);
1216 p->p_flags |= P_SUGID;
1217 if (!(p->p_pfsflags & PF_ISUGID))
1218 p->p_stops = 0;
1219 lwkt_reltoken(&p->p_token);
1223 * Helper function to change the effective uid of a process
1225 struct ucred *
1226 change_euid(uid_t euid)
1228 struct proc *p = curproc;
1229 struct ucred *cr;
1231 KKASSERT(p != NULL);
1232 lf_count_adjust(p, 0);
1233 cr = cratom_proc(p);
1234 cr->cr_uid = euid;
1235 uireplace(&cr->cr_uidinfo, uifind(euid));
1236 lf_count_adjust(p, 1);
1237 return (cr);
1241 * Helper function to change the real uid of a process
1243 * The per-uid process count for this process is transfered from
1244 * the old uid to the new uid.
1246 struct ucred *
1247 change_ruid(uid_t ruid)
1249 struct proc *p = curproc;
1250 struct ucred *cr;
1252 KKASSERT(p != NULL);
1254 cr = cratom_proc(p);
1255 chgproccnt(cr->cr_ruidinfo, -1, 0);
1256 cr->cr_ruid = ruid;
1257 uireplace(&cr->cr_ruidinfo, uifind(ruid));
1258 chgproccnt(cr->cr_ruidinfo, 1, 0);
1259 return (cr);