Loading...
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 | /* * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1990, 1996-1998 Apple Computer, Inc. * All Rights Reserved. */ /* * posix_sem.c : Support for POSIX semaphore APIs * * File: posix_sem.c * Author: Ananthakrishna Ramesh * * HISTORY * 2-Sep-1999 A.Ramesh * Created for MacOSX * */ /* * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. */ #include <sys/cdefs.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/file_internal.h> #include <sys/filedesc.h> #include <sys/stat.h> #include <sys/proc_internal.h> #include <sys/kauth.h> #include <sys/mount.h> #include <sys/namei.h> #include <sys/vnode.h> #include <sys/ioctl.h> #include <sys/tty.h> #include <sys/malloc.h> #include <sys/semaphore.h> #include <sys/sysproto.h> #include <sys/proc_info.h> #include <sys/random.h> #include <net/siphash.h> #if CONFIG_MACF #include <sys/vnode_internal.h> #include <security/mac_framework.h> #endif #include <security/audit/audit.h> #include <mach/mach_types.h> #include <mach/vm_prot.h> #include <mach/semaphore.h> #include <mach/sync_policy.h> #include <mach/task.h> #include <kern/kern_types.h> #include <kern/task.h> #include <kern/clock.h> #include <mach/kern_return.h> #define f_flag fp_glob->fg_flag #define f_ops fp_glob->fg_ops #define PSEMNAMLEN 31 /* maximum name segment length we bother with */ #define PSEMTEAMIDLEN 31 /* maximum length of team ID we consider */ struct pseminfo { unsigned int psem_flags; unsigned int psem_usecount; mode_t psem_mode; uid_t psem_uid; gid_t psem_gid; char psem_name[PSEMNAMLEN + 1]; /* segment name */ semaphore_t psem_semobject; struct label * psem_label; pid_t psem_creator_pid; uint64_t psem_creator_uniqueid; }; #define PSEMINFO_NULL (struct pseminfo *)0 #define PSEM_NONE 1 #define PSEM_DEFINED 2 #define PSEM_ALLOCATED 4 #define PSEM_MAPPED 8 #define PSEM_INUSE 0x10 #define PSEM_REMOVED 0x20 #define PSEM_INCREATE 0x40 #define PSEM_INDELETE 0x80 struct psemcache { LIST_ENTRY(psemcache) psem_hash; /* hash chain */ struct pseminfo *pseminfo; /* vnode the name refers to */ size_t psem_nlen; /* length of name */ size_t psem_teamidlen; /* length of team ID */ char psem_name[PSEMNAMLEN + 1]; /* segment name */ char psem_teamid[PSEMTEAMIDLEN + 1]; /* team ID of users, if any */ }; #define PSEMCACHE_NULL (struct psemcache *)0 #define PSEMCACHE_NOTFOUND (0) #define PSEMCACHE_FOUND (-1) #define PSEMCACHE_NEGATIVE (ENOENT) struct psemstats { long pstats_hits; long pstats_miss; long pstats_local_hits; long pstats_global_hits; long pstats_local_miss; long pstats_global_miss; long pstats_local_collisions; long pstats_global_collisions; long pstats_fallback_hits; /* hits that missed local but hit global */ long pstats_fallback_miss; /* hits that missed both local and global */ long pstats_neghits; /* hits to 'negative entries' (return ENOENT) */ long pstats_longnames; /* semaphore or team ID ENAMETOOLONG */ }; struct psemname { char *psem_nameptr; /* pointer to looked up name */ size_t psem_namelen; /* length of looked up component */ uint64_t psem_hash_local; /* hash value of looked up name and team */ uint64_t psem_hash_global; /* hash value of looked up name, without team */ const char *psem_teamidptr; size_t psem_teamidlen; }; struct psemnode { struct pseminfo *pinfo; #if DIAGNOSTIC unsigned int readcnt; unsigned int writecnt; #endif }; #define PSEMNODE_NULL (struct psemnode *)0 LIST_HEAD(psemhashhead, psemcache); struct psemhashtable { struct psemhashhead *psem_table; /* Hash table mask, i.e size - 1 */ u_long psem_table_mask; /* SipHash key, randomly assigned at boot */ uint8_t psem_siphash_key[SIPHASH_KEY_LENGTH]; }; #define PSEMHASH(table, hash) (&(table).psem_table[(hash) & (table).psem_table_mask]) struct psemhashtable psem_global, psem_local; long posix_sem_num; /* number of POSIX semaphores on the system */ long posix_sem_max = 10000; /* max number of POSIX semaphores on the system */ SYSCTL_NODE(_kern, KERN_POSIX, posix, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Posix"); SYSCTL_NODE(_kern_posix, OID_AUTO, sem, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Semaphores"); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, max, CTLFLAG_RW | CTLFLAG_LOCKED, &posix_sem_max, "max"); struct psemstats psemstats; /* cache effectiveness statistics */ #if DEBUG || DEVELOPMENT SYSCTL_LONG(_kern_posix_sem, OID_AUTO, hits, CTLFLAG_RD, &psemstats.pstats_hits, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, miss, CTLFLAG_RD, &psemstats.pstats_miss, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, local_hits, CTLFLAG_RD, &psemstats.pstats_local_hits, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, local_miss, CTLFLAG_RD, &psemstats.pstats_local_miss, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, global_hits, CTLFLAG_RD, &psemstats.pstats_global_hits, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, global_miss, CTLFLAG_RD, &psemstats.pstats_global_miss, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, fallback_hits, CTLFLAG_RD, &psemstats.pstats_fallback_hits, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, fallback_miss, CTLFLAG_RD, &psemstats.pstats_fallback_miss, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, local_collisions, CTLFLAG_RD, &psemstats.pstats_local_collisions, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, global_collisions, CTLFLAG_RD, &psemstats.pstats_global_collisions, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, neghits, CTLFLAG_RD, &psemstats.pstats_neghits, ""); SYSCTL_LONG(_kern_posix_sem, OID_AUTO, longnames, CTLFLAG_RD, &psemstats.pstats_longnames, ""); #endif static int psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred); static int psem_cache_search(struct pseminfo **, struct psemname *, struct psemcache **); static int psem_delete(struct pseminfo * pinfo); static int psem_closefile(struct fileglob *fp, vfs_context_t ctx); static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache); static const char *psem_get_teamid(proc_t p); static const struct fileops psemops = { .fo_type = DTYPE_PSXSEM, .fo_read = fo_no_read, .fo_write = fo_no_write, .fo_ioctl = fo_no_ioctl, .fo_select = fo_no_select, .fo_close = psem_closefile, .fo_drain = fo_no_drain, .fo_kqfilter = fo_no_kqfilter, }; static LCK_GRP_DECLARE(psx_sem_subsys_lck_grp, "posix semaphores"); static LCK_MTX_DECLARE(psx_sem_subsys_mutex, &psx_sem_subsys_lck_grp); #define PSEM_SUBSYS_LOCK() lck_mtx_lock(&psx_sem_subsys_mutex) #define PSEM_SUBSYS_UNLOCK() lck_mtx_unlock(&psx_sem_subsys_mutex) #define PSEM_SUBSYS_ASSERT_HELD() LCK_MTX_ASSERT(&psx_sem_subsys_mutex, LCK_MTX_ASSERT_OWNED) static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp); static void psem_cache_delete(struct psemcache *pcp); int psem_cache_purge_all(void); static struct psemname psem_cache_hash(char *name, size_t len, const char *teamid, size_t teamidlen) { SIPHASH_CTX ctx; struct psemname nd; nd.psem_nameptr = name; nd.psem_namelen = len; nd.psem_teamidptr = teamid; nd.psem_teamidlen = teamidlen; nd.psem_hash_local = 0; nd.psem_hash_global = 0; _Static_assert(sizeof(nd.psem_hash_local) == SIPHASH_DIGEST_LENGTH, "hash field is wrong size for SipHash"); _Static_assert(sizeof(nd.psem_hash_global) == SIPHASH_DIGEST_LENGTH, "hash field is wrong size for SipHash"); /* * This routine is called before taking the subsystem lock, so we'll prepare hashes * for both global and local tables up front. */ SipHash24_Init(&ctx); SipHash_SetKey(&ctx, psem_global.psem_siphash_key); SipHash_Update(&ctx, name, len); SipHash_Final((u_int8_t *)&nd.psem_hash_global, &ctx); if (teamidlen > 0) { SipHash24_Init(&ctx); SipHash_SetKey(&ctx, psem_local.psem_siphash_key); SipHash_Update(&ctx, name, len); SipHash_Update(&ctx, teamid, teamidlen); SipHash_Final((u_int8_t *)&nd.psem_hash_local, &ctx); } return nd; } /* * Returns 1 if the semaphore name matches what we're looking for, otherwise 0. * When searching the local table, the team ID must match too. */ static int psem_cache_is_match(struct psemcache *sem, struct psemname *target, bool local) { bool name_matches = target->psem_namelen == sem->psem_nlen && !bcmp(target->psem_nameptr, sem->psem_name, target->psem_namelen); if (local) { bool teamid_matches = target->psem_teamidlen == sem->psem_teamidlen && !bcmp(target->psem_teamidptr, sem->psem_teamid, target->psem_teamidlen); return name_matches && teamid_matches; } return name_matches; } /* * Lookup an entry in the cache * * * status of -1 is returned if matches * If the lookup determines that the name does not exist * (negative cacheing), a status of ENOENT is returned. If the lookup * fails, a status of zero is returned. */ static int psem_cache_search(struct pseminfo **psemp, struct psemname *pnp, struct psemcache **pcache) { struct psemcache *pcp = NULL, *nnp; struct psemhashhead *pcpp; if (pnp->psem_namelen > PSEMNAMLEN || pnp->psem_teamidlen > PSEMTEAMIDLEN) { os_atomic_inc(&psemstats.pstats_longnames, relaxed); return PSEMCACHE_NOTFOUND; } /* If Team ID is present, try to look up in the local table first. */ if (pnp->psem_teamidlen > 0) { pcpp = PSEMHASH(psem_local, pnp->psem_hash_local); for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { nnp = pcp->psem_hash.le_next; if (psem_cache_is_match(pcp, pnp, true)) { break; } os_atomic_inc(&psemstats.pstats_local_collisions, relaxed); } if (pcp == 0) { os_atomic_inc(&psemstats.pstats_local_miss, relaxed); } else { os_atomic_inc(&psemstats.pstats_local_hits, relaxed); } } /* Otherwise, or if the local lookup failed, search the global table. */ if (pcp == 0) { pcpp = PSEMHASH(psem_global, pnp->psem_hash_global); for (pcp = pcpp->lh_first; pcp != 0; pcp = nnp) { nnp = pcp->psem_hash.le_next; if (psem_cache_is_match(pcp, pnp, false)) { break; } os_atomic_inc(&psemstats.pstats_global_collisions, relaxed); } if (pcp == 0) { os_atomic_inc(&psemstats.pstats_global_miss, relaxed); if (pnp->psem_teamidlen > 0) { os_atomic_inc(&psemstats.pstats_fallback_miss, relaxed); } } else { os_atomic_inc(&psemstats.pstats_global_hits, relaxed); if (pnp->psem_teamidlen > 0) { os_atomic_inc(&psemstats.pstats_fallback_hits, relaxed); } } } if (pcp == 0) { os_atomic_inc(&psemstats.pstats_miss, relaxed); return PSEMCACHE_NOTFOUND; } /* We found a "positive" match, return the vnode */ if (pcp->pseminfo) { os_atomic_inc(&psemstats.pstats_hits, relaxed); /* TOUCH(ncp); */ *psemp = pcp->pseminfo; *pcache = pcp; return PSEMCACHE_FOUND; } /* * We found a "negative" match, ENOENT notifies client of this match. * The nc_vpid field records whether this is a whiteout. */ os_atomic_inc(&psemstats.pstats_neghits, relaxed); return PSEMCACHE_NEGATIVE; } /* * Add an entry to the cache. */ static int psem_cache_add(struct pseminfo *psemp, struct psemname *pnp, struct psemcache *pcp) { struct psemhashhead *pcpp; #if DIAGNOSTIC if (pnp->psem_namelen > PSEMNAMLEN) { panic("cache_enter: name too long"); } if (pnp->psem_teamidlen > PSEMTEAMIDLEN) { panic("cache_enter: teamid too long"); } #endif if (posix_sem_num >= posix_sem_max) { return ENOSPC; } posix_sem_num++; /* * Fill in cache info, if vp is NULL this is a "negative" cache entry. * For negative entries, we have to record whether it is a whiteout. * the whiteout flag is stored in the nc_vpid field which is * otherwise unused. */ pcp->pseminfo = psemp; pcp->psem_nlen = pnp->psem_namelen; bcopy(pnp->psem_nameptr, pcp->psem_name, pcp->psem_nlen); pcp->psem_teamidlen = pnp->psem_teamidlen; bcopy(pnp->psem_teamidptr, pcp->psem_teamid, pcp->psem_teamidlen); /* Insert into the right table based on Team ID. */ if (pcp->psem_teamidlen > 0) { pcpp = PSEMHASH(psem_local, pnp->psem_hash_local); } else { pcpp = PSEMHASH(psem_global, pnp->psem_hash_global); } #if DIAGNOSTIC { struct psemcache *p; for (p = pcpp->lh_first; p != 0; p = p->psem_hash.le_next) { if (p == pcp) { panic("psem:cache_enter duplicate"); } } } #endif LIST_INSERT_HEAD(pcpp, pcp, psem_hash); return 0; } /* * Name cache initialization, from vfs_init() when we are booting */ void psem_cache_init(void) { /* * The global table stores semaphores created by processes without a Team * ID (such as platform binaries). The local table stores all other semaphores. */ psem_global.psem_table = hashinit((int)(posix_sem_max / 2), M_SHM, &psem_global.psem_table_mask); psem_local.psem_table = hashinit((int)(posix_sem_max / 2), M_SHM, &psem_local.psem_table_mask); read_frandom(psem_global.psem_siphash_key, sizeof(psem_global.psem_siphash_key)); read_frandom(psem_local.psem_siphash_key, sizeof(psem_local.psem_siphash_key)); } static void psem_cache_delete(struct psemcache *pcp) { #if DIAGNOSTIC if (pcp->psem_hash.le_prev == 0) { panic("psem namecache purge le_prev"); } if (pcp->psem_hash.le_next == pcp) { panic("namecache purge le_next"); } #endif /* DIAGNOSTIC */ LIST_REMOVE(pcp, psem_hash); pcp->psem_hash.le_prev = NULL; posix_sem_num--; } static int psem_cache_purge_table(struct psemhashtable *table) { struct psemcache *pcp, *tmppcp; struct psemhashhead *pcpp; for (pcpp = &table->psem_table[table->psem_table_mask]; pcpp >= table->psem_table; pcpp--) { LIST_FOREACH_SAFE(pcp, pcpp, psem_hash, tmppcp) { assert(pcp->psem_nlen); /* * unconditionally unlink the cache entry */ int error = psem_unlink_internal(pcp->pseminfo, pcp); if (error) { return error; } } } return 0; } /* * Remove all cached psem entries. Open semaphores (with a positive refcount) * will continue to exist, but their cache entries tying them to a particular * name/path will be removed making all future lookups on the name fail. */ int psem_cache_purge_all(void) { int error = 0; if (kauth_cred_issuser(kauth_cred_get()) == 0) { return EPERM; } PSEM_SUBSYS_LOCK(); error = psem_cache_purge_table(&psem_global); if (error) { goto out; } error = psem_cache_purge_table(&psem_local); if (error) { goto out; } assert(posix_sem_num == 0); out: PSEM_SUBSYS_UNLOCK(); if (error) { printf("%s: Error %d removing all semaphores: %ld remain!\n", __func__, error, posix_sem_num); } return error; } /* * In order to support unnamed POSIX semaphores, the named * POSIX semaphores will have to move out of the per-process * open filetable, and into a global table that is shared with * unnamed POSIX semaphores, since unnamed POSIX semaphores * are typically used by declaring instances in shared memory, * and there's no other way to do this without changing the * underlying type, which would introduce binary compatibility * issues. */ int sem_open(proc_t p, struct sem_open_args *uap, user_addr_t *retval) { int indx, error; struct psemname nd; struct pseminfo *pinfo; struct fileproc *fp = NULL; char *pnbuf = NULL; const char *teamid = NULL; struct pseminfo *new_pinfo = PSEMINFO_NULL; struct psemnode *new_pnode = PSEMNODE_NULL; struct psemcache *pcache = PSEMCACHE_NULL; char * nameptr; size_t pathlen, plen, teamidlen; mode_t fmode; mode_t cmode = (mode_t)uap->mode; int value = uap->value; int incache = 0; struct psemcache *pcp = PSEMCACHE_NULL; kern_return_t kret = KERN_INVALID_ADDRESS; /* default fail */ AUDIT_ARG(fflags, uap->oflag); AUDIT_ARG(mode, (mode_t)uap->mode); AUDIT_ARG(value32, uap->value); pinfo = PSEMINFO_NULL; /* * Preallocate everything we might need up front to avoid taking * and dropping the lock, opening us up to race conditions. */ pnbuf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO); pathlen = MAXPATHLEN; error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); if (error) { goto bad; } AUDIT_ARG(text, pnbuf); if ((pathlen > PSEMNAMLEN)) { error = ENAMETOOLONG; goto bad; } #ifdef PSXSEM_NAME_RESTRICT nameptr = pnbuf; if (*nameptr == '/') { while (*(nameptr++) == '/') { plen--; error = EINVAL; goto bad; } } else { error = EINVAL; goto bad; } #endif /* PSXSEM_NAME_RESTRICT */ plen = pathlen; nameptr = pnbuf; teamid = psem_get_teamid(p); teamidlen = teamid ? strlen(teamid) : 0; if (teamidlen > PSEMTEAMIDLEN) { error = ENAMETOOLONG; goto bad; } nd = psem_cache_hash(nameptr, plen, teamid, teamidlen); /* * attempt to allocate a new fp; if unsuccessful, the fp will be * left unmodified (NULL). */ error = falloc(p, &fp, &indx); if (error) { goto bad; } /* * We allocate a new entry if we are less than the maximum * allowed and the one at the front of the LRU list is in use. * Otherwise we use the one at the front of the LRU list. */ pcp = kalloc_type(struct psemcache, Z_WAITOK | Z_ZERO | Z_NOFAIL); new_pinfo = kalloc_type(struct pseminfo, Z_WAITOK | Z_ZERO | Z_NOFAIL); #if CONFIG_MACF mac_posixsem_label_init(new_pinfo); #endif /* * Provisionally create the semaphore in the new_pinfo; we have to do * this here to prevent locking later. We use the value of kret to * signal success or failure, which is why we set its default value * to KERN_INVALID_ADDRESS, above. */ fmode = (mode_t)FFLAGS(uap->oflag); if ((fmode & O_CREAT)) { if ((value < 0) || (value > SEM_VALUE_MAX)) { error = EINVAL; goto bad; } kret = semaphore_create(kernel_task, &new_pinfo->psem_semobject, SYNC_POLICY_FIFO, value); if (kret != KERN_SUCCESS) { switch (kret) { case KERN_RESOURCE_SHORTAGE: error = ENOMEM; break; case KERN_PROTECTION_FAILURE: error = EACCES; break; default: error = EINVAL; } goto bad; } } new_pnode = kalloc_type(struct psemnode, Z_WAITOK | Z_ZERO | Z_NOFAIL); PSEM_SUBSYS_LOCK(); error = psem_cache_search(&pinfo, &nd, &pcache); if (error == PSEMCACHE_NEGATIVE) { error = EINVAL; goto bad_locked; } if (error == PSEMCACHE_FOUND) { incache = 1; } else { incache = 0; } cmode &= ALLPERMS; if (((fmode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) && incache) { /* sem exists and opened O_EXCL */ #if notyet if (pinfo->psem_flags & PSEM_INDELETE) { } #endif AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode); error = EEXIST; goto bad_locked; } if (((fmode & (O_CREAT | O_EXCL)) == O_CREAT) && incache) { /* As per POSIX, O_CREAT has no effect */ fmode &= ~O_CREAT; } if ((fmode & O_CREAT)) { /* create a new one (commit the allocation) */ pinfo = new_pinfo; pinfo->psem_flags = PSEM_DEFINED | PSEM_INCREATE; pinfo->psem_usecount = 1; pinfo->psem_mode = cmode; pinfo->psem_uid = kauth_getuid(); pinfo->psem_gid = kauth_getgid(); bcopy(pnbuf, &pinfo->psem_name[0], PSEMNAMLEN); pinfo->psem_name[PSEMNAMLEN] = 0; pinfo->psem_flags &= ~PSEM_DEFINED; pinfo->psem_flags |= PSEM_ALLOCATED; pinfo->psem_creator_pid = proc_getpid(p); pinfo->psem_creator_uniqueid = proc_uniqueid(p); #if CONFIG_MACF error = mac_posixsem_check_create(kauth_cred_get(), nameptr); if (error) { goto bad_locked; } mac_posixsem_label_associate(kauth_cred_get(), pinfo, nameptr); #endif } else { /* semaphore should exist as it is without O_CREAT */ if (!incache) { error = ENOENT; goto bad_locked; } if (pinfo->psem_flags & PSEM_INDELETE) { error = ENOENT; goto bad_locked; } AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode); #if CONFIG_MACF error = mac_posixsem_check_open(kauth_cred_get(), pinfo); if (error) { goto bad_locked; } #endif if ((error = psem_access(pinfo, fmode, kauth_cred_get()))) { goto bad_locked; } } if (!incache) { /* if successful, this will consume the pcp */ if ((error = psem_cache_add(pinfo, &nd, pcp))) { goto bad_locked; } } pinfo->psem_flags &= ~PSEM_INCREATE; pinfo->psem_usecount++; new_pnode->pinfo = pinfo; PSEM_SUBSYS_UNLOCK(); /* * if incache, we did not use the new pcp or the new pcp or the * new . and we must free them. */ if (incache) { kfree_type(struct psemcache, pcp); pcp = PSEMCACHE_NULL; if (new_pinfo != PSEMINFO_NULL) { /* return value ignored - we can't _not_ do this */ (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); #if CONFIG_MACF mac_posixsem_label_destroy(new_pinfo); #endif kfree_type(struct pseminfo, new_pinfo); new_pinfo = PSEMINFO_NULL; } } proc_fdlock(p); fp->f_flag = fmode & FMASK; fp->f_ops = &psemops; fp_set_data(fp, new_pnode); procfdtbl_releasefd(p, indx, NULL); fp_drop(p, indx, fp, 1); proc_fdunlock(p); *retval = CAST_USER_ADDR_T(indx); zfree(ZV_NAMEI, pnbuf); return 0; bad_locked: PSEM_SUBSYS_UNLOCK(); bad: kfree_type(struct psemcache, pcp); kfree_type(struct psemnode, new_pnode); if (fp != NULL) { fp_free(p, indx, fp); } if (new_pinfo != PSEMINFO_NULL) { /* * kret signals whether or not we successfully created a * Mach semaphore for this semaphore; if so, we need to * destroy it here. */ if (kret == KERN_SUCCESS) { /* return value ignored - we can't _not_ do this */ (void)semaphore_destroy(kernel_task, new_pinfo->psem_semobject); } #if CONFIG_MACF mac_posixsem_label_destroy(new_pinfo); #endif kfree_type(struct pseminfo, new_pinfo); } if (pnbuf != NULL) { zfree(ZV_NAMEI, pnbuf); } return error; } /* * XXX This code is repeated in several places */ static int psem_access(struct pseminfo *pinfo, mode_t mode, kauth_cred_t cred) { mode_t mode_req = ((mode & FREAD) ? S_IRUSR : 0) | ((mode & FWRITE) ? S_IWUSR : 0); /* Otherwise, user id 0 always gets access. */ if (!suser(cred, NULL)) { return 0; } return posix_cred_access(cred, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode, mode_req); } static int psem_unlink_internal(struct pseminfo *pinfo, struct psemcache *pcache) { PSEM_SUBSYS_ASSERT_HELD(); if (!pinfo || !pcache) { return EINVAL; } if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) == 0) { return EINVAL; } if (pinfo->psem_flags & PSEM_INDELETE) { return 0; } AUDIT_ARG(posix_ipc_perm, pinfo->psem_uid, pinfo->psem_gid, pinfo->psem_mode); pinfo->psem_flags |= PSEM_INDELETE; pinfo->psem_usecount--; if (!pinfo->psem_usecount) { psem_delete(pinfo); kfree_type(struct pseminfo, pinfo); } else { pinfo->psem_flags |= PSEM_REMOVED; } psem_cache_delete(pcache); kfree_type(struct psemcache, pcache); return 0; } static const char * psem_get_teamid(proc_t p) { #if XNU_TARGET_OS_OSX #pragma unused(p) return NULL; #else return csproc_get_teamid(p); #endif } int sem_unlink(__unused proc_t p, struct sem_unlink_args *uap, __unused int32_t *retval) { int error = 0; struct psemname nd; struct pseminfo *pinfo; char * nameptr; char * pnbuf; const char *teamid; size_t pathlen, teamidlen; struct psemcache *pcache = PSEMCACHE_NULL; pinfo = PSEMINFO_NULL; pnbuf = zalloc(ZV_NAMEI); pathlen = MAXPATHLEN; error = copyinstr(uap->name, pnbuf, MAXPATHLEN, &pathlen); if (error) { goto bad; } AUDIT_ARG(text, pnbuf); if (pathlen > PSEMNAMLEN) { error = ENAMETOOLONG; goto bad; } nameptr = pnbuf; #ifdef PSXSEM_NAME_RESTRICT if (*nameptr == '/') { while (*(nameptr++) == '/') { pathlen--; error = EINVAL; goto bad; } } else { error = EINVAL; goto bad; } #endif /* PSXSEM_NAME_RESTRICT */ teamid = psem_get_teamid(p); teamidlen = teamid ? strlen(teamid) : 0; if (teamidlen > PSEMTEAMIDLEN) { error = ENAMETOOLONG; goto bad; } nd = psem_cache_hash(nameptr, pathlen, teamid, teamidlen); PSEM_SUBSYS_LOCK(); error = psem_cache_search(&pinfo, &nd, &pcache); if (error != PSEMCACHE_FOUND) { PSEM_SUBSYS_UNLOCK(); error = ENOENT; goto bad; } #if CONFIG_MACF error = mac_posixsem_check_unlink(kauth_cred_get(), pinfo, nameptr); if (error) { PSEM_SUBSYS_UNLOCK(); goto bad; } #endif if ((error = psem_access(pinfo, pinfo->psem_mode, kauth_cred_get()))) { PSEM_SUBSYS_UNLOCK(); goto bad; } error = psem_unlink_internal(pinfo, pcache); PSEM_SUBSYS_UNLOCK(); bad: zfree(ZV_NAMEI, pnbuf); return error; } int sem_close(proc_t p, struct sem_close_args *uap, __unused int32_t *retval) { int fd = CAST_DOWN_EXPLICIT(int, uap->sem); kauth_cred_t p_cred; struct fileproc *fp; AUDIT_ARG(fd, fd); /* XXX This seems wrong; uap->sem is a pointer */ proc_fdlock(p); if ((fp = fp_get_noref_locked(p, fd)) == NULL) { proc_fdunlock(p); return EBADF; } if (FILEGLOB_DTYPE(fp->fp_glob) != DTYPE_PSXSEM) { proc_fdunlock(p); return EBADF; } p_cred = current_cached_proc_cred(p); return fp_close_and_unlock(p, p_cred, fd, fp, 0); } int sem_wait(proc_t p, struct sem_wait_args *uap, int32_t *retval) { __pthread_testcancel(1); return sem_wait_nocancel(p, (struct sem_wait_nocancel_args *)uap, retval); } int sem_wait_nocancel(proc_t p, struct sem_wait_nocancel_args *uap, __unused int32_t *retval) { int fd = CAST_DOWN_EXPLICIT(int, uap->sem); struct fileproc *fp; struct pseminfo * pinfo; struct psemnode * pnode; kern_return_t kret; int error; error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp); if (error) { return error; } pnode = (struct psemnode *)fp_get_data(fp); PSEM_SUBSYS_LOCK(); if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) != PSEM_ALLOCATED) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } #if CONFIG_MACF error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); if (error) { PSEM_SUBSYS_UNLOCK(); goto out; } #endif PSEM_SUBSYS_UNLOCK(); kret = semaphore_wait(pinfo->psem_semobject); switch (kret) { case KERN_INVALID_ADDRESS: case KERN_PROTECTION_FAILURE: error = EACCES; break; case KERN_ABORTED: case KERN_OPERATION_TIMED_OUT: error = EINTR; break; case KERN_SUCCESS: error = 0; break; default: error = EINVAL; break; } out: fp_drop(p, fd, fp, 0); return error; } int sem_trywait(proc_t p, struct sem_trywait_args *uap, __unused int32_t *retval) { int fd = CAST_DOWN_EXPLICIT(int, uap->sem); struct fileproc *fp; struct pseminfo * pinfo; struct psemnode * pnode; kern_return_t kret; mach_timespec_t wait_time; int error; error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp); if (error) { return error; } pnode = (struct psemnode *)fp_get_data(fp); PSEM_SUBSYS_LOCK(); if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) != PSEM_ALLOCATED) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } #if CONFIG_MACF error = mac_posixsem_check_wait(kauth_cred_get(), pinfo); if (error) { PSEM_SUBSYS_UNLOCK(); goto out; } #endif PSEM_SUBSYS_UNLOCK(); wait_time.tv_sec = 0; wait_time.tv_nsec = 0; kret = semaphore_timedwait(pinfo->psem_semobject, MACH_TIMESPEC_ZERO); switch (kret) { case KERN_INVALID_ADDRESS: case KERN_PROTECTION_FAILURE: error = EINVAL; break; case KERN_ABORTED: error = EINTR; break; case KERN_OPERATION_TIMED_OUT: error = EAGAIN; break; case KERN_SUCCESS: error = 0; break; default: error = EINVAL; break; } out: fp_drop(p, fd, fp, 0); return error; } int sem_post(proc_t p, struct sem_post_args *uap, __unused int32_t *retval) { int fd = CAST_DOWN_EXPLICIT(int, uap->sem); struct fileproc *fp; struct pseminfo * pinfo; struct psemnode * pnode; kern_return_t kret; int error; error = fp_get_ftype(p, fd, DTYPE_PSXSEM, EBADF, &fp); if (error) { return error; } pnode = (struct psemnode *)fp_get_data(fp); PSEM_SUBSYS_LOCK(); if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } if ((pinfo->psem_flags & (PSEM_DEFINED | PSEM_ALLOCATED)) != PSEM_ALLOCATED) { PSEM_SUBSYS_UNLOCK(); error = EINVAL; goto out; } #if CONFIG_MACF error = mac_posixsem_check_post(kauth_cred_get(), pinfo); if (error) { PSEM_SUBSYS_UNLOCK(); goto out; } #endif PSEM_SUBSYS_UNLOCK(); kret = semaphore_signal(pinfo->psem_semobject); switch (kret) { case KERN_INVALID_ADDRESS: case KERN_PROTECTION_FAILURE: error = EINVAL; break; case KERN_ABORTED: case KERN_OPERATION_TIMED_OUT: error = EINTR; break; case KERN_SUCCESS: error = 0; break; default: error = EINVAL; break; } out: fp_drop(p, fd, fp, 0); return error; } static int psem_close(struct psemnode *pnode) { int error = 0; struct pseminfo *pinfo; PSEM_SUBSYS_LOCK(); if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { PSEM_SUBSYS_UNLOCK(); return EINVAL; } if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { PSEM_SUBSYS_UNLOCK(); return EINVAL; } #if DIAGNOSTIC if (!pinfo->psem_usecount) { kprintf("negative usecount in psem_close\n"); } #endif /* DIAGNOSTIC */ pinfo->psem_usecount--; if ((pinfo->psem_flags & PSEM_REMOVED) && !pinfo->psem_usecount) { PSEM_SUBSYS_UNLOCK(); /* lock dropped as only semaphore is destroyed here */ error = psem_delete(pinfo); kfree_type(struct pseminfo, pinfo); } else { PSEM_SUBSYS_UNLOCK(); } /* subsystem lock is dropped when we get here */ kfree_type(struct psemnode, pnode); return error; } static int psem_closefile(struct fileglob *fg, __unused vfs_context_t ctx) { /* * Not locked as psem_close is called only from here and is locked * properly */ return psem_close((struct psemnode *)fg_get_data(fg)); } static int psem_delete(struct pseminfo * pinfo) { kern_return_t kret; kret = semaphore_destroy(kernel_task, pinfo->psem_semobject); #if CONFIG_MACF mac_posixsem_label_destroy(pinfo); #endif switch (kret) { case KERN_INVALID_ADDRESS: case KERN_PROTECTION_FAILURE: return EINVAL; case KERN_ABORTED: case KERN_OPERATION_TIMED_OUT: return EINTR; case KERN_SUCCESS: return 0; default: return EINVAL; } } int fill_pseminfo(struct psemnode *pnode, struct psem_info * info) { struct pseminfo *pinfo; struct vinfo_stat *sb; PSEM_SUBSYS_LOCK(); if ((pinfo = pnode->pinfo) == PSEMINFO_NULL) { PSEM_SUBSYS_UNLOCK(); return EINVAL; } #if 0 if ((pinfo->psem_flags & PSEM_ALLOCATED) != PSEM_ALLOCATED) { PSEM_SUBSYS_UNLOCK(); return EINVAL; } #endif sb = &info->psem_stat; bzero(sb, sizeof(struct vinfo_stat)); sb->vst_mode = pinfo->psem_mode; sb->vst_uid = pinfo->psem_uid; sb->vst_gid = pinfo->psem_gid; sb->vst_size = pinfo->psem_usecount; bcopy(&pinfo->psem_name[0], &info->psem_name[0], PSEMNAMLEN + 1); PSEM_SUBSYS_UNLOCK(); return 0; } #if CONFIG_MACF void psem_label_associate(struct fileproc *fp, struct vnode *vp, vfs_context_t ctx) { struct psemnode *pnode; struct pseminfo *psem; PSEM_SUBSYS_LOCK(); pnode = (struct psemnode *)fp_get_data(fp); if (pnode != NULL) { psem = pnode->pinfo; if (psem != NULL) { mac_posixsem_vnode_label_associate( vfs_context_ucred(ctx), psem, mac_posixsem_label(psem), vp, mac_vnode_label(vp)); } } PSEM_SUBSYS_UNLOCK(); } #endif |