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 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 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 | /* * 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@ */ /* * @OSF_FREE_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991,1990,1989 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ /* * NOTICE: This file was modified by McAfee Research in 2004 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. * Copyright (c) 2005-2006 SPARTA, Inc. */ /* */ /* * File: ipc/ipc_right.c * Author: Rich Draves * Date: 1989 * * Functions to manipulate IPC capabilities. */ #include <mach/boolean.h> #include <mach/kern_return.h> #include <mach/port.h> #include <mach/message.h> #include <kern/assert.h> #include <kern/ipc_kobject.h> #include <kern/misc_protos.h> #include <kern/coalition.h> #include <kern/policy_internal.h> #include <ipc/ipc_policy.h> #include <ipc/ipc_entry.h> #include <ipc/ipc_space.h> #include <ipc/ipc_object.h> #include <ipc/ipc_hash.h> #include <ipc/ipc_policy.h> #include <ipc/ipc_port.h> #include <ipc/ipc_pset.h> #include <ipc/ipc_right.h> #include <ipc/ipc_notify.h> #include <ipc/ipc_importance.h> #include <ipc/ipc_service_port.h> /* * Routine: ipc_right_lookup_read * Purpose: * Finds an entry in a space, given the name. * Conditions: * Nothing locked. * If an object is found, it is locked and active. * Returns: * KERN_SUCCESS Found an entry. * KERN_INVALID_TASK The space is dead. * KERN_INVALID_NAME Name doesn't exist in space. */ kern_return_t ipc_right_lookup_read( ipc_space_t space, mach_port_name_t name, ipc_entry_bits_t *bitsp, ipc_object_t *objectp) { mach_port_index_t index; ipc_entry_table_t table; ipc_entry_t entry; ipc_object_t object; kern_return_t kr; index = MACH_PORT_INDEX(name); if (__improbable(index == 0)) { *bitsp = 0; *objectp = IPC_OBJECT_NULL; return KERN_INVALID_NAME; } smr_ipc_enter(); /* * Acquire a (possibly stale) pointer to the table, * and guard it so that it can't be deallocated while we use it. * * smr_ipc_enter() has the property that it strongly serializes * after any store-release. This is important because it means that if * one considers this (broken) userspace usage: * * Thread 1: * - makes a semaphore, gets name 0x1003 * - stores that name to a global `sema` in userspace * * Thread 2: * - spins to observe `sema` becoming non 0 * - calls semaphore_wait() on 0x1003 * * Then, because in order to return 0x1003 this thread issued * a store-release (when calling is_write_unlock()), * then this smr_entered_load() can't possibly observe a table * pointer that is older than the one that was current when the * semaphore was made. * * This fundamental property allows us to never loop. */ table = smr_entered_load(&space->is_table); if (__improbable(table == NULL)) { kr = KERN_INVALID_TASK; goto out_put; } entry = ipc_entry_table_get(table, index); if (__improbable(entry == NULL)) { kr = KERN_INVALID_NAME; goto out_put; } /* * Note: this should be an atomic load, but PAC and atomics * don't work interact well together. */ object = entry->ie_volatile_object; /* * Attempt to lock an object that lives in this entry. * It might fail or be a completely different object by now. * * Make sure that acquiring the lock is fully ordered after any * lock-release (using os_atomic_barrier_before_lock_acquire()). * This allows us to always reliably observe space termination below. */ os_atomic_barrier_before_lock_acquire(); if (__improbable(object == IPC_OBJECT_NULL || !io_lock_allow_invalid(object))) { kr = KERN_INVALID_NAME; goto out_put; } /* * Now that we hold the object lock, we are preventing any entry * in this space for this object to be mutated. * * If the space didn't grow after we acquired our hazardous reference, * and before a mutation of the entry, then holding the object lock * guarantees we will observe the truth of ie_bits, ie_object and * ie_request (those are always mutated with the object lock held). * * However this ordering is problematic: * - [A]cquisition of the table pointer * - [G]rowth of the space (making the table pointer in [A] stale) * - [M]utation of the entry * - [L]ocking of the object read through [A]. * * The space lock is held for both [G] and [M], and the object lock * is held for [M], which means that once we lock the object we can * observe if [G] happenend by reloading the table pointer. * * We might still fail to observe any growth operation that happened * after the last mutation of this object's entry, because holding * an object lock doesn't guarantee anything about the liveness * of the space table pointer. This is not a problem at all: by * definition, those didn't affect the state of the entry. * * TODO: a data-structure where the entries are grown by "slabs", * would allow for the address of an ipc_entry_t to never * change once it exists in a space and would avoid a reload * (as well as making space growth faster). * We however still need to check for termination. */ table = smr_entered_load(&space->is_table); if (__improbable(table == NULL)) { kr = KERN_INVALID_TASK; goto out_put_unlock; } /* * Tables never shrink so we don't need to validate the length twice. */ entry = ipc_entry_table_get_nocheck(table, index); /* * Now that we hold the lock and have a "fresh enough" table pointer, * validate if this entry is what we think it is. * * To the risk of being repetitive, we still need to protect * those accesses under SMR, because subsequent * table growths might retire the memory. However we know * those growths will have left our entry unchanged. */ if (__improbable(entry->ie_object != object)) { kr = KERN_INVALID_NAME; goto out_put_unlock; } ipc_entry_bits_t bits = entry->ie_bits; if (__improbable(IE_BITS_GEN(bits) != MACH_PORT_GEN(name) || IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE)) { kr = KERN_INVALID_NAME; goto out_put_unlock; } /* Done with hazardous accesses to the table */ smr_ipc_leave(); *bitsp = bits; *objectp = object; return KERN_SUCCESS; out_put_unlock: io_unlock(object); out_put: smr_ipc_leave(); return kr; } /* * Routine: ipc_right_lookup_write * Purpose: * Finds an entry in a space, given the name. * Conditions: * Nothing locked. If successful, the space is write-locked. * Returns: * KERN_SUCCESS Found an entry. * KERN_INVALID_TASK The space is dead. * KERN_INVALID_NAME Name doesn't exist in space. */ kern_return_t ipc_right_lookup_write( ipc_space_t space, mach_port_name_t name, ipc_entry_t *entryp) { ipc_entry_t entry; assert(space != IS_NULL); is_write_lock(space); if (!is_active(space)) { is_write_unlock(space); return KERN_INVALID_TASK; } if ((entry = ipc_entry_lookup(space, name)) == IE_NULL) { is_write_unlock(space); return KERN_INVALID_NAME; } *entryp = entry; return KERN_SUCCESS; } /* * Routine: ipc_right_lookup_two_write * Purpose: * Like ipc_right_lookup except that it returns two * entries for two different names that were looked * up under the same space lock. * Conditions: * Nothing locked. If successful, the space is write-locked. * Returns: * KERN_INVALID_TASK The space is dead. * KERN_INVALID_NAME Name doesn't exist in space. */ kern_return_t ipc_right_lookup_two_write( ipc_space_t space, mach_port_name_t name1, ipc_entry_t *entryp1, mach_port_name_t name2, ipc_entry_t *entryp2) { ipc_entry_t entry1; ipc_entry_t entry2; assert(space != IS_NULL); is_write_lock(space); if (!is_active(space)) { is_write_unlock(space); return KERN_INVALID_TASK; } if ((entry1 = ipc_entry_lookup(space, name1)) == IE_NULL) { is_write_unlock(space); mach_port_guard_exception(name1, 0, kGUARD_EXC_INVALID_NAME); return KERN_INVALID_NAME; } if ((entry2 = ipc_entry_lookup(space, name2)) == IE_NULL) { is_write_unlock(space); mach_port_guard_exception(name2, 0, kGUARD_EXC_INVALID_NAME); return KERN_INVALID_NAME; } *entryp1 = entry1; *entryp2 = entry2; return KERN_SUCCESS; } /* * Routine: ipc_right_reverse * Purpose: * Translate (space, port) -> (name, entry). * Only finds send/receive rights. * Returns TRUE if an entry is found; if so, * the port active. * Conditions: * The space must be locked (read or write) and active. * The port is locked and active */ bool ipc_right_reverse( ipc_space_t space, ipc_port_t port, mach_port_name_t *namep, ipc_entry_t *entryp) { mach_port_name_t name; ipc_entry_t entry; assert(is_active(space)); require_ip_active(port); ip_mq_lock_held(port); if (ip_in_space(port, space)) { name = ip_get_receiver_name(port); assert(name != MACH_PORT_NULL); entry = ipc_entry_lookup(space, name); assert(entry != IE_NULL); assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE); assert(port == entry->ie_port); *namep = name; *entryp = entry; return true; } if (ipc_hash_lookup(space, ip_to_object(port), namep, entryp)) { entry = *entryp; assert(entry != IE_NULL); assert(IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_SEND); assert(port == entry->ie_port); return true; } return false; } /* * Routine: ipc_right_request_cancel * Purpose: * Cancel a notification request and return the send-once right. * Afterwards, entry->ie_request == 0. * Conditions: * The space must be write-locked; the port must be locked. * The port must be active. */ static inline ipc_port_t ipc_right_request_cancel( ipc_port_t port, mach_port_name_t name, ipc_entry_t entry) { ipc_port_request_index_t request = entry->ie_request; if (request != IE_REQ_NONE) { entry->ie_request = IE_REQ_NONE; return ipc_port_request_cancel(port, name, request); } return IP_NULL; } /* * Routine: ipc_right_dnrequest * Purpose: * Make a dead-name request, returning the previously * registered send-once right. If notify is IP_NULL, * just cancels the previously registered request. * * Conditions: * Nothing locked. May allocate memory. * Only consumes/returns refs if successful. * Returns: * KERN_SUCCESS Made/canceled dead-name request. * KERN_INVALID_TASK The space is dead. * KERN_INVALID_NAME Name doesn't exist in space. * KERN_INVALID_RIGHT Name doesn't denote port/dead rights. * KERN_INVALID_ARGUMENT Name denotes dead name, but * immediate is FALSE or notify is IP_NULL. * KERN_RESOURCE_SHORTAGE Couldn't allocate memory. */ kern_return_t ipc_right_request_alloc( ipc_space_t space, mach_port_name_t name, ipc_port_request_opts_t options, ipc_port_t notify, mach_msg_id_t id, ipc_port_t *previousp) { ipc_port_t previous = IP_NULL; ipc_entry_t entry; kern_return_t kr; #if IMPORTANCE_INHERITANCE bool will_arm = false; #endif /* IMPORTANCE_INHERITANCE */ for (;;) { ipc_port_t port = IP_NULL; kr = ipc_right_lookup_write(space, name, &entry); if (kr != KERN_SUCCESS) { return kr; } /* space is write-locked and active */ /* if nothing to do or undo, we're done */ if (notify == IP_NULL && entry->ie_request == IE_REQ_NONE) { is_write_unlock(space); *previousp = IP_NULL; return KERN_SUCCESS; } /* see if the entry is of proper type for requests */ if (entry->ie_bits & MACH_PORT_TYPE_PORT_RIGHTS) { ipc_port_request_index_t new_request; port = entry->ie_port; assert(port != IP_NULL); if (!ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { /* port is locked and active */ /* * if this port doesn't allow send_possible / * deadname notifications, fail We only need to * protect send_once rights since they do not * coalesce and allow for repeated notification * requests/allocations */ if ((entry->ie_bits & MACH_PORT_TYPE_SEND_ONCE) && !ipc_policy(port)->pol_notif_dead_name) { ip_mq_unlock(port); is_write_unlock(space); *previousp = IP_NULL; mach_port_guard_exception(ip_type(port), id, kGUARD_EXC_INVALID_NOTIFICATION_REQ); return KERN_DENIED; } /* * No matter what, we need to cancel any * previous request. */ previous = ipc_right_request_cancel(port, name, entry); /* if no new request, just cancel previous */ if (notify == IP_NULL) { ip_mq_unlock(port); ipc_entry_modified(space, name, entry); is_write_unlock(space); break; } /* * send-once rights, kernel objects, and non-full other queues * fire immediately (if immediate specified). */ if (options == (IPR_SOR_SPARM_MASK | IPR_SOR_SPREQ_MASK) && ((entry->ie_bits & MACH_PORT_TYPE_SEND_ONCE) || ip_in_space(port, ipc_space_kernel) || !ip_full(port))) { ip_mq_unlock(port); ipc_entry_modified(space, name, entry); is_write_unlock(space); ipc_notify_send_possible(notify, name); break; } /* * If there was a previous request, freeing it * above guarantees that the subsequent * allocation will find a slot and succeed, * thus assuring an atomic swap. */ #if IMPORTANCE_INHERITANCE will_arm = port->ip_sprequests == 0 && options == (IPR_SOR_SPARM_MASK | IPR_SOR_SPREQ_MASK); #endif /* IMPORTANCE_INHERITANCE */ kr = ipc_port_request_alloc(port, name, notify, options, &new_request); if (kr != KERN_SUCCESS) { assert(previous == IP_NULL); is_write_unlock(space); kr = ipc_port_request_grow(port); /* port is unlocked */ if (kr != KERN_SUCCESS) { return kr; } continue; } assert(new_request != IE_REQ_NONE); entry->ie_request = new_request; ipc_entry_modified(space, name, entry); is_write_unlock(space); #if IMPORTANCE_INHERITANCE if (will_arm && port->ip_impdonation != 0 && port->ip_spimportant == 0 && task_is_importance_donor(current_task())) { if (ipc_port_importance_delta(port, IPID_OPTION_SENDPOSSIBLE, 1) == FALSE) { ip_mq_unlock(port); } } else #endif /* IMPORTANCE_INHERITANCE */ ip_mq_unlock(port); break; } /* entry may have changed to dead-name by ipc_right_check() */ } /* treat send_possible requests as immediate w.r.t. dead-name */ if (options && notify != IP_NULL && (entry->ie_bits & MACH_PORT_TYPE_DEAD_NAME)) { mach_port_urefs_t urefs = IE_BITS_UREFS(entry->ie_bits); assert(urefs > 0); /* leave urefs pegged to maximum if it overflowed */ if (urefs < MACH_PORT_UREFS_MAX) { (entry->ie_bits)++; /* increment urefs */ } ipc_entry_modified(space, name, entry); is_write_unlock(space); if (port != IP_NULL) { ip_release(port); } ipc_notify_dead_name(notify, name); previous = IP_NULL; break; } kr = (entry->ie_bits & MACH_PORT_TYPE_PORT_OR_DEAD) ? KERN_INVALID_ARGUMENT : KERN_INVALID_RIGHT; is_write_unlock(space); if (port != IP_NULL) { ip_release(port); } return kr; } *previousp = previous; return KERN_SUCCESS; } /* * Routine: ipc_right_inuse * Purpose: * Check if an entry is being used. * Returns TRUE if it is. * Conditions: * The space is write-locked and active. */ bool ipc_right_inuse( ipc_entry_t entry) { return IE_BITS_TYPE(entry->ie_bits) != MACH_PORT_TYPE_NONE; } /* * Routine: ipc_right_check * Purpose: * Check if the port has died. If it has, * and the location is not IPC_COPYIN_KMSG_DESTINATION * and it is not a send once right then * clean up the entry and return TRUE. * Conditions: * The space is write-locked; the port is not locked. * If returns FALSE, the port is also locked. * Otherwise, entry is converted to a dead name. * * Caller is responsible for a reference to port if it * had died (returns TRUE). */ bool ipc_right_check( ipc_space_t space, ipc_port_t port, mach_port_name_t name, ipc_entry_t entry, ipc_copyin_op_t copyin_reason) { ipc_entry_bits_t bits; assert(is_active(space)); assert(port == entry->ie_port); ip_mq_lock(port); if (ip_active(port) || ((copyin_reason == IPC_COPYIN_KMSG_DESTINATION) && entry->ie_request == IE_REQ_NONE && (entry->ie_bits & MACH_PORT_TYPE_SEND_ONCE))) { return false; } /* this was either a pure send right or a send-once right */ bits = entry->ie_bits; assert((bits & MACH_PORT_TYPE_RECEIVE) == 0); assert(IE_BITS_UREFS(bits) > 0); if (bits & MACH_PORT_TYPE_SEND) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND); assert(IE_BITS_UREFS(bits) > 0); ip_srights_dec(port); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE); assert(IE_BITS_UREFS(bits) == 1); ip_sorights_dec(port); } /* * delete SEND rights from ipc hash. */ if ((bits & MACH_PORT_TYPE_SEND) != 0) { ipc_hash_delete(space, ip_to_object(port), name, entry); } /* convert entry to dead name */ bits &= ~(IE_BITS_TYPE_MASK | IE_BITS_PINNED_SEND); bits |= MACH_PORT_TYPE_DEAD_NAME; /* * If there was a notification request outstanding on this * name, and the port went dead, that notification * must already be on its way up from the port layer. * * Add the reference that the notification carries. It * is done here, and not in the notification delivery, * because the latter doesn't have a space reference and * trying to actually move a send-right reference would * get short-circuited into a MACH_PORT_DEAD by IPC. Since * all calls that deal with the right eventually come * through here, it has the same result. * * Once done, clear the request index so we only account * for it once. */ if (entry->ie_request != IE_REQ_NONE) { if (ipc_port_request_type(port, name, entry->ie_request) != 0) { /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { bits++; /* increment urefs */ } } entry->ie_request = IE_REQ_NONE; } entry->ie_bits = bits; entry->ie_object = IPC_OBJECT_NULL; ip_mq_unlock(port); ipc_entry_modified(space, name, entry); return true; } /* * Routine: ipc_right_terminate * Purpose: * Cleans up an entry in a terminated space. * The entry isn't deallocated or removed * from reverse hash tables. * Conditions: * The space is dead and unlocked. */ void ipc_right_terminate( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry) { mach_port_type_t type = IE_BITS_TYPE(entry->ie_bits); assert(!is_active(space)); /* * Hollow the entry under the port lock, * in order to avoid dangling pointers. * * ipc_right_lookup_read() doesn't need it for correctness, * but ipc_space_terminate() as it now goes through 2 rounds * of termination (receive rights first, the rest second). */ switch (type) { case MACH_PORT_TYPE_DEAD_NAME: assert(entry->ie_request == IE_REQ_NONE); assert(entry->ie_object == IPC_OBJECT_NULL); break; case MACH_PORT_TYPE_PORT_SET: { ipc_pset_t pset = entry->ie_pset; assert(entry->ie_request == IE_REQ_NONE); ips_mq_lock(pset); ipc_pset_destroy(space, pset); /* consumes ref, unlocks */ break; } case MACH_PORT_TYPE_SEND: case MACH_PORT_TYPE_RECEIVE: case MACH_PORT_TYPE_SEND_RECEIVE: case MACH_PORT_TYPE_SEND_ONCE: { ipc_port_t port = entry->ie_port; ipc_port_t request = IP_NULL; ipc_notify_nsenders_t nsrequest = { }; ip_mq_lock(port); if (ip_active(port)) { request = ipc_right_request_cancel(port, name, entry); } if (type & MACH_PORT_TYPE_SEND) { if (ip_srights_dec(port) == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } } if (type & MACH_PORT_TYPE_RECEIVE) { assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); ipc_port_destroy(port); /* clears receiver, consumes our ref, unlocks */ } else if (type & MACH_PORT_TYPE_SEND_ONCE) { ipc_notify_send_once_and_unlock(port); /* consumes our ref */ } else { /* port could be dead, in-transit, or in a foreign space */ assert(!ip_in_space(port, space)); ip_mq_unlock(port); ip_release(port); } ipc_notify_no_senders_emit(nsrequest); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } default: ipc_unreachable("ipc_right_terminate: strange type"); } entry->ie_object = IPC_OBJECT_NULL; entry->ie_bits &= (IE_BITS_GEN_MASK | IE_BITS_ROLL_MASK); } /* * Routine: ipc_right_destroy * Purpose: * Destroys an entry in a space. * Conditions: * The space is write-locked (returns unlocked). * The space must be active. * Returns: * KERN_SUCCESS The entry was destroyed. * KERN_INVALID_CAPABILITY The port is pinned. * KERN_INVALID_RIGHT Port guard violation. */ kern_return_t ipc_right_destroy( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry) { ipc_entry_bits_t bits; mach_port_type_t type; bits = entry->ie_bits; type = IE_BITS_TYPE(bits); assert(is_active(space)); switch (type) { case MACH_PORT_TYPE_DEAD_NAME: assert(entry->ie_request == IE_REQ_NONE); assert(entry->ie_object == IPC_OBJECT_NULL); ipc_entry_dealloc(space, IPC_OBJECT_NULL, name, entry); is_write_unlock(space); break; case MACH_PORT_TYPE_PORT_SET: { ipc_pset_t pset = entry->ie_pset; assert(entry->ie_request == IE_REQ_NONE); assert(pset != IPS_NULL); ips_mq_lock(pset); assert(ips_active(pset)); ipc_entry_dealloc(space, ips_to_object(pset), name, entry); is_write_unlock(space); ipc_pset_destroy(space, pset); /* consumes ref, unlocks */ break; } case MACH_PORT_TYPE_SEND: case MACH_PORT_TYPE_RECEIVE: case MACH_PORT_TYPE_SEND_RECEIVE: case MACH_PORT_TYPE_SEND_ONCE: { ipc_port_t port = entry->ie_port; ipc_notify_nsenders_t nsrequest = { }; ipc_port_t request = IP_NULL; ip_mq_lock(port); if ((type & MACH_PORT_TYPE_RECEIVE) && port->ip_guarded && port->ip_context != 0) { uint64_t portguard = port->ip_context; ip_mq_unlock(port); is_write_unlock(space); mach_port_guard_exception(name, portguard, kGUARD_EXC_DESTROY); return KERN_INVALID_RIGHT; } if ((bits & IE_BITS_PINNED_SEND) && ip_active(port)) { ip_mq_unlock(port); is_write_unlock(space); mach_port_guard_exception_pinned(space, name, MPG_FLAGS_MOD_REFS_PINNED_DESTROY); return KERN_INVALID_CAPABILITY; } /* point of no return */ if (ip_active(port)) { request = ipc_right_request_cancel(port, name, entry); } else { assert((type & MACH_PORT_TYPE_RECEIVE) == 0); entry->ie_request = IE_REQ_NONE; } if (type == MACH_PORT_TYPE_SEND) { ipc_hash_delete(space, ip_to_object(port), name, entry); } ipc_entry_dealloc(space, ip_to_object(port), name, entry); is_write_unlock(space); if (type & MACH_PORT_TYPE_SEND) { ip_srights_dec(port); if (port->ip_srights == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } } if (type & MACH_PORT_TYPE_RECEIVE) { require_ip_active(port); assert(ip_in_space(port, space)); ipc_port_destroy(port); /* clears receiver, consumes our ref, unlocks */ } else if (type & MACH_PORT_TYPE_SEND_ONCE) { ipc_notify_send_once_and_unlock(port); /* consumes our ref */ } else { assert(!ip_in_space(port, space)); ip_mq_unlock(port); ip_release(port); } ipc_notify_no_senders_emit(nsrequest); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } default: ipc_unreachable("ipc_right_destroy: strange type"); } return KERN_SUCCESS; } /* * Routine: ipc_right_dealloc * Purpose: * Releases a send/send-once/dead-name/port_set user ref. * Like ipc_right_delta with a delta of -1, * but looks at the entry to determine the right. * Conditions: * The space is write-locked, and is unlocked upon return. * The space must be active. * Returns: * KERN_SUCCESS A user ref was released. * KERN_INVALID_RIGHT Entry has wrong type. * KERN_INVALID_CAPABILITY Deallocating a pinned right. */ kern_return_t ipc_right_dealloc( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry) { ipc_port_t port = IP_NULL; ipc_entry_bits_t bits; mach_port_type_t type; bits = entry->ie_bits; type = IE_BITS_TYPE(bits); assert(is_active(space)); switch (type) { case MACH_PORT_TYPE_PORT_SET: { ipc_pset_t pset; assert(IE_BITS_UREFS(bits) == 0); assert(entry->ie_request == IE_REQ_NONE); pset = entry->ie_pset; ips_mq_lock(pset); assert(ips_active(pset)); ipc_entry_dealloc(space, ips_to_object(pset), name, entry); is_write_unlock(space); ipc_pset_destroy(space, pset); /* consumes ref, unlocks */ break; } case MACH_PORT_TYPE_DEAD_NAME: { dead_name: assert(IE_BITS_UREFS(bits) > 0); assert(entry->ie_request == IE_REQ_NONE); assert(entry->ie_object == IPC_OBJECT_NULL); if (IE_BITS_UREFS(bits) == 1) { ipc_entry_dealloc(space, IPC_OBJECT_NULL, name, entry); } else { /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { entry->ie_bits = bits - 1; /* decrement urefs */ } ipc_entry_modified(space, name, entry); } is_write_unlock(space); /* release any port that got converted to dead name below */ if (port != IP_NULL) { ip_release(port); } break; } case MACH_PORT_TYPE_SEND_ONCE: { ipc_port_t request; assert(IE_BITS_UREFS(bits) == 1); port = entry->ie_port; assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { bits = entry->ie_bits; assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); goto dead_name; /* it will release port */ } /* port is locked and active */ request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); is_write_unlock(space); ipc_notify_send_once_and_unlock(port); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } case MACH_PORT_TYPE_SEND: { ipc_port_t request = IP_NULL; ipc_notify_nsenders_t nsrequest = { }; assert(IE_BITS_UREFS(bits) > 0); port = entry->ie_port; assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { bits = entry->ie_bits; assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); goto dead_name; /* it will release port */ } /* port is locked and active */ assert(port->ip_srights > 0); if (IE_BITS_UREFS(bits) == 1) { if (bits & IE_BITS_PINNED_SEND) { ip_mq_unlock(port); goto destroy_pinned; } ip_srights_dec(port); if (port->ip_srights == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } request = ipc_right_request_cancel(port, name, entry); ipc_hash_delete(space, ip_to_object(port), name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); ip_mq_unlock(port); is_write_unlock(space); ip_release(port); } else { /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { entry->ie_bits = bits - 1; /* decrement urefs */ } ip_mq_unlock(port); ipc_entry_modified(space, name, entry); is_write_unlock(space); } ipc_notify_no_senders_emit(nsrequest); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } case MACH_PORT_TYPE_SEND_RECEIVE: { ipc_notify_nsenders_t nsrequest = { }; assert(IE_BITS_UREFS(bits) > 0); port = entry->ie_port; assert(port != IP_NULL); ip_mq_lock(port); require_ip_active(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); assert(port->ip_srights > 0); if (IE_BITS_UREFS(bits) == 1) { if (bits & IE_BITS_PINNED_SEND) { ip_mq_unlock(port); goto destroy_pinned; } ip_srights_dec(port); if (port->ip_srights == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } entry->ie_bits = bits & ~(IE_BITS_UREFS_MASK | MACH_PORT_TYPE_SEND); } else { /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { entry->ie_bits = bits - 1; /* decrement urefs */ } } ip_mq_unlock(port); ipc_entry_modified(space, name, entry); is_write_unlock(space); ipc_notify_no_senders_emit(nsrequest); break; } default: is_write_unlock(space); mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_DEALLOC, bits), kGUARD_EXC_INVALID_RIGHT); return KERN_INVALID_RIGHT; } return KERN_SUCCESS; destroy_pinned: is_write_unlock(space); mach_port_guard_exception_pinned(space, name, MPG_FLAGS_MOD_REFS_PINNED_DEALLOC); return KERN_INVALID_CAPABILITY; } /* * Routine: ipc_right_delta * Purpose: * Modifies the user-reference count for a right. * May deallocate the right, if the count goes to zero. * Conditions: * The space is write-locked, and is unlocked upon return. * The space must be active. * Returns: * KERN_SUCCESS Count was modified. * KERN_INVALID_RIGHT Entry has wrong type. * KERN_INVALID_VALUE Bad delta for the right. * KERN_INVALID_CAPABILITY Deallocating a pinned right. */ kern_return_t ipc_right_delta( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry, mach_port_right_t right, mach_port_delta_t delta) { ipc_port_t port = IP_NULL; ipc_port_t port_to_release = IP_NULL; ipc_entry_bits_t bits = entry->ie_bits; /* Debugging information in case a mach port guard exception is raised */ bool throw_exception = false; /* * The following is used (for case MACH_PORT_RIGHT_DEAD_NAME) in the * switch below. It is used to keep track of those cases (in DIPC) * where we have postponed the dropping of a port reference. Since * the dropping of the reference could cause the port to disappear * we postpone doing so when we are holding the space lock. */ assert(is_active(space)); assert(right < MACH_PORT_RIGHT_NUMBER); /* Rights-specific restrictions and operations. */ switch (right) { case MACH_PORT_RIGHT_PORT_SET: { ipc_pset_t pset; if ((bits & MACH_PORT_TYPE_PORT_SET) == 0) { throw_exception = true; goto invalid_right; } assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_PORT_SET); assert(IE_BITS_UREFS(bits) == 0); assert(entry->ie_request == IE_REQ_NONE); if (delta == 0) { goto success; } if (delta != -1) { goto invalid_value; } pset = entry->ie_pset; ips_mq_lock(pset); assert(ips_active(pset)); ipc_entry_dealloc(space, ips_to_object(pset), name, entry); is_write_unlock(space); ipc_pset_destroy(space, pset); /* consumes ref, unlocks */ break; } case MACH_PORT_RIGHT_RECEIVE: { ipc_port_t request = IP_NULL; if ((bits & MACH_PORT_TYPE_RECEIVE) == 0) { if ((bits & IE_BITS_EX_RECEIVE) == 0) { throw_exception = true; } goto invalid_right; } if (delta == 0) { goto success; } if (delta != -1) { goto invalid_value; } port = entry->ie_port; assert(port != IP_NULL); /* * The port lock is needed for ipc_right_dncancel; * otherwise, we wouldn't have to take the lock * until just before dropping the space lock. */ ip_mq_lock(port); require_ip_active(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); /* Mach Port Guard Checking */ if (port->ip_guarded) { uint64_t portguard = port->ip_context; ip_mq_unlock(port); is_write_unlock(space); /* Raise mach port guard exception */ mach_port_guard_exception(name, portguard, kGUARD_EXC_DESTROY); goto guard_failure; } if (bits & MACH_PORT_TYPE_SEND) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_RECEIVE); assert(IE_BITS_UREFS(bits) > 0); assert(port->ip_srights > 0); if (ipc_port_has_prdrequest(port)) { /* * Since another task has requested a * destroy notification for this port, it * isn't actually being destroyed - the receive * right is just being moved to another task. * Since we still have one or more send rights, * we need to record the loss of the receive * right and enter the remaining send right * into the hash table. */ bits &= ~MACH_PORT_TYPE_RECEIVE; bits |= IE_BITS_EX_RECEIVE; ipc_hash_insert(space, ip_to_object(port), name, entry); ip_reference(port); } else { /* * The remaining send right turns into a * dead name. Notice we don't decrement * ip_srights, generate a no-senders notif, * or use ipc_right_dncancel, because the * port is destroyed "first". */ bits &= ~(IE_BITS_TYPE_MASK | IE_BITS_PINNED_SEND | IE_BITS_IMMOVABLE_SEND); bits |= (MACH_PORT_TYPE_DEAD_NAME | IE_BITS_EX_RECEIVE); if (entry->ie_request) { entry->ie_request = IE_REQ_NONE; /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { bits++; /* increment urefs */ } } entry->ie_object = IPC_OBJECT_NULL; } entry->ie_bits = bits; ipc_entry_modified(space, name, entry); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE); assert(IE_BITS_UREFS(bits) == 0); request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); } is_write_unlock(space); ipc_port_destroy(port); /* clears receiver, consumes ref, unlocks */ if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } case MACH_PORT_RIGHT_SEND_ONCE: { ipc_port_t request; if ((bits & MACH_PORT_TYPE_SEND_ONCE) == 0) { goto invalid_right; } assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE); assert(IE_BITS_UREFS(bits) == 1); port = entry->ie_port; assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { assert(!(entry->ie_bits & MACH_PORT_TYPE_SEND_ONCE)); bits = entry->ie_bits; throw_exception = true; /* port has died and removed from entry, release port */ goto invalid_right; } /* port is locked and active */ assert(port->ip_sorights > 0); if ((delta > 0) || (delta < -1)) { ip_mq_unlock(port); goto invalid_value; } if (delta == 0) { ip_mq_unlock(port); goto success; } request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); is_write_unlock(space); ipc_notify_send_once_and_unlock(port); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } case MACH_PORT_RIGHT_DEAD_NAME: { mach_port_urefs_t urefs; if (bits & MACH_PORT_TYPE_SEND_RIGHTS) { port = entry->ie_port; assert(port != IP_NULL); if (!ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { /* port is locked and active */ ip_mq_unlock(port); port = IP_NULL; throw_exception = true; goto invalid_right; } bits = entry->ie_bits; /* port has died and removed from entry, release port */ port_to_release = port; port = IP_NULL; } else if ((bits & MACH_PORT_TYPE_DEAD_NAME) == 0) { throw_exception = true; goto invalid_right; } assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); assert(IE_BITS_UREFS(bits) > 0); assert(entry->ie_object == IPC_OBJECT_NULL); assert(entry->ie_request == IE_REQ_NONE); if (delta > ((mach_port_delta_t)MACH_PORT_UREFS_MAX) || delta < (-((mach_port_delta_t)MACH_PORT_UREFS_MAX))) { /* this will release port */ goto invalid_value; } urefs = IE_BITS_UREFS(bits); if (urefs == MACH_PORT_UREFS_MAX) { /* * urefs are pegged due to an overflow * only a delta removing all refs at once can change it */ if (delta != (-((mach_port_delta_t)MACH_PORT_UREFS_MAX))) { delta = 0; } } else { if (MACH_PORT_UREFS_UNDERFLOW(urefs, delta)) { /* this will release port */ goto invalid_value; } if (MACH_PORT_UREFS_OVERFLOW(urefs, delta)) { /* leave urefs pegged to maximum if it overflowed */ delta = MACH_PORT_UREFS_MAX - urefs; } } if ((urefs + delta) == 0) { ipc_entry_dealloc(space, IPC_OBJECT_NULL, name, entry); } else if (delta != 0) { entry->ie_bits = bits + delta; ipc_entry_modified(space, name, entry); } is_write_unlock(space); if (port_to_release != IP_NULL) { ip_release(port_to_release); port_to_release = IP_NULL; } break; } case MACH_PORT_RIGHT_SEND: { mach_port_urefs_t urefs; ipc_port_t request = IP_NULL; ipc_notify_nsenders_t nsrequest = { }; if ((bits & MACH_PORT_TYPE_SEND) == 0) { /* invalid right exception only when not live/dead confusion */ if ((bits & MACH_PORT_TYPE_DEAD_NAME) == 0 #if !defined(AE_MAKESENDRIGHT_FIXED) /* * AE tries to add single send right without knowing if it already owns one. * But if it doesn't, it should own the receive right and delta should be 1. */ && (((bits & MACH_PORT_TYPE_RECEIVE) == 0) || (delta != 1)) #endif ) { throw_exception = true; } goto invalid_right; } /* maximum urefs for send is MACH_PORT_UREFS_MAX */ port = entry->ie_port; assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { assert((entry->ie_bits & MACH_PORT_TYPE_SEND) == 0); bits = entry->ie_bits; /* port has died and removed from entry, release port */ goto invalid_right; } /* port is locked and active */ assert(port->ip_srights > 0); if (delta > ((mach_port_delta_t)MACH_PORT_UREFS_MAX) || delta < (-((mach_port_delta_t)MACH_PORT_UREFS_MAX))) { ip_mq_unlock(port); goto invalid_value; } urefs = IE_BITS_UREFS(bits); if (urefs == MACH_PORT_UREFS_MAX) { /* * urefs are pegged due to an overflow * only a delta removing all refs at once can change it */ if (delta != (-((mach_port_delta_t)MACH_PORT_UREFS_MAX))) { delta = 0; } } else { if (MACH_PORT_UREFS_UNDERFLOW(urefs, delta)) { ip_mq_unlock(port); goto invalid_value; } if (MACH_PORT_UREFS_OVERFLOW(urefs, delta)) { /* leave urefs pegged to maximum if it overflowed */ delta = MACH_PORT_UREFS_MAX - urefs; } } if ((urefs + delta) == 0) { if (bits & IE_BITS_PINNED_SEND) { ip_mq_unlock(port); goto destroy_pinned; } ip_srights_dec(port); if (port->ip_srights == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } if (bits & MACH_PORT_TYPE_RECEIVE) { assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_RECEIVE); entry->ie_bits = bits & ~(IE_BITS_UREFS_MASK | MACH_PORT_TYPE_SEND); ipc_entry_modified(space, name, entry); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND); request = ipc_right_request_cancel(port, name, entry); ipc_hash_delete(space, ip_to_object(port), name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); port_to_release = port; } } else if (delta != 0) { entry->ie_bits = bits + delta; ipc_entry_modified(space, name, entry); } ip_mq_unlock(port); is_write_unlock(space); if (port_to_release != IP_NULL) { ip_release(port_to_release); port_to_release = IP_NULL; } ipc_notify_no_senders_emit(nsrequest); if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } break; } case MACH_PORT_RIGHT_LABELH: goto invalid_right; default: panic("ipc_right_delta: strange right %d for 0x%x (%p) in space:%p", right, name, (void *)entry, (void *)space); } return KERN_SUCCESS; success: is_write_unlock(space); return KERN_SUCCESS; invalid_right: is_write_unlock(space); if (port != IP_NULL) { ip_release(port); } if (throw_exception) { mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_DELTA, right, bits), kGUARD_EXC_INVALID_RIGHT); } return KERN_INVALID_RIGHT; invalid_value: is_write_unlock(space); if (port_to_release) { ip_release(port_to_release); } mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_VALUE_DELTA, right, (uint16_t)delta, IE_BITS_UREFS(bits)), kGUARD_EXC_INVALID_VALUE); return KERN_INVALID_VALUE; guard_failure: return KERN_INVALID_RIGHT; destroy_pinned: is_write_unlock(space); mach_port_guard_exception_pinned(space, name, MPG_FLAGS_MOD_REFS_PINNED_DEALLOC); return KERN_INVALID_CAPABILITY; } /* * Routine: ipc_right_destruct * Purpose: * Deallocates the receive right and modifies the * user-reference count for the send rights as requested. * Conditions: * The space is write-locked, and is unlocked upon return. * The space must be active. * Returns: * KERN_SUCCESS Count was modified. * KERN_INVALID_RIGHT Entry has wrong type. * KERN_INVALID_VALUE Bad delta for the right. */ kern_return_t ipc_right_destruct( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry, mach_port_delta_t srdelta, uint64_t guard) { ipc_port_t port = IP_NULL; ipc_entry_bits_t bits; mach_port_urefs_t urefs; ipc_port_t request = IP_NULL; ipc_notify_nsenders_t nsrequest = { }; bits = entry->ie_bits; assert(is_active(space)); if ((bits & MACH_PORT_TYPE_RECEIVE) == 0) { is_write_unlock(space); /* No exception if we used to have receive and held entry since */ if ((bits & IE_BITS_EX_RECEIVE) == 0) { mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_DESTRUCT, bits), kGUARD_EXC_INVALID_RIGHT); } return KERN_INVALID_RIGHT; } if (srdelta && (bits & MACH_PORT_TYPE_SEND) == 0) { is_write_unlock(space); mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_DESTRUCT, bits), kGUARD_EXC_INVALID_RIGHT); return KERN_INVALID_RIGHT; } if (srdelta > 0) { goto invalid_value; } port = entry->ie_port; assert(port != IP_NULL); ip_mq_lock(port); require_ip_active(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); /* Mach Port Guard Checking */ if (port->ip_guarded && (guard != port->ip_context)) { uint64_t portguard = port->ip_context; ip_mq_unlock(port); is_write_unlock(space); mach_port_guard_exception(name, portguard, kGUARD_EXC_DESTROY); return KERN_INVALID_ARGUMENT; } /* * First reduce the send rights as requested and * adjust the entry->ie_bits accordingly. The * ipc_entry_modified() call is made once the receive * right is destroyed too. */ if (srdelta) { assert(port->ip_srights > 0); urefs = IE_BITS_UREFS(bits); /* * Since we made sure that srdelta is negative, * the check for urefs overflow is not required. */ if (MACH_PORT_UREFS_UNDERFLOW(urefs, srdelta)) { ip_mq_unlock(port); goto invalid_value; } if (urefs == MACH_PORT_UREFS_MAX) { /* * urefs are pegged due to an overflow * only a delta removing all refs at once can change it */ if (srdelta != (-((mach_port_delta_t)MACH_PORT_UREFS_MAX))) { srdelta = 0; } } if ((urefs + srdelta) == 0) { ip_srights_dec(port); if (port->ip_srights == 0) { nsrequest = ipc_notify_no_senders_prepare(port); } assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_RECEIVE); entry->ie_bits = bits & ~(IE_BITS_UREFS_MASK | MACH_PORT_TYPE_SEND); } else { entry->ie_bits = bits + srdelta; } } /* * Now destroy the receive right. Update space and * entry accordingly. */ bits = entry->ie_bits; if (bits & MACH_PORT_TYPE_SEND) { assert(IE_BITS_UREFS(bits) > 0); assert(IE_BITS_UREFS(bits) <= MACH_PORT_UREFS_MAX); if (ipc_port_has_prdrequest(port)) { /* * Since another task has requested a * destroy notification for this port, it * isn't actually being destroyed - the receive * right is just being moved to another task. * Since we still have one or more send rights, * we need to record the loss of the receive * right and enter the remaining send right * into the hash table. */ bits &= ~MACH_PORT_TYPE_RECEIVE; bits |= IE_BITS_EX_RECEIVE; ipc_hash_insert(space, ip_to_object(port), name, entry); ip_reference(port); } else { /* * The remaining send right turns into a * dead name. Notice we don't decrement * ip_srights, generate a no-senders notif, * or use ipc_right_dncancel, because the * port is destroyed "first". */ bits &= ~(IE_BITS_TYPE_MASK | IE_BITS_PINNED_SEND); bits |= (MACH_PORT_TYPE_DEAD_NAME | IE_BITS_EX_RECEIVE); if (entry->ie_request) { entry->ie_request = IE_REQ_NONE; if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { bits++; /* increment urefs */ } } entry->ie_object = IPC_OBJECT_NULL; } entry->ie_bits = bits; ipc_entry_modified(space, name, entry); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE); assert(IE_BITS_UREFS(bits) == 0); request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); } /* Unlock space */ is_write_unlock(space); ipc_notify_no_senders_emit(nsrequest); ipc_port_destroy(port); /* clears receiver, consumes ref, unlocks */ if (request != IP_NULL) { ipc_notify_port_deleted(request, name); } return KERN_SUCCESS; invalid_value: is_write_unlock(space); mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_VALUE_DESTRUCT, srdelta, IE_BITS_UREFS(bits)), kGUARD_EXC_INVALID_VALUE); return KERN_INVALID_VALUE; } /* * Routine: ipc_right_info * Purpose: * Retrieves information about the right. * Conditions: * The space is active and write-locked. * The space is unlocked upon return. * Returns: * KERN_SUCCESS Retrieved info */ kern_return_t ipc_right_info( ipc_space_t space, mach_port_name_t name, ipc_entry_t entry, mach_port_type_t *typep, mach_port_urefs_t *urefsp) { ipc_port_t port; ipc_entry_bits_t bits; mach_port_type_t type = 0; ipc_port_request_index_t request; bits = entry->ie_bits; request = entry->ie_request; port = entry->ie_port; if (bits & MACH_PORT_TYPE_RECEIVE) { assert(IP_VALID(port)); if (request != IE_REQ_NONE) { ip_mq_lock(port); require_ip_active(port); type |= ipc_port_request_type(port, name, request); ip_mq_unlock(port); } is_write_unlock(space); } else if (bits & MACH_PORT_TYPE_SEND_RIGHTS) { /* * validate port is still alive - if so, get request * types while we still have it locked. Otherwise, * recapture the (now dead) bits. */ if (!ipc_right_check(space, port, name, entry, IPC_COPYIN_REASON_NONE)) { if (request != IE_REQ_NONE) { type |= ipc_port_request_type(port, name, request); } ip_mq_unlock(port); is_write_unlock(space); } else { bits = entry->ie_bits; assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); is_write_unlock(space); ip_release(port); } } else { is_write_unlock(space); } type |= IE_BITS_TYPE(bits); *typep = type; *urefsp = IE_BITS_UREFS(bits); return KERN_SUCCESS; } /* * Routine: ipc_right_copyin_check_reply * Purpose: * Check if a subsequent ipc_right_copyin would succeed. Used only * by ipc_kmsg_copyin_header to check if reply_port can be copied in. * If the reply port is an immovable send right, it errors out. * Conditions: * The space is locked (read or write) and active. */ bool ipc_right_copyin_check_reply( __assert_only ipc_space_t space, mach_port_name_t reply_name, ipc_entry_t reply_entry, mach_msg_type_name_t reply_type) { ipc_entry_bits_t reply_bits = reply_entry->ie_bits; ipc_port_t reply_port = reply_entry->ie_port; assert(is_active(space)); if (ip_is_reply_port(reply_port) && !MACH_MSG_TYPE_PORT_ANY_SEND_ONCE(reply_type)) { return false; } switch (reply_type) { case MACH_MSG_TYPE_MAKE_SEND: if ((reply_bits & MACH_PORT_TYPE_RECEIVE) == 0) { return false; } break; case MACH_MSG_TYPE_MAKE_SEND_ONCE: if ((reply_bits & MACH_PORT_TYPE_RECEIVE) == 0) { return false; } break; case MACH_MSG_TYPE_MOVE_RECEIVE: /* ipc_kmsg_copyin_header already filters it out */ return false; case MACH_MSG_TYPE_MOVE_SEND: if ((reply_bits & IE_BITS_PINNED_SEND) && ip_active(reply_port) && IE_BITS_UREFS(reply_bits) == 1) { mach_port_guard_exception_pinned(space, reply_name, MPG_FLAGS_MOD_REFS_PINNED_COPYIN); return false; } OS_FALLTHROUGH; case MACH_MSG_TYPE_COPY_SEND: case MACH_MSG_TYPE_MOVE_SEND_ONCE: { if (reply_bits & MACH_PORT_TYPE_DEAD_NAME) { break; } if ((reply_bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0) { return false; } assert(reply_port != IP_NULL); /* * active status peek to avoid checks that will be skipped * on copyin for dead ports. Lock not held, so will not be * atomic (but once dead, there's no going back). */ if (!ip_active(reply_port)) { break; } /* * Can't copyin a send right that is marked immovable. This bit is on * the entry and protected by the space lock. */ if (reply_entry->ie_bits & IE_BITS_IMMOVABLE_SEND) { mach_port_guard_exception_immovable(space, reply_name, reply_port, MACH_MSG_TYPE_MOVE_SEND_ONCE, reply_entry); return false; } if (reply_type == MACH_MSG_TYPE_MOVE_SEND_ONCE) { if ((reply_bits & MACH_PORT_TYPE_SEND_ONCE) == 0) { return false; } } else { if ((reply_bits & MACH_PORT_TYPE_SEND) == 0) { return false; } } break; } default: panic("ipc_right_copyin_check: strange rights"); } return true; } /* * Routine: ipc_right_copyin_check_guard_locked * Purpose: * Check if the port is guarded and the guard * value matches the one passed in the arguments. * If MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND is set, * check if the port is unguarded. * Conditions: * The port is locked. * Returns: * KERN_SUCCESS Port is either unguarded * or guarded with expected value * KERN_INVALID_ARGUMENT Port is either unguarded already or guard mismatch. * This also raises a EXC_GUARD exception. */ static kern_return_t ipc_right_copyin_check_guard_locked( ipc_port_t port, mach_port_name_t name, mach_msg_guarded_port_descriptor_t *gdesc) { mach_port_context_t context = gdesc->u_context; mach_msg_guard_flags_t flags = gdesc->flags; if ((flags & MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND) && !port->ip_guarded && !context) { return KERN_SUCCESS; } else if (port->ip_guarded && (port->ip_context == context)) { return KERN_SUCCESS; } /* Incorrect guard; Raise exception */ mach_port_guard_exception(name, port->ip_context, kGUARD_EXC_INCORRECT_GUARD); return KERN_INVALID_ARGUMENT; } void ipc_right_copyin_rcleanup_init( ipc_copyin_rcleanup_t *icrc, mach_msg_guarded_port_descriptor_t *gdesc) { *icrc = (ipc_copyin_rcleanup_t){ .icrc_guarded_desc = gdesc, }; } void ipc_right_copyin_cleanup_destroy( ipc_copyin_cleanup_t *icc, mach_port_name_t name) { if (icc->icc_release_port) { ip_release(icc->icc_release_port); } if (icc->icc_deleted_port) { ipc_notify_port_deleted(icc->icc_deleted_port, name); } } void ipc_right_copyin_rcleanup_destroy(ipc_copyin_rcleanup_t *icrc) { #if IMPORTANCE_INHERITANCE if (icrc->icrc_assert_count) { ipc_importance_task_drop_internal_assertion(current_task()->task_imp_base, icrc->icrc_assert_count); } #endif /* IMPORTANCE_INHERITANCE */ if (icrc->icrc_free_list.next) { waitq_link_free_list(WQT_PORT_SET, &icrc->icrc_free_list); } } /* * Routine: ipc_right_copyin * Purpose: * Copyin a capability from a space. * If successful, the caller gets a ref * for the resulting port, unless it is IP_DEAD, * and possibly a send-once right which should * be used in a port-deleted notification. * * If deadok is not TRUE, the copyin operation * will fail instead of producing IO_DEAD. * * The entry is deallocated if the entry type becomes * MACH_PORT_TYPE_NONE. * Conditions: * The space is write-locked and active. * Returns: * KERN_SUCCESS Acquired a port, possibly IP_DEAD. * KERN_INVALID_RIGHT Name doesn't denote correct right. * KERN_INVALID_CAPABILITY Trying to move a kobject port, * an immovable right or * the last ref of a pinned right * KERN_INVALID_ARGUMENT Port is unguarded or guard mismatch */ kern_return_t ipc_right_copyin( ipc_space_t space, mach_port_name_t name, mach_msg_type_name_t msgt_name, ipc_object_copyin_flags_t flags, ipc_copyin_op_t copyin_reason, ipc_entry_t entry, ipc_port_t *portp, ipc_copyin_cleanup_t *icc, ipc_copyin_rcleanup_t *icrc) { ipc_entry_bits_t bits = entry->ie_bits; ipc_port_t port = entry->ie_port; ipc_object_label_t label; kern_return_t kr; uint32_t moves = (flags & IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_MOVE) ? 2 : 1; bool deadok = !!(flags & IPC_OBJECT_COPYIN_FLAGS_DEADOK); bool allow_imm_send = !!(flags & IPC_OBJECT_COPYIN_FLAGS_ALLOW_IMMOVABLE_SEND); /* * This is a temporary fix to allow re-sending a connection port after * pseudo receive. We need to fix this properly. See rdar://164492988. */ allow_imm_send |= (bits & MACH_PORT_TYPE_RECEIVE) && IP_VALID(port) && ip_is_connection_port(port); if (flags & IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_MOVE) { assert((flags & IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_COPY) == 0); assert(msgt_name == MACH_MSG_TYPE_MOVE_SEND); } if (flags & IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_COPY) { assert(msgt_name == MACH_MSG_TYPE_MOVE_SEND || msgt_name == MACH_MSG_TYPE_COPY_SEND); } *portp = IP_NULL; icc->icc_release_port = IP_NULL; icc->icc_deleted_port = IP_NULL; assert(is_active(space)); /* On certain port types (reply ports), only allow SEND_ONCE dispositions */ if (IP_VALID(port) && ip_is_reply_port(port) && !MACH_MSG_TYPE_PORT_ANY_SEND_ONCE(msgt_name)) { mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_NONE, ip_type(port), msgt_name), kGUARD_EXC_IMMOVABLE); return KERN_INVALID_CAPABILITY; } switch (msgt_name) { case MACH_MSG_TYPE_MAKE_SEND: { if ((bits & MACH_PORT_TYPE_RECEIVE) == 0) { goto invalid_right; } assert(port != IP_NULL); ip_mq_lock(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); ipc_port_make_send_any_locked(port); ip_mq_unlock(port); *portp = port; break; } case MACH_MSG_TYPE_MAKE_SEND_ONCE: { bool send_telemetry = false; if ((bits & MACH_PORT_TYPE_RECEIVE) == 0) { goto invalid_right; } assert(port != IP_NULL); if (ip_is_reply_port(port) && (copyin_reason != IPC_COPYIN_KMSG_REPLY && copyin_reason != IPC_COPYIN_KMSG_DESTINATION)) { mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_COPYIN, copyin_reason, msgt_name), kGUARD_EXC_INVALID_RIGHT); return KERN_INVALID_CAPABILITY; } ip_mq_lock(port); require_ip_active(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); /* * Reply ports can extend one single * send-once right at any given moment. */ #if DEVELOPMENT || DEBUG if (ip_is_reply_port(port) && (port->ip_sorights > 0)) { send_telemetry = true; } #endif /* DEVELOPMENT || DEBUG */ ipc_port_make_sonce_locked(port); ip_mq_unlock(port); if (__improbable(send_telemetry)) { mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_COPYIN, copyin_reason, msgt_name), kGUARD_EXC_REPLY_PORT_SINGLE_SO_RIGHT); } *portp = port; break; } case MACH_MSG_TYPE_MOVE_RECEIVE: { ipc_port_t request = IP_NULL; if ((bits & MACH_PORT_TYPE_RECEIVE) == 0) { goto invalid_right; } assert(port != IP_NULL); /* * ipc_move_receive_allowed raises the appropritate * Guard exception if needed */ if (!ipc_move_receive_allowed(space, port, name)) { return KERN_INVALID_CAPABILITY; } label = ip_mq_lock_label_get(port); require_ip_active(port); assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); if (icrc->icrc_guarded_desc) { kr = ipc_right_copyin_check_guard_locked(port, name, icrc->icrc_guarded_desc); if (kr != KERN_SUCCESS) { ip_mq_unlock_label_put(port, &label); return kr; } /* this flag will be cleared during copyout */ icrc->icrc_guarded_desc->flags |= MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND; } if (bits & MACH_PORT_TYPE_SEND) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_RECEIVE); assert(IE_BITS_UREFS(bits) > 0); assert(port->ip_srights > 0); bits &= ~MACH_PORT_TYPE_RECEIVE; bits |= IE_BITS_EX_RECEIVE; entry->ie_bits = bits; ipc_hash_insert(space, ip_to_object(port), name, entry); ip_reference(port); ipc_entry_modified(space, name, entry); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE); assert(IE_BITS_UREFS(bits) == 0); request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); } /* This will unguard the port and make it movable. */ ipc_port_mark_in_limbo(port, &label, &icrc->icrc_free_list); #if IMPORTANCE_INHERITANCE /* * Account for boosts the current task is going to lose when * copying this right in. Tempowner ports have either not * been accounting to any task (and therefore are already in * "limbo" state w.r.t. assertions) or to some other specific * task. As we have no way to drop the latter task's assertions * here, We'll deduct those when we enqueue it on its * destination port (see ipc_port_check_circularity()). */ if (port->ip_tempowner == 0) { assert(IIT_NULL == ip_get_imp_task(port)); /* ports in limbo have to be tempowner */ port->ip_tempowner = 1; icrc->icrc_assert_count = port->ip_impcount; } #endif /* IMPORTANCE_INHERITANCE */ /* We already set the label above */ ip_mq_unlock(port); *portp = port; icc->icc_deleted_port = request; break; } case MACH_MSG_TYPE_COPY_SEND: { if (bits & MACH_PORT_TYPE_DEAD_NAME) { goto copy_dead; } /* allow for dead send-once rights */ if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0) { goto invalid_right; } assert(IE_BITS_UREFS(bits) > 0); if (ipc_right_check(space, port, name, entry, copyin_reason)) { bits = entry->ie_bits; icc->icc_release_port = port; goto copy_dead; } /* port is locked and active */ if ((bits & MACH_PORT_TYPE_SEND) == 0) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE); assert(port->ip_sorights > 0); ip_mq_unlock(port); goto invalid_right; } if (!allow_imm_send && (entry->ie_bits & IE_BITS_IMMOVABLE_SEND)) { ip_mq_unlock(port); mach_port_guard_exception_immovable(space, name, port, MACH_MSG_TYPE_COPY_SEND, entry); return KERN_INVALID_CAPABILITY; } ipc_port_copy_send_any_locked(port); if (flags & IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_COPY) { ipc_port_copy_send_any_locked(port); } ip_mq_unlock(port); *portp = port; break; } case MACH_MSG_TYPE_MOVE_SEND: { ipc_port_t request = IP_NULL; if (bits & MACH_PORT_TYPE_DEAD_NAME) { goto move_dead; } /* allow for dead send-once rights */ if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0) { goto invalid_right; } assert(IE_BITS_UREFS(bits) > 0); assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, copyin_reason)) { bits = entry->ie_bits; icc->icc_release_port = port; goto move_dead; } /* port is locked and active */ if ((bits & MACH_PORT_TYPE_SEND) == 0 || IE_BITS_UREFS(bits) < moves) { ip_mq_unlock(port); goto invalid_right; } if ((bits & IE_BITS_PINNED_SEND) && IE_BITS_UREFS(bits) == moves) { ip_mq_unlock(port); mach_port_guard_exception_pinned(space, name, MPG_FLAGS_MOD_REFS_PINNED_COPYIN); return KERN_INVALID_CAPABILITY; } if (!allow_imm_send && (entry->ie_bits & IE_BITS_IMMOVABLE_SEND)) { ip_mq_unlock(port); mach_port_guard_exception_immovable(space, name, port, MACH_MSG_TYPE_MOVE_SEND, entry); return KERN_INVALID_CAPABILITY; } if (IE_BITS_UREFS(bits) == moves) { assert(port->ip_srights > 0); /* * We have exactly "moves" send rights for this port * in this space, which means that we will liberate the * naked send right held by this entry. * * However refcounting rules around entries are that * naked send rights on behalf of spaces do not have an * associated port reference, so we need to donate one * ... */ if (bits & MACH_PORT_TYPE_RECEIVE) { assert(ip_get_receiver_name(port) == name); assert(ip_in_space(port, space)); assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_RECEIVE); /* * ... that we inject manually when the entry * stays alive */ entry->ie_bits = bits & ~ (IE_BITS_UREFS_MASK | MACH_PORT_TYPE_SEND); ipc_entry_modified(space, name, entry); ip_reference(port); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND); /* ... that we steal from the entry when it dies */ request = ipc_right_request_cancel(port, name, entry); ipc_hash_delete(space, ip_to_object(port), name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); /* transfer entry's reference to caller */ } } else { ipc_port_copy_send_any_locked(port); /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { entry->ie_bits = bits - moves; /* decrement urefs */ } ipc_entry_modified(space, name, entry); } if (flags & (IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_COPY | IPC_OBJECT_COPYIN_FLAGS_DEST_EXTRA_MOVE)) { ipc_port_copy_send_any_locked(port); } ip_mq_unlock(port); *portp = port; icc->icc_deleted_port = request; break; } case MACH_MSG_TYPE_MOVE_SEND_ONCE: { ipc_port_t request = IP_NULL; if (bits & MACH_PORT_TYPE_DEAD_NAME) { goto move_dead; } /* allow for dead send rights */ if ((bits & MACH_PORT_TYPE_SEND_RIGHTS) == 0) { goto invalid_right; } assert(IE_BITS_UREFS(bits) > 0); assert(port != IP_NULL); if (ipc_right_check(space, port, name, entry, copyin_reason)) { bits = entry->ie_bits; icc->icc_release_port = port; goto move_dead; } /* * port is locked, but may not be active: * Allow copyin of inactive ports with no dead name request and treat it * as if the copyin of the port was successful and port became inactive * later. */ if ((bits & MACH_PORT_TYPE_SEND_ONCE) == 0) { assert(bits & MACH_PORT_TYPE_SEND); assert(port->ip_srights > 0); ip_mq_unlock(port); goto invalid_right; } if (ip_is_reply_port(port) && copyin_reason != IPC_COPYIN_KMSG_DESTINATION) { ip_mq_unlock(port); mach_port_guard_exception(name, MPG_PAYLOAD(MPG_FLAGS_INVALID_RIGHT_COPYIN, copyin_reason, msgt_name), kGUARD_EXC_INVALID_RIGHT); return KERN_INVALID_CAPABILITY; } if (!allow_imm_send && (entry->ie_bits & IE_BITS_IMMOVABLE_SEND)) { ip_mq_unlock(port); mach_port_guard_exception_immovable(space, name, port, MACH_MSG_TYPE_MOVE_SEND_ONCE, entry); return KERN_INVALID_CAPABILITY; } assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND_ONCE); assert(IE_BITS_UREFS(bits) == 1); assert(port->ip_sorights > 0); request = ipc_right_request_cancel(port, name, entry); ipc_entry_dealloc(space, ip_to_object(port), name, entry); ip_mq_unlock(port); *portp = port; icc->icc_deleted_port = request; break; } default: invalid_right: return KERN_INVALID_RIGHT; } return KERN_SUCCESS; copy_dead: assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); assert(IE_BITS_UREFS(bits) > 0); assert(entry->ie_request == IE_REQ_NONE); assert(entry->ie_object == 0); if (!deadok) { goto invalid_right; } *portp = IP_DEAD; return KERN_SUCCESS; move_dead: assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_DEAD_NAME); assert(IE_BITS_UREFS(bits) > 0); assert(entry->ie_request == IE_REQ_NONE); assert(entry->ie_object == IPC_OBJECT_NULL); if (!deadok || IE_BITS_UREFS(bits) < moves) { goto invalid_right; } if (IE_BITS_UREFS(bits) == moves) { ipc_entry_dealloc(space, IPC_OBJECT_NULL, name, entry); } else { /* if urefs are pegged due to overflow, leave them pegged */ if (IE_BITS_UREFS(bits) < MACH_PORT_UREFS_MAX) { entry->ie_bits = bits - moves; /* decrement urefs */ } ipc_entry_modified(space, name, entry); } *portp = IP_DEAD; return KERN_SUCCESS; } /* * Routine: ipc_right_copyout_any_send * Purpose: * Copyout a capability to a space. * If successful, consumes a ref for the port. * * Always succeeds when given a newly-allocated entry, * because user-reference overflow isn't a possibility. * * If copying out the port would cause the user-reference * count in the entry to overflow, then the user-reference * count is left pegged to its maximum value and the copyout * succeeds anyway. * Conditions: * The space is write-locked and active. * The port is locked and active. * The port is unlocked; the space isn't. */ void ipc_right_copyout_any_send( ipc_space_t space, ipc_port_t port, mach_msg_type_name_t msgt_name, ipc_object_copyout_flags_t flags, mach_port_name_t name, ipc_entry_t entry) { ipc_entry_bits_t bits = entry->ie_bits; assert(IP_VALID(port)); assert(ip_active(port)); assert(entry->ie_port == port); ipc_object_label_t label = ip_label_get(port); if (ipc_should_mark_immovable_send(space->is_task, port, label)) { bits |= IE_BITS_IMMOVABLE_SEND; } ip_label_put(port, &label); /* * A caller that copies out an immovable send right for a new entry * must demonstrate sufficient awareness of what they're doing. */ if ((bits & IE_BITS_IMMOVABLE_SEND) && !(flags & IPC_OBJECT_COPYOUT_FLAGS_ALLOW_IMMOVABLE_SEND) && IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE) { ipc_triage_policy_violation_and_expect_continue( IPC_SEC_POLICY_RESTRICT_IMMOVABLE_SEND_RIGHT_CREATION, space, 0, 0, IP_NULL, 0); } switch (msgt_name) { case MACH_MSG_TYPE_PORT_SEND_ONCE: assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE); assert(IE_BITS_UREFS(bits) == 0); assert(port->ip_sorights > 0); if (ip_is_special_reply_port(port)) { ipc_port_adjust_special_reply_port_locked(port, current_thread()->ith_knote, IPC_PORT_ADJUST_SR_LINK_WORKLOOP, FALSE); /* port unlocked on return */ } else { ip_mq_unlock(port); } entry->ie_bits = bits | (MACH_PORT_TYPE_SEND_ONCE | 1); /* set urefs to 1 */ ipc_entry_modified(space, name, entry); break; case MACH_MSG_TYPE_PORT_SEND: assert(port->ip_srights > 0); if (bits & MACH_PORT_TYPE_SEND) { __assert_only mach_port_urefs_t urefs = IE_BITS_UREFS(bits); assert(port->ip_srights > 1); assert(urefs > 0); assert(urefs <= MACH_PORT_UREFS_MAX); /* consume send right and ref */ ip_srights_dec(port); ip_mq_unlock(port); ip_release_live(port); } else if (bits & MACH_PORT_TYPE_RECEIVE) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_RECEIVE); assert(IE_BITS_UREFS(bits) == 0); /* transfer send right to entry, consume ref */ ip_mq_unlock(port); ip_release_live(port); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE); assert(IE_BITS_UREFS(bits) == 0); /* transfer send right and ref to entry */ ip_mq_unlock(port); /* entry is locked holding ref, so can use port */ ipc_hash_insert(space, ip_to_object(port), name, entry); } if (flags & IPC_OBJECT_COPYOUT_FLAGS_PINNED) { bits |= IE_BITS_PINNED_SEND; } if (IE_BITS_UREFS(bits) != MACH_PORT_UREFS_MAX) { bits = (bits | MACH_PORT_TYPE_SEND) + 1; /* increment urefs */ } entry->ie_bits = bits; ipc_entry_modified(space, name, entry); break; default: ipc_unreachable("ipc_right_copyout_any_send: strange rights"); } } void ipc_right_copyout_recv_and_unlock_space( ipc_space_t space, ipc_port_t port, ipc_object_label_t *label, mach_port_name_t name, ipc_entry_t entry, mach_msg_guarded_port_descriptor_t *gdesc) { struct knote *kn; ipc_port_t dest; #if IMPORTANCE_INHERITANCE natural_t assertcnt = port->ip_impcount; #endif /* IMPORTANCE_INHERITANCE */ ipc_object_state_t in_space = IO_STATE_INACTIVE; /* means default */ ipc_entry_bits_t bits = entry->ie_bits; assert(IP_VALID(port)); assert(ip_active(port)); assert(entry->ie_port == port); assert(port->ip_mscount == 0); kn = current_thread()->ith_knote; if (gdesc && gdesc->flags & MACH_MSG_GUARD_FLAGS_IMMOVABLE_RECEIVE) { port->ip_guarded = 1; port->ip_strict_guard = 0; port->ip_context = current_thread()->ith_recv_bufs.recv_msg_addr; gdesc->u_context = port->ip_context; gdesc->flags &= ~MACH_MSG_GUARD_FLAGS_UNGUARDED_ON_SEND; in_space = IO_STATE_IN_SPACE_IMMOVABLE; } if (ip_is_any_service_port_type(label->io_type) && label->io_state == IO_STATE_IN_TRANSIT_PD) { ipc_service_port_label_t sp_label = label->iol_service; /* * Check if this is a special port-destroyed * notification to ensure that initproc doesnt end up * with a guarded service port sent in a regular message */ #if !(DEVELOPMENT || DEBUG) #if CONFIG_COALITIONS if (!task_is_in_privileged_coalition(current_task(), COALITION_TYPE_JETSAM)) { panic("Service port not sent back to launchd"); } #else /* CONFIG_COALITIONS */ if (!task_is_initproc(current_task())) { panic("Service port not sent back to launchd"); } #endif /* CONFIG_COALITIONS */ #endif /* !(DEVELOPMENT || DEBUG) */ /* * If the service port was guarded, verify the port name * and restore the guard value. * * See mach_port_construct(). */ if (sp_label->ispl_launchd_name) { if (name != sp_label->ispl_launchd_name) { panic("Service port name = 0x%x doesnt match " "the stored launchd port name = 0x%x", name, sp_label->ispl_launchd_name); } port->ip_guarded = 1; port->ip_strict_guard = 1; port->ip_context = sp_label->ispl_launchd_context; } } /* * pseudo receive shouldn't set the receive right * as immovable in the sender's space, it clearly moved once. */ dest = ipc_port_mark_in_space(port, label, space, name, (kn == ITH_KNOTE_PSEUDO) ? IO_STATE_IN_SPACE : in_space); assert((bits & MACH_PORT_TYPE_RECEIVE) == 0); if (bits & MACH_PORT_TYPE_SEND) { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_SEND); assert(IE_BITS_UREFS(bits) > 0); assert(port->ip_srights > 0); } else { assert(IE_BITS_TYPE(bits) == MACH_PORT_TYPE_NONE); assert(IE_BITS_UREFS(bits) == 0); } entry->ie_bits = bits | MACH_PORT_TYPE_RECEIVE; ipc_entry_modified(space, name, entry); boolean_t sync_bootstrap_checkin = FALSE; if (kn != ITH_KNOTE_PSEUDO && port->ip_sync_bootstrap_checkin) { sync_bootstrap_checkin = TRUE; } if (!ITH_KNOTE_VALID(kn, MACH_MSG_TYPE_PORT_RECEIVE)) { kn = NULL; } ipc_port_adjust_port_locked(port, kn, sync_bootstrap_checkin); /* port unlocked */ if (bits & MACH_PORT_TYPE_SEND) { ip_release_live(port); /* entry is locked holding ref, so can use port */ ipc_hash_delete(space, ip_to_object(port), name, entry); } is_write_unlock(space); if (dest != IP_NULL) { #if IMPORTANCE_INHERITANCE /* * Deduct the assertion counts we contributed to * the old destination port. They've already * been reflected into the task as a result of * getting enqueued. */ ip_mq_lock(dest); ipc_port_impcount_delta(dest, 0 - assertcnt, IP_NULL); ip_mq_unlock(dest); #endif /* IMPORTANCE_INHERITANCE */ /* Drop turnstile ref on dest */ ipc_port_send_turnstile_complete(dest); ip_release(dest); } } |