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