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