]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_prot.c
Move most of the contents of opt_compat.h to opt_global.h.
[FreeBSD/FreeBSD.git] / sys / kern / kern_prot.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
5  *      The Regents of the University of California.
6  * (c) UNIX System Laboratories, Inc.
7  * Copyright (c) 2000-2001 Robert N. M. Watson.
8  * All rights reserved.
9  *
10  * All or some portions of this file are derived from material licensed
11  * to the University of California by American Telephone and Telegraph
12  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13  * the permission of UNIX System Laboratories, Inc.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
40  */
41
42 /*
43  * System calls related to processes and protection
44  */
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/acct.h>
55 #include <sys/kdb.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/loginclass.h>
59 #include <sys/malloc.h>
60 #include <sys/mutex.h>
61 #include <sys/refcount.h>
62 #include <sys/sx.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/sysproto.h>
66 #include <sys/jail.h>
67 #include <sys/pioctl.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
74
75 #ifdef REGRESSION
76 FEATURE(regression,
77     "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
78 #endif
79
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
82
83 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
84
85 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
86
87 static void crsetgroups_locked(struct ucred *cr, int ngrp,
88     gid_t *groups);
89
90 #ifndef _SYS_SYSPROTO_H_
91 struct getpid_args {
92         int     dummy;
93 };
94 #endif
95 /* ARGSUSED */
96 int
97 sys_getpid(struct thread *td, struct getpid_args *uap)
98 {
99         struct proc *p = td->td_proc;
100
101         td->td_retval[0] = p->p_pid;
102 #if defined(COMPAT_43)
103         td->td_retval[1] = kern_getppid(td);
104 #endif
105         return (0);
106 }
107
108 #ifndef _SYS_SYSPROTO_H_
109 struct getppid_args {
110         int     dummy;
111 };
112 #endif
113 /* ARGSUSED */
114 int
115 sys_getppid(struct thread *td, struct getppid_args *uap)
116 {
117
118         td->td_retval[0] = kern_getppid(td);
119         return (0);
120 }
121
122 int
123 kern_getppid(struct thread *td)
124 {
125         struct proc *p = td->td_proc;
126         struct proc *pp;
127         int ppid;
128
129         PROC_LOCK(p);
130         if (!(p->p_flag & P_TRACED)) {
131                 ppid = p->p_pptr->p_pid;
132                 PROC_UNLOCK(p);
133         } else {
134                 PROC_UNLOCK(p);
135                 sx_slock(&proctree_lock);
136                 pp = proc_realparent(p);
137                 ppid = pp->p_pid;
138                 sx_sunlock(&proctree_lock);
139         }
140
141         return (ppid);
142 }
143
144 /*
145  * Get process group ID; note that POSIX getpgrp takes no parameter.
146  */
147 #ifndef _SYS_SYSPROTO_H_
148 struct getpgrp_args {
149         int     dummy;
150 };
151 #endif
152 int
153 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
154 {
155         struct proc *p = td->td_proc;
156
157         PROC_LOCK(p);
158         td->td_retval[0] = p->p_pgrp->pg_id;
159         PROC_UNLOCK(p);
160         return (0);
161 }
162
163 /* Get an arbitrary pid's process group id */
164 #ifndef _SYS_SYSPROTO_H_
165 struct getpgid_args {
166         pid_t   pid;
167 };
168 #endif
169 int
170 sys_getpgid(struct thread *td, struct getpgid_args *uap)
171 {
172         struct proc *p;
173         int error;
174
175         if (uap->pid == 0) {
176                 p = td->td_proc;
177                 PROC_LOCK(p);
178         } else {
179                 p = pfind(uap->pid);
180                 if (p == NULL)
181                         return (ESRCH);
182                 error = p_cansee(td, p);
183                 if (error) {
184                         PROC_UNLOCK(p);
185                         return (error);
186                 }
187         }
188         td->td_retval[0] = p->p_pgrp->pg_id;
189         PROC_UNLOCK(p);
190         return (0);
191 }
192
193 /*
194  * Get an arbitrary pid's session id.
195  */
196 #ifndef _SYS_SYSPROTO_H_
197 struct getsid_args {
198         pid_t   pid;
199 };
200 #endif
201 int
202 sys_getsid(struct thread *td, struct getsid_args *uap)
203 {
204         struct proc *p;
205         int error;
206
207         if (uap->pid == 0) {
208                 p = td->td_proc;
209                 PROC_LOCK(p);
210         } else {
211                 p = pfind(uap->pid);
212                 if (p == NULL)
213                         return (ESRCH);
214                 error = p_cansee(td, p);
215                 if (error) {
216                         PROC_UNLOCK(p);
217                         return (error);
218                 }
219         }
220         td->td_retval[0] = p->p_session->s_sid;
221         PROC_UNLOCK(p);
222         return (0);
223 }
224
225 #ifndef _SYS_SYSPROTO_H_
226 struct getuid_args {
227         int     dummy;
228 };
229 #endif
230 /* ARGSUSED */
231 int
232 sys_getuid(struct thread *td, struct getuid_args *uap)
233 {
234
235         td->td_retval[0] = td->td_ucred->cr_ruid;
236 #if defined(COMPAT_43)
237         td->td_retval[1] = td->td_ucred->cr_uid;
238 #endif
239         return (0);
240 }
241
242 #ifndef _SYS_SYSPROTO_H_
243 struct geteuid_args {
244         int     dummy;
245 };
246 #endif
247 /* ARGSUSED */
248 int
249 sys_geteuid(struct thread *td, struct geteuid_args *uap)
250 {
251
252         td->td_retval[0] = td->td_ucred->cr_uid;
253         return (0);
254 }
255
256 #ifndef _SYS_SYSPROTO_H_
257 struct getgid_args {
258         int     dummy;
259 };
260 #endif
261 /* ARGSUSED */
262 int
263 sys_getgid(struct thread *td, struct getgid_args *uap)
264 {
265
266         td->td_retval[0] = td->td_ucred->cr_rgid;
267 #if defined(COMPAT_43)
268         td->td_retval[1] = td->td_ucred->cr_groups[0];
269 #endif
270         return (0);
271 }
272
273 /*
274  * Get effective group ID.  The "egid" is groups[0], and could be obtained
275  * via getgroups.  This syscall exists because it is somewhat painful to do
276  * correctly in a library function.
277  */
278 #ifndef _SYS_SYSPROTO_H_
279 struct getegid_args {
280         int     dummy;
281 };
282 #endif
283 /* ARGSUSED */
284 int
285 sys_getegid(struct thread *td, struct getegid_args *uap)
286 {
287
288         td->td_retval[0] = td->td_ucred->cr_groups[0];
289         return (0);
290 }
291
292 #ifndef _SYS_SYSPROTO_H_
293 struct getgroups_args {
294         u_int   gidsetsize;
295         gid_t   *gidset;
296 };
297 #endif
298 int
299 sys_getgroups(struct thread *td, struct getgroups_args *uap)
300 {
301         struct ucred *cred;
302         u_int ngrp;
303         int error;
304
305         cred = td->td_ucred;
306         ngrp = cred->cr_ngroups;
307
308         if (uap->gidsetsize == 0) {
309                 error = 0;
310                 goto out;
311         }
312         if (uap->gidsetsize < ngrp)
313                 return (EINVAL);
314
315         error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
316 out:
317         td->td_retval[0] = ngrp;
318         return (error);
319 }
320
321 #ifndef _SYS_SYSPROTO_H_
322 struct setsid_args {
323         int     dummy;
324 };
325 #endif
326 /* ARGSUSED */
327 int
328 sys_setsid(struct thread *td, struct setsid_args *uap)
329 {
330         struct pgrp *pgrp;
331         int error;
332         struct proc *p = td->td_proc;
333         struct pgrp *newpgrp;
334         struct session *newsess;
335
336         error = 0;
337         pgrp = NULL;
338
339         newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
340         newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
341
342         sx_xlock(&proctree_lock);
343
344         if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
345                 if (pgrp != NULL)
346                         PGRP_UNLOCK(pgrp);
347                 error = EPERM;
348         } else {
349                 (void)enterpgrp(p, p->p_pid, newpgrp, newsess);
350                 td->td_retval[0] = p->p_pid;
351                 newpgrp = NULL;
352                 newsess = NULL;
353         }
354
355         sx_xunlock(&proctree_lock);
356
357         if (newpgrp != NULL)
358                 free(newpgrp, M_PGRP);
359         if (newsess != NULL)
360                 free(newsess, M_SESSION);
361
362         return (error);
363 }
364
365 /*
366  * set process group (setpgid/old setpgrp)
367  *
368  * caller does setpgid(targpid, targpgid)
369  *
370  * pid must be caller or child of caller (ESRCH)
371  * if a child
372  *      pid must be in same session (EPERM)
373  *      pid can't have done an exec (EACCES)
374  * if pgid != pid
375  *      there must exist some pid in same session having pgid (EPERM)
376  * pid must not be session leader (EPERM)
377  */
378 #ifndef _SYS_SYSPROTO_H_
379 struct setpgid_args {
380         int     pid;            /* target process id */
381         int     pgid;           /* target pgrp id */
382 };
383 #endif
384 /* ARGSUSED */
385 int
386 sys_setpgid(struct thread *td, struct setpgid_args *uap)
387 {
388         struct proc *curp = td->td_proc;
389         struct proc *targp;     /* target process */
390         struct pgrp *pgrp;      /* target pgrp */
391         int error;
392         struct pgrp *newpgrp;
393
394         if (uap->pgid < 0)
395                 return (EINVAL);
396
397         error = 0;
398
399         newpgrp = malloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
400
401         sx_xlock(&proctree_lock);
402         if (uap->pid != 0 && uap->pid != curp->p_pid) {
403                 if ((targp = pfind(uap->pid)) == NULL) {
404                         error = ESRCH;
405                         goto done;
406                 }
407                 if (!inferior(targp)) {
408                         PROC_UNLOCK(targp);
409                         error = ESRCH;
410                         goto done;
411                 }
412                 if ((error = p_cansee(td, targp))) {
413                         PROC_UNLOCK(targp);
414                         goto done;
415                 }
416                 if (targp->p_pgrp == NULL ||
417                     targp->p_session != curp->p_session) {
418                         PROC_UNLOCK(targp);
419                         error = EPERM;
420                         goto done;
421                 }
422                 if (targp->p_flag & P_EXEC) {
423                         PROC_UNLOCK(targp);
424                         error = EACCES;
425                         goto done;
426                 }
427                 PROC_UNLOCK(targp);
428         } else
429                 targp = curp;
430         if (SESS_LEADER(targp)) {
431                 error = EPERM;
432                 goto done;
433         }
434         if (uap->pgid == 0)
435                 uap->pgid = targp->p_pid;
436         if ((pgrp = pgfind(uap->pgid)) == NULL) {
437                 if (uap->pgid == targp->p_pid) {
438                         error = enterpgrp(targp, uap->pgid, newpgrp,
439                             NULL);
440                         if (error == 0)
441                                 newpgrp = NULL;
442                 } else
443                         error = EPERM;
444         } else {
445                 if (pgrp == targp->p_pgrp) {
446                         PGRP_UNLOCK(pgrp);
447                         goto done;
448                 }
449                 if (pgrp->pg_id != targp->p_pid &&
450                     pgrp->pg_session != curp->p_session) {
451                         PGRP_UNLOCK(pgrp);
452                         error = EPERM;
453                         goto done;
454                 }
455                 PGRP_UNLOCK(pgrp);
456                 error = enterthispgrp(targp, pgrp);
457         }
458 done:
459         sx_xunlock(&proctree_lock);
460         KASSERT((error == 0) || (newpgrp != NULL),
461             ("setpgid failed and newpgrp is NULL"));
462         if (newpgrp != NULL)
463                 free(newpgrp, M_PGRP);
464         return (error);
465 }
466
467 /*
468  * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
469  * compatible.  It says that setting the uid/gid to euid/egid is a special
470  * case of "appropriate privilege".  Once the rules are expanded out, this
471  * basically means that setuid(nnn) sets all three id's, in all permitted
472  * cases unless _POSIX_SAVED_IDS is enabled.  In that case, setuid(getuid())
473  * does not set the saved id - this is dangerous for traditional BSD
474  * programs.  For this reason, we *really* do not want to set
475  * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
476  */
477 #define POSIX_APPENDIX_B_4_2_2
478
479 #ifndef _SYS_SYSPROTO_H_
480 struct setuid_args {
481         uid_t   uid;
482 };
483 #endif
484 /* ARGSUSED */
485 int
486 sys_setuid(struct thread *td, struct setuid_args *uap)
487 {
488         struct proc *p = td->td_proc;
489         struct ucred *newcred, *oldcred;
490         uid_t uid;
491         struct uidinfo *uip;
492         int error;
493
494         uid = uap->uid;
495         AUDIT_ARG_UID(uid);
496         newcred = crget();
497         uip = uifind(uid);
498         PROC_LOCK(p);
499         /*
500          * Copy credentials so other references do not see our changes.
501          */
502         oldcred = crcopysafe(p, newcred);
503
504 #ifdef MAC
505         error = mac_cred_check_setuid(oldcred, uid);
506         if (error)
507                 goto fail;
508 #endif
509
510         /*
511          * See if we have "permission" by POSIX 1003.1 rules.
512          *
513          * Note that setuid(geteuid()) is a special case of
514          * "appropriate privileges" in appendix B.4.2.2.  We need
515          * to use this clause to be compatible with traditional BSD
516          * semantics.  Basically, it means that "setuid(xx)" sets all
517          * three id's (assuming you have privs).
518          *
519          * Notes on the logic.  We do things in three steps.
520          * 1: We determine if the euid is going to change, and do EPERM
521          *    right away.  We unconditionally change the euid later if this
522          *    test is satisfied, simplifying that part of the logic.
523          * 2: We determine if the real and/or saved uids are going to
524          *    change.  Determined by compile options.
525          * 3: Change euid last. (after tests in #2 for "appropriate privs")
526          */
527         if (uid != oldcred->cr_ruid &&          /* allow setuid(getuid()) */
528 #ifdef _POSIX_SAVED_IDS
529             uid != oldcred->cr_svuid &&         /* allow setuid(saved gid) */
530 #endif
531 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use BSD-compat clause from B.4.2.2 */
532             uid != oldcred->cr_uid &&           /* allow setuid(geteuid()) */
533 #endif
534             (error = priv_check_cred(oldcred, PRIV_CRED_SETUID, 0)) != 0)
535                 goto fail;
536
537 #ifdef _POSIX_SAVED_IDS
538         /*
539          * Do we have "appropriate privileges" (are we root or uid == euid)
540          * If so, we are changing the real uid and/or saved uid.
541          */
542         if (
543 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use the clause from B.4.2.2 */
544             uid == oldcred->cr_uid ||
545 #endif
546             /* We are using privs. */
547             priv_check_cred(oldcred, PRIV_CRED_SETUID, 0) == 0)
548 #endif
549         {
550                 /*
551                  * Set the real uid and transfer proc count to new user.
552                  */
553                 if (uid != oldcred->cr_ruid) {
554                         change_ruid(newcred, uip);
555                         setsugid(p);
556                 }
557                 /*
558                  * Set saved uid
559                  *
560                  * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
561                  * the security of seteuid() depends on it.  B.4.2.2 says it
562                  * is important that we should do this.
563                  */
564                 if (uid != oldcred->cr_svuid) {
565                         change_svuid(newcred, uid);
566                         setsugid(p);
567                 }
568         }
569
570         /*
571          * In all permitted cases, we are changing the euid.
572          */
573         if (uid != oldcred->cr_uid) {
574                 change_euid(newcred, uip);
575                 setsugid(p);
576         }
577         proc_set_cred(p, newcred);
578         PROC_UNLOCK(p);
579 #ifdef RACCT
580         racct_proc_ucred_changed(p, oldcred, newcred);
581 #endif
582         uifree(uip);
583         crfree(oldcred);
584         return (0);
585
586 fail:
587         PROC_UNLOCK(p);
588         uifree(uip);
589         crfree(newcred);
590         return (error);
591 }
592
593 #ifndef _SYS_SYSPROTO_H_
594 struct seteuid_args {
595         uid_t   euid;
596 };
597 #endif
598 /* ARGSUSED */
599 int
600 sys_seteuid(struct thread *td, struct seteuid_args *uap)
601 {
602         struct proc *p = td->td_proc;
603         struct ucred *newcred, *oldcred;
604         uid_t euid;
605         struct uidinfo *euip;
606         int error;
607
608         euid = uap->euid;
609         AUDIT_ARG_EUID(euid);
610         newcred = crget();
611         euip = uifind(euid);
612         PROC_LOCK(p);
613         /*
614          * Copy credentials so other references do not see our changes.
615          */
616         oldcred = crcopysafe(p, newcred);
617
618 #ifdef MAC
619         error = mac_cred_check_seteuid(oldcred, euid);
620         if (error)
621                 goto fail;
622 #endif
623
624         if (euid != oldcred->cr_ruid &&         /* allow seteuid(getuid()) */
625             euid != oldcred->cr_svuid &&        /* allow seteuid(saved uid) */
626             (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID, 0)) != 0)
627                 goto fail;
628
629         /*
630          * Everything's okay, do it.
631          */
632         if (oldcred->cr_uid != euid) {
633                 change_euid(newcred, euip);
634                 setsugid(p);
635         }
636         proc_set_cred(p, newcred);
637         PROC_UNLOCK(p);
638         uifree(euip);
639         crfree(oldcred);
640         return (0);
641
642 fail:
643         PROC_UNLOCK(p);
644         uifree(euip);
645         crfree(newcred);
646         return (error);
647 }
648
649 #ifndef _SYS_SYSPROTO_H_
650 struct setgid_args {
651         gid_t   gid;
652 };
653 #endif
654 /* ARGSUSED */
655 int
656 sys_setgid(struct thread *td, struct setgid_args *uap)
657 {
658         struct proc *p = td->td_proc;
659         struct ucred *newcred, *oldcred;
660         gid_t gid;
661         int error;
662
663         gid = uap->gid;
664         AUDIT_ARG_GID(gid);
665         newcred = crget();
666         PROC_LOCK(p);
667         oldcred = crcopysafe(p, newcred);
668
669 #ifdef MAC
670         error = mac_cred_check_setgid(oldcred, gid);
671         if (error)
672                 goto fail;
673 #endif
674
675         /*
676          * See if we have "permission" by POSIX 1003.1 rules.
677          *
678          * Note that setgid(getegid()) is a special case of
679          * "appropriate privileges" in appendix B.4.2.2.  We need
680          * to use this clause to be compatible with traditional BSD
681          * semantics.  Basically, it means that "setgid(xx)" sets all
682          * three id's (assuming you have privs).
683          *
684          * For notes on the logic here, see setuid() above.
685          */
686         if (gid != oldcred->cr_rgid &&          /* allow setgid(getgid()) */
687 #ifdef _POSIX_SAVED_IDS
688             gid != oldcred->cr_svgid &&         /* allow setgid(saved gid) */
689 #endif
690 #ifdef POSIX_APPENDIX_B_4_2_2   /* Use BSD-compat clause from B.4.2.2 */
691             gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
692 #endif
693             (error = priv_check_cred(oldcred, PRIV_CRED_SETGID, 0)) != 0)
694                 goto fail;
695
696 #ifdef _POSIX_SAVED_IDS
697         /*
698          * Do we have "appropriate privileges" (are we root or gid == egid)
699          * If so, we are changing the real uid and saved gid.
700          */
701         if (
702 #ifdef POSIX_APPENDIX_B_4_2_2   /* use the clause from B.4.2.2 */
703             gid == oldcred->cr_groups[0] ||
704 #endif
705             /* We are using privs. */
706             priv_check_cred(oldcred, PRIV_CRED_SETGID, 0) == 0)
707 #endif
708         {
709                 /*
710                  * Set real gid
711                  */
712                 if (oldcred->cr_rgid != gid) {
713                         change_rgid(newcred, gid);
714                         setsugid(p);
715                 }
716                 /*
717                  * Set saved gid
718                  *
719                  * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
720                  * the security of setegid() depends on it.  B.4.2.2 says it
721                  * is important that we should do this.
722                  */
723                 if (oldcred->cr_svgid != gid) {
724                         change_svgid(newcred, gid);
725                         setsugid(p);
726                 }
727         }
728         /*
729          * In all cases permitted cases, we are changing the egid.
730          * Copy credentials so other references do not see our changes.
731          */
732         if (oldcred->cr_groups[0] != gid) {
733                 change_egid(newcred, gid);
734                 setsugid(p);
735         }
736         proc_set_cred(p, newcred);
737         PROC_UNLOCK(p);
738         crfree(oldcred);
739         return (0);
740
741 fail:
742         PROC_UNLOCK(p);
743         crfree(newcred);
744         return (error);
745 }
746
747 #ifndef _SYS_SYSPROTO_H_
748 struct setegid_args {
749         gid_t   egid;
750 };
751 #endif
752 /* ARGSUSED */
753 int
754 sys_setegid(struct thread *td, struct setegid_args *uap)
755 {
756         struct proc *p = td->td_proc;
757         struct ucred *newcred, *oldcred;
758         gid_t egid;
759         int error;
760
761         egid = uap->egid;
762         AUDIT_ARG_EGID(egid);
763         newcred = crget();
764         PROC_LOCK(p);
765         oldcred = crcopysafe(p, newcred);
766
767 #ifdef MAC
768         error = mac_cred_check_setegid(oldcred, egid);
769         if (error)
770                 goto fail;
771 #endif
772
773         if (egid != oldcred->cr_rgid &&         /* allow setegid(getgid()) */
774             egid != oldcred->cr_svgid &&        /* allow setegid(saved gid) */
775             (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID, 0)) != 0)
776                 goto fail;
777
778         if (oldcred->cr_groups[0] != egid) {
779                 change_egid(newcred, egid);
780                 setsugid(p);
781         }
782         proc_set_cred(p, newcred);
783         PROC_UNLOCK(p);
784         crfree(oldcred);
785         return (0);
786
787 fail:
788         PROC_UNLOCK(p);
789         crfree(newcred);
790         return (error);
791 }
792
793 #ifndef _SYS_SYSPROTO_H_
794 struct setgroups_args {
795         u_int   gidsetsize;
796         gid_t   *gidset;
797 };
798 #endif
799 /* ARGSUSED */
800 int
801 sys_setgroups(struct thread *td, struct setgroups_args *uap)
802 {
803         gid_t smallgroups[XU_NGROUPS];
804         gid_t *groups;
805         u_int gidsetsize;
806         int error;
807
808         gidsetsize = uap->gidsetsize;
809         if (gidsetsize > ngroups_max + 1)
810                 return (EINVAL);
811
812         if (gidsetsize > XU_NGROUPS)
813                 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
814         else
815                 groups = smallgroups;
816
817         error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
818         if (error == 0)
819                 error = kern_setgroups(td, gidsetsize, groups);
820
821         if (gidsetsize > XU_NGROUPS)
822                 free(groups, M_TEMP);
823         return (error);
824 }
825
826 int
827 kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
828 {
829         struct proc *p = td->td_proc;
830         struct ucred *newcred, *oldcred;
831         int error;
832
833         MPASS(ngrp <= ngroups_max + 1);
834         AUDIT_ARG_GROUPSET(groups, ngrp);
835         newcred = crget();
836         crextend(newcred, ngrp);
837         PROC_LOCK(p);
838         oldcred = crcopysafe(p, newcred);
839
840 #ifdef MAC
841         error = mac_cred_check_setgroups(oldcred, ngrp, groups);
842         if (error)
843                 goto fail;
844 #endif
845
846         error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0);
847         if (error)
848                 goto fail;
849
850         if (ngrp == 0) {
851                 /*
852                  * setgroups(0, NULL) is a legitimate way of clearing the
853                  * groups vector on non-BSD systems (which generally do not
854                  * have the egid in the groups[0]).  We risk security holes
855                  * when running non-BSD software if we do not do the same.
856                  */
857                 newcred->cr_ngroups = 1;
858         } else {
859                 crsetgroups_locked(newcred, ngrp, groups);
860         }
861         setsugid(p);
862         proc_set_cred(p, newcred);
863         PROC_UNLOCK(p);
864         crfree(oldcred);
865         return (0);
866
867 fail:
868         PROC_UNLOCK(p);
869         crfree(newcred);
870         return (error);
871 }
872
873 #ifndef _SYS_SYSPROTO_H_
874 struct setreuid_args {
875         uid_t   ruid;
876         uid_t   euid;
877 };
878 #endif
879 /* ARGSUSED */
880 int
881 sys_setreuid(struct thread *td, struct setreuid_args *uap)
882 {
883         struct proc *p = td->td_proc;
884         struct ucred *newcred, *oldcred;
885         uid_t euid, ruid;
886         struct uidinfo *euip, *ruip;
887         int error;
888
889         euid = uap->euid;
890         ruid = uap->ruid;
891         AUDIT_ARG_EUID(euid);
892         AUDIT_ARG_RUID(ruid);
893         newcred = crget();
894         euip = uifind(euid);
895         ruip = uifind(ruid);
896         PROC_LOCK(p);
897         oldcred = crcopysafe(p, newcred);
898
899 #ifdef MAC
900         error = mac_cred_check_setreuid(oldcred, ruid, euid);
901         if (error)
902                 goto fail;
903 #endif
904
905         if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
906               ruid != oldcred->cr_svuid) ||
907              (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
908               euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
909             (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID, 0)) != 0)
910                 goto fail;
911
912         if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
913                 change_euid(newcred, euip);
914                 setsugid(p);
915         }
916         if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
917                 change_ruid(newcred, ruip);
918                 setsugid(p);
919         }
920         if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
921             newcred->cr_svuid != newcred->cr_uid) {
922                 change_svuid(newcred, newcred->cr_uid);
923                 setsugid(p);
924         }
925         proc_set_cred(p, newcred);
926         PROC_UNLOCK(p);
927 #ifdef RACCT
928         racct_proc_ucred_changed(p, oldcred, newcred);
929 #endif
930         uifree(ruip);
931         uifree(euip);
932         crfree(oldcred);
933         return (0);
934
935 fail:
936         PROC_UNLOCK(p);
937         uifree(ruip);
938         uifree(euip);
939         crfree(newcred);
940         return (error);
941 }
942
943 #ifndef _SYS_SYSPROTO_H_
944 struct setregid_args {
945         gid_t   rgid;
946         gid_t   egid;
947 };
948 #endif
949 /* ARGSUSED */
950 int
951 sys_setregid(struct thread *td, struct setregid_args *uap)
952 {
953         struct proc *p = td->td_proc;
954         struct ucred *newcred, *oldcred;
955         gid_t egid, rgid;
956         int error;
957
958         egid = uap->egid;
959         rgid = uap->rgid;
960         AUDIT_ARG_EGID(egid);
961         AUDIT_ARG_RGID(rgid);
962         newcred = crget();
963         PROC_LOCK(p);
964         oldcred = crcopysafe(p, newcred);
965
966 #ifdef MAC
967         error = mac_cred_check_setregid(oldcred, rgid, egid);
968         if (error)
969                 goto fail;
970 #endif
971
972         if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
973             rgid != oldcred->cr_svgid) ||
974              (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
975              egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
976             (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID, 0)) != 0)
977                 goto fail;
978
979         if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
980                 change_egid(newcred, egid);
981                 setsugid(p);
982         }
983         if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
984                 change_rgid(newcred, rgid);
985                 setsugid(p);
986         }
987         if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
988             newcred->cr_svgid != newcred->cr_groups[0]) {
989                 change_svgid(newcred, newcred->cr_groups[0]);
990                 setsugid(p);
991         }
992         proc_set_cred(p, newcred);
993         PROC_UNLOCK(p);
994         crfree(oldcred);
995         return (0);
996
997 fail:
998         PROC_UNLOCK(p);
999         crfree(newcred);
1000         return (error);
1001 }
1002
1003 /*
1004  * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1005  * uid is explicit.
1006  */
1007 #ifndef _SYS_SYSPROTO_H_
1008 struct setresuid_args {
1009         uid_t   ruid;
1010         uid_t   euid;
1011         uid_t   suid;
1012 };
1013 #endif
1014 /* ARGSUSED */
1015 int
1016 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1017 {
1018         struct proc *p = td->td_proc;
1019         struct ucred *newcred, *oldcred;
1020         uid_t euid, ruid, suid;
1021         struct uidinfo *euip, *ruip;
1022         int error;
1023
1024         euid = uap->euid;
1025         ruid = uap->ruid;
1026         suid = uap->suid;
1027         AUDIT_ARG_EUID(euid);
1028         AUDIT_ARG_RUID(ruid);
1029         AUDIT_ARG_SUID(suid);
1030         newcred = crget();
1031         euip = uifind(euid);
1032         ruip = uifind(ruid);
1033         PROC_LOCK(p);
1034         oldcred = crcopysafe(p, newcred);
1035
1036 #ifdef MAC
1037         error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1038         if (error)
1039                 goto fail;
1040 #endif
1041
1042         if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1043              ruid != oldcred->cr_svuid &&
1044               ruid != oldcred->cr_uid) ||
1045              (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1046             euid != oldcred->cr_svuid &&
1047               euid != oldcred->cr_uid) ||
1048              (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1049             suid != oldcred->cr_svuid &&
1050               suid != oldcred->cr_uid)) &&
1051             (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID, 0)) != 0)
1052                 goto fail;
1053
1054         if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1055                 change_euid(newcred, euip);
1056                 setsugid(p);
1057         }
1058         if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1059                 change_ruid(newcred, ruip);
1060                 setsugid(p);
1061         }
1062         if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1063                 change_svuid(newcred, suid);
1064                 setsugid(p);
1065         }
1066         proc_set_cred(p, newcred);
1067         PROC_UNLOCK(p);
1068 #ifdef RACCT
1069         racct_proc_ucred_changed(p, oldcred, newcred);
1070 #endif
1071         uifree(ruip);
1072         uifree(euip);
1073         crfree(oldcred);
1074         return (0);
1075
1076 fail:
1077         PROC_UNLOCK(p);
1078         uifree(ruip);
1079         uifree(euip);
1080         crfree(newcred);
1081         return (error);
1082
1083 }
1084
1085 /*
1086  * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1087  * gid is explicit.
1088  */
1089 #ifndef _SYS_SYSPROTO_H_
1090 struct setresgid_args {
1091         gid_t   rgid;
1092         gid_t   egid;
1093         gid_t   sgid;
1094 };
1095 #endif
1096 /* ARGSUSED */
1097 int
1098 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1099 {
1100         struct proc *p = td->td_proc;
1101         struct ucred *newcred, *oldcred;
1102         gid_t egid, rgid, sgid;
1103         int error;
1104
1105         egid = uap->egid;
1106         rgid = uap->rgid;
1107         sgid = uap->sgid;
1108         AUDIT_ARG_EGID(egid);
1109         AUDIT_ARG_RGID(rgid);
1110         AUDIT_ARG_SGID(sgid);
1111         newcred = crget();
1112         PROC_LOCK(p);
1113         oldcred = crcopysafe(p, newcred);
1114
1115 #ifdef MAC
1116         error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1117         if (error)
1118                 goto fail;
1119 #endif
1120
1121         if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1122               rgid != oldcred->cr_svgid &&
1123               rgid != oldcred->cr_groups[0]) ||
1124              (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1125               egid != oldcred->cr_svgid &&
1126               egid != oldcred->cr_groups[0]) ||
1127              (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1128               sgid != oldcred->cr_svgid &&
1129               sgid != oldcred->cr_groups[0])) &&
1130             (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID, 0)) != 0)
1131                 goto fail;
1132
1133         if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1134                 change_egid(newcred, egid);
1135                 setsugid(p);
1136         }
1137         if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1138                 change_rgid(newcred, rgid);
1139                 setsugid(p);
1140         }
1141         if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1142                 change_svgid(newcred, sgid);
1143                 setsugid(p);
1144         }
1145         proc_set_cred(p, newcred);
1146         PROC_UNLOCK(p);
1147         crfree(oldcred);
1148         return (0);
1149
1150 fail:
1151         PROC_UNLOCK(p);
1152         crfree(newcred);
1153         return (error);
1154 }
1155
1156 #ifndef _SYS_SYSPROTO_H_
1157 struct getresuid_args {
1158         uid_t   *ruid;
1159         uid_t   *euid;
1160         uid_t   *suid;
1161 };
1162 #endif
1163 /* ARGSUSED */
1164 int
1165 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1166 {
1167         struct ucred *cred;
1168         int error1 = 0, error2 = 0, error3 = 0;
1169
1170         cred = td->td_ucred;
1171         if (uap->ruid)
1172                 error1 = copyout(&cred->cr_ruid,
1173                     uap->ruid, sizeof(cred->cr_ruid));
1174         if (uap->euid)
1175                 error2 = copyout(&cred->cr_uid,
1176                     uap->euid, sizeof(cred->cr_uid));
1177         if (uap->suid)
1178                 error3 = copyout(&cred->cr_svuid,
1179                     uap->suid, sizeof(cred->cr_svuid));
1180         return (error1 ? error1 : error2 ? error2 : error3);
1181 }
1182
1183 #ifndef _SYS_SYSPROTO_H_
1184 struct getresgid_args {
1185         gid_t   *rgid;
1186         gid_t   *egid;
1187         gid_t   *sgid;
1188 };
1189 #endif
1190 /* ARGSUSED */
1191 int
1192 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1193 {
1194         struct ucred *cred;
1195         int error1 = 0, error2 = 0, error3 = 0;
1196
1197         cred = td->td_ucred;
1198         if (uap->rgid)
1199                 error1 = copyout(&cred->cr_rgid,
1200                     uap->rgid, sizeof(cred->cr_rgid));
1201         if (uap->egid)
1202                 error2 = copyout(&cred->cr_groups[0],
1203                     uap->egid, sizeof(cred->cr_groups[0]));
1204         if (uap->sgid)
1205                 error3 = copyout(&cred->cr_svgid,
1206                     uap->sgid, sizeof(cred->cr_svgid));
1207         return (error1 ? error1 : error2 ? error2 : error3);
1208 }
1209
1210 #ifndef _SYS_SYSPROTO_H_
1211 struct issetugid_args {
1212         int dummy;
1213 };
1214 #endif
1215 /* ARGSUSED */
1216 int
1217 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1218 {
1219         struct proc *p = td->td_proc;
1220
1221         /*
1222          * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1223          * we use P_SUGID because we consider changing the owners as
1224          * "tainting" as well.
1225          * This is significant for procs that start as root and "become"
1226          * a user without an exec - programs cannot know *everything*
1227          * that libc *might* have put in their data segment.
1228          */
1229         td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1230         return (0);
1231 }
1232
1233 int
1234 sys___setugid(struct thread *td, struct __setugid_args *uap)
1235 {
1236 #ifdef REGRESSION
1237         struct proc *p;
1238
1239         p = td->td_proc;
1240         switch (uap->flag) {
1241         case 0:
1242                 PROC_LOCK(p);
1243                 p->p_flag &= ~P_SUGID;
1244                 PROC_UNLOCK(p);
1245                 return (0);
1246         case 1:
1247                 PROC_LOCK(p);
1248                 p->p_flag |= P_SUGID;
1249                 PROC_UNLOCK(p);
1250                 return (0);
1251         default:
1252                 return (EINVAL);
1253         }
1254 #else /* !REGRESSION */
1255
1256         return (ENOSYS);
1257 #endif /* REGRESSION */
1258 }
1259
1260 /*
1261  * Check if gid is a member of the group set.
1262  */
1263 int
1264 groupmember(gid_t gid, struct ucred *cred)
1265 {
1266         int l;
1267         int h;
1268         int m;
1269
1270         if (cred->cr_groups[0] == gid)
1271                 return(1);
1272
1273         /*
1274          * If gid was not our primary group, perform a binary search
1275          * of the supplemental groups.  This is possible because we
1276          * sort the groups in crsetgroups().
1277          */
1278         l = 1;
1279         h = cred->cr_ngroups;
1280         while (l < h) {
1281                 m = l + ((h - l) / 2);
1282                 if (cred->cr_groups[m] < gid)
1283                         l = m + 1; 
1284                 else
1285                         h = m; 
1286         }
1287         if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1288                 return (1);
1289
1290         return (0);
1291 }
1292
1293 /*
1294  * Test the active securelevel against a given level.  securelevel_gt()
1295  * implements (securelevel > level).  securelevel_ge() implements
1296  * (securelevel >= level).  Note that the logic is inverted -- these
1297  * functions return EPERM on "success" and 0 on "failure".
1298  *
1299  * Due to care taken when setting the securelevel, we know that no jail will
1300  * be less secure that its parent (or the physical system), so it is sufficient
1301  * to test the current jail only.
1302  *
1303  * XXXRW: Possibly since this has to do with privilege, it should move to
1304  * kern_priv.c.
1305  */
1306 int
1307 securelevel_gt(struct ucred *cr, int level)
1308 {
1309
1310         return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1311 }
1312
1313 int
1314 securelevel_ge(struct ucred *cr, int level)
1315 {
1316
1317         return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1318 }
1319
1320 /*
1321  * 'see_other_uids' determines whether or not visibility of processes
1322  * and sockets with credentials holding different real uids is possible
1323  * using a variety of system MIBs.
1324  * XXX: data declarations should be together near the beginning of the file.
1325  */
1326 static int      see_other_uids = 1;
1327 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1328     &see_other_uids, 0,
1329     "Unprivileged processes may see subjects/objects with different real uid");
1330
1331 /*-
1332  * Determine if u1 "can see" the subject specified by u2, according to the
1333  * 'see_other_uids' policy.
1334  * Returns: 0 for permitted, ESRCH otherwise
1335  * Locks: none
1336  * References: *u1 and *u2 must not change during the call
1337  *             u1 may equal u2, in which case only one reference is required
1338  */
1339 int
1340 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1341 {
1342
1343         if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1344                 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS, 0) != 0)
1345                         return (ESRCH);
1346         }
1347         return (0);
1348 }
1349
1350 /*
1351  * 'see_other_gids' determines whether or not visibility of processes
1352  * and sockets with credentials holding different real gids is possible
1353  * using a variety of system MIBs.
1354  * XXX: data declarations should be together near the beginning of the file.
1355  */
1356 static int      see_other_gids = 1;
1357 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1358     &see_other_gids, 0,
1359     "Unprivileged processes may see subjects/objects with different real gid");
1360
1361 /*
1362  * Determine if u1 can "see" the subject specified by u2, according to the
1363  * 'see_other_gids' policy.
1364  * Returns: 0 for permitted, ESRCH otherwise
1365  * Locks: none
1366  * References: *u1 and *u2 must not change during the call
1367  *             u1 may equal u2, in which case only one reference is required
1368  */
1369 int
1370 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1371 {
1372         int i, match;
1373         
1374         if (!see_other_gids) {
1375                 match = 0;
1376                 for (i = 0; i < u1->cr_ngroups; i++) {
1377                         if (groupmember(u1->cr_groups[i], u2))
1378                                 match = 1;
1379                         if (match)
1380                                 break;
1381                 }
1382                 if (!match) {
1383                         if (priv_check_cred(u1, PRIV_SEEOTHERGIDS, 0) != 0)
1384                                 return (ESRCH);
1385                 }
1386         }
1387         return (0);
1388 }
1389
1390 /*
1391  * 'see_jail_proc' determines whether or not visibility of processes and
1392  * sockets with credentials holding different jail ids is possible using a
1393  * variety of system MIBs.
1394  *
1395  * XXX: data declarations should be together near the beginning of the file.
1396  */
1397
1398 static int      see_jail_proc = 1;
1399 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1400     &see_jail_proc, 0,
1401     "Unprivileged processes may see subjects/objects with different jail ids");
1402
1403 /*-
1404  * Determine if u1 "can see" the subject specified by u2, according to the
1405  * 'see_jail_proc' policy.
1406  * Returns: 0 for permitted, ESRCH otherwise
1407  * Locks: none
1408  * References: *u1 and *u2 must not change during the call
1409  *             u1 may equal u2, in which case only one reference is required
1410  */
1411 int
1412 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1413 {
1414         if (u1->cr_uid == 0)
1415                 return (0);
1416         return (!see_jail_proc && u1->cr_prison != u2->cr_prison ? ESRCH : 0);
1417 }
1418
1419 /*-
1420  * Determine if u1 "can see" the subject specified by u2.
1421  * Returns: 0 for permitted, an errno value otherwise
1422  * Locks: none
1423  * References: *u1 and *u2 must not change during the call
1424  *             u1 may equal u2, in which case only one reference is required
1425  */
1426 int
1427 cr_cansee(struct ucred *u1, struct ucred *u2)
1428 {
1429         int error;
1430
1431         if ((error = prison_check(u1, u2)))
1432                 return (error);
1433 #ifdef MAC
1434         if ((error = mac_cred_check_visible(u1, u2)))
1435                 return (error);
1436 #endif
1437         if ((error = cr_canseeotheruids(u1, u2)))
1438                 return (error);
1439         if ((error = cr_canseeothergids(u1, u2)))
1440                 return (error);
1441         if ((error = cr_canseejailproc(u1, u2)))
1442                 return (error);
1443         return (0);
1444 }
1445
1446 /*-
1447  * Determine if td "can see" the subject specified by p.
1448  * Returns: 0 for permitted, an errno value otherwise
1449  * Locks: Sufficient locks to protect p->p_ucred must be held.  td really
1450  *        should be curthread.
1451  * References: td and p must be valid for the lifetime of the call
1452  */
1453 int
1454 p_cansee(struct thread *td, struct proc *p)
1455 {
1456
1457         /* Wrap cr_cansee() for all functionality. */
1458         KASSERT(td == curthread, ("%s: td not curthread", __func__));
1459         PROC_LOCK_ASSERT(p, MA_OWNED);
1460         return (cr_cansee(td->td_ucred, p->p_ucred));
1461 }
1462
1463 /*
1464  * 'conservative_signals' prevents the delivery of a broad class of
1465  * signals by unprivileged processes to processes that have changed their
1466  * credentials since the last invocation of execve().  This can prevent
1467  * the leakage of cached information or retained privileges as a result
1468  * of a common class of signal-related vulnerabilities.  However, this
1469  * may interfere with some applications that expect to be able to
1470  * deliver these signals to peer processes after having given up
1471  * privilege.
1472  */
1473 static int      conservative_signals = 1;
1474 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1475     &conservative_signals, 0, "Unprivileged processes prevented from "
1476     "sending certain signals to processes whose credentials have changed");
1477 /*-
1478  * Determine whether cred may deliver the specified signal to proc.
1479  * Returns: 0 for permitted, an errno value otherwise.
1480  * Locks: A lock must be held for proc.
1481  * References: cred and proc must be valid for the lifetime of the call.
1482  */
1483 int
1484 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1485 {
1486         int error;
1487
1488         PROC_LOCK_ASSERT(proc, MA_OWNED);
1489         /*
1490          * Jail semantics limit the scope of signalling to proc in the
1491          * same jail as cred, if cred is in jail.
1492          */
1493         error = prison_check(cred, proc->p_ucred);
1494         if (error)
1495                 return (error);
1496 #ifdef MAC
1497         if ((error = mac_proc_check_signal(cred, proc, signum)))
1498                 return (error);
1499 #endif
1500         if ((error = cr_canseeotheruids(cred, proc->p_ucred)))
1501                 return (error);
1502         if ((error = cr_canseeothergids(cred, proc->p_ucred)))
1503                 return (error);
1504
1505         /*
1506          * UNIX signal semantics depend on the status of the P_SUGID
1507          * bit on the target process.  If the bit is set, then additional
1508          * restrictions are placed on the set of available signals.
1509          */
1510         if (conservative_signals && (proc->p_flag & P_SUGID)) {
1511                 switch (signum) {
1512                 case 0:
1513                 case SIGKILL:
1514                 case SIGINT:
1515                 case SIGTERM:
1516                 case SIGALRM:
1517                 case SIGSTOP:
1518                 case SIGTTIN:
1519                 case SIGTTOU:
1520                 case SIGTSTP:
1521                 case SIGHUP:
1522                 case SIGUSR1:
1523                 case SIGUSR2:
1524                         /*
1525                          * Generally, permit job and terminal control
1526                          * signals.
1527                          */
1528                         break;
1529                 default:
1530                         /* Not permitted without privilege. */
1531                         error = priv_check_cred(cred, PRIV_SIGNAL_SUGID, 0);
1532                         if (error)
1533                                 return (error);
1534                 }
1535         }
1536
1537         /*
1538          * Generally, the target credential's ruid or svuid must match the
1539          * subject credential's ruid or euid.
1540          */
1541         if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
1542             cred->cr_ruid != proc->p_ucred->cr_svuid &&
1543             cred->cr_uid != proc->p_ucred->cr_ruid &&
1544             cred->cr_uid != proc->p_ucred->cr_svuid) {
1545                 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED, 0);
1546                 if (error)
1547                         return (error);
1548         }
1549
1550         return (0);
1551 }
1552
1553 /*-
1554  * Determine whether td may deliver the specified signal to p.
1555  * Returns: 0 for permitted, an errno value otherwise
1556  * Locks: Sufficient locks to protect various components of td and p
1557  *        must be held.  td must be curthread, and a lock must be
1558  *        held for p.
1559  * References: td and p must be valid for the lifetime of the call
1560  */
1561 int
1562 p_cansignal(struct thread *td, struct proc *p, int signum)
1563 {
1564
1565         KASSERT(td == curthread, ("%s: td not curthread", __func__));
1566         PROC_LOCK_ASSERT(p, MA_OWNED);
1567         if (td->td_proc == p)
1568                 return (0);
1569
1570         /*
1571          * UNIX signalling semantics require that processes in the same
1572          * session always be able to deliver SIGCONT to one another,
1573          * overriding the remaining protections.
1574          */
1575         /* XXX: This will require an additional lock of some sort. */
1576         if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
1577                 return (0);
1578         /*
1579          * Some compat layers use SIGTHR and higher signals for
1580          * communication between different kernel threads of the same
1581          * process, so that they expect that it's always possible to
1582          * deliver them, even for suid applications where cr_cansignal() can
1583          * deny such ability for security consideration.  It should be
1584          * pretty safe to do since the only way to create two processes
1585          * with the same p_leader is via rfork(2).
1586          */
1587         if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
1588             signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
1589                 return (0);
1590
1591         return (cr_cansignal(td->td_ucred, p, signum));
1592 }
1593
1594 /*-
1595  * Determine whether td may reschedule p.
1596  * Returns: 0 for permitted, an errno value otherwise
1597  * Locks: Sufficient locks to protect various components of td and p
1598  *        must be held.  td must be curthread, and a lock must
1599  *        be held for p.
1600  * References: td and p must be valid for the lifetime of the call
1601  */
1602 int
1603 p_cansched(struct thread *td, struct proc *p)
1604 {
1605         int error;
1606
1607         KASSERT(td == curthread, ("%s: td not curthread", __func__));
1608         PROC_LOCK_ASSERT(p, MA_OWNED);
1609         if (td->td_proc == p)
1610                 return (0);
1611         if ((error = prison_check(td->td_ucred, p->p_ucred)))
1612                 return (error);
1613 #ifdef MAC
1614         if ((error = mac_proc_check_sched(td->td_ucred, p)))
1615                 return (error);
1616 #endif
1617         if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1618                 return (error);
1619         if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1620                 return (error);
1621         if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
1622             td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
1623                 error = priv_check(td, PRIV_SCHED_DIFFCRED);
1624                 if (error)
1625                         return (error);
1626         }
1627         return (0);
1628 }
1629
1630 /*
1631  * The 'unprivileged_proc_debug' flag may be used to disable a variety of
1632  * unprivileged inter-process debugging services, including some procfs
1633  * functionality, ptrace(), and ktrace().  In the past, inter-process
1634  * debugging has been involved in a variety of security problems, and sites
1635  * not requiring the service might choose to disable it when hardening
1636  * systems.
1637  *
1638  * XXX: Should modifying and reading this variable require locking?
1639  * XXX: data declarations should be together near the beginning of the file.
1640  */
1641 static int      unprivileged_proc_debug = 1;
1642 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
1643     &unprivileged_proc_debug, 0,
1644     "Unprivileged processes may use process debugging facilities");
1645
1646 /*-
1647  * Determine whether td may debug p.
1648  * Returns: 0 for permitted, an errno value otherwise
1649  * Locks: Sufficient locks to protect various components of td and p
1650  *        must be held.  td must be curthread, and a lock must
1651  *        be held for p.
1652  * References: td and p must be valid for the lifetime of the call
1653  */
1654 int
1655 p_candebug(struct thread *td, struct proc *p)
1656 {
1657         int credentialchanged, error, grpsubset, i, uidsubset;
1658
1659         KASSERT(td == curthread, ("%s: td not curthread", __func__));
1660         PROC_LOCK_ASSERT(p, MA_OWNED);
1661         if (!unprivileged_proc_debug) {
1662                 error = priv_check(td, PRIV_DEBUG_UNPRIV);
1663                 if (error)
1664                         return (error);
1665         }
1666         if (td->td_proc == p)
1667                 return (0);
1668         if ((error = prison_check(td->td_ucred, p->p_ucred)))
1669                 return (error);
1670 #ifdef MAC
1671         if ((error = mac_proc_check_debug(td->td_ucred, p)))
1672                 return (error);
1673 #endif
1674         if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1675                 return (error);
1676         if ((error = cr_canseeothergids(td->td_ucred, p->p_ucred)))
1677                 return (error);
1678
1679         /*
1680          * Is p's group set a subset of td's effective group set?  This
1681          * includes p's egid, group access list, rgid, and svgid.
1682          */
1683         grpsubset = 1;
1684         for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
1685                 if (!groupmember(p->p_ucred->cr_groups[i], td->td_ucred)) {
1686                         grpsubset = 0;
1687                         break;
1688                 }
1689         }
1690         grpsubset = grpsubset &&
1691             groupmember(p->p_ucred->cr_rgid, td->td_ucred) &&
1692             groupmember(p->p_ucred->cr_svgid, td->td_ucred);
1693
1694         /*
1695          * Are the uids present in p's credential equal to td's
1696          * effective uid?  This includes p's euid, svuid, and ruid.
1697          */
1698         uidsubset = (td->td_ucred->cr_uid == p->p_ucred->cr_uid &&
1699             td->td_ucred->cr_uid == p->p_ucred->cr_svuid &&
1700             td->td_ucred->cr_uid == p->p_ucred->cr_ruid);
1701
1702         /*
1703          * Has the credential of the process changed since the last exec()?
1704          */
1705         credentialchanged = (p->p_flag & P_SUGID);
1706
1707         /*
1708          * If p's gids aren't a subset, or the uids aren't a subset,
1709          * or the credential has changed, require appropriate privilege
1710          * for td to debug p.
1711          */
1712         if (!grpsubset || !uidsubset) {
1713                 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
1714                 if (error)
1715                         return (error);
1716         }
1717
1718         if (credentialchanged) {
1719                 error = priv_check(td, PRIV_DEBUG_SUGID);
1720                 if (error)
1721                         return (error);
1722         }
1723
1724         /* Can't trace init when securelevel > 0. */
1725         if (p == initproc) {
1726                 error = securelevel_gt(td->td_ucred, 0);
1727                 if (error)
1728                         return (error);
1729         }
1730
1731         /*
1732          * Can't trace a process that's currently exec'ing.
1733          *
1734          * XXX: Note, this is not a security policy decision, it's a
1735          * basic correctness/functionality decision.  Therefore, this check
1736          * should be moved to the caller's of p_candebug().
1737          */
1738         if ((p->p_flag & P_INEXEC) != 0)
1739                 return (EBUSY);
1740
1741         /* Denied explicitely */
1742         if ((p->p_flag2 & P2_NOTRACE) != 0) {
1743                 error = priv_check(td, PRIV_DEBUG_DENIED);
1744                 if (error != 0)
1745                         return (error);
1746         }
1747
1748         return (0);
1749 }
1750
1751 /*-
1752  * Determine whether the subject represented by cred can "see" a socket.
1753  * Returns: 0 for permitted, ENOENT otherwise.
1754  */
1755 int
1756 cr_canseesocket(struct ucred *cred, struct socket *so)
1757 {
1758         int error;
1759
1760         error = prison_check(cred, so->so_cred);
1761         if (error)
1762                 return (ENOENT);
1763 #ifdef MAC
1764         error = mac_socket_check_visible(cred, so);
1765         if (error)
1766                 return (error);
1767 #endif
1768         if (cr_canseeotheruids(cred, so->so_cred))
1769                 return (ENOENT);
1770         if (cr_canseeothergids(cred, so->so_cred))
1771                 return (ENOENT);
1772
1773         return (0);
1774 }
1775
1776 /*-
1777  * Determine whether td can wait for the exit of p.
1778  * Returns: 0 for permitted, an errno value otherwise
1779  * Locks: Sufficient locks to protect various components of td and p
1780  *        must be held.  td must be curthread, and a lock must
1781  *        be held for p.
1782  * References: td and p must be valid for the lifetime of the call
1783
1784  */
1785 int
1786 p_canwait(struct thread *td, struct proc *p)
1787 {
1788         int error;
1789
1790         KASSERT(td == curthread, ("%s: td not curthread", __func__));
1791         PROC_LOCK_ASSERT(p, MA_OWNED);
1792         if ((error = prison_check(td->td_ucred, p->p_ucred)))
1793                 return (error);
1794 #ifdef MAC
1795         if ((error = mac_proc_check_wait(td->td_ucred, p)))
1796                 return (error);
1797 #endif
1798 #if 0
1799         /* XXXMAC: This could have odd effects on some shells. */
1800         if ((error = cr_canseeotheruids(td->td_ucred, p->p_ucred)))
1801                 return (error);
1802 #endif
1803
1804         return (0);
1805 }
1806
1807 /*
1808  * Allocate a zeroed cred structure.
1809  */
1810 struct ucred *
1811 crget(void)
1812 {
1813         struct ucred *cr;
1814
1815         cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
1816         refcount_init(&cr->cr_ref, 1);
1817 #ifdef AUDIT
1818         audit_cred_init(cr);
1819 #endif
1820 #ifdef MAC
1821         mac_cred_init(cr);
1822 #endif
1823         cr->cr_groups = cr->cr_smallgroups;
1824         cr->cr_agroups =
1825             sizeof(cr->cr_smallgroups) / sizeof(cr->cr_smallgroups[0]);
1826         return (cr);
1827 }
1828
1829 /*
1830  * Claim another reference to a ucred structure.
1831  */
1832 struct ucred *
1833 crhold(struct ucred *cr)
1834 {
1835
1836         refcount_acquire(&cr->cr_ref);
1837         return (cr);
1838 }
1839
1840 /*
1841  * Free a cred structure.  Throws away space when ref count gets to 0.
1842  */
1843 void
1844 crfree(struct ucred *cr)
1845 {
1846
1847         KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
1848         KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
1849         if (refcount_release(&cr->cr_ref)) {
1850                 /*
1851                  * Some callers of crget(), such as nfs_statfs(),
1852                  * allocate a temporary credential, but don't
1853                  * allocate a uidinfo structure.
1854                  */
1855                 if (cr->cr_uidinfo != NULL)
1856                         uifree(cr->cr_uidinfo);
1857                 if (cr->cr_ruidinfo != NULL)
1858                         uifree(cr->cr_ruidinfo);
1859                 /*
1860                  * Free a prison, if any.
1861                  */
1862                 if (cr->cr_prison != NULL)
1863                         prison_free(cr->cr_prison);
1864                 if (cr->cr_loginclass != NULL)
1865                         loginclass_free(cr->cr_loginclass);
1866 #ifdef AUDIT
1867                 audit_cred_destroy(cr);
1868 #endif
1869 #ifdef MAC
1870                 mac_cred_destroy(cr);
1871 #endif
1872                 if (cr->cr_groups != cr->cr_smallgroups)
1873                         free(cr->cr_groups, M_CRED);
1874                 free(cr, M_CRED);
1875         }
1876 }
1877
1878 /*
1879  * Copy a ucred's contents from a template.  Does not block.
1880  */
1881 void
1882 crcopy(struct ucred *dest, struct ucred *src)
1883 {
1884
1885         KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
1886         bcopy(&src->cr_startcopy, &dest->cr_startcopy,
1887             (unsigned)((caddr_t)&src->cr_endcopy -
1888                 (caddr_t)&src->cr_startcopy));
1889         crsetgroups(dest, src->cr_ngroups, src->cr_groups);
1890         uihold(dest->cr_uidinfo);
1891         uihold(dest->cr_ruidinfo);
1892         prison_hold(dest->cr_prison);
1893         loginclass_hold(dest->cr_loginclass);
1894 #ifdef AUDIT
1895         audit_cred_copy(src, dest);
1896 #endif
1897 #ifdef MAC
1898         mac_cred_copy(src, dest);
1899 #endif
1900 }
1901
1902 /*
1903  * Dup cred struct to a new held one.
1904  */
1905 struct ucred *
1906 crdup(struct ucred *cr)
1907 {
1908         struct ucred *newcr;
1909
1910         newcr = crget();
1911         crcopy(newcr, cr);
1912         return (newcr);
1913 }
1914
1915 /*
1916  * Fill in a struct xucred based on a struct ucred.
1917  */
1918 void
1919 cru2x(struct ucred *cr, struct xucred *xcr)
1920 {
1921         int ngroups;
1922
1923         bzero(xcr, sizeof(*xcr));
1924         xcr->cr_version = XUCRED_VERSION;
1925         xcr->cr_uid = cr->cr_uid;
1926
1927         ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
1928         xcr->cr_ngroups = ngroups;
1929         bcopy(cr->cr_groups, xcr->cr_groups,
1930             ngroups * sizeof(*cr->cr_groups));
1931 }
1932
1933 /*
1934  * Set initial process credentials.
1935  * Callers are responsible for providing the reference for provided credentials.
1936  */
1937 void
1938 proc_set_cred_init(struct proc *p, struct ucred *newcred)
1939 {
1940
1941         p->p_ucred = newcred;
1942 }
1943
1944 /*
1945  * Change process credentials.
1946  * Callers are responsible for providing the reference for passed credentials
1947  * and for freeing old ones.
1948  *
1949  * Process has to be locked except when it does not have credentials (as it
1950  * should not be visible just yet) or when newcred is NULL (as this can be
1951  * only used when the process is about to be freed, at which point it should
1952  * not be visible anymore).
1953  */
1954 struct ucred *
1955 proc_set_cred(struct proc *p, struct ucred *newcred)
1956 {
1957         struct ucred *oldcred;
1958
1959         MPASS(p->p_ucred != NULL);
1960         if (newcred == NULL)
1961                 MPASS(p->p_state == PRS_ZOMBIE);
1962         else
1963                 PROC_LOCK_ASSERT(p, MA_OWNED);
1964
1965         oldcred = p->p_ucred;
1966         p->p_ucred = newcred;
1967         if (newcred != NULL)
1968                 PROC_UPDATE_COW(p);
1969         return (oldcred);
1970 }
1971
1972 struct ucred *
1973 crcopysafe(struct proc *p, struct ucred *cr)
1974 {
1975         struct ucred *oldcred;
1976         int groups;
1977
1978         PROC_LOCK_ASSERT(p, MA_OWNED);
1979
1980         oldcred = p->p_ucred;
1981         while (cr->cr_agroups < oldcred->cr_agroups) {
1982                 groups = oldcred->cr_agroups;
1983                 PROC_UNLOCK(p);
1984                 crextend(cr, groups);
1985                 PROC_LOCK(p);
1986                 oldcred = p->p_ucred;
1987         }
1988         crcopy(cr, oldcred);
1989
1990         return (oldcred);
1991 }
1992
1993 /*
1994  * Extend the passed in credential to hold n items.
1995  */
1996 void
1997 crextend(struct ucred *cr, int n)
1998 {
1999         int cnt;
2000
2001         /* Truncate? */
2002         if (n <= cr->cr_agroups)
2003                 return;
2004
2005         /*
2006          * We extend by 2 each time since we're using a power of two
2007          * allocator until we need enough groups to fill a page.
2008          * Once we're allocating multiple pages, only allocate as many
2009          * as we actually need.  The case of processes needing a
2010          * non-power of two number of pages seems more likely than
2011          * a real world process that adds thousands of groups one at a
2012          * time.
2013          */
2014         if ( n < PAGE_SIZE / sizeof(gid_t) ) {
2015                 if (cr->cr_agroups == 0)
2016                         cnt = MINALLOCSIZE / sizeof(gid_t);
2017                 else
2018                         cnt = cr->cr_agroups * 2;
2019
2020                 while (cnt < n)
2021                         cnt *= 2;
2022         } else
2023                 cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
2024
2025         /* Free the old array. */
2026         if (cr->cr_groups != cr->cr_smallgroups)
2027                 free(cr->cr_groups, M_CRED);
2028
2029         cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
2030         cr->cr_agroups = cnt;
2031 }
2032
2033 /*
2034  * Copy groups in to a credential, preserving any necessary invariants.
2035  * Currently this includes the sorting of all supplemental gids.
2036  * crextend() must have been called before hand to ensure sufficient
2037  * space is available.
2038  */
2039 static void
2040 crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
2041 {
2042         int i;
2043         int j;
2044         gid_t g;
2045         
2046         KASSERT(cr->cr_agroups >= ngrp, ("cr_ngroups is too small"));
2047
2048         bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2049         cr->cr_ngroups = ngrp;
2050
2051         /*
2052          * Sort all groups except cr_groups[0] to allow groupmember to
2053          * perform a binary search.
2054          *
2055          * XXX: If large numbers of groups become common this should
2056          * be replaced with shell sort like linux uses or possibly
2057          * heap sort.
2058          */
2059         for (i = 2; i < ngrp; i++) {
2060                 g = cr->cr_groups[i];
2061                 for (j = i-1; j >= 1 && g < cr->cr_groups[j]; j--)
2062                         cr->cr_groups[j + 1] = cr->cr_groups[j];
2063                 cr->cr_groups[j + 1] = g;
2064         }
2065 }
2066
2067 /*
2068  * Copy groups in to a credential after expanding it if required.
2069  * Truncate the list to (ngroups_max + 1) if it is too large.
2070  */
2071 void
2072 crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
2073 {
2074
2075         if (ngrp > ngroups_max + 1)
2076                 ngrp = ngroups_max + 1;
2077
2078         crextend(cr, ngrp);
2079         crsetgroups_locked(cr, ngrp, groups);
2080 }
2081
2082 /*
2083  * Get login name, if available.
2084  */
2085 #ifndef _SYS_SYSPROTO_H_
2086 struct getlogin_args {
2087         char    *namebuf;
2088         u_int   namelen;
2089 };
2090 #endif
2091 /* ARGSUSED */
2092 int
2093 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2094 {
2095         char login[MAXLOGNAME];
2096         struct proc *p = td->td_proc;
2097         size_t len;
2098
2099         if (uap->namelen > MAXLOGNAME)
2100                 uap->namelen = MAXLOGNAME;
2101         PROC_LOCK(p);
2102         SESS_LOCK(p->p_session);
2103         len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2104         SESS_UNLOCK(p->p_session);
2105         PROC_UNLOCK(p);
2106         if (len > uap->namelen)
2107                 return (ERANGE);
2108         return (copyout(login, uap->namebuf, len));
2109 }
2110
2111 /*
2112  * Set login name.
2113  */
2114 #ifndef _SYS_SYSPROTO_H_
2115 struct setlogin_args {
2116         char    *namebuf;
2117 };
2118 #endif
2119 /* ARGSUSED */
2120 int
2121 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2122 {
2123         struct proc *p = td->td_proc;
2124         int error;
2125         char logintmp[MAXLOGNAME];
2126
2127         CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2128
2129         error = priv_check(td, PRIV_PROC_SETLOGIN);
2130         if (error)
2131                 return (error);
2132         error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2133         if (error != 0) {
2134                 if (error == ENAMETOOLONG)
2135                         error = EINVAL;
2136                 return (error);
2137         }
2138         AUDIT_ARG_LOGIN(logintmp);
2139         PROC_LOCK(p);
2140         SESS_LOCK(p->p_session);
2141         strcpy(p->p_session->s_login, logintmp);
2142         SESS_UNLOCK(p->p_session);
2143         PROC_UNLOCK(p);
2144         return (0);
2145 }
2146
2147 void
2148 setsugid(struct proc *p)
2149 {
2150
2151         PROC_LOCK_ASSERT(p, MA_OWNED);
2152         p->p_flag |= P_SUGID;
2153         if (!(p->p_pfsflags & PF_ISUGID))
2154                 p->p_stops = 0;
2155 }
2156
2157 /*-
2158  * Change a process's effective uid.
2159  * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2160  * References: newcred must be an exclusive credential reference for the
2161  *             duration of the call.
2162  */
2163 void
2164 change_euid(struct ucred *newcred, struct uidinfo *euip)
2165 {
2166
2167         newcred->cr_uid = euip->ui_uid;
2168         uihold(euip);
2169         uifree(newcred->cr_uidinfo);
2170         newcred->cr_uidinfo = euip;
2171 }
2172
2173 /*-
2174  * Change a process's effective gid.
2175  * Side effects: newcred->cr_gid will be modified.
2176  * References: newcred must be an exclusive credential reference for the
2177  *             duration of the call.
2178  */
2179 void
2180 change_egid(struct ucred *newcred, gid_t egid)
2181 {
2182
2183         newcred->cr_groups[0] = egid;
2184 }
2185
2186 /*-
2187  * Change a process's real uid.
2188  * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
2189  *               will be updated, and the old and new cr_ruidinfo proc
2190  *               counts will be updated.
2191  * References: newcred must be an exclusive credential reference for the
2192  *             duration of the call.
2193  */
2194 void
2195 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
2196 {
2197
2198         (void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
2199         newcred->cr_ruid = ruip->ui_uid;
2200         uihold(ruip);
2201         uifree(newcred->cr_ruidinfo);
2202         newcred->cr_ruidinfo = ruip;
2203         (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2204 }
2205
2206 /*-
2207  * Change a process's real gid.
2208  * Side effects: newcred->cr_rgid will be updated.
2209  * References: newcred must be an exclusive credential reference for the
2210  *             duration of the call.
2211  */
2212 void
2213 change_rgid(struct ucred *newcred, gid_t rgid)
2214 {
2215
2216         newcred->cr_rgid = rgid;
2217 }
2218
2219 /*-
2220  * Change a process's saved uid.
2221  * Side effects: newcred->cr_svuid will be updated.
2222  * References: newcred must be an exclusive credential reference for the
2223  *             duration of the call.
2224  */
2225 void
2226 change_svuid(struct ucred *newcred, uid_t svuid)
2227 {
2228
2229         newcred->cr_svuid = svuid;
2230 }
2231
2232 /*-
2233  * Change a process's saved gid.
2234  * Side effects: newcred->cr_svgid will be updated.
2235  * References: newcred must be an exclusive credential reference for the
2236  *             duration of the call.
2237  */
2238 void
2239 change_svgid(struct ucred *newcred, gid_t svgid)
2240 {
2241
2242         newcred->cr_svgid = svgid;
2243 }