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