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
40 /*
41 * System calls related to processes and protection
42 */
43
44 #include <sys/cdefs.h>
45 #include "opt_inet.h"
46 #include "opt_inet6.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/abi_compat.h>
51 #include <sys/acct.h>
52 #include <sys/kdb.h>
53 #include <sys/kernel.h>
54 #include <sys/libkern.h>
55 #include <sys/lock.h>
56 #include <sys/loginclass.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/ptrace.h>
60 #include <sys/refcount.h>
61 #include <sys/sx.h>
62 #include <sys/priv.h>
63 #include <sys/proc.h>
64 #ifdef COMPAT_43
65 #include <sys/sysent.h>
66 #endif
67 #include <sys/sysproto.h>
68 #include <sys/jail.h>
69 #include <sys/racct.h>
70 #include <sys/rctl.h>
71 #include <sys/resourcevar.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/syscallsubr.h>
75 #include <sys/sysctl.h>
76
77 #ifdef MAC
78 #include <security/mac/mac_syscalls.h>
79 #endif
80
81 #include <vm/uma.h>
82
83 #ifdef REGRESSION
84 FEATURE(regression,
85 "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
86 #endif
87
88 #include <security/audit/audit.h>
89 #include <security/mac/mac_framework.h>
90
91 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
92
93 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
94 "BSD security policy");
95
96 static void crfree_final(struct ucred *cr);
97
98 static inline void
groups_check_positive_len(int ngrp)99 groups_check_positive_len(int ngrp)
100 {
101 MPASS2(ngrp >= 0, "negative number of groups");
102 MPASS2(ngrp != 0, "at least one group expected (effective GID)");
103 }
104 static inline void
groups_check_max_len(int ngrp)105 groups_check_max_len(int ngrp)
106 {
107 MPASS2(ngrp <= ngroups_max + 1, "too many groups");
108 }
109
110 static void groups_normalize(int *ngrp, gid_t *groups);
111 static void crsetgroups_internal(struct ucred *cr, int ngrp,
112 const gid_t *groups);
113
114 static int cr_canseeotheruids(struct ucred *u1, struct ucred *u2);
115 static int cr_canseeothergids(struct ucred *u1, struct ucred *u2);
116 static int cr_canseejailproc(struct ucred *u1, struct ucred *u2);
117
118 #ifndef _SYS_SYSPROTO_H_
119 struct getpid_args {
120 int dummy;
121 };
122 #endif
123 /* ARGSUSED */
124 int
sys_getpid(struct thread * td,struct getpid_args * uap)125 sys_getpid(struct thread *td, struct getpid_args *uap)
126 {
127 struct proc *p = td->td_proc;
128
129 td->td_retval[0] = p->p_pid;
130 #if defined(COMPAT_43)
131 if (SV_PROC_FLAG(p, SV_AOUT))
132 td->td_retval[1] = kern_getppid(td);
133 #endif
134 return (0);
135 }
136
137 #ifndef _SYS_SYSPROTO_H_
138 struct getppid_args {
139 int dummy;
140 };
141 #endif
142 /* ARGSUSED */
143 int
sys_getppid(struct thread * td,struct getppid_args * uap)144 sys_getppid(struct thread *td, struct getppid_args *uap)
145 {
146
147 td->td_retval[0] = kern_getppid(td);
148 return (0);
149 }
150
151 int
kern_getppid(struct thread * td)152 kern_getppid(struct thread *td)
153 {
154 struct proc *p = td->td_proc;
155
156 return (p->p_oppid);
157 }
158
159 /*
160 * Get process group ID; note that POSIX getpgrp takes no parameter.
161 */
162 #ifndef _SYS_SYSPROTO_H_
163 struct getpgrp_args {
164 int dummy;
165 };
166 #endif
167 int
sys_getpgrp(struct thread * td,struct getpgrp_args * uap)168 sys_getpgrp(struct thread *td, struct getpgrp_args *uap)
169 {
170 struct proc *p = td->td_proc;
171
172 PROC_LOCK(p);
173 td->td_retval[0] = p->p_pgrp->pg_id;
174 PROC_UNLOCK(p);
175 return (0);
176 }
177
178 /* Get an arbitrary pid's process group id */
179 #ifndef _SYS_SYSPROTO_H_
180 struct getpgid_args {
181 pid_t pid;
182 };
183 #endif
184 int
sys_getpgid(struct thread * td,struct getpgid_args * uap)185 sys_getpgid(struct thread *td, struct getpgid_args *uap)
186 {
187 struct proc *p;
188 int error;
189
190 if (uap->pid == 0) {
191 p = td->td_proc;
192 PROC_LOCK(p);
193 } else {
194 p = pfind(uap->pid);
195 if (p == NULL)
196 return (ESRCH);
197 error = p_cansee(td, p);
198 if (error) {
199 PROC_UNLOCK(p);
200 return (error);
201 }
202 }
203 td->td_retval[0] = p->p_pgrp->pg_id;
204 PROC_UNLOCK(p);
205 return (0);
206 }
207
208 /*
209 * Get an arbitrary pid's session id.
210 */
211 #ifndef _SYS_SYSPROTO_H_
212 struct getsid_args {
213 pid_t pid;
214 };
215 #endif
216 int
sys_getsid(struct thread * td,struct getsid_args * uap)217 sys_getsid(struct thread *td, struct getsid_args *uap)
218 {
219
220 return (kern_getsid(td, uap->pid));
221 }
222
223 int
kern_getsid(struct thread * td,pid_t pid)224 kern_getsid(struct thread *td, pid_t pid)
225 {
226 struct proc *p;
227 int error;
228
229 if (pid == 0) {
230 p = td->td_proc;
231 PROC_LOCK(p);
232 } else {
233 p = pfind(pid);
234 if (p == NULL)
235 return (ESRCH);
236 error = p_cansee(td, p);
237 if (error) {
238 PROC_UNLOCK(p);
239 return (error);
240 }
241 }
242 td->td_retval[0] = p->p_session->s_sid;
243 PROC_UNLOCK(p);
244 return (0);
245 }
246
247 #ifndef _SYS_SYSPROTO_H_
248 struct getuid_args {
249 int dummy;
250 };
251 #endif
252 /* ARGSUSED */
253 int
sys_getuid(struct thread * td,struct getuid_args * uap)254 sys_getuid(struct thread *td, struct getuid_args *uap)
255 {
256
257 td->td_retval[0] = td->td_ucred->cr_ruid;
258 #if defined(COMPAT_43)
259 td->td_retval[1] = td->td_ucred->cr_uid;
260 #endif
261 return (0);
262 }
263
264 #ifndef _SYS_SYSPROTO_H_
265 struct geteuid_args {
266 int dummy;
267 };
268 #endif
269 /* ARGSUSED */
270 int
sys_geteuid(struct thread * td,struct geteuid_args * uap)271 sys_geteuid(struct thread *td, struct geteuid_args *uap)
272 {
273
274 td->td_retval[0] = td->td_ucred->cr_uid;
275 return (0);
276 }
277
278 #ifndef _SYS_SYSPROTO_H_
279 struct getgid_args {
280 int dummy;
281 };
282 #endif
283 /* ARGSUSED */
284 int
sys_getgid(struct thread * td,struct getgid_args * uap)285 sys_getgid(struct thread *td, struct getgid_args *uap)
286 {
287
288 td->td_retval[0] = td->td_ucred->cr_rgid;
289 #if defined(COMPAT_43)
290 td->td_retval[1] = td->td_ucred->cr_groups[0];
291 #endif
292 return (0);
293 }
294
295 /*
296 * Get effective group ID. The "egid" is groups[0], and could be obtained
297 * via getgroups. This syscall exists because it is somewhat painful to do
298 * correctly in a library function.
299 */
300 #ifndef _SYS_SYSPROTO_H_
301 struct getegid_args {
302 int dummy;
303 };
304 #endif
305 /* ARGSUSED */
306 int
sys_getegid(struct thread * td,struct getegid_args * uap)307 sys_getegid(struct thread *td, struct getegid_args *uap)
308 {
309
310 td->td_retval[0] = td->td_ucred->cr_groups[0];
311 return (0);
312 }
313
314 #ifndef _SYS_SYSPROTO_H_
315 struct getgroups_args {
316 int gidsetsize;
317 gid_t *gidset;
318 };
319 #endif
320 int
sys_getgroups(struct thread * td,struct getgroups_args * uap)321 sys_getgroups(struct thread *td, struct getgroups_args *uap)
322 {
323 struct ucred *cred;
324 int ngrp, error;
325
326 cred = td->td_ucred;
327 ngrp = cred->cr_ngroups;
328
329 if (uap->gidsetsize == 0) {
330 error = 0;
331 goto out;
332 }
333 if (uap->gidsetsize < ngrp)
334 return (EINVAL);
335
336 error = copyout(cred->cr_groups, uap->gidset, ngrp * sizeof(gid_t));
337 out:
338 td->td_retval[0] = ngrp;
339 return (error);
340 }
341
342 #ifndef _SYS_SYSPROTO_H_
343 struct setsid_args {
344 int dummy;
345 };
346 #endif
347 /* ARGSUSED */
348 int
sys_setsid(struct thread * td,struct setsid_args * uap)349 sys_setsid(struct thread *td, struct setsid_args *uap)
350 {
351 struct pgrp *pgrp;
352 int error;
353 struct proc *p = td->td_proc;
354 struct pgrp *newpgrp;
355 struct session *newsess;
356
357 pgrp = NULL;
358
359 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
360 newsess = malloc(sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
361
362 again:
363 error = 0;
364 sx_xlock(&proctree_lock);
365
366 if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
367 if (pgrp != NULL)
368 PGRP_UNLOCK(pgrp);
369 error = EPERM;
370 } else {
371 error = enterpgrp(p, p->p_pid, newpgrp, newsess);
372 if (error == ERESTART)
373 goto again;
374 MPASS(error == 0);
375 td->td_retval[0] = p->p_pid;
376 newpgrp = NULL;
377 newsess = NULL;
378 }
379
380 sx_xunlock(&proctree_lock);
381
382 uma_zfree(pgrp_zone, newpgrp);
383 free(newsess, M_SESSION);
384
385 return (error);
386 }
387
388 /*
389 * set process group (setpgid/old setpgrp)
390 *
391 * caller does setpgid(targpid, targpgid)
392 *
393 * pid must be caller or child of caller (ESRCH)
394 * if a child
395 * pid must be in same session (EPERM)
396 * pid can't have done an exec (EACCES)
397 * if pgid != pid
398 * there must exist some pid in same session having pgid (EPERM)
399 * pid must not be session leader (EPERM)
400 */
401 #ifndef _SYS_SYSPROTO_H_
402 struct setpgid_args {
403 int pid; /* target process id */
404 int pgid; /* target pgrp id */
405 };
406 #endif
407 /* ARGSUSED */
408 int
sys_setpgid(struct thread * td,struct setpgid_args * uap)409 sys_setpgid(struct thread *td, struct setpgid_args *uap)
410 {
411 struct proc *curp = td->td_proc;
412 struct proc *targp; /* target process */
413 struct pgrp *pgrp; /* target pgrp */
414 int error;
415 struct pgrp *newpgrp;
416
417 if (uap->pgid < 0)
418 return (EINVAL);
419
420 newpgrp = uma_zalloc(pgrp_zone, M_WAITOK);
421
422 again:
423 error = 0;
424
425 sx_xlock(&proctree_lock);
426 if (uap->pid != 0 && uap->pid != curp->p_pid) {
427 if ((targp = pfind(uap->pid)) == NULL) {
428 error = ESRCH;
429 goto done;
430 }
431 if (!inferior(targp)) {
432 PROC_UNLOCK(targp);
433 error = ESRCH;
434 goto done;
435 }
436 if ((error = p_cansee(td, targp))) {
437 PROC_UNLOCK(targp);
438 goto done;
439 }
440 if (targp->p_pgrp == NULL ||
441 targp->p_session != curp->p_session) {
442 PROC_UNLOCK(targp);
443 error = EPERM;
444 goto done;
445 }
446 if (targp->p_flag & P_EXEC) {
447 PROC_UNLOCK(targp);
448 error = EACCES;
449 goto done;
450 }
451 PROC_UNLOCK(targp);
452 } else
453 targp = curp;
454 if (SESS_LEADER(targp)) {
455 error = EPERM;
456 goto done;
457 }
458 if (uap->pgid == 0)
459 uap->pgid = targp->p_pid;
460 if ((pgrp = pgfind(uap->pgid)) == NULL) {
461 if (uap->pgid == targp->p_pid) {
462 error = enterpgrp(targp, uap->pgid, newpgrp,
463 NULL);
464 if (error == 0)
465 newpgrp = NULL;
466 } else
467 error = EPERM;
468 } else {
469 if (pgrp == targp->p_pgrp) {
470 PGRP_UNLOCK(pgrp);
471 goto done;
472 }
473 if (pgrp->pg_id != targp->p_pid &&
474 pgrp->pg_session != curp->p_session) {
475 PGRP_UNLOCK(pgrp);
476 error = EPERM;
477 goto done;
478 }
479 PGRP_UNLOCK(pgrp);
480 error = enterthispgrp(targp, pgrp);
481 }
482 done:
483 KASSERT(error == 0 || newpgrp != NULL,
484 ("setpgid failed and newpgrp is NULL"));
485 if (error == ERESTART)
486 goto again;
487 sx_xunlock(&proctree_lock);
488 uma_zfree(pgrp_zone, newpgrp);
489 return (error);
490 }
491
492 static int
gidp_cmp(const void * p1,const void * p2)493 gidp_cmp(const void *p1, const void *p2)
494 {
495 const gid_t g1 = *(const gid_t *)p1;
496 const gid_t g2 = *(const gid_t *)p2;
497
498 return ((g1 > g2) - (g1 < g2));
499 }
500
501 /*
502 * Final storage for groups (including the effective GID) will be returned via
503 * 'groups'. '*groups' must be NULL on input, and if not equal to 'smallgroups'
504 * on output, must be freed (M_TEMP) *even if* an error is returned.
505 */
506 static int
kern_setcred_copyin_supp_groups(struct setcred * const wcred,const u_int flags,gid_t * const smallgroups,gid_t ** const groups)507 kern_setcred_copyin_supp_groups(struct setcred *const wcred,
508 const u_int flags, gid_t *const smallgroups, gid_t **const groups)
509 {
510 MPASS(*groups == NULL);
511
512 if (flags & SETCREDF_SUPP_GROUPS) {
513 int error;
514
515 /*
516 * Check for the limit for number of groups right now in order
517 * to limit the amount of bytes to copy.
518 */
519 if (wcred->sc_supp_groups_nb > ngroups_max)
520 return (EINVAL);
521
522 /*
523 * Since we are going to be copying the supplementary groups
524 * from userland, make room also for the effective GID right
525 * now, to avoid having to allocate and copy again the
526 * supplementary groups.
527 */
528 *groups = wcred->sc_supp_groups_nb < CRED_SMALLGROUPS_NB ?
529 smallgroups : malloc((wcred->sc_supp_groups_nb + 1) *
530 sizeof(*groups), M_TEMP, M_WAITOK);
531
532 error = copyin(wcred->sc_supp_groups, *groups + 1,
533 wcred->sc_supp_groups_nb * sizeof(*groups));
534 if (error != 0)
535 return (error);
536 wcred->sc_supp_groups = *groups + 1;
537 } else {
538 wcred->sc_supp_groups_nb = 0;
539 wcred->sc_supp_groups = NULL;
540 }
541
542 return (0);
543 }
544
545 int
user_setcred(struct thread * td,const u_int flags,const void * const uwcred,const size_t size,bool is_32bit)546 user_setcred(struct thread *td, const u_int flags,
547 const void *const uwcred, const size_t size, bool is_32bit)
548 {
549 struct setcred wcred;
550 #ifdef MAC
551 struct mac mac;
552 /* Pointer to 'struct mac' or 'struct mac32'. */
553 void *umac;
554 #endif
555 gid_t smallgroups[CRED_SMALLGROUPS_NB];
556 gid_t *groups = NULL;
557 int error;
558
559 /*
560 * As the only point of this wrapper function is to copyin() from
561 * userland, we only interpret the data pieces we need to perform this
562 * operation and defer further sanity checks to kern_setcred(), except
563 * that we redundantly check here that no unknown flags have been
564 * passed.
565 */
566 if ((flags & ~SETCREDF_MASK) != 0)
567 return (EINVAL);
568
569 #ifdef COMPAT_FREEBSD32
570 if (is_32bit) {
571 struct setcred32 wcred32;
572
573 if (size != sizeof(wcred32))
574 return (EINVAL);
575 error = copyin(uwcred, &wcred32, sizeof(wcred32));
576 if (error != 0)
577 return (error);
578 /* These fields have exactly the same sizes and positions. */
579 memcpy(&wcred, &wcred32, &wcred32.setcred32_copy_end -
580 &wcred32.setcred32_copy_start);
581 /* Remaining fields are pointers and need PTRIN*(). */
582 PTRIN_CP(wcred32, wcred, sc_supp_groups);
583 PTRIN_CP(wcred32, wcred, sc_label);
584 } else
585 #endif /* COMPAT_FREEBSD32 */
586 {
587 if (size != sizeof(wcred))
588 return (EINVAL);
589 error = copyin(uwcred, &wcred, sizeof(wcred));
590 if (error != 0)
591 return (error);
592 }
593 #ifdef MAC
594 umac = wcred.sc_label;
595 #endif
596 /* Also done on !MAC as a defensive measure. */
597 wcred.sc_label = NULL;
598
599 /*
600 * Copy supplementary groups as needed. There is no specific
601 * alternative for 32-bit compatibility as 'gid_t' has the same size
602 * everywhere.
603 */
604 error = kern_setcred_copyin_supp_groups(&wcred, flags, smallgroups,
605 &groups);
606 if (error != 0)
607 goto free_groups;
608
609 #ifdef MAC
610 if ((flags & SETCREDF_MAC_LABEL) != 0) {
611 #ifdef COMPAT_FREEBSD32
612 if (is_32bit)
613 error = mac_label_copyin32(umac, &mac, NULL);
614 else
615 #endif
616 error = mac_label_copyin(umac, &mac, NULL);
617 if (error != 0)
618 goto free_groups;
619 wcred.sc_label = &mac;
620 }
621 #endif
622
623 error = kern_setcred(td, flags, &wcred, groups);
624
625 #ifdef MAC
626 if (wcred.sc_label != NULL)
627 free_copied_label(wcred.sc_label);
628 #endif
629
630 free_groups:
631 if (groups != smallgroups)
632 free(groups, M_TEMP);
633
634 return (error);
635 }
636
637 #ifndef _SYS_SYSPROTO_H_
638 struct setcred_args {
639 u_int flags; /* Flags. */
640 const struct setcred *wcred;
641 size_t size; /* Passed 'setcred' structure length. */
642 };
643 #endif
644 /* ARGSUSED */
645 int
sys_setcred(struct thread * td,struct setcred_args * uap)646 sys_setcred(struct thread *td, struct setcred_args *uap)
647 {
648 return (user_setcred(td, uap->flags, uap->wcred, uap->size, false));
649 }
650
651 /*
652 * CAUTION: This function normalizes groups in 'wcred'.
653 *
654 * If 'preallocated_groups' is non-NULL, it must be an already allocated array
655 * of size 'wcred->sc_supp_groups_nb + 1', with the supplementary groups
656 * starting at index 1, and 'wcred->sc_supp_groups' then must point to the first
657 * supplementary group.
658 */
659 int
kern_setcred(struct thread * const td,const u_int flags,struct setcred * const wcred,gid_t * preallocated_groups)660 kern_setcred(struct thread *const td, const u_int flags,
661 struct setcred *const wcred, gid_t *preallocated_groups)
662 {
663 struct proc *const p = td->td_proc;
664 struct ucred *new_cred, *old_cred, *to_free_cred;
665 struct uidinfo *uip = NULL, *ruip = NULL;
666 #ifdef MAC
667 void *mac_set_proc_data = NULL;
668 bool proc_label_set = false;
669 #endif
670 gid_t *groups = NULL;
671 gid_t smallgroups[CRED_SMALLGROUPS_NB];
672 int error;
673 bool cred_set;
674
675 /* Bail out on unrecognized flags. */
676 if (flags & ~SETCREDF_MASK)
677 return (EINVAL);
678
679 /*
680 * Part 1: We allocate and perform preparatory operations with no locks.
681 */
682
683 if (flags & SETCREDF_SUPP_GROUPS) {
684 if (wcred->sc_supp_groups_nb > ngroups_max)
685 return (EINVAL);
686 if (preallocated_groups != NULL) {
687 groups = preallocated_groups;
688 MPASS(preallocated_groups + 1 == wcred->sc_supp_groups);
689 } else {
690 groups = wcred->sc_supp_groups_nb < CRED_SMALLGROUPS_NB ?
691 smallgroups :
692 malloc((wcred->sc_supp_groups_nb + 1) *
693 sizeof(*groups), M_TEMP, M_WAITOK);
694 memcpy(groups + 1, wcred->sc_supp_groups,
695 wcred->sc_supp_groups_nb * sizeof(*groups));
696 }
697 }
698
699 if (flags & SETCREDF_MAC_LABEL) {
700 #ifdef MAC
701 error = mac_set_proc_prepare(td, wcred->sc_label,
702 &mac_set_proc_data);
703 if (error != 0)
704 goto free_groups;
705 #else
706 error = ENOTSUP;
707 goto free_groups;
708 #endif
709 }
710
711 if (flags & SETCREDF_UID) {
712 AUDIT_ARG_EUID(wcred->sc_uid);
713 uip = uifind(wcred->sc_uid);
714 }
715 if (flags & SETCREDF_RUID) {
716 AUDIT_ARG_RUID(wcred->sc_ruid);
717 ruip = uifind(wcred->sc_ruid);
718 }
719 if (flags & SETCREDF_SVUID)
720 AUDIT_ARG_SUID(wcred->sc_svuid);
721
722 if (flags & SETCREDF_GID)
723 AUDIT_ARG_EGID(wcred->sc_gid);
724 if (flags & SETCREDF_RGID)
725 AUDIT_ARG_RGID(wcred->sc_rgid);
726 if (flags & SETCREDF_SVGID)
727 AUDIT_ARG_SGID(wcred->sc_svgid);
728 if (flags & SETCREDF_SUPP_GROUPS) {
729 int ngrp = wcred->sc_supp_groups_nb;
730
731 /*
732 * Output the raw supplementary groups array for better
733 * traceability.
734 */
735 AUDIT_ARG_GROUPSET(groups + 1, ngrp);
736 ++ngrp;
737 groups_normalize(&ngrp, groups);
738 wcred->sc_supp_groups_nb = ngrp - 1;
739 }
740
741 /*
742 * We first completely build the new credentials and only then pass them
743 * to MAC along with the old ones so that modules can check whether the
744 * requested transition is allowed.
745 */
746 new_cred = crget();
747 to_free_cred = new_cred;
748 if (flags & SETCREDF_SUPP_GROUPS)
749 crextend(new_cred, wcred->sc_supp_groups_nb + 1);
750
751 #ifdef MAC
752 mac_cred_setcred_enter();
753 #endif
754
755 /*
756 * Part 2: We grab the process lock as to have a stable view of its
757 * current credentials, and prepare a copy of them with the requested
758 * changes applied under that lock.
759 */
760
761 PROC_LOCK(p);
762 old_cred = crcopysafe(p, new_cred);
763
764 /*
765 * Change user IDs.
766 */
767 if (flags & SETCREDF_UID)
768 change_euid(new_cred, uip);
769 if (flags & SETCREDF_RUID)
770 change_ruid(new_cred, ruip);
771 if (flags & SETCREDF_SVUID)
772 change_svuid(new_cred, wcred->sc_svuid);
773
774 /*
775 * Change groups.
776 *
777 * crsetgroups_internal() changes both the effective and supplementary
778 * ones.
779 */
780 if (flags & SETCREDF_SUPP_GROUPS) {
781 groups[0] = flags & SETCREDF_GID ? wcred->sc_gid :
782 new_cred->cr_gid;
783 crsetgroups_internal(new_cred, wcred->sc_supp_groups_nb + 1,
784 groups);
785 } else if (flags & SETCREDF_GID)
786 change_egid(new_cred, wcred->sc_gid);
787 if (flags & SETCREDF_RGID)
788 change_rgid(new_cred, wcred->sc_rgid);
789 if (flags & SETCREDF_SVGID)
790 change_svgid(new_cred, wcred->sc_svgid);
791
792 #ifdef MAC
793 /*
794 * Change the MAC label.
795 */
796 if (flags & SETCREDF_MAC_LABEL) {
797 error = mac_set_proc_core(td, new_cred, mac_set_proc_data);
798 if (error != 0)
799 goto unlock_finish;
800 proc_label_set = true;
801 }
802
803 /*
804 * MAC security modules checks.
805 */
806 error = mac_cred_check_setcred(flags, old_cred, new_cred);
807 if (error != 0)
808 goto unlock_finish;
809 #endif
810 /*
811 * Privilege check.
812 */
813 error = priv_check_cred(old_cred, PRIV_CRED_SETCRED);
814 if (error != 0)
815 goto unlock_finish;
816
817 /*
818 * Set the new credentials, noting that they have changed.
819 */
820 cred_set = proc_set_cred_enforce_proc_lim(p, new_cred);
821 if (cred_set) {
822 setsugid(p);
823 to_free_cred = old_cred;
824 MPASS(error == 0);
825 } else
826 error = EAGAIN;
827
828 unlock_finish:
829 PROC_UNLOCK(p);
830 /*
831 * Part 3: After releasing the process lock, we perform cleanups and
832 * finishing operations.
833 */
834
835 #ifdef MAC
836 if (mac_set_proc_data != NULL)
837 mac_set_proc_finish(td, proc_label_set, mac_set_proc_data);
838 mac_cred_setcred_exit();
839 #endif
840 crfree(to_free_cred);
841 if (uip != NULL)
842 uifree(uip);
843 if (ruip != NULL)
844 uifree(ruip);
845 free_groups:
846 if (groups != preallocated_groups && groups != smallgroups)
847 free(groups, M_TEMP); /* Deals with 'groups' being NULL. */
848 return (error);
849 }
850
851 /*
852 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
853 * compatible. It says that setting the uid/gid to euid/egid is a special
854 * case of "appropriate privilege". Once the rules are expanded out, this
855 * basically means that setuid(nnn) sets all three id's, in all permitted
856 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
857 * does not set the saved id - this is dangerous for traditional BSD
858 * programs. For this reason, we *really* do not want to set
859 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
860 */
861 #define POSIX_APPENDIX_B_4_2_2
862
863 #ifndef _SYS_SYSPROTO_H_
864 struct setuid_args {
865 uid_t uid;
866 };
867 #endif
868 /* ARGSUSED */
869 int
sys_setuid(struct thread * td,struct setuid_args * uap)870 sys_setuid(struct thread *td, struct setuid_args *uap)
871 {
872 struct proc *p = td->td_proc;
873 struct ucred *newcred, *oldcred;
874 uid_t uid;
875 struct uidinfo *uip;
876 int error;
877
878 uid = uap->uid;
879 AUDIT_ARG_UID(uid);
880 newcred = crget();
881 uip = uifind(uid);
882 PROC_LOCK(p);
883 /*
884 * Copy credentials so other references do not see our changes.
885 */
886 oldcred = crcopysafe(p, newcred);
887
888 #ifdef MAC
889 error = mac_cred_check_setuid(oldcred, uid);
890 if (error)
891 goto fail;
892 #endif
893
894 /*
895 * See if we have "permission" by POSIX 1003.1 rules.
896 *
897 * Note that setuid(geteuid()) is a special case of
898 * "appropriate privileges" in appendix B.4.2.2. We need
899 * to use this clause to be compatible with traditional BSD
900 * semantics. Basically, it means that "setuid(xx)" sets all
901 * three id's (assuming you have privs).
902 *
903 * Notes on the logic. We do things in three steps.
904 * 1: We determine if the euid is going to change, and do EPERM
905 * right away. We unconditionally change the euid later if this
906 * test is satisfied, simplifying that part of the logic.
907 * 2: We determine if the real and/or saved uids are going to
908 * change. Determined by compile options.
909 * 3: Change euid last. (after tests in #2 for "appropriate privs")
910 */
911 if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
912 #ifdef _POSIX_SAVED_IDS
913 uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
914 #endif
915 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
916 uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
917 #endif
918 (error = priv_check_cred(oldcred, PRIV_CRED_SETUID)) != 0)
919 goto fail;
920
921 #ifdef _POSIX_SAVED_IDS
922 /*
923 * Do we have "appropriate privileges" (are we root or uid == euid)
924 * If so, we are changing the real uid and/or saved uid.
925 */
926 if (
927 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
928 uid == oldcred->cr_uid ||
929 #endif
930 /* We are using privs. */
931 priv_check_cred(oldcred, PRIV_CRED_SETUID) == 0)
932 #endif
933 {
934 /*
935 * Set the real uid.
936 */
937 if (uid != oldcred->cr_ruid) {
938 change_ruid(newcred, uip);
939 setsugid(p);
940 }
941 /*
942 * Set saved uid
943 *
944 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as
945 * the security of seteuid() depends on it. B.4.2.2 says it
946 * is important that we should do this.
947 */
948 if (uid != oldcred->cr_svuid) {
949 change_svuid(newcred, uid);
950 setsugid(p);
951 }
952 }
953
954 /*
955 * In all permitted cases, we are changing the euid.
956 */
957 if (uid != oldcred->cr_uid) {
958 change_euid(newcred, uip);
959 setsugid(p);
960 }
961 /*
962 * This also transfers the proc count to the new user.
963 */
964 proc_set_cred(p, newcred);
965 #ifdef RACCT
966 racct_proc_ucred_changed(p, oldcred, newcred);
967 crhold(newcred);
968 #endif
969 PROC_UNLOCK(p);
970 #ifdef RCTL
971 rctl_proc_ucred_changed(p, newcred);
972 crfree(newcred);
973 #endif
974 uifree(uip);
975 crfree(oldcred);
976 return (0);
977
978 fail:
979 PROC_UNLOCK(p);
980 uifree(uip);
981 crfree(newcred);
982 return (error);
983 }
984
985 #ifndef _SYS_SYSPROTO_H_
986 struct seteuid_args {
987 uid_t euid;
988 };
989 #endif
990 /* ARGSUSED */
991 int
sys_seteuid(struct thread * td,struct seteuid_args * uap)992 sys_seteuid(struct thread *td, struct seteuid_args *uap)
993 {
994 struct proc *p = td->td_proc;
995 struct ucred *newcred, *oldcred;
996 uid_t euid;
997 struct uidinfo *euip;
998 int error;
999
1000 euid = uap->euid;
1001 AUDIT_ARG_EUID(euid);
1002 newcred = crget();
1003 euip = uifind(euid);
1004 PROC_LOCK(p);
1005 /*
1006 * Copy credentials so other references do not see our changes.
1007 */
1008 oldcred = crcopysafe(p, newcred);
1009
1010 #ifdef MAC
1011 error = mac_cred_check_seteuid(oldcred, euid);
1012 if (error)
1013 goto fail;
1014 #endif
1015
1016 if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
1017 euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
1018 (error = priv_check_cred(oldcred, PRIV_CRED_SETEUID)) != 0)
1019 goto fail;
1020
1021 /*
1022 * Everything's okay, do it.
1023 */
1024 if (oldcred->cr_uid != euid) {
1025 change_euid(newcred, euip);
1026 setsugid(p);
1027 }
1028 proc_set_cred(p, newcred);
1029 PROC_UNLOCK(p);
1030 uifree(euip);
1031 crfree(oldcred);
1032 return (0);
1033
1034 fail:
1035 PROC_UNLOCK(p);
1036 uifree(euip);
1037 crfree(newcred);
1038 return (error);
1039 }
1040
1041 #ifndef _SYS_SYSPROTO_H_
1042 struct setgid_args {
1043 gid_t gid;
1044 };
1045 #endif
1046 /* ARGSUSED */
1047 int
sys_setgid(struct thread * td,struct setgid_args * uap)1048 sys_setgid(struct thread *td, struct setgid_args *uap)
1049 {
1050 struct proc *p = td->td_proc;
1051 struct ucred *newcred, *oldcred;
1052 gid_t gid;
1053 int error;
1054
1055 gid = uap->gid;
1056 AUDIT_ARG_GID(gid);
1057 newcred = crget();
1058 PROC_LOCK(p);
1059 oldcred = crcopysafe(p, newcred);
1060
1061 #ifdef MAC
1062 error = mac_cred_check_setgid(oldcred, gid);
1063 if (error)
1064 goto fail;
1065 #endif
1066
1067 /*
1068 * See if we have "permission" by POSIX 1003.1 rules.
1069 *
1070 * Note that setgid(getegid()) is a special case of
1071 * "appropriate privileges" in appendix B.4.2.2. We need
1072 * to use this clause to be compatible with traditional BSD
1073 * semantics. Basically, it means that "setgid(xx)" sets all
1074 * three id's (assuming you have privs).
1075 *
1076 * For notes on the logic here, see setuid() above.
1077 */
1078 if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
1079 #ifdef _POSIX_SAVED_IDS
1080 gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
1081 #endif
1082 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
1083 gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
1084 #endif
1085 (error = priv_check_cred(oldcred, PRIV_CRED_SETGID)) != 0)
1086 goto fail;
1087
1088 #ifdef _POSIX_SAVED_IDS
1089 /*
1090 * Do we have "appropriate privileges" (are we root or gid == egid)
1091 * If so, we are changing the real uid and saved gid.
1092 */
1093 if (
1094 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
1095 gid == oldcred->cr_groups[0] ||
1096 #endif
1097 /* We are using privs. */
1098 priv_check_cred(oldcred, PRIV_CRED_SETGID) == 0)
1099 #endif
1100 {
1101 /*
1102 * Set real gid
1103 */
1104 if (oldcred->cr_rgid != gid) {
1105 change_rgid(newcred, gid);
1106 setsugid(p);
1107 }
1108 /*
1109 * Set saved gid
1110 *
1111 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as
1112 * the security of setegid() depends on it. B.4.2.2 says it
1113 * is important that we should do this.
1114 */
1115 if (oldcred->cr_svgid != gid) {
1116 change_svgid(newcred, gid);
1117 setsugid(p);
1118 }
1119 }
1120 /*
1121 * In all cases permitted cases, we are changing the egid.
1122 * Copy credentials so other references do not see our changes.
1123 */
1124 if (oldcred->cr_groups[0] != gid) {
1125 change_egid(newcred, gid);
1126 setsugid(p);
1127 }
1128 proc_set_cred(p, newcred);
1129 PROC_UNLOCK(p);
1130 crfree(oldcred);
1131 return (0);
1132
1133 fail:
1134 PROC_UNLOCK(p);
1135 crfree(newcred);
1136 return (error);
1137 }
1138
1139 #ifndef _SYS_SYSPROTO_H_
1140 struct setegid_args {
1141 gid_t egid;
1142 };
1143 #endif
1144 /* ARGSUSED */
1145 int
sys_setegid(struct thread * td,struct setegid_args * uap)1146 sys_setegid(struct thread *td, struct setegid_args *uap)
1147 {
1148 struct proc *p = td->td_proc;
1149 struct ucred *newcred, *oldcred;
1150 gid_t egid;
1151 int error;
1152
1153 egid = uap->egid;
1154 AUDIT_ARG_EGID(egid);
1155 newcred = crget();
1156 PROC_LOCK(p);
1157 oldcred = crcopysafe(p, newcred);
1158
1159 #ifdef MAC
1160 error = mac_cred_check_setegid(oldcred, egid);
1161 if (error)
1162 goto fail;
1163 #endif
1164
1165 if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
1166 egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
1167 (error = priv_check_cred(oldcred, PRIV_CRED_SETEGID)) != 0)
1168 goto fail;
1169
1170 if (oldcred->cr_groups[0] != egid) {
1171 change_egid(newcred, egid);
1172 setsugid(p);
1173 }
1174 proc_set_cred(p, newcred);
1175 PROC_UNLOCK(p);
1176 crfree(oldcred);
1177 return (0);
1178
1179 fail:
1180 PROC_UNLOCK(p);
1181 crfree(newcred);
1182 return (error);
1183 }
1184
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct setgroups_args {
1187 int gidsetsize;
1188 gid_t *gidset;
1189 };
1190 #endif
1191 /* ARGSUSED */
1192 int
sys_setgroups(struct thread * td,struct setgroups_args * uap)1193 sys_setgroups(struct thread *td, struct setgroups_args *uap)
1194 {
1195 gid_t smallgroups[CRED_SMALLGROUPS_NB];
1196 gid_t *groups;
1197 int gidsetsize, error;
1198
1199 /*
1200 * Sanity check size now to avoid passing too big a value to copyin(),
1201 * even if kern_setgroups() will do it again.
1202 *
1203 * Ideally, the 'gidsetsize' argument should have been a 'u_int' (and it
1204 * was, in this implementation, for a long time), but POSIX standardized
1205 * getgroups() to take an 'int' and it would be quite entrapping to have
1206 * setgroups() differ.
1207 */
1208 gidsetsize = uap->gidsetsize;
1209 if (gidsetsize > ngroups_max + 1 || gidsetsize < 0)
1210 return (EINVAL);
1211
1212 if (gidsetsize > CRED_SMALLGROUPS_NB)
1213 groups = malloc(gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
1214 else
1215 groups = smallgroups;
1216
1217 error = copyin(uap->gidset, groups, gidsetsize * sizeof(gid_t));
1218 if (error == 0)
1219 error = kern_setgroups(td, &gidsetsize, groups);
1220
1221 if (groups != smallgroups)
1222 free(groups, M_TEMP);
1223 return (error);
1224 }
1225
1226 /*
1227 * CAUTION: This function normalizes 'groups', possibly also changing the value
1228 * of '*ngrpp' as a consequence.
1229 */
1230 int
kern_setgroups(struct thread * td,int * ngrpp,gid_t * groups)1231 kern_setgroups(struct thread *td, int *ngrpp, gid_t *groups)
1232 {
1233 struct proc *p = td->td_proc;
1234 struct ucred *newcred, *oldcred;
1235 int ngrp, error;
1236
1237 ngrp = *ngrpp;
1238 /* Sanity check size. */
1239 if (ngrp < 0 || ngrp > ngroups_max + 1)
1240 return (EINVAL);
1241
1242 AUDIT_ARG_GROUPSET(groups, ngrp);
1243 if (ngrp != 0) {
1244 /* We allow and treat 0 specially below. */
1245 groups_normalize(ngrpp, groups);
1246 ngrp = *ngrpp;
1247 }
1248 newcred = crget();
1249 if (ngrp != 0)
1250 crextend(newcred, ngrp);
1251 PROC_LOCK(p);
1252 oldcred = crcopysafe(p, newcred);
1253
1254 #ifdef MAC
1255 error = ngrp == 0 ?
1256 /* If 'ngrp' is 0, we'll keep just the current effective GID. */
1257 mac_cred_check_setgroups(oldcred, 1, oldcred->cr_groups) :
1258 mac_cred_check_setgroups(oldcred, ngrp, groups);
1259 if (error)
1260 goto fail;
1261 #endif
1262
1263 error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS);
1264 if (error)
1265 goto fail;
1266
1267 if (ngrp == 0) {
1268 /*
1269 * setgroups(0, NULL) is a legitimate way of clearing the
1270 * groups vector on non-BSD systems (which generally do not
1271 * have the egid in the groups[0]). We risk security holes
1272 * when running non-BSD software if we do not do the same.
1273 */
1274 newcred->cr_ngroups = 1;
1275 } else
1276 crsetgroups_internal(newcred, ngrp, groups);
1277
1278 setsugid(p);
1279 proc_set_cred(p, newcred);
1280 PROC_UNLOCK(p);
1281 crfree(oldcred);
1282 return (0);
1283
1284 fail:
1285 PROC_UNLOCK(p);
1286 crfree(newcred);
1287 return (error);
1288 }
1289
1290 #ifndef _SYS_SYSPROTO_H_
1291 struct setreuid_args {
1292 uid_t ruid;
1293 uid_t euid;
1294 };
1295 #endif
1296 /* ARGSUSED */
1297 int
sys_setreuid(struct thread * td,struct setreuid_args * uap)1298 sys_setreuid(struct thread *td, struct setreuid_args *uap)
1299 {
1300 struct proc *p = td->td_proc;
1301 struct ucred *newcred, *oldcred;
1302 uid_t euid, ruid;
1303 struct uidinfo *euip, *ruip;
1304 int error;
1305
1306 euid = uap->euid;
1307 ruid = uap->ruid;
1308 AUDIT_ARG_EUID(euid);
1309 AUDIT_ARG_RUID(ruid);
1310 newcred = crget();
1311 euip = uifind(euid);
1312 ruip = uifind(ruid);
1313 PROC_LOCK(p);
1314 oldcred = crcopysafe(p, newcred);
1315
1316 #ifdef MAC
1317 error = mac_cred_check_setreuid(oldcred, ruid, euid);
1318 if (error)
1319 goto fail;
1320 #endif
1321
1322 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1323 ruid != oldcred->cr_svuid) ||
1324 (euid != (uid_t)-1 && euid != oldcred->cr_uid &&
1325 euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
1326 (error = priv_check_cred(oldcred, PRIV_CRED_SETREUID)) != 0)
1327 goto fail;
1328
1329 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1330 change_euid(newcred, euip);
1331 setsugid(p);
1332 }
1333 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1334 change_ruid(newcred, ruip);
1335 setsugid(p);
1336 }
1337 if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
1338 newcred->cr_svuid != newcred->cr_uid) {
1339 change_svuid(newcred, newcred->cr_uid);
1340 setsugid(p);
1341 }
1342 proc_set_cred(p, newcred);
1343 #ifdef RACCT
1344 racct_proc_ucred_changed(p, oldcred, newcred);
1345 crhold(newcred);
1346 #endif
1347 PROC_UNLOCK(p);
1348 #ifdef RCTL
1349 rctl_proc_ucred_changed(p, newcred);
1350 crfree(newcred);
1351 #endif
1352 uifree(ruip);
1353 uifree(euip);
1354 crfree(oldcred);
1355 return (0);
1356
1357 fail:
1358 PROC_UNLOCK(p);
1359 uifree(ruip);
1360 uifree(euip);
1361 crfree(newcred);
1362 return (error);
1363 }
1364
1365 #ifndef _SYS_SYSPROTO_H_
1366 struct setregid_args {
1367 gid_t rgid;
1368 gid_t egid;
1369 };
1370 #endif
1371 /* ARGSUSED */
1372 int
sys_setregid(struct thread * td,struct setregid_args * uap)1373 sys_setregid(struct thread *td, struct setregid_args *uap)
1374 {
1375 struct proc *p = td->td_proc;
1376 struct ucred *newcred, *oldcred;
1377 gid_t egid, rgid;
1378 int error;
1379
1380 egid = uap->egid;
1381 rgid = uap->rgid;
1382 AUDIT_ARG_EGID(egid);
1383 AUDIT_ARG_RGID(rgid);
1384 newcred = crget();
1385 PROC_LOCK(p);
1386 oldcred = crcopysafe(p, newcred);
1387
1388 #ifdef MAC
1389 error = mac_cred_check_setregid(oldcred, rgid, egid);
1390 if (error)
1391 goto fail;
1392 #endif
1393
1394 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1395 rgid != oldcred->cr_svgid) ||
1396 (egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
1397 egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
1398 (error = priv_check_cred(oldcred, PRIV_CRED_SETREGID)) != 0)
1399 goto fail;
1400
1401 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1402 change_egid(newcred, egid);
1403 setsugid(p);
1404 }
1405 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1406 change_rgid(newcred, rgid);
1407 setsugid(p);
1408 }
1409 if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
1410 newcred->cr_svgid != newcred->cr_groups[0]) {
1411 change_svgid(newcred, newcred->cr_groups[0]);
1412 setsugid(p);
1413 }
1414 proc_set_cred(p, newcred);
1415 PROC_UNLOCK(p);
1416 crfree(oldcred);
1417 return (0);
1418
1419 fail:
1420 PROC_UNLOCK(p);
1421 crfree(newcred);
1422 return (error);
1423 }
1424
1425 /*
1426 * setresuid(ruid, euid, suid) is like setreuid except control over the saved
1427 * uid is explicit.
1428 */
1429 #ifndef _SYS_SYSPROTO_H_
1430 struct setresuid_args {
1431 uid_t ruid;
1432 uid_t euid;
1433 uid_t suid;
1434 };
1435 #endif
1436 /* ARGSUSED */
1437 int
sys_setresuid(struct thread * td,struct setresuid_args * uap)1438 sys_setresuid(struct thread *td, struct setresuid_args *uap)
1439 {
1440 struct proc *p = td->td_proc;
1441 struct ucred *newcred, *oldcred;
1442 uid_t euid, ruid, suid;
1443 struct uidinfo *euip, *ruip;
1444 int error;
1445
1446 euid = uap->euid;
1447 ruid = uap->ruid;
1448 suid = uap->suid;
1449 AUDIT_ARG_EUID(euid);
1450 AUDIT_ARG_RUID(ruid);
1451 AUDIT_ARG_SUID(suid);
1452 newcred = crget();
1453 euip = uifind(euid);
1454 ruip = uifind(ruid);
1455 PROC_LOCK(p);
1456 oldcred = crcopysafe(p, newcred);
1457
1458 #ifdef MAC
1459 error = mac_cred_check_setresuid(oldcred, ruid, euid, suid);
1460 if (error)
1461 goto fail;
1462 #endif
1463
1464 if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
1465 ruid != oldcred->cr_svuid &&
1466 ruid != oldcred->cr_uid) ||
1467 (euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
1468 euid != oldcred->cr_svuid &&
1469 euid != oldcred->cr_uid) ||
1470 (suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
1471 suid != oldcred->cr_svuid &&
1472 suid != oldcred->cr_uid)) &&
1473 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESUID)) != 0)
1474 goto fail;
1475
1476 if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
1477 change_euid(newcred, euip);
1478 setsugid(p);
1479 }
1480 if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
1481 change_ruid(newcred, ruip);
1482 setsugid(p);
1483 }
1484 if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
1485 change_svuid(newcred, suid);
1486 setsugid(p);
1487 }
1488 proc_set_cred(p, newcred);
1489 #ifdef RACCT
1490 racct_proc_ucred_changed(p, oldcred, newcred);
1491 crhold(newcred);
1492 #endif
1493 PROC_UNLOCK(p);
1494 #ifdef RCTL
1495 rctl_proc_ucred_changed(p, newcred);
1496 crfree(newcred);
1497 #endif
1498 uifree(ruip);
1499 uifree(euip);
1500 crfree(oldcred);
1501 return (0);
1502
1503 fail:
1504 PROC_UNLOCK(p);
1505 uifree(ruip);
1506 uifree(euip);
1507 crfree(newcred);
1508 return (error);
1509
1510 }
1511
1512 /*
1513 * setresgid(rgid, egid, sgid) is like setregid except control over the saved
1514 * gid is explicit.
1515 */
1516 #ifndef _SYS_SYSPROTO_H_
1517 struct setresgid_args {
1518 gid_t rgid;
1519 gid_t egid;
1520 gid_t sgid;
1521 };
1522 #endif
1523 /* ARGSUSED */
1524 int
sys_setresgid(struct thread * td,struct setresgid_args * uap)1525 sys_setresgid(struct thread *td, struct setresgid_args *uap)
1526 {
1527 struct proc *p = td->td_proc;
1528 struct ucred *newcred, *oldcred;
1529 gid_t egid, rgid, sgid;
1530 int error;
1531
1532 egid = uap->egid;
1533 rgid = uap->rgid;
1534 sgid = uap->sgid;
1535 AUDIT_ARG_EGID(egid);
1536 AUDIT_ARG_RGID(rgid);
1537 AUDIT_ARG_SGID(sgid);
1538 newcred = crget();
1539 PROC_LOCK(p);
1540 oldcred = crcopysafe(p, newcred);
1541
1542 #ifdef MAC
1543 error = mac_cred_check_setresgid(oldcred, rgid, egid, sgid);
1544 if (error)
1545 goto fail;
1546 #endif
1547
1548 if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
1549 rgid != oldcred->cr_svgid &&
1550 rgid != oldcred->cr_groups[0]) ||
1551 (egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
1552 egid != oldcred->cr_svgid &&
1553 egid != oldcred->cr_groups[0]) ||
1554 (sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
1555 sgid != oldcred->cr_svgid &&
1556 sgid != oldcred->cr_groups[0])) &&
1557 (error = priv_check_cred(oldcred, PRIV_CRED_SETRESGID)) != 0)
1558 goto fail;
1559
1560 if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
1561 change_egid(newcred, egid);
1562 setsugid(p);
1563 }
1564 if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
1565 change_rgid(newcred, rgid);
1566 setsugid(p);
1567 }
1568 if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
1569 change_svgid(newcred, sgid);
1570 setsugid(p);
1571 }
1572 proc_set_cred(p, newcred);
1573 PROC_UNLOCK(p);
1574 crfree(oldcred);
1575 return (0);
1576
1577 fail:
1578 PROC_UNLOCK(p);
1579 crfree(newcred);
1580 return (error);
1581 }
1582
1583 #ifndef _SYS_SYSPROTO_H_
1584 struct getresuid_args {
1585 uid_t *ruid;
1586 uid_t *euid;
1587 uid_t *suid;
1588 };
1589 #endif
1590 /* ARGSUSED */
1591 int
sys_getresuid(struct thread * td,struct getresuid_args * uap)1592 sys_getresuid(struct thread *td, struct getresuid_args *uap)
1593 {
1594 struct ucred *cred;
1595 int error1 = 0, error2 = 0, error3 = 0;
1596
1597 cred = td->td_ucred;
1598 if (uap->ruid)
1599 error1 = copyout(&cred->cr_ruid,
1600 uap->ruid, sizeof(cred->cr_ruid));
1601 if (uap->euid)
1602 error2 = copyout(&cred->cr_uid,
1603 uap->euid, sizeof(cred->cr_uid));
1604 if (uap->suid)
1605 error3 = copyout(&cred->cr_svuid,
1606 uap->suid, sizeof(cred->cr_svuid));
1607 return (error1 ? error1 : error2 ? error2 : error3);
1608 }
1609
1610 #ifndef _SYS_SYSPROTO_H_
1611 struct getresgid_args {
1612 gid_t *rgid;
1613 gid_t *egid;
1614 gid_t *sgid;
1615 };
1616 #endif
1617 /* ARGSUSED */
1618 int
sys_getresgid(struct thread * td,struct getresgid_args * uap)1619 sys_getresgid(struct thread *td, struct getresgid_args *uap)
1620 {
1621 struct ucred *cred;
1622 int error1 = 0, error2 = 0, error3 = 0;
1623
1624 cred = td->td_ucred;
1625 if (uap->rgid)
1626 error1 = copyout(&cred->cr_rgid,
1627 uap->rgid, sizeof(cred->cr_rgid));
1628 if (uap->egid)
1629 error2 = copyout(&cred->cr_groups[0],
1630 uap->egid, sizeof(cred->cr_groups[0]));
1631 if (uap->sgid)
1632 error3 = copyout(&cred->cr_svgid,
1633 uap->sgid, sizeof(cred->cr_svgid));
1634 return (error1 ? error1 : error2 ? error2 : error3);
1635 }
1636
1637 #ifndef _SYS_SYSPROTO_H_
1638 struct issetugid_args {
1639 int dummy;
1640 };
1641 #endif
1642 /* ARGSUSED */
1643 int
sys_issetugid(struct thread * td,struct issetugid_args * uap)1644 sys_issetugid(struct thread *td, struct issetugid_args *uap)
1645 {
1646 struct proc *p = td->td_proc;
1647
1648 /*
1649 * Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
1650 * we use P_SUGID because we consider changing the owners as
1651 * "tainting" as well.
1652 * This is significant for procs that start as root and "become"
1653 * a user without an exec - programs cannot know *everything*
1654 * that libc *might* have put in their data segment.
1655 */
1656 td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
1657 return (0);
1658 }
1659
1660 int
sys___setugid(struct thread * td,struct __setugid_args * uap)1661 sys___setugid(struct thread *td, struct __setugid_args *uap)
1662 {
1663 #ifdef REGRESSION
1664 struct proc *p;
1665
1666 p = td->td_proc;
1667 switch (uap->flag) {
1668 case 0:
1669 PROC_LOCK(p);
1670 p->p_flag &= ~P_SUGID;
1671 PROC_UNLOCK(p);
1672 return (0);
1673 case 1:
1674 PROC_LOCK(p);
1675 p->p_flag |= P_SUGID;
1676 PROC_UNLOCK(p);
1677 return (0);
1678 default:
1679 return (EINVAL);
1680 }
1681 #else /* !REGRESSION */
1682
1683 return (ENOSYS);
1684 #endif /* REGRESSION */
1685 }
1686
1687 #ifdef INVARIANTS
1688 static void
groups_check_normalized(int ngrp,const gid_t * groups)1689 groups_check_normalized(int ngrp, const gid_t *groups)
1690 {
1691 gid_t prev_g;
1692
1693 groups_check_positive_len(ngrp);
1694 groups_check_max_len(ngrp);
1695
1696 if (ngrp == 1)
1697 return;
1698
1699 prev_g = groups[1];
1700 for (int i = 2; i < ngrp; ++i) {
1701 const gid_t g = groups[i];
1702
1703 if (prev_g >= g)
1704 panic("%s: groups[%d] (%u) >= groups[%d] (%u)",
1705 __func__, i - 1, prev_g, i, g);
1706 prev_g = g;
1707 }
1708 }
1709 #else
1710 #define groups_check_normalized(...)
1711 #endif
1712
1713 /*
1714 * Returns whether gid designates a supplementary group in cred.
1715 */
1716 bool
group_is_supplementary(const gid_t gid,const struct ucred * const cred)1717 group_is_supplementary(const gid_t gid, const struct ucred *const cred)
1718 {
1719
1720 groups_check_normalized(cred->cr_ngroups, cred->cr_groups);
1721
1722 /*
1723 * Perform a binary search of the supplementary groups. This is
1724 * possible because we sort the groups in crsetgroups().
1725 */
1726 return (bsearch(&gid, cred->cr_groups + 1, cred->cr_ngroups - 1,
1727 sizeof(gid), gidp_cmp) != NULL);
1728 }
1729
1730 /*
1731 * Check if gid is a member of the (effective) group set (i.e., effective and
1732 * supplementary groups).
1733 */
1734 bool
groupmember(gid_t gid,const struct ucred * cred)1735 groupmember(gid_t gid, const struct ucred *cred)
1736 {
1737
1738 groups_check_positive_len(cred->cr_ngroups);
1739
1740 if (gid == cred->cr_groups[0])
1741 return (true);
1742
1743 return (group_is_supplementary(gid, cred));
1744 }
1745
1746 /*
1747 * Check if gid is a member of the real group set (i.e., real and supplementary
1748 * groups).
1749 */
1750 bool
realgroupmember(gid_t gid,const struct ucred * cred)1751 realgroupmember(gid_t gid, const struct ucred *cred)
1752 {
1753 /*
1754 * Although the equality test on 'cr_rgid' below doesn't access
1755 * 'cr_groups', we check for the latter's length here as we assume that,
1756 * if 'cr_ngroups' is 0, the passed 'struct ucred' is invalid, and
1757 * 'cr_rgid' may not have been filled.
1758 */
1759 groups_check_positive_len(cred->cr_ngroups);
1760
1761 if (gid == cred->cr_rgid)
1762 return (true);
1763
1764 return (group_is_supplementary(gid, cred));
1765 }
1766
1767 /*
1768 * Test the active securelevel against a given level. securelevel_gt()
1769 * implements (securelevel > level). securelevel_ge() implements
1770 * (securelevel >= level). Note that the logic is inverted -- these
1771 * functions return EPERM on "success" and 0 on "failure".
1772 *
1773 * Due to care taken when setting the securelevel, we know that no jail will
1774 * be less secure that its parent (or the physical system), so it is sufficient
1775 * to test the current jail only.
1776 *
1777 * XXXRW: Possibly since this has to do with privilege, it should move to
1778 * kern_priv.c.
1779 */
1780 int
securelevel_gt(struct ucred * cr,int level)1781 securelevel_gt(struct ucred *cr, int level)
1782 {
1783
1784 return (cr->cr_prison->pr_securelevel > level ? EPERM : 0);
1785 }
1786
1787 int
securelevel_ge(struct ucred * cr,int level)1788 securelevel_ge(struct ucred *cr, int level)
1789 {
1790
1791 return (cr->cr_prison->pr_securelevel >= level ? EPERM : 0);
1792 }
1793
1794 /*
1795 * 'see_other_uids' determines whether or not visibility of processes
1796 * and sockets with credentials holding different real uids is possible
1797 * using a variety of system MIBs.
1798 * XXX: data declarations should be together near the beginning of the file.
1799 */
1800 static int see_other_uids = 1;
1801 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
1802 &see_other_uids, 0,
1803 "Unprivileged processes may see subjects/objects with different real uid");
1804
1805 /*-
1806 * Determine if u1 "can see" the subject specified by u2, according to the
1807 * 'see_other_uids' policy.
1808 * Returns: 0 for permitted, ESRCH otherwise
1809 * Locks: none
1810 * References: *u1 and *u2 must not change during the call
1811 * u1 may equal u2, in which case only one reference is required
1812 */
1813 static int
cr_canseeotheruids(struct ucred * u1,struct ucred * u2)1814 cr_canseeotheruids(struct ucred *u1, struct ucred *u2)
1815 {
1816
1817 if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
1818 if (priv_check_cred(u1, PRIV_SEEOTHERUIDS) != 0)
1819 return (ESRCH);
1820 }
1821 return (0);
1822 }
1823
1824 /*
1825 * 'see_other_gids' determines whether or not visibility of processes
1826 * and sockets with credentials holding different real gids is possible
1827 * using a variety of system MIBs.
1828 * XXX: data declarations should be together near the beginning of the file.
1829 */
1830 static int see_other_gids = 1;
1831 SYSCTL_INT(_security_bsd, OID_AUTO, see_other_gids, CTLFLAG_RW,
1832 &see_other_gids, 0,
1833 "Unprivileged processes may see subjects/objects with different real gid");
1834
1835 /*
1836 * Determine if u1 can "see" the subject specified by u2, according to the
1837 * 'see_other_gids' policy.
1838 * Returns: 0 for permitted, ESRCH otherwise
1839 * Locks: none
1840 * References: *u1 and *u2 must not change during the call
1841 * u1 may equal u2, in which case only one reference is required
1842 */
1843 static int
cr_canseeothergids(struct ucred * u1,struct ucred * u2)1844 cr_canseeothergids(struct ucred *u1, struct ucred *u2)
1845 {
1846 if (!see_other_gids) {
1847 if (realgroupmember(u1->cr_rgid, u2))
1848 return (0);
1849
1850 for (int i = 1; i < u1->cr_ngroups; i++)
1851 if (realgroupmember(u1->cr_groups[i], u2))
1852 return (0);
1853
1854 if (priv_check_cred(u1, PRIV_SEEOTHERGIDS) != 0)
1855 return (ESRCH);
1856 }
1857
1858 return (0);
1859 }
1860
1861 /*
1862 * 'see_jail_proc' determines whether or not visibility of processes and
1863 * sockets with credentials holding different jail ids is possible using a
1864 * variety of system MIBs.
1865 *
1866 * XXX: data declarations should be together near the beginning of the file.
1867 */
1868
1869 static int see_jail_proc = 1;
1870 SYSCTL_INT(_security_bsd, OID_AUTO, see_jail_proc, CTLFLAG_RW,
1871 &see_jail_proc, 0,
1872 "Unprivileged processes may see subjects/objects with different jail ids");
1873
1874 /*-
1875 * Determine if u1 "can see" the subject specified by u2, according to the
1876 * 'see_jail_proc' policy.
1877 * Returns: 0 for permitted, ESRCH otherwise
1878 * Locks: none
1879 * References: *u1 and *u2 must not change during the call
1880 * u1 may equal u2, in which case only one reference is required
1881 */
1882 static int
cr_canseejailproc(struct ucred * u1,struct ucred * u2)1883 cr_canseejailproc(struct ucred *u1, struct ucred *u2)
1884 {
1885 if (see_jail_proc || /* Policy deactivated. */
1886 u1->cr_prison == u2->cr_prison || /* Same jail. */
1887 priv_check_cred(u1, PRIV_SEEJAILPROC) == 0) /* Privileged. */
1888 return (0);
1889
1890 return (ESRCH);
1891 }
1892
1893 /*
1894 * Helper for cr_cansee*() functions to abide by system-wide security.bsd.see_*
1895 * policies. Determines if u1 "can see" u2 according to these policies.
1896 * Returns: 0 for permitted, ESRCH otherwise
1897 */
1898 int
cr_bsd_visible(struct ucred * u1,struct ucred * u2)1899 cr_bsd_visible(struct ucred *u1, struct ucred *u2)
1900 {
1901 int error;
1902
1903 error = cr_canseeotheruids(u1, u2);
1904 if (error != 0)
1905 return (error);
1906 error = cr_canseeothergids(u1, u2);
1907 if (error != 0)
1908 return (error);
1909 error = cr_canseejailproc(u1, u2);
1910 if (error != 0)
1911 return (error);
1912 return (0);
1913 }
1914
1915 /*-
1916 * Determine if u1 "can see" the subject specified by u2.
1917 * Returns: 0 for permitted, an errno value otherwise
1918 * Locks: none
1919 * References: *u1 and *u2 must not change during the call
1920 * u1 may equal u2, in which case only one reference is required
1921 */
1922 int
cr_cansee(struct ucred * u1,struct ucred * u2)1923 cr_cansee(struct ucred *u1, struct ucred *u2)
1924 {
1925 int error;
1926
1927 if ((error = prison_check(u1, u2)))
1928 return (error);
1929 #ifdef MAC
1930 if ((error = mac_cred_check_visible(u1, u2)))
1931 return (error);
1932 #endif
1933 if ((error = cr_bsd_visible(u1, u2)))
1934 return (error);
1935 return (0);
1936 }
1937
1938 /*-
1939 * Determine if td "can see" the subject specified by p.
1940 * Returns: 0 for permitted, an errno value otherwise
1941 * Locks: Sufficient locks to protect p->p_ucred must be held. td really
1942 * should be curthread.
1943 * References: td and p must be valid for the lifetime of the call
1944 */
1945 int
p_cansee(struct thread * td,struct proc * p)1946 p_cansee(struct thread *td, struct proc *p)
1947 {
1948 /* Wrap cr_cansee() for all functionality. */
1949 KASSERT(td == curthread, ("%s: td not curthread", __func__));
1950 PROC_LOCK_ASSERT(p, MA_OWNED);
1951
1952 if (td->td_proc == p)
1953 return (0);
1954 return (cr_cansee(td->td_ucred, p->p_ucred));
1955 }
1956
1957 /*
1958 * 'conservative_signals' prevents the delivery of a broad class of
1959 * signals by unprivileged processes to processes that have changed their
1960 * credentials since the last invocation of execve(). This can prevent
1961 * the leakage of cached information or retained privileges as a result
1962 * of a common class of signal-related vulnerabilities. However, this
1963 * may interfere with some applications that expect to be able to
1964 * deliver these signals to peer processes after having given up
1965 * privilege.
1966 */
1967 static int conservative_signals = 1;
1968 SYSCTL_INT(_security_bsd, OID_AUTO, conservative_signals, CTLFLAG_RW,
1969 &conservative_signals, 0, "Unprivileged processes prevented from "
1970 "sending certain signals to processes whose credentials have changed");
1971 /*-
1972 * Determine whether cred may deliver the specified signal to proc.
1973 * Returns: 0 for permitted, an errno value otherwise.
1974 * Locks: A lock must be held for proc.
1975 * References: cred and proc must be valid for the lifetime of the call.
1976 */
1977 int
cr_cansignal(struct ucred * cred,struct proc * proc,int signum)1978 cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
1979 {
1980 int error;
1981
1982 PROC_LOCK_ASSERT(proc, MA_OWNED);
1983 /*
1984 * Jail semantics limit the scope of signalling to proc in the
1985 * same jail as cred, if cred is in jail.
1986 */
1987 error = prison_check(cred, proc->p_ucred);
1988 if (error)
1989 return (error);
1990 #ifdef MAC
1991 if ((error = mac_proc_check_signal(cred, proc, signum)))
1992 return (error);
1993 #endif
1994 if ((error = cr_bsd_visible(cred, proc->p_ucred)))
1995 return (error);
1996
1997 /*
1998 * UNIX signal semantics depend on the status of the P_SUGID
1999 * bit on the target process. If the bit is set, then additional
2000 * restrictions are placed on the set of available signals.
2001 */
2002 if (conservative_signals && (proc->p_flag & P_SUGID)) {
2003 switch (signum) {
2004 case 0:
2005 case SIGKILL:
2006 case SIGINT:
2007 case SIGTERM:
2008 case SIGALRM:
2009 case SIGSTOP:
2010 case SIGTTIN:
2011 case SIGTTOU:
2012 case SIGTSTP:
2013 case SIGHUP:
2014 case SIGUSR1:
2015 case SIGUSR2:
2016 /*
2017 * Generally, permit job and terminal control
2018 * signals.
2019 */
2020 break;
2021 default:
2022 /* Not permitted without privilege. */
2023 error = priv_check_cred(cred, PRIV_SIGNAL_SUGID);
2024 if (error)
2025 return (error);
2026 }
2027 }
2028
2029 /*
2030 * Generally, the target credential's ruid or svuid must match the
2031 * subject credential's ruid or euid.
2032 */
2033 if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
2034 cred->cr_ruid != proc->p_ucred->cr_svuid &&
2035 cred->cr_uid != proc->p_ucred->cr_ruid &&
2036 cred->cr_uid != proc->p_ucred->cr_svuid) {
2037 error = priv_check_cred(cred, PRIV_SIGNAL_DIFFCRED);
2038 if (error)
2039 return (error);
2040 }
2041
2042 return (0);
2043 }
2044
2045 /*-
2046 * Determine whether td may deliver the specified signal to p.
2047 * Returns: 0 for permitted, an errno value otherwise
2048 * Locks: Sufficient locks to protect various components of td and p
2049 * must be held. td must be curthread, and a lock must be
2050 * held for p.
2051 * References: td and p must be valid for the lifetime of the call
2052 */
2053 int
p_cansignal(struct thread * td,struct proc * p,int signum)2054 p_cansignal(struct thread *td, struct proc *p, int signum)
2055 {
2056
2057 KASSERT(td == curthread, ("%s: td not curthread", __func__));
2058 PROC_LOCK_ASSERT(p, MA_OWNED);
2059 if (td->td_proc == p)
2060 return (0);
2061
2062 /*
2063 * UNIX signalling semantics require that processes in the same
2064 * session always be able to deliver SIGCONT to one another,
2065 * overriding the remaining protections.
2066 */
2067 /* XXX: This will require an additional lock of some sort. */
2068 if (signum == SIGCONT && td->td_proc->p_session == p->p_session)
2069 return (0);
2070 /*
2071 * Some compat layers use SIGTHR and higher signals for
2072 * communication between different kernel threads of the same
2073 * process, so that they expect that it's always possible to
2074 * deliver them, even for suid applications where cr_cansignal() can
2075 * deny such ability for security consideration. It should be
2076 * pretty safe to do since the only way to create two processes
2077 * with the same p_leader is via rfork(2).
2078 */
2079 if (td->td_proc->p_leader != NULL && signum >= SIGTHR &&
2080 signum < SIGTHR + 4 && td->td_proc->p_leader == p->p_leader)
2081 return (0);
2082
2083 return (cr_cansignal(td->td_ucred, p, signum));
2084 }
2085
2086 /*-
2087 * Determine whether td may reschedule p.
2088 * Returns: 0 for permitted, an errno value otherwise
2089 * Locks: Sufficient locks to protect various components of td and p
2090 * must be held. td must be curthread, and a lock must
2091 * be held for p.
2092 * References: td and p must be valid for the lifetime of the call
2093 */
2094 int
p_cansched(struct thread * td,struct proc * p)2095 p_cansched(struct thread *td, struct proc *p)
2096 {
2097 int error;
2098
2099 KASSERT(td == curthread, ("%s: td not curthread", __func__));
2100 PROC_LOCK_ASSERT(p, MA_OWNED);
2101 if (td->td_proc == p)
2102 return (0);
2103 if ((error = prison_check(td->td_ucred, p->p_ucred)))
2104 return (error);
2105 #ifdef MAC
2106 if ((error = mac_proc_check_sched(td->td_ucred, p)))
2107 return (error);
2108 #endif
2109 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2110 return (error);
2111
2112 if (td->td_ucred->cr_ruid != p->p_ucred->cr_ruid &&
2113 td->td_ucred->cr_uid != p->p_ucred->cr_ruid) {
2114 error = priv_check(td, PRIV_SCHED_DIFFCRED);
2115 if (error)
2116 return (error);
2117 }
2118 return (0);
2119 }
2120
2121 /*
2122 * Handle getting or setting the prison's unprivileged_proc_debug
2123 * value.
2124 */
2125 static int
sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)2126 sysctl_unprivileged_proc_debug(SYSCTL_HANDLER_ARGS)
2127 {
2128 int error, val;
2129
2130 val = prison_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG);
2131 error = sysctl_handle_int(oidp, &val, 0, req);
2132 if (error != 0 || req->newptr == NULL)
2133 return (error);
2134 if (val != 0 && val != 1)
2135 return (EINVAL);
2136 prison_set_allow(req->td->td_ucred, PR_ALLOW_UNPRIV_DEBUG, val);
2137 return (0);
2138 }
2139
2140 /*
2141 * The 'unprivileged_proc_debug' flag may be used to disable a variety of
2142 * unprivileged inter-process debugging services, including some procfs
2143 * functionality, ptrace(), and ktrace(). In the past, inter-process
2144 * debugging has been involved in a variety of security problems, and sites
2145 * not requiring the service might choose to disable it when hardening
2146 * systems.
2147 */
2148 SYSCTL_PROC(_security_bsd, OID_AUTO, unprivileged_proc_debug,
2149 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_SECURE |
2150 CTLFLAG_MPSAFE, 0, 0, sysctl_unprivileged_proc_debug, "I",
2151 "Unprivileged processes may use process debugging facilities");
2152
2153 /*
2154 * Return true if the object owner/group ids are subset of the active
2155 * credentials.
2156 */
2157 bool
cr_xids_subset(struct ucred * active_cred,struct ucred * obj_cred)2158 cr_xids_subset(struct ucred *active_cred, struct ucred *obj_cred)
2159 {
2160 int i;
2161 bool grpsubset, uidsubset;
2162
2163 /*
2164 * Is p's group set a subset of td's effective group set? This
2165 * includes p's egid, group access list, rgid, and svgid.
2166 */
2167 grpsubset = true;
2168 for (i = 0; i < obj_cred->cr_ngroups; i++) {
2169 if (!groupmember(obj_cred->cr_groups[i], active_cred)) {
2170 grpsubset = false;
2171 break;
2172 }
2173 }
2174 grpsubset = grpsubset &&
2175 groupmember(obj_cred->cr_rgid, active_cred) &&
2176 groupmember(obj_cred->cr_svgid, active_cred);
2177
2178 /*
2179 * Are the uids present in obj_cred's credential equal to
2180 * active_cred's effective uid? This includes obj_cred's
2181 * euid, svuid, and ruid.
2182 */
2183 uidsubset = (active_cred->cr_uid == obj_cred->cr_uid &&
2184 active_cred->cr_uid == obj_cred->cr_svuid &&
2185 active_cred->cr_uid == obj_cred->cr_ruid);
2186
2187 return (uidsubset && grpsubset);
2188 }
2189
2190 /*-
2191 * Determine whether td may debug p.
2192 * Returns: 0 for permitted, an errno value otherwise
2193 * Locks: Sufficient locks to protect various components of td and p
2194 * must be held. td must be curthread, and a lock must
2195 * be held for p.
2196 * References: td and p must be valid for the lifetime of the call
2197 */
2198 int
p_candebug(struct thread * td,struct proc * p)2199 p_candebug(struct thread *td, struct proc *p)
2200 {
2201 int error;
2202
2203 KASSERT(td == curthread, ("%s: td not curthread", __func__));
2204 PROC_LOCK_ASSERT(p, MA_OWNED);
2205 if (td->td_proc == p)
2206 return (0);
2207 if ((error = priv_check(td, PRIV_DEBUG_UNPRIV)))
2208 return (error);
2209 if ((error = prison_check(td->td_ucred, p->p_ucred)))
2210 return (error);
2211 #ifdef MAC
2212 if ((error = mac_proc_check_debug(td->td_ucred, p)))
2213 return (error);
2214 #endif
2215 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2216 return (error);
2217
2218 /*
2219 * If p's gids aren't a subset, or the uids aren't a subset,
2220 * or the credential has changed, require appropriate privilege
2221 * for td to debug p.
2222 */
2223 if (!cr_xids_subset(td->td_ucred, p->p_ucred)) {
2224 error = priv_check(td, PRIV_DEBUG_DIFFCRED);
2225 if (error)
2226 return (error);
2227 }
2228
2229 /*
2230 * Has the credential of the process changed since the last exec()?
2231 */
2232 if ((p->p_flag & P_SUGID) != 0) {
2233 error = priv_check(td, PRIV_DEBUG_SUGID);
2234 if (error)
2235 return (error);
2236 }
2237
2238 /* Can't trace init when securelevel > 0. */
2239 if (p == initproc) {
2240 error = securelevel_gt(td->td_ucred, 0);
2241 if (error)
2242 return (error);
2243 }
2244
2245 /*
2246 * Can't trace a process that's currently exec'ing.
2247 *
2248 * XXX: Note, this is not a security policy decision, it's a
2249 * basic correctness/functionality decision. Therefore, this check
2250 * should be moved to the caller's of p_candebug().
2251 */
2252 if ((p->p_flag & P_INEXEC) != 0)
2253 return (EBUSY);
2254
2255 /* Denied explicitly */
2256 if ((p->p_flag2 & P2_NOTRACE) != 0) {
2257 error = priv_check(td, PRIV_DEBUG_DENIED);
2258 if (error != 0)
2259 return (error);
2260 }
2261
2262 return (0);
2263 }
2264
2265 /*-
2266 * Determine whether the subject represented by cred can "see" a socket.
2267 * Returns: 0 for permitted, ENOENT otherwise.
2268 */
2269 int
cr_canseesocket(struct ucred * cred,struct socket * so)2270 cr_canseesocket(struct ucred *cred, struct socket *so)
2271 {
2272 int error;
2273
2274 error = prison_check(cred, so->so_cred);
2275 if (error)
2276 return (ENOENT);
2277 #ifdef MAC
2278 error = mac_socket_check_visible(cred, so);
2279 if (error)
2280 return (error);
2281 #endif
2282 if (cr_bsd_visible(cred, so->so_cred))
2283 return (ENOENT);
2284
2285 return (0);
2286 }
2287
2288 /*-
2289 * Determine whether td can wait for the exit of p.
2290 * Returns: 0 for permitted, an errno value otherwise
2291 * Locks: Sufficient locks to protect various components of td and p
2292 * must be held. td must be curthread, and a lock must
2293 * be held for p.
2294 * References: td and p must be valid for the lifetime of the call
2295
2296 */
2297 int
p_canwait(struct thread * td,struct proc * p)2298 p_canwait(struct thread *td, struct proc *p)
2299 {
2300 int error;
2301
2302 KASSERT(td == curthread, ("%s: td not curthread", __func__));
2303 PROC_LOCK_ASSERT(p, MA_OWNED);
2304 if ((error = prison_check(td->td_ucred, p->p_ucred)))
2305 return (error);
2306 #ifdef MAC
2307 if ((error = mac_proc_check_wait(td->td_ucred, p)))
2308 return (error);
2309 #endif
2310 #if 0
2311 /* XXXMAC: This could have odd effects on some shells. */
2312 if ((error = cr_bsd_visible(td->td_ucred, p->p_ucred)))
2313 return (error);
2314 #endif
2315
2316 return (0);
2317 }
2318
2319 /*
2320 * Credential management.
2321 *
2322 * struct ucred objects are rarely allocated but gain and lose references all
2323 * the time (e.g., on struct file alloc/dealloc) turning refcount updates into
2324 * a significant source of cache-line ping ponging. Common cases are worked
2325 * around by modifying thread-local counter instead if the cred to operate on
2326 * matches td_realucred.
2327 *
2328 * The counter is split into 2 parts:
2329 * - cr_users -- total count of all struct proc and struct thread objects
2330 * which have given cred in p_ucred and td_ucred respectively
2331 * - cr_ref -- the actual ref count, only valid if cr_users == 0
2332 *
2333 * If users == 0 then cr_ref behaves similarly to refcount(9), in particular if
2334 * the count reaches 0 the object is freeable.
2335 * If users > 0 and curthread->td_realucred == cred, then updates are performed
2336 * against td_ucredref.
2337 * In other cases updates are performed against cr_ref.
2338 *
2339 * Changing td_realucred into something else decrements cr_users and transfers
2340 * accumulated updates.
2341 */
2342 struct ucred *
crcowget(struct ucred * cr)2343 crcowget(struct ucred *cr)
2344 {
2345
2346 mtx_lock(&cr->cr_mtx);
2347 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2348 __func__, cr->cr_users, cr));
2349 cr->cr_users++;
2350 cr->cr_ref++;
2351 mtx_unlock(&cr->cr_mtx);
2352 return (cr);
2353 }
2354
2355 static struct ucred *
crunuse(struct thread * td)2356 crunuse(struct thread *td)
2357 {
2358 struct ucred *cr, *crold;
2359
2360 MPASS(td->td_realucred == td->td_ucred);
2361 cr = td->td_realucred;
2362 mtx_lock(&cr->cr_mtx);
2363 cr->cr_ref += td->td_ucredref;
2364 td->td_ucredref = 0;
2365 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2366 __func__, cr->cr_users, cr));
2367 cr->cr_users--;
2368 if (cr->cr_users == 0) {
2369 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2370 __func__, cr->cr_ref, cr));
2371 crold = cr;
2372 } else {
2373 cr->cr_ref--;
2374 crold = NULL;
2375 }
2376 mtx_unlock(&cr->cr_mtx);
2377 td->td_realucred = NULL;
2378 return (crold);
2379 }
2380
2381 static void
crunusebatch(struct ucred * cr,u_int users,long ref)2382 crunusebatch(struct ucred *cr, u_int users, long ref)
2383 {
2384
2385 KASSERT(users > 0, ("%s: passed users %d not > 0 ; cred %p",
2386 __func__, users, cr));
2387 mtx_lock(&cr->cr_mtx);
2388 KASSERT(cr->cr_users >= users, ("%s: users %d not > %d on cred %p",
2389 __func__, cr->cr_users, users, cr));
2390 cr->cr_users -= users;
2391 cr->cr_ref += ref;
2392 cr->cr_ref -= users;
2393 if (cr->cr_users > 0) {
2394 mtx_unlock(&cr->cr_mtx);
2395 return;
2396 }
2397 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2398 __func__, cr->cr_ref, cr));
2399 if (cr->cr_ref > 0) {
2400 mtx_unlock(&cr->cr_mtx);
2401 return;
2402 }
2403 crfree_final(cr);
2404 }
2405
2406 void
crcowfree(struct thread * td)2407 crcowfree(struct thread *td)
2408 {
2409 struct ucred *cr;
2410
2411 cr = crunuse(td);
2412 if (cr != NULL)
2413 crfree(cr);
2414 }
2415
2416 struct ucred *
crcowsync(void)2417 crcowsync(void)
2418 {
2419 struct thread *td;
2420 struct proc *p;
2421 struct ucred *crnew, *crold;
2422
2423 td = curthread;
2424 p = td->td_proc;
2425 PROC_LOCK_ASSERT(p, MA_OWNED);
2426
2427 MPASS(td->td_realucred == td->td_ucred);
2428 if (td->td_realucred == p->p_ucred)
2429 return (NULL);
2430
2431 crnew = crcowget(p->p_ucred);
2432 crold = crunuse(td);
2433 td->td_realucred = crnew;
2434 td->td_ucred = td->td_realucred;
2435 return (crold);
2436 }
2437
2438 /*
2439 * Batching.
2440 */
2441 void
credbatch_add(struct credbatch * crb,struct thread * td)2442 credbatch_add(struct credbatch *crb, struct thread *td)
2443 {
2444 struct ucred *cr;
2445
2446 MPASS(td->td_realucred != NULL);
2447 MPASS(td->td_realucred == td->td_ucred);
2448 MPASS(TD_GET_STATE(td) == TDS_INACTIVE);
2449 cr = td->td_realucred;
2450 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2451 __func__, cr->cr_users, cr));
2452 if (crb->cred != cr) {
2453 if (crb->users > 0) {
2454 MPASS(crb->cred != NULL);
2455 crunusebatch(crb->cred, crb->users, crb->ref);
2456 crb->users = 0;
2457 crb->ref = 0;
2458 }
2459 }
2460 crb->cred = cr;
2461 crb->users++;
2462 crb->ref += td->td_ucredref;
2463 td->td_ucredref = 0;
2464 td->td_realucred = NULL;
2465 }
2466
2467 void
credbatch_final(struct credbatch * crb)2468 credbatch_final(struct credbatch *crb)
2469 {
2470
2471 MPASS(crb->cred != NULL);
2472 MPASS(crb->users > 0);
2473 crunusebatch(crb->cred, crb->users, crb->ref);
2474 }
2475
2476 /*
2477 * Allocate a zeroed cred structure.
2478 */
2479 struct ucred *
crget(void)2480 crget(void)
2481 {
2482 struct ucred *cr;
2483
2484 cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
2485 mtx_init(&cr->cr_mtx, "cred", NULL, MTX_DEF);
2486 cr->cr_ref = 1;
2487 #ifdef AUDIT
2488 audit_cred_init(cr);
2489 #endif
2490 #ifdef MAC
2491 mac_cred_init(cr);
2492 #endif
2493 cr->cr_groups = cr->cr_smallgroups;
2494 cr->cr_agroups = nitems(cr->cr_smallgroups);
2495 return (cr);
2496 }
2497
2498 /*
2499 * Claim another reference to a ucred structure.
2500 */
2501 struct ucred *
crhold(struct ucred * cr)2502 crhold(struct ucred *cr)
2503 {
2504 struct thread *td;
2505
2506 td = curthread;
2507 if (__predict_true(td->td_realucred == cr)) {
2508 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2509 __func__, cr->cr_users, cr));
2510 td->td_ucredref++;
2511 return (cr);
2512 }
2513 mtx_lock(&cr->cr_mtx);
2514 cr->cr_ref++;
2515 mtx_unlock(&cr->cr_mtx);
2516 return (cr);
2517 }
2518
2519 /*
2520 * Free a cred structure. Throws away space when ref count gets to 0.
2521 */
2522 void
crfree(struct ucred * cr)2523 crfree(struct ucred *cr)
2524 {
2525 struct thread *td;
2526
2527 td = curthread;
2528 if (__predict_true(td->td_realucred == cr)) {
2529 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2530 __func__, cr->cr_users, cr));
2531 td->td_ucredref--;
2532 return;
2533 }
2534 mtx_lock(&cr->cr_mtx);
2535 KASSERT(cr->cr_users >= 0, ("%s: users %d not >= 0 on cred %p",
2536 __func__, cr->cr_users, cr));
2537 cr->cr_ref--;
2538 if (cr->cr_users > 0) {
2539 mtx_unlock(&cr->cr_mtx);
2540 return;
2541 }
2542 KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p",
2543 __func__, cr->cr_ref, cr));
2544 if (cr->cr_ref > 0) {
2545 mtx_unlock(&cr->cr_mtx);
2546 return;
2547 }
2548 crfree_final(cr);
2549 }
2550
2551 static void
crfree_final(struct ucred * cr)2552 crfree_final(struct ucred *cr)
2553 {
2554
2555 KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p",
2556 __func__, cr->cr_users, cr));
2557 KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p",
2558 __func__, cr->cr_ref, cr));
2559
2560 /*
2561 * Some callers of crget(), such as nfs_statfs(), allocate a temporary
2562 * credential, but don't allocate a uidinfo structure.
2563 */
2564 if (cr->cr_uidinfo != NULL)
2565 uifree(cr->cr_uidinfo);
2566 if (cr->cr_ruidinfo != NULL)
2567 uifree(cr->cr_ruidinfo);
2568 if (cr->cr_prison != NULL)
2569 prison_free(cr->cr_prison);
2570 if (cr->cr_loginclass != NULL)
2571 loginclass_free(cr->cr_loginclass);
2572 #ifdef AUDIT
2573 audit_cred_destroy(cr);
2574 #endif
2575 #ifdef MAC
2576 mac_cred_destroy(cr);
2577 #endif
2578 mtx_destroy(&cr->cr_mtx);
2579 if (cr->cr_groups != cr->cr_smallgroups)
2580 free(cr->cr_groups, M_CRED);
2581 free(cr, M_CRED);
2582 }
2583
2584 /*
2585 * Copy a ucred's contents from a template. Does not block.
2586 */
2587 void
crcopy(struct ucred * dest,struct ucred * src)2588 crcopy(struct ucred *dest, struct ucred *src)
2589 {
2590
2591 /*
2592 * Ideally, 'cr_ngroups' should be moved out of 'struct ucred''s bcopied
2593 * area, but this would break the ABI, so is deferred until there is
2594 * a compelling need to change it.
2595 */
2596 bcopy(&src->cr_startcopy, &dest->cr_startcopy,
2597 (unsigned)((caddr_t)&src->cr_endcopy -
2598 (caddr_t)&src->cr_startcopy));
2599 dest->cr_flags = src->cr_flags;
2600 crsetgroups(dest, src->cr_ngroups, src->cr_groups);
2601 uihold(dest->cr_uidinfo);
2602 uihold(dest->cr_ruidinfo);
2603 prison_hold(dest->cr_prison);
2604 loginclass_hold(dest->cr_loginclass);
2605 #ifdef AUDIT
2606 audit_cred_copy(src, dest);
2607 #endif
2608 #ifdef MAC
2609 mac_cred_copy(src, dest);
2610 #endif
2611 }
2612
2613 /*
2614 * Dup cred struct to a new held one.
2615 */
2616 struct ucred *
crdup(struct ucred * cr)2617 crdup(struct ucred *cr)
2618 {
2619 struct ucred *newcr;
2620
2621 newcr = crget();
2622 crcopy(newcr, cr);
2623 return (newcr);
2624 }
2625
2626 /*
2627 * Fill in a struct xucred based on a struct ucred.
2628 */
2629 void
cru2x(struct ucred * cr,struct xucred * xcr)2630 cru2x(struct ucred *cr, struct xucred *xcr)
2631 {
2632 int ngroups;
2633
2634 bzero(xcr, sizeof(*xcr));
2635 xcr->cr_version = XUCRED_VERSION;
2636 xcr->cr_uid = cr->cr_uid;
2637
2638 ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
2639 xcr->cr_ngroups = ngroups;
2640 bcopy(cr->cr_groups, xcr->cr_groups,
2641 ngroups * sizeof(*cr->cr_groups));
2642 }
2643
2644 void
cru2xt(struct thread * td,struct xucred * xcr)2645 cru2xt(struct thread *td, struct xucred *xcr)
2646 {
2647
2648 cru2x(td->td_ucred, xcr);
2649 xcr->cr_pid = td->td_proc->p_pid;
2650 }
2651
2652 /*
2653 * Change process credentials.
2654 *
2655 * Callers are responsible for providing the reference for passed credentials
2656 * and for freeing old ones. Calls chgproccnt() to correctly account the
2657 * current process to the proper real UID, if the latter has changed. Returns
2658 * whether the operation was successful. Failure can happen only on
2659 * 'enforce_proc_lim' being true and if no new process can be accounted to the
2660 * new real UID because of the current limit (see the inner comment for more
2661 * details) and the caller does not have privilege (PRIV_PROC_LIMIT) to override
2662 * that.
2663 */
2664 static bool
_proc_set_cred(struct proc * p,struct ucred * newcred,bool enforce_proc_lim)2665 _proc_set_cred(struct proc *p, struct ucred *newcred, bool enforce_proc_lim)
2666 {
2667 struct ucred *const oldcred = p->p_ucred;
2668
2669 MPASS(oldcred != NULL);
2670 PROC_LOCK_ASSERT(p, MA_OWNED);
2671 KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
2672 __func__, newcred->cr_users, newcred));
2673 KASSERT(newcred->cr_ref == 1, ("%s: ref %ld not 1 on cred %p",
2674 __func__, newcred->cr_ref, newcred));
2675
2676 if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo) {
2677 /*
2678 * XXXOC: This check is flawed but nonetheless the best we can
2679 * currently do as we don't really track limits per UID contrary
2680 * to what we pretend in setrlimit(2). Until this is reworked,
2681 * we just check here that the number of processes for our new
2682 * real UID doesn't exceed this process' process number limit
2683 * (which is meant to be associated with the current real UID).
2684 */
2685 const int proccnt_changed = chgproccnt(newcred->cr_ruidinfo, 1,
2686 enforce_proc_lim ? lim_cur_proc(p, RLIMIT_NPROC) : 0);
2687
2688 if (!proccnt_changed) {
2689 if (priv_check_cred(oldcred, PRIV_PROC_LIMIT) != 0)
2690 return (false);
2691 (void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
2692 }
2693 }
2694
2695 mtx_lock(&oldcred->cr_mtx);
2696 KASSERT(oldcred->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2697 __func__, oldcred->cr_users, oldcred));
2698 oldcred->cr_users--;
2699 mtx_unlock(&oldcred->cr_mtx);
2700 p->p_ucred = newcred;
2701 newcred->cr_users = 1;
2702 PROC_UPDATE_COW(p);
2703 if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo)
2704 (void)chgproccnt(oldcred->cr_ruidinfo, -1, 0);
2705 return (true);
2706 }
2707
2708 void
proc_set_cred(struct proc * p,struct ucred * newcred)2709 proc_set_cred(struct proc *p, struct ucred *newcred)
2710 {
2711 bool success __diagused = _proc_set_cred(p, newcred, false);
2712
2713 MPASS(success);
2714 }
2715
2716 bool
proc_set_cred_enforce_proc_lim(struct proc * p,struct ucred * newcred)2717 proc_set_cred_enforce_proc_lim(struct proc *p, struct ucred *newcred)
2718 {
2719 return (_proc_set_cred(p, newcred, true));
2720 }
2721
2722 void
proc_unset_cred(struct proc * p,bool decrement_proc_count)2723 proc_unset_cred(struct proc *p, bool decrement_proc_count)
2724 {
2725 struct ucred *cr;
2726
2727 MPASS(p->p_state == PRS_ZOMBIE || p->p_state == PRS_NEW);
2728 cr = p->p_ucred;
2729 p->p_ucred = NULL;
2730 KASSERT(cr->cr_users > 0, ("%s: users %d not > 0 on cred %p",
2731 __func__, cr->cr_users, cr));
2732 mtx_lock(&cr->cr_mtx);
2733 cr->cr_users--;
2734 if (cr->cr_users == 0)
2735 KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p",
2736 __func__, cr->cr_ref, cr));
2737 mtx_unlock(&cr->cr_mtx);
2738 if (decrement_proc_count)
2739 (void)chgproccnt(cr->cr_ruidinfo, -1, 0);
2740 crfree(cr);
2741 }
2742
2743 struct ucred *
crcopysafe(struct proc * p,struct ucred * cr)2744 crcopysafe(struct proc *p, struct ucred *cr)
2745 {
2746 struct ucred *oldcred;
2747 int groups;
2748
2749 PROC_LOCK_ASSERT(p, MA_OWNED);
2750
2751 oldcred = p->p_ucred;
2752 while (cr->cr_agroups < oldcred->cr_agroups) {
2753 groups = oldcred->cr_agroups;
2754 PROC_UNLOCK(p);
2755 crextend(cr, groups);
2756 PROC_LOCK(p);
2757 oldcred = p->p_ucred;
2758 }
2759 crcopy(cr, oldcred);
2760
2761 return (oldcred);
2762 }
2763
2764 /*
2765 * Extend the passed-in credentials to hold n groups.
2766 *
2767 * Must not be called after groups have been set.
2768 */
2769 void
crextend(struct ucred * cr,int n)2770 crextend(struct ucred *cr, int n)
2771 {
2772 size_t nbytes;
2773
2774 MPASS2(cr->cr_ref == 1, "'cr_ref' must be 1 (referenced, unshared)");
2775 MPASS2(cr->cr_ngroups == 0, "groups on 'cr' already set!");
2776 groups_check_positive_len(n);
2777 groups_check_max_len(n);
2778
2779 if (n <= cr->cr_agroups)
2780 return;
2781
2782 nbytes = n * sizeof(gid_t);
2783 if (nbytes < n)
2784 panic("Too many groups (memory size overflow)! "
2785 "Computation of 'kern.ngroups' should have prevented this, "
2786 "please fix it. In the meantime, reduce 'kern.ngroups'.");
2787
2788 /*
2789 * We allocate a power of 2 larger than 'nbytes', except when that
2790 * exceeds PAGE_SIZE, in which case we allocate the right multiple of
2791 * pages. We assume PAGE_SIZE is a power of 2 (the call to roundup2()
2792 * below) but do not need to for sizeof(gid_t).
2793 */
2794 if (nbytes < PAGE_SIZE) {
2795 if (!powerof2(nbytes))
2796 /* fls*() return a bit index starting at 1. */
2797 nbytes = 1 << flsl(nbytes);
2798 } else
2799 nbytes = roundup2(nbytes, PAGE_SIZE);
2800
2801 /* Free the old array. */
2802 if (cr->cr_groups != cr->cr_smallgroups)
2803 free(cr->cr_groups, M_CRED);
2804
2805 cr->cr_groups = malloc(nbytes, M_CRED, M_WAITOK | M_ZERO);
2806 cr->cr_agroups = nbytes / sizeof(gid_t);
2807 }
2808
2809 /*
2810 * Normalizes a set of groups to be applied to a 'struct ucred'.
2811 *
2812 * The set of groups is an array that must comprise the effective GID as its
2813 * first element (so its length cannot be 0).
2814 *
2815 * Normalization ensures that elements after the first, which stand for the
2816 * supplementary groups, are sorted in ascending order and do not contain
2817 * duplicates.
2818 */
2819 static void
groups_normalize(int * ngrp,gid_t * groups)2820 groups_normalize(int *ngrp, gid_t *groups)
2821 {
2822 gid_t prev_g;
2823 int ins_idx;
2824
2825 groups_check_positive_len(*ngrp);
2826 groups_check_max_len(*ngrp);
2827
2828 if (*ngrp == 1)
2829 return;
2830
2831 qsort(groups + 1, *ngrp - 1, sizeof(*groups), gidp_cmp);
2832
2833 /* Remove duplicates. */
2834 prev_g = groups[1];
2835 ins_idx = 2;
2836 for (int i = 2; i < *ngrp; ++i) {
2837 const gid_t g = groups[i];
2838
2839 if (g != prev_g) {
2840 if (i != ins_idx)
2841 groups[ins_idx] = g;
2842 ++ins_idx;
2843 prev_g = g;
2844 }
2845 }
2846 *ngrp = ins_idx;
2847
2848 groups_check_normalized(*ngrp, groups);
2849 }
2850
2851 /*
2852 * Internal function copying groups into a credential.
2853 *
2854 * 'ngrp' must be strictly positive. Either the passed 'groups' array must have
2855 * been normalized in advance (see groups_normalize()), else it must be so
2856 * before the structure is to be used again.
2857 *
2858 * This function is suitable to be used under any lock (it doesn't take any lock
2859 * itself nor sleep, and in particular doesn't allocate memory). crextend()
2860 * must have been called beforehand to ensure sufficient space is available.
2861 * See also crsetgroups(), which handles that.
2862 */
2863 static void
crsetgroups_internal(struct ucred * cr,int ngrp,const gid_t * groups)2864 crsetgroups_internal(struct ucred *cr, int ngrp, const gid_t *groups)
2865 {
2866
2867 MPASS2(cr->cr_ref == 1, "'cr_ref' must be 1 (referenced, unshared)");
2868 MPASS2(cr->cr_agroups >= ngrp, "'cr_agroups' too small");
2869 groups_check_positive_len(ngrp);
2870
2871 bcopy(groups, cr->cr_groups, ngrp * sizeof(gid_t));
2872 cr->cr_ngroups = ngrp;
2873 }
2874
2875 /*
2876 * Copy groups in to a credential after expanding it if required.
2877 *
2878 * May sleep in order to allocate memory (except if, e.g., crextend() was called
2879 * before with 'ngrp' or greater). Truncates the list to (ngroups_max + 1) if
2880 * it is too large. Array 'groups' doesn't need to be sorted. 'ngrp' must be
2881 * strictly positive.
2882 */
2883 void
crsetgroups(struct ucred * cr,int ngrp,const gid_t * groups)2884 crsetgroups(struct ucred *cr, int ngrp, const gid_t *groups)
2885 {
2886
2887 if (ngrp > ngroups_max + 1)
2888 ngrp = ngroups_max + 1;
2889 /*
2890 * crextend() asserts that groups are not set, as it may allocate a new
2891 * backing storage without copying the content of the old one. Since we
2892 * are going to install a completely new set anyway, signal that we
2893 * consider the old ones thrown away.
2894 */
2895 cr->cr_ngroups = 0;
2896 crextend(cr, ngrp);
2897 crsetgroups_internal(cr, ngrp, groups);
2898 groups_normalize(&cr->cr_ngroups, cr->cr_groups);
2899 }
2900
2901 /*
2902 * Same as crsetgroups() but accepts an empty groups array.
2903 *
2904 * This function ensures that an effective GID is always present in credentials.
2905 * An empty array is treated as a one-size one holding the passed effective GID
2906 * fallback.
2907 */
2908 void
crsetgroups_fallback(struct ucred * cr,int ngrp,const gid_t * groups,const gid_t fallback)2909 crsetgroups_fallback(struct ucred *cr, int ngrp, const gid_t *groups,
2910 const gid_t fallback)
2911 {
2912 if (ngrp == 0)
2913 /* Shortcut. */
2914 crsetgroups_internal(cr, 1, &fallback);
2915 else
2916 crsetgroups(cr, ngrp, groups);
2917 }
2918
2919 /*
2920 * Get login name, if available.
2921 */
2922 #ifndef _SYS_SYSPROTO_H_
2923 struct getlogin_args {
2924 char *namebuf;
2925 u_int namelen;
2926 };
2927 #endif
2928 /* ARGSUSED */
2929 int
sys_getlogin(struct thread * td,struct getlogin_args * uap)2930 sys_getlogin(struct thread *td, struct getlogin_args *uap)
2931 {
2932 char login[MAXLOGNAME];
2933 struct proc *p = td->td_proc;
2934 size_t len;
2935
2936 if (uap->namelen > MAXLOGNAME)
2937 uap->namelen = MAXLOGNAME;
2938 PROC_LOCK(p);
2939 SESS_LOCK(p->p_session);
2940 len = strlcpy(login, p->p_session->s_login, uap->namelen) + 1;
2941 SESS_UNLOCK(p->p_session);
2942 PROC_UNLOCK(p);
2943 if (len > uap->namelen)
2944 return (ERANGE);
2945 return (copyout(login, uap->namebuf, len));
2946 }
2947
2948 /*
2949 * Set login name.
2950 */
2951 #ifndef _SYS_SYSPROTO_H_
2952 struct setlogin_args {
2953 char *namebuf;
2954 };
2955 #endif
2956 /* ARGSUSED */
2957 int
sys_setlogin(struct thread * td,struct setlogin_args * uap)2958 sys_setlogin(struct thread *td, struct setlogin_args *uap)
2959 {
2960 struct proc *p = td->td_proc;
2961 int error;
2962 char logintmp[MAXLOGNAME];
2963
2964 CTASSERT(sizeof(p->p_session->s_login) >= sizeof(logintmp));
2965
2966 error = priv_check(td, PRIV_PROC_SETLOGIN);
2967 if (error)
2968 return (error);
2969 error = copyinstr(uap->namebuf, logintmp, sizeof(logintmp), NULL);
2970 if (error != 0) {
2971 if (error == ENAMETOOLONG)
2972 error = EINVAL;
2973 return (error);
2974 }
2975 AUDIT_ARG_LOGIN(logintmp);
2976 PROC_LOCK(p);
2977 SESS_LOCK(p->p_session);
2978 strcpy(p->p_session->s_login, logintmp);
2979 SESS_UNLOCK(p->p_session);
2980 PROC_UNLOCK(p);
2981 return (0);
2982 }
2983
2984 void
setsugid(struct proc * p)2985 setsugid(struct proc *p)
2986 {
2987
2988 PROC_LOCK_ASSERT(p, MA_OWNED);
2989 p->p_flag |= P_SUGID;
2990 }
2991
2992 /*-
2993 * Change a process's effective uid.
2994 * Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
2995 * References: newcred must be an exclusive credential reference for the
2996 * duration of the call.
2997 */
2998 void
change_euid(struct ucred * newcred,struct uidinfo * euip)2999 change_euid(struct ucred *newcred, struct uidinfo *euip)
3000 {
3001
3002 newcred->cr_uid = euip->ui_uid;
3003 uihold(euip);
3004 uifree(newcred->cr_uidinfo);
3005 newcred->cr_uidinfo = euip;
3006 }
3007
3008 /*-
3009 * Change a process's effective gid.
3010 * Side effects: newcred->cr_gid will be modified.
3011 * References: newcred must be an exclusive credential reference for the
3012 * duration of the call.
3013 */
3014 void
change_egid(struct ucred * newcred,gid_t egid)3015 change_egid(struct ucred *newcred, gid_t egid)
3016 {
3017
3018 newcred->cr_groups[0] = egid;
3019 }
3020
3021 /*-
3022 * Change a process's real uid.
3023 * Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
3024 * will be updated.
3025 * References: newcred must be an exclusive credential reference for the
3026 * duration of the call.
3027 */
3028 void
change_ruid(struct ucred * newcred,struct uidinfo * ruip)3029 change_ruid(struct ucred *newcred, struct uidinfo *ruip)
3030 {
3031
3032 newcred->cr_ruid = ruip->ui_uid;
3033 uihold(ruip);
3034 uifree(newcred->cr_ruidinfo);
3035 newcred->cr_ruidinfo = ruip;
3036 }
3037
3038 /*-
3039 * Change a process's real gid.
3040 * Side effects: newcred->cr_rgid will be updated.
3041 * References: newcred must be an exclusive credential reference for the
3042 * duration of the call.
3043 */
3044 void
change_rgid(struct ucred * newcred,gid_t rgid)3045 change_rgid(struct ucred *newcred, gid_t rgid)
3046 {
3047
3048 newcred->cr_rgid = rgid;
3049 }
3050
3051 /*-
3052 * Change a process's saved uid.
3053 * Side effects: newcred->cr_svuid will be updated.
3054 * References: newcred must be an exclusive credential reference for the
3055 * duration of the call.
3056 */
3057 void
change_svuid(struct ucred * newcred,uid_t svuid)3058 change_svuid(struct ucred *newcred, uid_t svuid)
3059 {
3060
3061 newcred->cr_svuid = svuid;
3062 }
3063
3064 /*-
3065 * Change a process's saved gid.
3066 * Side effects: newcred->cr_svgid will be updated.
3067 * References: newcred must be an exclusive credential reference for the
3068 * duration of the call.
3069 */
3070 void
change_svgid(struct ucred * newcred,gid_t svgid)3071 change_svgid(struct ucred *newcred, gid_t svgid)
3072 {
3073
3074 newcred->cr_svgid = svgid;
3075 }
3076
3077 bool allow_ptrace = true;
3078 SYSCTL_BOOL(_security_bsd, OID_AUTO, allow_ptrace, CTLFLAG_RWTUN,
3079 &allow_ptrace, 0,
3080 "Deny ptrace(2) use by returning ENOSYS");
3081