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