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 | /* * Copyright (c) 2000-2012 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@ */ #include <sys/types.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/proc_internal.h> #include <sys/sysctl.h> #include <sys/signal.h> #include <sys/signalvar.h> #include <sys/codesign.h> #include <sys/code_signing.h> #include <sys/fcntl.h> #include <sys/file.h> #include <sys/file_internal.h> #include <sys/kauth.h> #include <sys/mount.h> #include <sys/msg.h> #include <sys/proc.h> #include <sys/socketvar.h> #include <sys/vnode.h> #include <sys/vnode_internal.h> #include <sys/ubc.h> #include <sys/ubc_internal.h> #include <security/mac.h> #include <security/mac_policy.h> #include <security/mac_framework.h> #include <mach/mach_types.h> #include <mach/vm_map.h> #include <mach/mach_vm.h> #include <kern/kern_types.h> #include <kern/startup.h> #include <kern/task.h> #include <vm/vm_map_xnu.h> #include <vm/pmap.h> #include <vm/vm_kern.h> #include <kern/assert.h> #include <kern/cs_blobs.h> #include <pexpert/pexpert.h> #include <mach/shared_region.h> #include <libkern/section_keywords.h> #include <libkern/amfi/amfi.h> unsigned long cs_procs_killed = 0; unsigned long cs_procs_invalidated = 0; int cs_force_kill = 0; int cs_force_hard = 0; int cs_debug = 0; // If set, AMFI will error out early on unsigned code, before evaluation the normal policy. int cs_debug_fail_on_unsigned_code = 0; // If the previous mode is enabled, we count the resulting failures here. unsigned int cs_debug_unsigned_exec_failures = 0; unsigned int cs_debug_unsigned_mmap_failures = 0; #if CONFIG_ENFORCE_SIGNED_CODE #define DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE 1 #define DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE 1 #else #define DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE 1 #define DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE 0 #endif #if CONFIG_ENFORCE_LIBRARY_VALIDATION #define DEFAULT_CS_LIBRARY_VA_ENABLE 1 #else #define DEFAULT_CS_LIBRARY_VA_ENABLE 0 #endif #if SECURE_KERNEL /* * Here we split cs_enforcement_enable into cs_system_enforcement_enable and cs_process_enforcement_enable * * cs_system_enforcement_enable governs whether or not system level code signing enforcement mechanisms * are applied on the system. Today, the only such mechanism is code signing enforcement of the dyld shared * cache. * * cs_process_enforcement_enable governs whether code signing enforcement mechanisms are applied to all * processes or only those that opt into such enforcement. * * (On iOS and related, both of these are set by default. On macOS, only cs_system_enforcement_enable * is set by default. Processes can then be opted into code signing enforcement on a case by case basis.) */ SECURITY_READ_ONLY_EARLY(int) cs_system_enforcement_enable = DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE; SECURITY_READ_ONLY_EARLY(int) cs_process_enforcement_enable = DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE; SECURITY_READ_ONLY_EARLY(int) cs_library_val_enable = DEFAULT_CS_LIBRARY_VA_ENABLE; #else /* !SECURE_KERNEL */ int cs_enforcement_panic = 0; int cs_relax_platform_task_ports = 0; SECURITY_READ_ONLY_LATE(int) cs_system_enforcement_enable = DEFAULT_CS_SYSTEM_ENFORCEMENT_ENABLE; SECURITY_READ_ONLY_LATE(int) cs_process_enforcement_enable = DEFAULT_CS_PROCESS_ENFORCEMENT_ENABLE; SECURITY_READ_ONLY_LATE(int) cs_library_val_enable = DEFAULT_CS_LIBRARY_VA_ENABLE; #endif /* !SECURE_KERNEL */ int cs_all_vnodes = 0; SYSCTL_INT(_vm, OID_AUTO, cs_force_kill, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_force_kill, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_force_hard, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_force_hard, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_debug, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_debug, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_debug_fail_on_unsigned_code, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_debug_fail_on_unsigned_code, 0, ""); SYSCTL_UINT(_vm, OID_AUTO, cs_debug_unsigned_exec_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_debug_unsigned_exec_failures, 0, ""); SYSCTL_UINT(_vm, OID_AUTO, cs_debug_unsigned_mmap_failures, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_debug_unsigned_mmap_failures, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_all_vnodes, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_all_vnodes, 0, ""); #if !SECURE_KERNEL SYSCTL_INT(_vm, OID_AUTO, cs_system_enforcement, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_system_enforcement_enable, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_process_enforcement, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_process_enforcement_enable, 0, ""); SYSCTL_INT(_vm, OID_AUTO, cs_enforcement_panic, CTLFLAG_RW | CTLFLAG_LOCKED, &cs_enforcement_panic, 0, ""); #if !CONFIG_ENFORCE_LIBRARY_VALIDATION SYSCTL_INT(_vm, OID_AUTO, cs_library_validation, CTLFLAG_RD | CTLFLAG_LOCKED, &cs_library_val_enable, 0, ""); #endif #endif /* !SECURE_KERNEL */ __startup_func static void cs_init(void) { #if !SECURE_KERNEL int disable_cs_enforcement = 0; PE_parse_boot_argn("cs_enforcement_disable", &disable_cs_enforcement, sizeof(disable_cs_enforcement)); if (disable_cs_enforcement && PE_i_can_has_debugger(NULL) != 0) { cs_system_enforcement_enable = 0; cs_process_enforcement_enable = 0; } else { int panic = 0; PE_parse_boot_argn("cs_enforcement_panic", &panic, sizeof(panic)); cs_enforcement_panic = (panic != 0); } PE_parse_boot_argn("cs_relax_platform_task_ports", &cs_relax_platform_task_ports, sizeof(cs_relax_platform_task_ports)); PE_parse_boot_argn("cs_debug", &cs_debug, sizeof(cs_debug)); #if !CONFIG_ENFORCE_LIBRARY_VALIDATION PE_parse_boot_argn("cs_library_val_enable", &cs_library_val_enable, sizeof(cs_library_val_enable)); #endif #endif /* !SECURE_KERNEL */ } STARTUP(CODESIGNING, STARTUP_RANK_FIRST, cs_init); int cs_allow_invalid(struct proc *p) { uint64_t flags; #if MACH_ASSERT lck_mtx_assert(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED); #endif #if CONFIG_MACF /* There needs to be a MAC policy to implement this hook, or else the * kill bits will be cleared here every time. If we have * CONFIG_ENFORCE_SIGNED_CODE, we can assume there is a policy * implementing the hook. */ if (0 != mac_proc_check_run_cs_invalid(p)) { if (cs_debug) { printf("CODE SIGNING: cs_allow_invalid() " "not allowed: pid %d\n", proc_getpid(p)); } return 0; } if (cs_debug) { printf("CODE SIGNING: cs_allow_invalid() " "allowed: pid %d\n", proc_getpid(p)); } proc_lock(p); flags = proc_getcsflags(p) & ~(CS_KILL | CS_HARD); if (flags & CS_VALID) { flags |= CS_DEBUGGED; } proc_csflags_update(p, flags); task_t procTask = proc_task(p); if (procTask) { vm_map_t proc_map = get_task_map_reference(procTask); if (proc_map) { if (vm_map_cs_wx_enable(proc_map) != KERN_SUCCESS) { printf("CODE SIGNING: cs_allow_invalid() not allowed by pmap: pid %d\n", proc_getpid(p)); } vm_map_deallocate(proc_map); } } proc_unlock(p); /* allow a debugged process to hide some (debug-only!) memory */ task_set_memory_ownership_transfer(proc_task(p), TRUE); vm_map_switch_protect(get_task_map(proc_task(p)), FALSE); vm_map_cs_debugged_set(get_task_map(proc_task(p)), TRUE); #endif return (proc_getcsflags(p) & (CS_KILL | CS_HARD)) == 0; } int cs_invalid_page(addr64_t vaddr, boolean_t *cs_killed) { struct proc *p; int send_kill = 0, retval = 0, verbose = cs_debug; uint64_t flags; p = current_proc(); if (verbose) { printf("CODE SIGNING: cs_invalid_page(0x%llx): p=%d[%s]\n", vaddr, proc_getpid(p), p->p_comm); } proc_lock(p); flags = proc_getcsflags(p); /* XXX for testing */ if (cs_force_kill) { flags |= CS_KILL; } if (cs_force_hard) { flags |= CS_HARD; } /* CS_KILL triggers a kill signal, and no you can't have the page. Nothing else. */ if (flags & CS_KILL) { flags |= CS_KILLED; cs_procs_killed++; send_kill = 1; retval = 1; } /* CS_HARD means fail the mapping operation so the process stays valid. */ if (flags & CS_HARD) { retval = 1; proc_csflags_update(p, flags); } else { if (flags & CS_VALID) { flags &= ~CS_VALID; cs_procs_invalidated++; verbose = 1; proc_csflags_update(p, flags); cs_process_invalidated(NULL); } else { proc_csflags_update(p, flags); } } proc_unlock(p); if (verbose) { printf("CODE SIGNING: cs_invalid_page(0x%llx): " "p=%d[%s] final status 0x%x, %s page%s\n", vaddr, proc_getpid(p), p->p_comm, (unsigned int)proc_getcsflags(p), retval ? "denying" : "allowing (remove VALID)", send_kill ? " sending SIGKILL" : ""); } if (send_kill) { /* We will set the exit reason for the thread later */ threadsignal(current_thread(), SIGKILL, EXC_BAD_ACCESS, FALSE); if (cs_killed) { *cs_killed = TRUE; } } else if (cs_killed) { *cs_killed = FALSE; } return retval; } /* * Called after a process got its CS_VALID bit removed, either by * a previous call to cs_invalid_page, or through other means. * Called from fault handler with vm object lock held. * Called with proc lock held for current_proc or, if passed in, p, * to ensure MACF hook can suspend the task before other threads * can access the memory that is paged in after cs_invalid_page * returns 0 due to missing CS_HARD|CS_KILL. */ void cs_process_invalidated(struct proc * __unused p) { #if CONFIG_MACF if (p == NULL) { p = current_proc(); } mac_proc_notify_cs_invalidated(p); #endif } /* * Assumes p (if passed in) is locked with proc_lock(). */ int cs_process_enforcement(struct proc *p) { if (cs_process_enforcement_enable) { return 1; } if (p == NULL) { p = current_proc(); } if (p != NULL && (proc_getcsflags(p) & CS_ENFORCEMENT)) { return 1; } return 0; } int cs_process_global_enforcement(void) { return cs_process_enforcement_enable ? 1 : 0; } int cs_system_enforcement(void) { return cs_system_enforcement_enable ? 1 : 0; } int cs_vm_supports_4k_translations(void) { return 0; } /* * Returns whether a given process is still valid. */ int cs_valid(struct proc *p) { if (p == NULL) { p = current_proc(); } if (p != NULL && (proc_getcsflags(p) & CS_VALID)) { return 1; } return 0; } /* * Library validation functions */ int cs_require_lv(struct proc *p) { if (cs_library_val_enable) { return 1; } if (p == NULL) { p = current_proc(); } if (p != NULL && (proc_getcsflags(p) & CS_REQUIRE_LV)) { return 1; } return 0; } int csproc_forced_lv(struct proc* p) { if (p == NULL) { p = current_proc(); } if (p != NULL && (proc_getcsflags(p) & CS_FORCED_LV)) { return 1; } return 0; } /* * <rdar://problem/24634089> added to allow system level library * validation check at mac_cred_label_update_execve time */ int cs_system_require_lv(void) { return cs_library_val_enable ? 1 : 0; } /* * Function: csblob_get_base_offset * * Description: This function returns the base offset into the (possibly universal) binary * for a given blob. */ off_t csblob_get_base_offset(struct cs_blob *blob) { return blob->csb_base_offset; } /* * Function: csblob_get_size * * Description: This function returns the size of a given blob. */ vm_size_t csblob_get_size(struct cs_blob *blob) { return blob->csb_mem_size; } /* * Function: csblob_get_addr * * Description: This function returns the address of a given blob. */ vm_address_t csblob_get_addr(struct cs_blob *blob) { return (vm_address_t)blob->csb_mem_kaddr; } /* * Function: csblob_get_platform_binary * * Description: This function returns true if the binary is * in the trust cache. */ int csblob_get_platform_binary(struct cs_blob *blob) { if (blob && blob->csb_platform_binary) { return 1; } return 0; } /* * Function: csblob_invalidate_flags * * Description: This function is used to clear the CS_VALID bit on a blob * when a vnode may have been modified. * */ void csblob_invalidate_flags(struct cs_blob *csblob) { bool ro_blob = csblob == csblob->csb_ro_addr; unsigned int current_flags = csblob->csb_flags; unsigned int updated_flags = current_flags & (~CS_VALID); if (ro_blob == true) { zalloc_ro_update_field(ZONE_ID_CS_BLOB, csblob, csb_flags, &updated_flags); } else { csblob->csb_flags = updated_flags; } if (csblob->csb_entitlements != NULL) { amfi->OSEntitlements_invalidate(csblob->csb_entitlements); } printf("Invalidated flags, old %x new %x\n", current_flags, csblob->csb_flags); } /* * Function: csvnode_invalidate_flags * * Description: This function is used to clear the CS_VALID bit on all blobs * attached to a vnode. * */ void csvnode_invalidate_flags(struct vnode *vp) { struct cs_blob* oblob; bool mark_ubcinfo = false; for (oblob = ubc_get_cs_blobs(vp); oblob != NULL; oblob = oblob->csb_next) { if (!mark_ubcinfo) { mark_ubcinfo = true; vnode_lock(vp); if (vp->v_ubcinfo) { vp->v_ubcinfo->ui_flags |= UI_CSBLOBINVALID; } vnode_unlock(vp); } csblob_invalidate_flags(oblob); } } /* * Function: csblob_get_flags * * Description: This function returns the flags for a given blob */ unsigned int csblob_get_flags(struct cs_blob *blob) { return blob->csb_flags; } /* * Function: csblob_get_hashtype * * Description: This function returns the hash type for a given blob */ uint8_t csblob_get_hashtype(struct cs_blob const * const blob) { return blob->csb_hashtype != NULL ? cs_hash_type(blob->csb_hashtype) : 0; } /* * Function: csproc_get_blob * * Description: This function returns the cs_blob * for the process p */ struct cs_blob * csproc_get_blob(struct proc *p) { if (NULL == p) { return NULL; } if (NULL == p->p_textvp) { return NULL; } if ((proc_getcsflags(p) & CS_SIGNED) == 0) { return NULL; } return ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff); } /* * Function: csvnode_get_blob * * Description: This function returns the cs_blob * for the vnode vp */ struct cs_blob * csvnode_get_blob(struct vnode *vp, off_t offset) { return ubc_cs_blob_get(vp, -1, -1, offset); } /* * Function: csblob_get_teamid * * Description: This function returns a pointer to the * team id of csblob */ const char * csblob_get_teamid(struct cs_blob *csblob) { return csblob->csb_teamid; } /* * Function: csblob_get_identity * * Description: This function returns a pointer to the * identity string */ const char * csblob_get_identity(struct cs_blob *csblob) { const CS_CodeDirectory *cd; cd = (const CS_CodeDirectory *)csblob_find_blob(csblob, CSSLOT_CODEDIRECTORY, CSMAGIC_CODEDIRECTORY); if (cd == NULL) { return NULL; } if (cd->identOffset == 0) { return NULL; } return ((const char *)cd) + ntohl(cd->identOffset); } /* * Function: csblob_get_cdhash * * Description: This function returns a pointer to the * cdhash of csblob (20 byte array) */ const uint8_t * csblob_get_cdhash(struct cs_blob *csblob) { return csblob->csb_cdhash; } /* * Function: csblob_get_signer_type * * Description: This function returns the signer type * as an integer */ unsigned int csblob_get_signer_type(struct cs_blob *csblob) { return csblob->csb_signer_type; } /* * Function: csblob_set_validation_category * * Description: This function is used to set the validation * category on a cs_blob. Can only be set once, * except when set as none. * * Return: 0 on success, otherwise -1. */ int csblob_set_validation_category(struct cs_blob *csblob, unsigned int category) { bool ro_blob = csblob == csblob->csb_ro_addr; if ((csblob->csb_validation_category == CS_VALIDATION_CATEGORY_INVALID) || (csblob->csb_validation_category == CS_VALIDATION_CATEGORY_NONE)) { if (ro_blob == true) { zalloc_ro_update_field(ZONE_ID_CS_BLOB, csblob, csb_validation_category, &category); } else { csblob->csb_validation_category = category; } return 0; } /* Always allow when setting to none */ if (category == CS_VALIDATION_CATEGORY_NONE) { if (ro_blob == true) { zalloc_ro_update_field(ZONE_ID_CS_BLOB, csblob, csb_validation_category, &category); } else { csblob->csb_validation_category = category; } return 0; } /* Allow setting to the same category */ if (category == csblob->csb_validation_category) { return 0; } return -1; } /* * Function: csblob_get_validation_category * * Description: This function is used to get the validation * category on a cs_blob. */ unsigned int csblob_get_validation_category(struct cs_blob *csblob) { return csblob->csb_validation_category; } /* * Function: csblob_set_auxiliary_info * * Description: This function is used to set the auxiliary info * bits on a cs_blob. * * Return: 0 on success, otherwise -1. */ int csblob_set_auxiliary_info(struct cs_blob *csblob, uint64_t info) { bool ro_blob = csblob == csblob->csb_ro_addr; if (ro_blob == true) { zalloc_ro_update_field(ZONE_ID_CS_BLOB, csblob, csb_auxiliary_info, &info); } else { csblob->csb_auxiliary_info = info; } return 0; } /* * Function: csblob_get_auxiliary_info * * Description: This function is used to get the auxiliary info * bits on a cs_blob. */ uint64_t csblob_get_auxiliary_info(struct cs_blob *csblob) { return csblob->csb_auxiliary_info; } /* * Function: csblob_get_trust_level * * Description: This function is used to get the trust level set * on a cs_blob. This trust level is only set whe there * is a code-signing-monitor, and it is enabled. Otherwise, * this function returns a UINT32_MAX. */ uint32_t csblob_get_trust_level(__unused struct cs_blob *csblob) { #if CODE_SIGNING_MONITOR if (csblob->csb_csm_obj != NULL) { return csblob->csb_csm_trust_level; } #endif return UINT32_MAX; } /* * Function: csblob_get_code_directory * * Description: This function returns the best code directory * as chosen by the system */ const CS_CodeDirectory* csblob_get_code_directory(struct cs_blob *csblob) { return csblob->csb_cd; } void * csblob_entitlements_dictionary_copy(struct cs_blob *csblob) { if (!csblob->csb_entitlements) { return NULL; } if (!amfi) { panic("CoreEntitlements: missing AMFI bridge\n"); } return amfi->OSEntitlements_asdict(csblob->csb_entitlements); } OS_NORETURN void csblob_entitlements_dictionary_set(struct cs_blob __unused *csblob, void __unused *entitlements) { panic("CoreEntitlements: This API is no longer supported\n"); } void csblob_os_entitlements_set(struct cs_blob *csblob, void * entitlements) { assert(csblob->csb_entitlements == NULL); if (entitlements) { osobject_retain(entitlements); } csblob->csb_entitlements = entitlements; } void * csblob_os_entitlements_copy(struct cs_blob *csblob) { if (!csblob->csb_entitlements) { return NULL; } osobject_retain(csblob->csb_entitlements); return csblob->csb_entitlements; } void * csblob_os_entitlements_get(struct cs_blob *csblob) { if (!csblob->csb_entitlements) { return NULL; } return csblob->csb_entitlements; } void * csblob_get_storage_addr(struct cs_blob *csblob) { void *addr = csblob->csb_ro_addr; cs_blob_require((struct cs_blob *)addr, NULL); return addr; } /* * Function: csproc_get_teamid * * Description: This function returns a pointer to the * team id of the process p */ const char * csproc_get_teamid(struct proc *p) { struct cs_blob *csblob; csblob = csproc_get_blob(p); if (csblob == NULL) { return NULL; } return csblob_get_teamid(csblob); } const char * csproc_get_identity(struct proc *p) { struct cs_blob *csblob = NULL; csblob = csproc_get_blob(p); if (csblob == NULL) { return NULL; } return csblob_get_identity(csblob); } /* * Function: csproc_get_signer_type * * Description: This function returns the signer type * of the process p */ unsigned int csproc_get_signer_type(struct proc *p) { struct cs_blob *csblob; csblob = csproc_get_blob(p); if (csblob == NULL) { return CS_SIGNER_TYPE_UNKNOWN; } return csblob_get_signer_type(csblob); } /* * Function: csvnode_get_teamid * * Description: This function returns a pointer to the * team id of the binary at the given offset in vnode vp */ const char * csvnode_get_teamid(struct vnode *vp, off_t offset) { struct cs_blob *csblob; if (vp == NULL) { return NULL; } csblob = ubc_cs_blob_get(vp, -1, -1, offset); if (csblob == NULL) { return NULL; } return csblob_get_teamid(csblob); } /* * Function: csproc_get_platform_binary * * Description: This function returns the value * of the platform_binary field for proc p */ int csproc_get_platform_binary(struct proc *p) { struct cs_blob *csblob; csblob = csproc_get_blob(p); /* If there is no csblob this returns 0 because * it is true that it is not a platform binary */ return (csblob == NULL) ? 0 : csblob->csb_platform_binary; } int csproc_get_platform_path(struct proc *p) { struct cs_blob *csblob; csblob = csproc_get_blob(p); return (csblob == NULL) ? 0 : csblob->csb_platform_path; } #if DEVELOPMENT || DEBUG void csproc_clear_platform_binary(struct proc *p) { struct cs_blob *csblob = csproc_get_blob(p); struct cs_blob_platform_flags platform_flags; if (csblob == NULL) { return; } if (cs_debug) { printf("clearing platform binary on proc/task: pid = %d\n", proc_getpid(p)); } platform_flags = csblob->csb_platform_flags; platform_flags.csb_platform_binary = 0; platform_flags.csb_platform_path = 0; zalloc_ro_update_field(ZONE_ID_CS_BLOB, csblob, csb_platform_flags, &platform_flags); task_set_platform_binary(proc_task(p), FALSE); } #endif void csproc_disable_enforcement(struct proc* __unused p) { #if !CONFIG_ENFORCE_SIGNED_CODE if (p != NULL) { proc_lock(p); proc_csflags_clear(p, CS_ENFORCEMENT); vm_map_cs_enforcement_set(get_task_map(proc_task(p)), FALSE); proc_unlock(p); } #endif } /* Function: csproc_mark_invalid_allowed * * Description: Mark the process as being allowed to go invalid. Called as part of * task_for_pid and ptrace policy. Note CS_INVALID_ALLOWED only matters for * processes that have been opted into CS_ENFORCEMENT. */ void csproc_mark_invalid_allowed(struct proc* __unused p) { #if !CONFIG_ENFORCE_SIGNED_CODE if (p != NULL) { proc_lock(p); proc_csflags_set(p, CS_INVALID_ALLOWED); proc_unlock(p); } #endif } /* * Function: csproc_check_invalid_allowed * * Description: Returns 1 if the process has been marked as allowed to go invalid * because it gave its task port to an allowed process. */ int csproc_check_invalid_allowed(struct proc* __unused p) { #if !CONFIG_ENFORCE_SIGNED_CODE if (p == NULL) { p = current_proc(); } if (p != NULL && (proc_getcsflags(p) & CS_INVALID_ALLOWED)) { return 1; } #endif return 0; } /* * Function: csproc_get_prod_signed * * Description: Returns 1 if process is not signed with a developer identity. * Note the inverted meaning from the cs_flag to make the error case safer. * Will go away with rdar://problem/28322552. */ int csproc_get_prod_signed(struct proc *p) { return (proc_getcsflags(p) & CS_DEV_CODE) == 0; } int csproc_get_validation_category(struct proc *pt, unsigned int *out_validation_category) { struct cs_blob* blob = NULL; unsigned int validation_category = CS_VALIDATION_CATEGORY_INVALID; int error; proc_lock(pt); if ((proc_getcsflags(pt) & (CS_VALID | CS_DEBUGGED)) == 0) { proc_unlock(pt); error = EINVAL; goto out; } blob = csproc_get_blob(pt); proc_unlock(pt); if (!blob) { error = EBADEXEC; goto out; } validation_category = csblob_get_validation_category(blob); if (out_validation_category) { *out_validation_category = validation_category; } error = KERN_SUCCESS; out: return error; } /* * Function: csfg_get_platform_binary * * Description: This function returns the * platform binary field for the * fileglob fg */ int csfg_get_platform_binary(struct fileglob *fg) { int platform_binary = 0; struct ubc_info *uip; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return 0; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return 0; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blobs == NULL) { goto out; } /* It is OK to extract the teamid from the first blob * because all blobs of a vnode must have the same teamid */ platform_binary = uip->cs_blobs->csb_platform_binary; out: vnode_unlock(vp); return platform_binary; } int csfg_get_supplement_platform_binary(struct fileglob *fg __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES int platform_binary = 0; struct ubc_info *uip; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return 0; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return 0; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blob_supplement == NULL) { goto out; } platform_binary = uip->cs_blob_supplement->csb_platform_binary; out: vnode_unlock(vp); return platform_binary; #else // Supplemental signatures are only allowed in CONFIG_SUPPLEMENTAL_SIGNATURES // Return false if anyone asks about them return 0; #endif } uint8_t * csfg_get_cdhash(struct fileglob *fg, uint64_t offset, size_t *cdhash_size) { vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get(vp, -1, -1, offset)) == NULL) { return NULL; } if (cdhash_size) { *cdhash_size = CS_CDHASH_LEN; } return csblob->csb_cdhash; } uint8_t * csfg_get_supplement_cdhash(struct fileglob *fg __unused, uint64_t offset __unused, size_t *cdhash_size __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get_supplement(vp, offset)) == NULL) { return NULL; } if (cdhash_size) { *cdhash_size = CS_CDHASH_LEN; } return csblob->csb_cdhash; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // return NULL if anyone asks about them return NULL; #endif } const uint8_t * csfg_get_supplement_linkage_cdhash(struct fileglob *fg __unused, uint64_t offset __unused, size_t *cdhash_size __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get_supplement(vp, offset)) == NULL) { return NULL; } if (cdhash_size) { *cdhash_size = CS_CDHASH_LEN; } return csblob->csb_linkage; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // return NULL if anyone asks about them return NULL; #endif } /* * Function: csfg_get_signer_type * * Description: This returns the signer type * for the fileglob fg */ unsigned int csfg_get_signer_type(struct fileglob *fg) { struct ubc_info *uip; unsigned int signer_type = CS_SIGNER_TYPE_UNKNOWN; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return CS_SIGNER_TYPE_UNKNOWN; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return CS_SIGNER_TYPE_UNKNOWN; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blobs == NULL) { goto out; } /* It is OK to extract the signer type from the first blob, * because all blobs of a vnode must have the same signer type. */ signer_type = uip->cs_blobs->csb_signer_type; out: vnode_unlock(vp); return signer_type; } unsigned int csfg_get_supplement_signer_type(struct fileglob *fg __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES struct ubc_info *uip; unsigned int signer_type = CS_SIGNER_TYPE_UNKNOWN; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return CS_SIGNER_TYPE_UNKNOWN; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return CS_SIGNER_TYPE_UNKNOWN; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blob_supplement == NULL) { goto out; } signer_type = uip->cs_blob_supplement->csb_signer_type; out: vnode_unlock(vp); return signer_type; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // Return unknown if anyone asks return CS_SIGNER_TYPE_UNKNOWN; #endif } /* * Function: csfg_get_validation_category * * Description: This returns the validation category * for the fileglob fg */ unsigned int csfg_get_validation_category(struct fileglob *fg, uint64_t offset) { unsigned int validation_category = CS_VALIDATION_CATEGORY_INVALID; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return CS_VALIDATION_CATEGORY_INVALID; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return CS_VALIDATION_CATEGORY_INVALID; } vnode_lock(vp); struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get(vp, -1, -1, offset)) == NULL) { goto out; } validation_category = csblob->csb_validation_category; out: vnode_unlock(vp); return validation_category; } unsigned int csfg_get_supplement_validation_category(struct fileglob *fg __unused, uint64_t offset __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES unsigned int validation_category = CS_VALIDATION_CATEGORY_INVALID; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return CS_SIGNER_TYPE_UNKNOWN; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return CS_SIGNER_TYPE_UNKNOWN; } vnode_lock(vp); struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get_supplement(vp, offset)) == NULL) { goto out; } validation_category = csblob->csb_validation_category; out: vnode_unlock(vp); return validation_category; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // Return invalid if anyone asks return CS_VALIDATION_CATEGORY_INVALID; #endif } /* * Function: csfg_get_teamid * * Description: This returns a pointer to * the teamid for the fileglob fg */ const char * csfg_get_teamid(struct fileglob *fg) { struct ubc_info *uip; const char *str = NULL; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blobs == NULL) { goto out; } /* It is OK to extract the teamid from the first blob * because all blobs of a vnode must have the same teamid */ str = uip->cs_blobs->csb_teamid; out: vnode_unlock(vp); return str; } const char * csfg_get_supplement_teamid(struct fileglob *fg __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES struct ubc_info *uip; const char *str = NULL; vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blob_supplement == NULL) { goto out; } str = uip->cs_blob_supplement->csb_supplement_teamid; out: vnode_unlock(vp); return str; #else // Supplemental Signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // Return NULL if anyone asks return NULL; #endif } /* * Function: csfg_get_csblob * * Description: This returns a pointer to * the csblob for the fileglob fg */ struct cs_blob* csfg_get_csblob(struct fileglob *fg, uint64_t offset) { vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get(vp, -1, -1, offset)) == NULL) { return NULL; } return csblob; } struct cs_blob* csfg_get_supplement_csblob(__unused struct fileglob *fg, __unused uint64_t offset) { #if CONFIG_SUPPLEMENTAL_SIGNATURES vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } struct cs_blob *csblob = NULL; if ((csblob = ubc_cs_blob_get_supplement(vp, offset)) == NULL) { return NULL; } return csblob; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // return NULL if anyone asks about them return NULL; #endif } /* * Function: csfg_get_prod_signed * * Description: Returns 1 if code is not signed with a developer identity. * Note the inverted meaning from the cs_flag to make the error case safer. * Will go away with rdar://problem/28322552. */ int csfg_get_prod_signed(struct fileglob *fg) { struct ubc_info *uip; vnode_t vp; int prod_signed = 0; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return 0; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return 0; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blobs == NULL) { goto out; } /* It is OK to extract the flag from the first blob * because all blobs of a vnode must have the same cs_flags */ prod_signed = (uip->cs_blobs->csb_flags & CS_DEV_CODE) == 0; out: vnode_unlock(vp); return prod_signed; } int csfg_get_supplement_prod_signed(struct fileglob *fg __unused) { #if CONFIG_SUPPLEMENTAL_SIGNATURES struct ubc_info *uip; vnode_t vp; int prod_signed = 0; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return 0; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return 0; } vnode_lock(vp); if (!UBCINFOEXISTS(vp)) { goto out; } uip = vp->v_ubcinfo; if (uip == NULL) { goto out; } if (uip->cs_blob_supplement == NULL) { goto out; } /* It is OK to extract the flag from the first blob * because all blobs of a vnode must have the same cs_flags */ prod_signed = (uip->cs_blob_supplement->csb_flags & CS_DEV_CODE) == 0; out: vnode_unlock(vp); return prod_signed; #else // Supplemental signatures are only available in CONFIG_SUPPLEMENTAL_SIGNATURES // Indicate development signed if anyone tries to ask about one. return 0; #endif } /* * Function: csfg_get_identity * * Description: This function returns the codesign identity * for the fileglob */ const char * csfg_get_identity(struct fileglob *fg, off_t offset) { vnode_t vp; struct cs_blob *csblob = NULL; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return NULL; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return NULL; } csblob = ubc_cs_blob_get(vp, -1, -1, offset); if (csblob == NULL) { return NULL; } return csblob_get_identity(csblob); } /* * Function: csfg_get_platform_identifier * * Description: This function returns the codesign platform * identifier for the fileglob. Assumes the fileproc * is being held busy to keep the fileglob consistent. */ uint8_t csfg_get_platform_identifier(struct fileglob *fg, off_t offset) { vnode_t vp; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return 0; } vp = (struct vnode *)fg_get_data(fg); if (vp == NULL) { return 0; } return csvnode_get_platform_identifier(vp, offset); } /* * Function: csvnode_get_platform_identifier * * Description: This function returns the codesign platform * identifier for the vnode. Assumes a vnode reference * is held. */ uint8_t csvnode_get_platform_identifier(struct vnode *vp, off_t offset) { struct cs_blob *csblob; const CS_CodeDirectory *code_dir; csblob = ubc_cs_blob_get(vp, -1, -1, offset); if (csblob == NULL) { return 0; } code_dir = csblob->csb_cd; if (code_dir == NULL || ntohl(code_dir->length) < 8) { return 0; } return code_dir->platform; } /* * Function: csproc_get_platform_identifier * * Description: This function returns the codesign platform * identifier for the proc. Assumes proc will remain * valid through call. */ uint8_t csproc_get_platform_identifier(struct proc *p) { if (NULL == p->p_textvp) { return 0; } return csvnode_get_platform_identifier(p->p_textvp, p->p_textoff); } uint32_t cs_entitlement_flags(struct proc *p) { return proc_getcsflags(p) & CS_ENTITLEMENT_FLAGS; } int cs_restricted(struct proc *p) { return (proc_getcsflags(p) & CS_RESTRICT) ? 1 : 0; } int csproc_hardened_runtime(struct proc* p) { return (proc_getcsflags(p) & CS_RUNTIME) ? 1 : 0; } /* * Function: csfg_get_path * * Description: This populates the buffer passed in * with the path of the vnode * When calling this, the fileglob * cannot go away. The caller must have a * a reference on the fileglob or fileproc */ int csfg_get_path(struct fileglob *fg, char *path, int *len) { vnode_t vp = NULL; if (FILEGLOB_DTYPE(fg) != DTYPE_VNODE) { return -1; } vp = (struct vnode *)fg_get_data(fg); /* vn_getpath returns 0 for success, * or an error code */ return vn_getpath(vp, path, len); } /* * Retrieve the entitlements blob for a vnode * Returns: * EINVAL no text vnode associated with the process * EBADEXEC invalid code signing data * 0 no error occurred * * On success, out_start and out_length will point to the * entitlements blob if found; or will be set to NULL/zero * if there were no entitlements. */ int cs_entitlements_blob_get_vnode(vnode_t vnode, off_t offset, void **out_start, size_t *out_length) { struct cs_blob *csblob; *out_start = NULL; *out_length = 0; if (vnode == NULL) { return EINVAL; } if ((csblob = ubc_cs_blob_get(vnode, -1, -1, offset)) == NULL) { return 0; } return csblob_get_entitlements(csblob, out_start, out_length); } /* Retrieve the cached entitlements for a vnode * Returns: * EINVAL no vnode * EBADEXEC invalid code signing data * 0 no error occurred * * Note: the entitlements may be NULL if there is nothing cached. */ int cs_entitlements_dictionary_copy_vnode(vnode_t vnode, off_t offset, void **entitlements) { struct cs_blob *csblob; *entitlements = NULL; if (vnode == NULL) { return EINVAL; } if ((csblob = ubc_cs_blob_get(vnode, -1, -1, offset)) == NULL) { return 0; } *entitlements = csblob_entitlements_dictionary_copy(csblob); return 0; } /* * Retrieve the entitlements blob for a process. * Returns: * EINVAL no text vnode associated with the process * EBADEXEC invalid code signing data * 0 no error occurred * * On success, out_start and out_length will point to the * entitlements blob if found; or will be set to NULL/zero * if there were no entitlements. */ int cs_entitlements_blob_get(proc_t p, void **out_start, size_t *out_length) { if ((proc_getcsflags(p) & CS_SIGNED) == 0) { return 0; } return cs_entitlements_blob_get_vnode(p->p_textvp, p->p_textoff, out_start, out_length); } /* Retrieve the cached entitlements for a process * Returns: * EINVAL no text vnode associated with the process * EBADEXEC invalid code signing data * 0 no error occurred * * Note: the entitlements may be NULL if there is nothing cached. */ int cs_entitlements_dictionary_copy(proc_t p, void **entitlements) { struct cs_blob *csblob; *entitlements = NULL; if ((proc_getcsflags(p) & CS_SIGNED) == 0) { return 0; } if (NULL == p->p_textvp) { return EINVAL; } if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff)) == NULL) { return 0; } *entitlements = csblob_entitlements_dictionary_copy(csblob); return 0; } /* Retrieve the codesign identity for a process. * Returns: * NULL an error occured * string the cs_identity */ const char * cs_identity_get(proc_t p) { struct cs_blob *csblob; if ((proc_getcsflags(p) & CS_SIGNED) == 0) { return NULL; } if (NULL == p->p_textvp) { return NULL; } if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff)) == NULL) { return NULL; } return csblob_get_identity(csblob); } /* * DO NOT USE THIS FUNCTION! * Use the properly guarded csproc_get_blob instead. * * This is currently here to allow detached signatures to work * properly. The only user of this function is also checking * for CS_VALID. */ int cs_blob_get(proc_t p, void **out_start, size_t *out_length) { struct cs_blob *csblob; *out_start = NULL; *out_length = 0; if (NULL == p->p_textvp) { return EINVAL; } if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff)) == NULL) { return 0; } *out_start = csblob->csb_mem_kaddr; *out_length = csblob->csb_mem_size; return 0; } /* * return cshash of a process, cdhash is of size CS_CDHASH_LEN */ uint8_t * cs_get_cdhash(struct proc *p) { struct cs_blob *csblob; if ((proc_getcsflags(p) & CS_SIGNED) == 0) { return NULL; } if (NULL == p->p_textvp) { return NULL; } if ((csblob = ubc_cs_blob_get(p->p_textvp, -1, -1, p->p_textoff)) == NULL) { return NULL; } return csblob->csb_cdhash; } /* * return launch type of a process being created. */ cs_launch_type_t launch_constraint_data_get_launch_type(launch_constraint_data_t lcd) { return lcd->launch_type; } |