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