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 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_prot.c 8.6 (Berkeley) 1/21/94 35 * $FreeBSD: src/sys/kern/kern_prot.c,v 1.53.2.9 2002/03/09 05:20:26 dd Exp $ 36 */ 37 38 /* 39 * System calls related to processes and protection 40 */ 41 42 #include <sys/param.h> 43 #include <sys/acct.h> 44 #include <sys/systm.h> 45 #include <sys/sysproto.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/proc.h> 49 #include <sys/priv.h> 50 #include <sys/malloc.h> 51 #include <sys/pioctl.h> 52 #include <sys/resourcevar.h> 53 #include <sys/jail.h> 54 #include <sys/lockf.h> 55 #include <sys/spinlock.h> 56 57 #include <sys/spinlock2.h> 58 59 static MALLOC_DEFINE(M_CRED, "cred", "credentials"); 60 61 int 62 sys_getpid(struct getpid_args *uap) 63 { 64 struct proc *p = curproc; 65 66 uap->sysmsg_fds[0] = p->p_pid; 67 return (0); 68 } 69 70 int 71 sys_getppid(struct getppid_args *uap) 72 { 73 struct proc *p = curproc; 74 75 uap->sysmsg_result = p->p_ppid; 76 77 return (0); 78 } 79 80 int 81 sys_lwp_gettid(struct lwp_gettid_args *uap) 82 { 83 struct lwp *lp = curthread->td_lwp; 84 uap->sysmsg_result = lp->lwp_tid; 85 return (0); 86 } 87 88 /* 89 * Get process group ID; note that POSIX getpgrp takes no parameter 90 */ 91 int 92 sys_getpgrp(struct getpgrp_args *uap) 93 { 94 struct proc *p = curproc; 95 96 lwkt_gettoken_shared(&p->p_token); 97 uap->sysmsg_result = p->p_pgrp->pg_id; 98 lwkt_reltoken(&p->p_token); 99 100 return (0); 101 } 102 103 /* 104 * Get an arbitrary pid's process group id 105 */ 106 int 107 sys_getpgid(struct getpgid_args *uap) 108 { 109 struct proc *p = curproc; 110 struct proc *pt; 111 int error; 112 113 error = 0; 114 115 if (uap->pid == 0) { 116 pt = p; 117 PHOLD(pt); 118 } else { 119 pt = pfind(uap->pid); 120 if (pt == NULL) 121 error = ESRCH; 122 } 123 if (error == 0) { 124 lwkt_gettoken_shared(&pt->p_token); 125 uap->sysmsg_result = pt->p_pgrp->pg_id; 126 lwkt_reltoken(&pt->p_token); 127 } 128 if (pt) 129 PRELE(pt); 130 return (error); 131 } 132 133 /* 134 * Get an arbitrary pid's session id. 135 */ 136 int 137 sys_getsid(struct getsid_args *uap) 138 { 139 struct proc *p = curproc; 140 struct proc *pt; 141 int error; 142 143 error = 0; 144 145 if (uap->pid == 0) { 146 pt = p; 147 PHOLD(pt); 148 } else { 149 pt = pfind(uap->pid); 150 if (pt == NULL) 151 error = ESRCH; 152 } 153 if (error == 0) 154 uap->sysmsg_result = pt->p_session->s_sid; 155 if (pt) 156 PRELE(pt); 157 return (error); 158 } 159 160 161 /* 162 * getuid() 163 */ 164 int 165 sys_getuid(struct getuid_args *uap) 166 { 167 struct ucred *cred = curthread->td_ucred; 168 169 uap->sysmsg_fds[0] = cred->cr_ruid; 170 return (0); 171 } 172 173 /* 174 * geteuid() 175 */ 176 int 177 sys_geteuid(struct geteuid_args *uap) 178 { 179 struct ucred *cred = curthread->td_ucred; 180 181 uap->sysmsg_result = cred->cr_uid; 182 return (0); 183 } 184 185 /* 186 * getgid() 187 */ 188 int 189 sys_getgid(struct getgid_args *uap) 190 { 191 struct ucred *cred = curthread->td_ucred; 192 193 uap->sysmsg_fds[0] = cred->cr_rgid; 194 return (0); 195 } 196 197 /* 198 * Get effective group ID. The "egid" is groups[0], and could be obtained 199 * via getgroups. This syscall exists because it is somewhat painful to do 200 * correctly in a library function. 201 */ 202 int 203 sys_getegid(struct getegid_args *uap) 204 { 205 struct ucred *cred = curthread->td_ucred; 206 207 uap->sysmsg_result = cred->cr_groups[0]; 208 return (0); 209 } 210 211 int 212 sys_getgroups(struct getgroups_args *uap) 213 { 214 struct ucred *cr; 215 u_int ngrp; 216 int error; 217 218 cr = curthread->td_ucred; 219 if ((ngrp = uap->gidsetsize) == 0) { 220 uap->sysmsg_result = cr->cr_ngroups; 221 return (0); 222 } 223 if (ngrp < cr->cr_ngroups) 224 return (EINVAL); 225 ngrp = cr->cr_ngroups; 226 error = copyout((caddr_t)cr->cr_groups, 227 (caddr_t)uap->gidset, ngrp * sizeof(gid_t)); 228 if (error == 0) 229 uap->sysmsg_result = ngrp; 230 return (error); 231 } 232 233 int 234 sys_lwp_setname(struct lwp_setname_args *uap) 235 { 236 struct proc *p = curproc; 237 char comm0[MAXCOMLEN + 1]; 238 const char *comm = NULL; 239 struct lwp *lp; 240 int error; 241 242 if (uap->name != NULL) { 243 error = copyinstr(uap->name, comm0, sizeof(comm0), NULL); 244 if (error) { 245 if (error != ENAMETOOLONG) 246 return error; 247 /* Truncate */ 248 comm0[MAXCOMLEN] = '\0'; 249 } 250 comm = comm0; 251 } else { 252 /* Restore to the default name, i.e. process name. */ 253 comm = p->p_comm; 254 } 255 256 lwkt_gettoken(&p->p_token); 257 258 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, uap->tid); 259 if (lp != NULL) { 260 strlcpy(lp->lwp_thread->td_comm, comm, 261 sizeof(lp->lwp_thread->td_comm)); 262 error = 0; 263 } else { 264 error = ESRCH; 265 } 266 267 lwkt_reltoken(&p->p_token); 268 return error; 269 } 270 271 int 272 sys_setsid(struct setsid_args *uap) 273 { 274 struct proc *p = curproc; 275 struct pgrp *pg = NULL; 276 int error; 277 278 lwkt_gettoken(&p->p_token); 279 if (p->p_pgid == p->p_pid || (pg = pgfind(p->p_pid)) != NULL) { 280 error = EPERM; 281 if (pg) 282 pgrel(pg); 283 } else { 284 enterpgrp(p, p->p_pid, 1); 285 uap->sysmsg_result = p->p_pid; 286 error = 0; 287 } 288 lwkt_reltoken(&p->p_token); 289 return (error); 290 } 291 292 /* 293 * set process group (setpgid/old setpgrp) 294 * 295 * caller does setpgid(targpid, targpgid) 296 * 297 * pid must be caller or child of caller (ESRCH) 298 * if a child 299 * pid must be in same session (EPERM) 300 * pid can't have done an exec (EACCES) 301 * if pgid != pid 302 * there must exist some pid in same session having pgid (EPERM) 303 * pid must not be session leader (EPERM) 304 */ 305 int 306 sys_setpgid(struct setpgid_args *uap) 307 { 308 struct proc *curp = curproc; 309 struct proc *targp; /* target process */ 310 struct pgrp *pgrp = NULL; /* target pgrp */ 311 int error; 312 313 if (uap->pgid < 0) 314 return (EINVAL); 315 316 if (uap->pid != 0 && uap->pid != curp->p_pid) { 317 if ((targp = pfind(uap->pid)) == NULL || !inferior(targp)) { 318 if (targp) 319 PRELE(targp); 320 error = ESRCH; 321 targp = NULL; 322 goto done; 323 } 324 lwkt_gettoken(&targp->p_token); 325 /* targp now referenced and its token is held */ 326 327 if (targp->p_pgrp == NULL || 328 targp->p_session != curp->p_session) { 329 error = EPERM; 330 goto done; 331 } 332 if (targp->p_flags & P_EXEC) { 333 error = EACCES; 334 goto done; 335 } 336 } else { 337 targp = curp; 338 PHOLD(targp); 339 lwkt_gettoken(&targp->p_token); 340 } 341 if (SESS_LEADER(targp)) { 342 error = EPERM; 343 goto done; 344 } 345 if (uap->pgid == 0) { 346 uap->pgid = targp->p_pid; 347 } else if (uap->pgid != targp->p_pid) { 348 if ((pgrp = pgfind(uap->pgid)) == NULL || 349 pgrp->pg_session != curp->p_session) { 350 error = EPERM; 351 goto done; 352 } 353 } 354 error = enterpgrp(targp, uap->pgid, 0); 355 done: 356 if (pgrp) 357 pgrel(pgrp); 358 if (targp) { 359 lwkt_reltoken(&targp->p_token); 360 PRELE(targp); 361 } 362 return (error); 363 } 364 365 /* 366 * Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD 367 * compatible. It says that setting the uid/gid to euid/egid is a special 368 * case of "appropriate privilege". Once the rules are expanded out, this 369 * basically means that setuid(nnn) sets all three id's, in all permitted 370 * cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid()) 371 * does not set the saved id - this is dangerous for traditional BSD 372 * programs. For this reason, we *really* do not want to set 373 * _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2. 374 */ 375 #define POSIX_APPENDIX_B_4_2_2 376 377 int 378 sys_setuid(struct setuid_args *uap) 379 { 380 struct proc *p = curproc; 381 struct ucred *cr; 382 uid_t uid; 383 int error; 384 385 lwkt_gettoken(&p->p_token); 386 cr = p->p_ucred; 387 388 /* 389 * See if we have "permission" by POSIX 1003.1 rules. 390 * 391 * Note that setuid(geteuid()) is a special case of 392 * "appropriate privileges" in appendix B.4.2.2. We need 393 * to use this clause to be compatible with traditional BSD 394 * semantics. Basically, it means that "setuid(xx)" sets all 395 * three id's (assuming you have privs). 396 * 397 * Notes on the logic. We do things in three steps. 398 * 1: We determine if the euid is going to change, and do EPERM 399 * right away. We unconditionally change the euid later if this 400 * test is satisfied, simplifying that part of the logic. 401 * 2: We determine if the real and/or saved uid's are going to 402 * change. Determined by compile options. 403 * 3: Change euid last. (after tests in #2 for "appropriate privs") 404 */ 405 uid = uap->uid; 406 if (uid != cr->cr_ruid && /* allow setuid(getuid()) */ 407 #ifdef _POSIX_SAVED_IDS 408 uid != crc->cr_svuid && /* allow setuid(saved gid) */ 409 #endif 410 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */ 411 uid != cr->cr_uid && /* allow setuid(geteuid()) */ 412 #endif 413 (error = priv_check_cred(cr, PRIV_CRED_SETUID, 0))) 414 goto done; 415 416 #ifdef _POSIX_SAVED_IDS 417 /* 418 * Do we have "appropriate privileges" (are we root or uid == euid) 419 * If so, we are changing the real uid and/or saved uid. 420 */ 421 if ( 422 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */ 423 uid == cr->cr_uid || 424 #endif 425 priv_check_cred(cr, PRIV_CRED_SETUID, 0) == 0) /* we are using privs */ 426 #endif 427 { 428 /* 429 * Set the real uid and transfer proc count to new user. 430 */ 431 if (uid != cr->cr_ruid) { 432 cr = change_ruid(uid); 433 setsugid(); 434 } 435 /* 436 * Set saved uid 437 * 438 * XXX always set saved uid even if not _POSIX_SAVED_IDS, as 439 * the security of seteuid() depends on it. B.4.2.2 says it 440 * is important that we should do this. 441 */ 442 if (cr->cr_svuid != uid) { 443 cr = cratom_proc(p); 444 cr->cr_svuid = uid; 445 setsugid(); 446 } 447 } 448 449 /* 450 * In all permitted cases, we are changing the euid. 451 * Copy credentials so other references do not see our changes. 452 */ 453 if (cr->cr_uid != uid) { 454 change_euid(uid); 455 setsugid(); 456 } 457 error = 0; 458 done: 459 lwkt_reltoken(&p->p_token); 460 return (error); 461 } 462 463 int 464 sys_seteuid(struct seteuid_args *uap) 465 { 466 struct proc *p = curproc; 467 struct ucred *cr; 468 uid_t euid; 469 int error; 470 471 lwkt_gettoken(&p->p_token); 472 cr = p->p_ucred; 473 euid = uap->euid; 474 if (euid != cr->cr_ruid && /* allow seteuid(getuid()) */ 475 euid != cr->cr_svuid && /* allow seteuid(saved uid) */ 476 (error = priv_check_cred(cr, PRIV_CRED_SETEUID, 0))) { 477 lwkt_reltoken(&p->p_token); 478 return (error); 479 } 480 481 /* 482 * Everything's okay, do it. Copy credentials so other references do 483 * not see our changes. 484 */ 485 if (cr->cr_uid != euid) { 486 change_euid(euid); 487 setsugid(); 488 } 489 lwkt_reltoken(&p->p_token); 490 return (0); 491 } 492 493 int 494 sys_setgid(struct setgid_args *uap) 495 { 496 struct proc *p = curproc; 497 struct ucred *cr; 498 gid_t gid; 499 int error; 500 501 lwkt_gettoken(&p->p_token); 502 cr = p->p_ucred; 503 504 /* 505 * See if we have "permission" by POSIX 1003.1 rules. 506 * 507 * Note that setgid(getegid()) is a special case of 508 * "appropriate privileges" in appendix B.4.2.2. We need 509 * to use this clause to be compatible with traditional BSD 510 * semantics. Basically, it means that "setgid(xx)" sets all 511 * three id's (assuming you have privs). 512 * 513 * For notes on the logic here, see setuid() above. 514 */ 515 gid = uap->gid; 516 if (gid != cr->cr_rgid && /* allow setgid(getgid()) */ 517 #ifdef _POSIX_SAVED_IDS 518 gid != cr->cr_svgid && /* allow setgid(saved gid) */ 519 #endif 520 #ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */ 521 gid != cr->cr_groups[0] && /* allow setgid(getegid()) */ 522 #endif 523 (error = priv_check_cred(cr, PRIV_CRED_SETGID, 0))) { 524 goto done; 525 } 526 527 #ifdef _POSIX_SAVED_IDS 528 /* 529 * Do we have "appropriate privileges" (are we root or gid == egid) 530 * If so, we are changing the real uid and saved gid. 531 */ 532 if ( 533 #ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */ 534 gid == cr->cr_groups[0] || 535 #endif 536 priv_check_cred(cr, PRIV_CRED_SETGID, 0) == 0) /* we are using privs */ 537 #endif 538 { 539 /* 540 * Set real gid 541 */ 542 if (cr->cr_rgid != gid) { 543 cr = cratom_proc(p); 544 cr->cr_rgid = gid; 545 setsugid(); 546 } 547 /* 548 * Set saved gid 549 * 550 * XXX always set saved gid even if not _POSIX_SAVED_IDS, as 551 * the security of setegid() depends on it. B.4.2.2 says it 552 * is important that we should do this. 553 */ 554 if (cr->cr_svgid != gid) { 555 cr = cratom_proc(p); 556 cr->cr_svgid = gid; 557 setsugid(); 558 } 559 } 560 /* 561 * In all cases permitted cases, we are changing the egid. 562 * Copy credentials so other references do not see our changes. 563 */ 564 if (cr->cr_groups[0] != gid) { 565 cr = cratom_proc(p); 566 cr->cr_groups[0] = gid; 567 setsugid(); 568 } 569 error = 0; 570 done: 571 lwkt_reltoken(&p->p_token); 572 return (error); 573 } 574 575 int 576 sys_setegid(struct setegid_args *uap) 577 { 578 struct proc *p = curproc; 579 struct ucred *cr; 580 gid_t egid; 581 int error; 582 583 lwkt_gettoken(&p->p_token); 584 cr = p->p_ucred; 585 egid = uap->egid; 586 if (egid != cr->cr_rgid && /* allow setegid(getgid()) */ 587 egid != cr->cr_svgid && /* allow setegid(saved gid) */ 588 (error = priv_check_cred(cr, PRIV_CRED_SETEGID, 0))) { 589 goto done; 590 } 591 if (cr->cr_groups[0] != egid) { 592 cr = cratom_proc(p); 593 cr->cr_groups[0] = egid; 594 setsugid(); 595 } 596 error = 0; 597 done: 598 lwkt_reltoken(&p->p_token); 599 return (error); 600 } 601 602 int 603 sys_setgroups(struct setgroups_args *uap) 604 { 605 struct proc *p = curproc; 606 struct ucred *cr; 607 u_int ngrp; 608 int error; 609 610 lwkt_gettoken(&p->p_token); 611 cr = p->p_ucred; 612 613 if ((error = priv_check_cred(cr, PRIV_CRED_SETGROUPS, 0))) 614 goto done; 615 ngrp = uap->gidsetsize; 616 if (ngrp > NGROUPS) { 617 error = EINVAL; 618 goto done; 619 } 620 /* 621 * XXX A little bit lazy here. We could test if anything has 622 * changed before cratom() and setting P_SUGID. 623 */ 624 cr = cratom_proc(p); 625 if (ngrp < 1) { 626 /* 627 * setgroups(0, NULL) is a legitimate way of clearing the 628 * groups vector on non-BSD systems (which generally do not 629 * have the egid in the groups[0]). We risk security holes 630 * when running non-BSD software if we do not do the same. 631 */ 632 cr->cr_ngroups = 1; 633 } else { 634 error = copyin(uap->gidset, cr->cr_groups, 635 ngrp * sizeof(gid_t)); 636 if (error) 637 goto done; 638 cr->cr_ngroups = ngrp; 639 } 640 setsugid(); 641 error = 0; 642 done: 643 lwkt_reltoken(&p->p_token); 644 return (error); 645 } 646 647 int 648 sys_setreuid(struct setreuid_args *uap) 649 { 650 struct proc *p = curproc; 651 struct ucred *cr; 652 uid_t ruid, euid; 653 int error; 654 655 lwkt_gettoken(&p->p_token); 656 cr = p->p_ucred; 657 658 ruid = uap->ruid; 659 euid = uap->euid; 660 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && 661 ruid != cr->cr_svuid) || 662 (euid != (uid_t)-1 && euid != cr->cr_uid && 663 euid != cr->cr_ruid && euid != cr->cr_svuid)) && 664 (error = priv_check_cred(cr, PRIV_CRED_SETREUID, 0)) != 0) { 665 goto done; 666 } 667 668 if (euid != (uid_t)-1 && cr->cr_uid != euid) { 669 cr = change_euid(euid); 670 setsugid(); 671 } 672 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) { 673 cr = change_ruid(ruid); 674 setsugid(); 675 } 676 if ((ruid != (uid_t)-1 || cr->cr_uid != cr->cr_ruid) && 677 cr->cr_svuid != cr->cr_uid) { 678 cr = cratom_proc(p); 679 cr->cr_svuid = cr->cr_uid; 680 setsugid(); 681 } 682 error = 0; 683 done: 684 lwkt_reltoken(&p->p_token); 685 return (error); 686 } 687 688 int 689 sys_setregid(struct setregid_args *uap) 690 { 691 struct proc *p = curproc; 692 struct ucred *cr; 693 gid_t rgid, egid; 694 int error; 695 696 lwkt_gettoken(&p->p_token); 697 cr = p->p_ucred; 698 699 rgid = uap->rgid; 700 egid = uap->egid; 701 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && 702 rgid != cr->cr_svgid) || 703 (egid != (gid_t)-1 && egid != cr->cr_groups[0] && 704 egid != cr->cr_rgid && egid != cr->cr_svgid)) && 705 (error = priv_check_cred(cr, PRIV_CRED_SETREGID, 0)) != 0) { 706 goto done; 707 } 708 709 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) { 710 cr = cratom_proc(p); 711 cr->cr_groups[0] = egid; 712 setsugid(); 713 } 714 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) { 715 cr = cratom_proc(p); 716 cr->cr_rgid = rgid; 717 setsugid(); 718 } 719 if ((rgid != (gid_t)-1 || cr->cr_groups[0] != cr->cr_rgid) && 720 cr->cr_svgid != cr->cr_groups[0]) { 721 cr = cratom_proc(p); 722 cr->cr_svgid = cr->cr_groups[0]; 723 setsugid(); 724 } 725 error = 0; 726 done: 727 lwkt_reltoken(&p->p_token); 728 return (error); 729 } 730 731 /* 732 * setresuid(ruid, euid, suid) is like setreuid except control over the 733 * saved uid is explicit. 734 */ 735 int 736 sys_setresuid(struct setresuid_args *uap) 737 { 738 struct proc *p = curproc; 739 struct ucred *cr; 740 uid_t ruid, euid, suid; 741 int error; 742 743 lwkt_gettoken(&p->p_token); 744 cr = p->p_ucred; 745 746 ruid = uap->ruid; 747 euid = uap->euid; 748 suid = uap->suid; 749 if (((ruid != (uid_t)-1 && ruid != cr->cr_ruid && 750 ruid != cr->cr_svuid && ruid != cr->cr_uid) || 751 (euid != (uid_t)-1 && euid != cr->cr_ruid && 752 euid != cr->cr_svuid && euid != cr->cr_uid) || 753 (suid != (uid_t)-1 && suid != cr->cr_ruid && 754 suid != cr->cr_svuid && suid != cr->cr_uid)) && 755 (error = priv_check_cred(cr, PRIV_CRED_SETRESUID, 0)) != 0) { 756 goto done; 757 } 758 if (euid != (uid_t)-1 && cr->cr_uid != euid) { 759 cr = change_euid(euid); 760 setsugid(); 761 } 762 if (ruid != (uid_t)-1 && cr->cr_ruid != ruid) { 763 cr = change_ruid(ruid); 764 setsugid(); 765 } 766 if (suid != (uid_t)-1 && cr->cr_svuid != suid) { 767 cr = cratom_proc(p); 768 cr->cr_svuid = suid; 769 setsugid(); 770 } 771 error = 0; 772 done: 773 lwkt_reltoken(&p->p_token); 774 return (error); 775 } 776 777 /* 778 * setresgid(rgid, egid, sgid) is like setregid except control over the 779 * saved gid is explicit. 780 */ 781 int 782 sys_setresgid(struct setresgid_args *uap) 783 { 784 struct proc *p = curproc; 785 struct ucred *cr; 786 gid_t rgid, egid, sgid; 787 int error; 788 789 lwkt_gettoken(&p->p_token); 790 cr = p->p_ucred; 791 rgid = uap->rgid; 792 egid = uap->egid; 793 sgid = uap->sgid; 794 if (((rgid != (gid_t)-1 && rgid != cr->cr_rgid && 795 rgid != cr->cr_svgid && rgid != cr->cr_groups[0]) || 796 (egid != (gid_t)-1 && egid != cr->cr_rgid && 797 egid != cr->cr_svgid && egid != cr->cr_groups[0]) || 798 (sgid != (gid_t)-1 && sgid != cr->cr_rgid && 799 sgid != cr->cr_svgid && sgid != cr->cr_groups[0])) && 800 (error = priv_check_cred(cr, PRIV_CRED_SETRESGID, 0)) != 0) { 801 goto done; 802 } 803 804 if (egid != (gid_t)-1 && cr->cr_groups[0] != egid) { 805 cr = cratom_proc(p); 806 cr->cr_groups[0] = egid; 807 setsugid(); 808 } 809 if (rgid != (gid_t)-1 && cr->cr_rgid != rgid) { 810 cr = cratom_proc(p); 811 cr->cr_rgid = rgid; 812 setsugid(); 813 } 814 if (sgid != (gid_t)-1 && cr->cr_svgid != sgid) { 815 cr = cratom_proc(p); 816 cr->cr_svgid = sgid; 817 setsugid(); 818 } 819 error = 0; 820 done: 821 lwkt_reltoken(&p->p_token); 822 return (error); 823 } 824 825 int 826 sys_getresuid(struct getresuid_args *uap) 827 { 828 struct ucred *cr; 829 int error1 = 0, error2 = 0, error3 = 0; 830 831 /* 832 * copyout's can fault synchronously so we cannot use a shared 833 * token here. 834 */ 835 cr = curthread->td_ucred; 836 if (uap->ruid) 837 error1 = copyout((caddr_t)&cr->cr_ruid, 838 (caddr_t)uap->ruid, sizeof(cr->cr_ruid)); 839 if (uap->euid) 840 error2 = copyout((caddr_t)&cr->cr_uid, 841 (caddr_t)uap->euid, sizeof(cr->cr_uid)); 842 if (uap->suid) 843 error3 = copyout((caddr_t)&cr->cr_svuid, 844 (caddr_t)uap->suid, sizeof(cr->cr_svuid)); 845 return error1 ? error1 : (error2 ? error2 : error3); 846 } 847 848 int 849 sys_getresgid(struct getresgid_args *uap) 850 { 851 struct ucred *cr; 852 int error1 = 0, error2 = 0, error3 = 0; 853 854 cr = curthread->td_ucred; 855 if (uap->rgid) 856 error1 = copyout(&cr->cr_rgid, uap->rgid, 857 sizeof(cr->cr_rgid)); 858 if (uap->egid) 859 error2 = copyout(&cr->cr_groups[0], uap->egid, 860 sizeof(cr->cr_groups[0])); 861 if (uap->sgid) 862 error3 = copyout(&cr->cr_svgid, uap->sgid, 863 sizeof(cr->cr_svgid)); 864 return error1 ? error1 : (error2 ? error2 : error3); 865 } 866 867 868 /* 869 * NOTE: OpenBSD sets a P_SUGIDEXEC flag set at execve() time, 870 * we use P_SUGID because we consider changing the owners as 871 * "tainting" as well. 872 * This is significant for procs that start as root and "become" 873 * a user without an exec - programs cannot know *everything* 874 * that libc *might* have put in their data segment. 875 */ 876 int 877 sys_issetugid(struct issetugid_args *uap) 878 { 879 uap->sysmsg_result = (curproc->p_flags & P_SUGID) ? 1 : 0; 880 return (0); 881 } 882 883 /* 884 * Check if gid is a member of the group set. 885 */ 886 int 887 groupmember(gid_t gid, struct ucred *cred) 888 { 889 gid_t *gp; 890 gid_t *egp; 891 892 egp = &(cred->cr_groups[cred->cr_ngroups]); 893 for (gp = cred->cr_groups; gp < egp; gp++) { 894 if (*gp == gid) 895 return (1); 896 } 897 return (0); 898 } 899 900 /* 901 * Test whether the specified credentials have the privilege 902 * in question. 903 * 904 * A kernel thread without a process context is assumed to have 905 * the privilege in question. In situations where the caller always 906 * expect a cred to exist, the cred should be passed separately and 907 * priv_check_cred() should be used instead of priv_check(). 908 * 909 * Returns 0 or error. 910 */ 911 int 912 priv_check(struct thread *td, int priv) 913 { 914 if (td->td_lwp != NULL) 915 return priv_check_cred(td->td_ucred, priv, 0); 916 return (0); 917 } 918 919 /* 920 * Check a credential for privilege. 921 * 922 * A non-null credential is expected unless NULL_CRED_OKAY is set. 923 */ 924 int 925 priv_check_cred(struct ucred *cred, int priv, int flags) 926 { 927 int error; 928 929 KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege")); 930 931 KASSERT(cred != NULL || (flags & NULL_CRED_OKAY), 932 ("priv_check_cred: NULL cred!")); 933 934 if (cred == NULL) { 935 if (flags & NULL_CRED_OKAY) 936 return (0); 937 else 938 return (EPERM); 939 } 940 if (cred->cr_uid != 0) 941 return (EPERM); 942 943 error = prison_priv_check(cred, priv); 944 if (error) 945 return (error); 946 947 /* NOTE: accounting for suser access (p_acflag/ASU) removed */ 948 return (0); 949 } 950 951 /* 952 * Return zero if p1 can fondle p2, return errno (EPERM/ESRCH) otherwise. 953 */ 954 int 955 p_trespass(struct ucred *cr1, struct ucred *cr2) 956 { 957 if (cr1 == cr2) 958 return (0); 959 if (!PRISON_CHECK(cr1, cr2)) 960 return (ESRCH); 961 if (cr1->cr_ruid == cr2->cr_ruid) 962 return (0); 963 if (cr1->cr_uid == cr2->cr_ruid) 964 return (0); 965 if (cr1->cr_ruid == cr2->cr_uid) 966 return (0); 967 if (cr1->cr_uid == cr2->cr_uid) 968 return (0); 969 if (priv_check_cred(cr1, PRIV_PROC_TRESPASS, 0) == 0) 970 return (0); 971 return (EPERM); 972 } 973 974 static __inline void 975 _crinit(struct ucred *cr) 976 { 977 cr->cr_ref = 1; 978 } 979 980 void 981 crinit(struct ucred *cr) 982 { 983 bzero(cr, sizeof(*cr)); 984 _crinit(cr); 985 } 986 987 /* 988 * Allocate a zeroed cred structure. 989 */ 990 struct ucred * 991 crget(void) 992 { 993 struct ucred *cr; 994 995 cr = kmalloc(sizeof(*cr), M_CRED, M_WAITOK|M_ZERO); 996 _crinit(cr); 997 return (cr); 998 } 999 1000 /* 1001 * Claim another reference to a ucred structure. Can be used with special 1002 * creds. 1003 * 1004 * It must be possible to call this routine with spinlocks held, meaning 1005 * that this routine itself cannot obtain a spinlock. 1006 */ 1007 struct ucred * 1008 crhold(struct ucred *cr) 1009 { 1010 if (cr != NOCRED && cr != FSCRED) 1011 atomic_add_long(&cr->cr_ref, 1); 1012 return(cr); 1013 } 1014 1015 /* 1016 * Drop a reference from the cred structure, free it if the reference count 1017 * reaches 0. 1018 * 1019 * NOTE: because we used atomic_add_int() above, without a spinlock, we 1020 * must also use atomic_subtract_int() below. A spinlock is required 1021 * in crfree() to handle multiple callers racing the refcount to 0. 1022 */ 1023 void 1024 crfree(struct ucred *cr) 1025 { 1026 if (cr->cr_ref <= 0) 1027 panic("Freeing already free credential! %p", cr); 1028 if (atomic_fetchadd_long(&cr->cr_ref, -1) == 1) { 1029 /* 1030 * Some callers of crget(), such as nfs_statfs(), 1031 * allocate a temporary credential, but don't 1032 * allocate a uidinfo structure. 1033 */ 1034 if (cr->cr_uidinfo != NULL) { 1035 uidrop(cr->cr_uidinfo); 1036 cr->cr_uidinfo = NULL; 1037 } 1038 if (cr->cr_ruidinfo != NULL) { 1039 uidrop(cr->cr_ruidinfo); 1040 cr->cr_ruidinfo = NULL; 1041 } 1042 1043 /* 1044 * Destroy empty prisons 1045 */ 1046 if (jailed(cr)) 1047 prison_free(cr->cr_prison); 1048 cr->cr_prison = NULL; /* safety */ 1049 1050 kfree((caddr_t)cr, M_CRED); 1051 } 1052 } 1053 1054 /* 1055 * Atomize a cred structure so it can be modified without polluting 1056 * other references to it. 1057 * 1058 * MPSAFE (however, *pcr must be stable) 1059 */ 1060 struct ucred * 1061 cratom(struct ucred **pcr) 1062 { 1063 struct ucred *oldcr; 1064 struct ucred *newcr; 1065 1066 oldcr = *pcr; 1067 if (oldcr->cr_ref == 1) 1068 return (oldcr); 1069 newcr = crget(); /* this might block */ 1070 oldcr = *pcr; /* re-cache after potentially blocking */ 1071 *newcr = *oldcr; 1072 uihold(newcr->cr_uidinfo); 1073 uihold(newcr->cr_ruidinfo); 1074 if (jailed(newcr)) 1075 prison_hold(newcr->cr_prison); 1076 newcr->cr_ref = 1; 1077 crfree(oldcr); 1078 *pcr = newcr; 1079 1080 return (newcr); 1081 } 1082 1083 /* 1084 * Called with a modifying token held, but must still obtain p_spin to 1085 * actually replace p_ucred to handle races against syscall entry from 1086 * other threads which cache p_ucred->td_ucred. 1087 * 1088 * (the threads will only get the spin-lock, and they only need to in 1089 * the case where td_ucred != p_ucred so this is optimal). 1090 */ 1091 struct ucred * 1092 cratom_proc(struct proc *p) 1093 { 1094 struct ucred *oldcr; 1095 struct ucred *newcr; 1096 1097 oldcr = p->p_ucred; 1098 if (oldcr->cr_ref == 1) 1099 return(oldcr); 1100 1101 newcr = crget(); /* this might block */ 1102 oldcr = p->p_ucred; /* so re-cache oldcr (do not re-test) */ 1103 *newcr = *oldcr; 1104 uihold(newcr->cr_uidinfo); 1105 uihold(newcr->cr_ruidinfo); 1106 if (jailed(newcr)) 1107 prison_hold(newcr->cr_prison); 1108 newcr->cr_ref = 1; 1109 1110 spin_lock(&p->p_spin); 1111 p->p_ucred = newcr; 1112 spin_unlock(&p->p_spin); 1113 crfree(oldcr); 1114 1115 return newcr; 1116 } 1117 1118 /* 1119 * Dup cred struct to a new held one. 1120 */ 1121 struct ucred * 1122 crdup(struct ucred *cr) 1123 { 1124 struct ucred *newcr; 1125 1126 newcr = crget(); 1127 *newcr = *cr; 1128 uihold(newcr->cr_uidinfo); 1129 uihold(newcr->cr_ruidinfo); 1130 if (jailed(newcr)) 1131 prison_hold(newcr->cr_prison); 1132 newcr->cr_ref = 1; 1133 return (newcr); 1134 } 1135 1136 /* 1137 * Fill in a struct xucred based on a struct ucred. 1138 */ 1139 void 1140 cru2x(struct ucred *cr, struct xucred *xcr) 1141 { 1142 1143 bzero(xcr, sizeof(*xcr)); 1144 xcr->cr_version = XUCRED_VERSION; 1145 xcr->cr_uid = cr->cr_uid; 1146 xcr->cr_ngroups = cr->cr_ngroups; 1147 bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups)); 1148 } 1149 1150 /* 1151 * Get login name, if available. 1152 */ 1153 int 1154 sys_getlogin(struct getlogin_args *uap) 1155 { 1156 struct proc *p = curproc; 1157 char buf[MAXLOGNAME]; 1158 int error; 1159 1160 if (uap->namelen > MAXLOGNAME) /* namelen is unsigned */ 1161 uap->namelen = MAXLOGNAME; 1162 bzero(buf, sizeof(buf)); 1163 lwkt_gettoken_shared(&p->p_token); 1164 bcopy(p->p_pgrp->pg_session->s_login, buf, uap->namelen); 1165 lwkt_reltoken(&p->p_token); 1166 1167 error = copyout(buf, uap->namebuf, uap->namelen); 1168 return (error); 1169 } 1170 1171 /* 1172 * Set login name. 1173 */ 1174 int 1175 sys_setlogin(struct setlogin_args *uap) 1176 { 1177 struct thread *td = curthread; 1178 struct proc *p; 1179 struct ucred *cred; 1180 char buf[MAXLOGNAME]; 1181 int error; 1182 1183 cred = td->td_ucred; 1184 p = td->td_proc; 1185 1186 if ((error = priv_check_cred(cred, PRIV_PROC_SETLOGIN, 0))) 1187 return (error); 1188 bzero(buf, sizeof(buf)); 1189 error = copyinstr(uap->namebuf, buf, sizeof(buf), NULL); 1190 if (error == ENAMETOOLONG) 1191 error = EINVAL; 1192 if (error == 0) { 1193 lwkt_gettoken_shared(&p->p_token); 1194 memcpy(p->p_pgrp->pg_session->s_login, buf, sizeof(buf)); 1195 lwkt_reltoken(&p->p_token); 1196 } 1197 return (error); 1198 } 1199 1200 void 1201 setsugid(void) 1202 { 1203 struct proc *p = curproc; 1204 1205 KKASSERT(p != NULL); 1206 lwkt_gettoken(&p->p_token); 1207 p->p_flags |= P_SUGID; 1208 if (!(p->p_pfsflags & PF_ISUGID)) 1209 p->p_stops = 0; 1210 lwkt_reltoken(&p->p_token); 1211 } 1212 1213 /* 1214 * Helper function to change the effective uid of a process 1215 */ 1216 struct ucred * 1217 change_euid(uid_t euid) 1218 { 1219 struct proc *p = curproc; 1220 struct ucred *cr; 1221 1222 KKASSERT(p != NULL); 1223 lf_count_adjust(p, 0); 1224 cr = cratom_proc(p); 1225 cr->cr_uid = euid; 1226 uireplace(&cr->cr_uidinfo, uifind(euid)); 1227 lf_count_adjust(p, 1); 1228 return (cr); 1229 } 1230 1231 /* 1232 * Helper function to change the real uid of a process 1233 * 1234 * The per-uid process count for this process is transfered from 1235 * the old uid to the new uid. 1236 */ 1237 struct ucred * 1238 change_ruid(uid_t ruid) 1239 { 1240 struct proc *p = curproc; 1241 struct ucred *cr; 1242 1243 KKASSERT(p != NULL); 1244 1245 cr = cratom_proc(p); 1246 chgproccnt(cr->cr_ruidinfo, -1, 0); 1247 cr->cr_ruid = ruid; 1248 uireplace(&cr->cr_ruidinfo, uifind(ruid)); 1249 chgproccnt(cr->cr_ruidinfo, 1, 0); 1250 return (cr); 1251 } 1252