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 | /* * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1998 Apple Computer, Inc. All rights reserved. * * HISTORY * */ /* * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. */ #include <IOKit/IODeviceTreeSupport.h> #include <IOKit/IOService.h> #include <libkern/c++/OSContainers.h> #include <IOKit/IOCatalogue.h> #include <libkern/c++/OSUnserialize.h> extern "C" { #include <machine/machine_routines.h> #include <mach/kmod.h> #include <mach-o/mach_header.h> #include <kern/host.h> #include <security/mac_data.h> }; #include <IOKit/IOLib.h> #include <IOKit/assert.h> extern "C" { int IODTGetLoaderInfo( char *key, void **infoAddr, int *infoSize ); extern void IODTFreeLoaderInfo( char *key, void *infoAddr, int infoSize ); /* operates on 32 bit segments */ extern void OSRuntimeUnloadCPPForSegment(struct segment_command * segment); }; /***** * At startup these function pointers are set to use the libsa in-kernel * linker for recording and loading kmods. Once the root filesystem * is available, the kmod_load_function pointer gets switched to point * at the kmod_load_extension() function built into the kernel, and the * others are set to zero. Those two functions must *always* be checked * before being invoked. */ extern "C" { kern_return_t (*kmod_load_function)(char *extension_name) = &kmod_load_extension; bool (*record_startup_extensions_function)(void) = 0; bool (*add_from_mkext_function)(OSData * mkext) = 0; void (*remove_startup_extension_function)(const char * name) = 0; }; /***** * A few parts of IOCatalogue require knowledge of * whether the in-kernel linker is present. This * variable is set by libsa's bootstrap code. */ int kernelLinkerPresent = 0; #define kModuleKey "CFBundleIdentifier" #define super OSObject OSDefineMetaClassAndStructors(IOCatalogue, OSObject) #define CATALOGTEST 0 IOCatalogue * gIOCatalogue; const OSSymbol * gIOClassKey; const OSSymbol * gIOProbeScoreKey; const OSSymbol * gIOModuleIdentifierKey; OSSet * gIOCatalogModuleRequests; OSSet * gIOCatalogCacheMisses; OSSet * gIOCatalogROMMkexts; IOLock * gIOCatalogLock; IOLock * gIOKLDLock; /********************************************************************* *********************************************************************/ OSArray * gIOPrelinkedModules = 0; extern "C" kern_return_t kmod_create_internal( kmod_info_t *info, kmod_t *id); extern "C" kern_return_t kmod_destroy_internal(kmod_t id); extern "C" kern_return_t kmod_start_or_stop( kmod_t id, int start, kmod_args_t *data, mach_msg_type_number_t *dataCount); extern "C" kern_return_t kmod_retain(kmod_t id); extern "C" kern_return_t kmod_release(kmod_t id); #if CONFIG_MACF_KEXT /* MAC Framework support */ /* * define IOC_DEBUG to display run-time debugging information * #define IOC_DEBUG 1 */ #ifdef IOC_DEBUG #define DPRINTF(x) printf x #else #define IOC_DEBUG #define DPRINTF(x) #endif static bool primitive_type(OSObject *obj) { const OSMetaClass *typeID; typeID = OSTypeIDInst(obj); if (typeID == OSTypeID(OSString) || typeID == OSTypeID(OSNumber) || typeID == OSTypeID(OSBoolean) || typeID == OSTypeID(OSData)) return(true); else return(false); } static int primitive_type_length(OSObject *obj) { const OSMetaClass *typeID; int len; typeID = OSTypeIDInst(obj); if (typeID == OSTypeID(OSString)) { OSString * stringObj = OSDynamicCast(OSString, obj); len = stringObj->getLength() + 1; } else if (typeID == OSTypeID(OSNumber)) { len = sizeof("4294967295"); /* UINT32_MAX */ } else if (typeID == OSTypeID(OSBoolean)) { OSBoolean * boolObj = OSDynamicCast(OSBoolean, obj); len = boolObj->isTrue() ? sizeof("true") : sizeof("false"); } else if (typeID == OSTypeID(OSData)) { OSData * dataObj = OSDynamicCast(OSData, obj); len = dataObj->getLength(); } else { len = 0; } return(len); } static void primitive_type_collect(struct mac_module_data_element *element, OSObject *value) { const OSMetaClass *typeID; typeID = OSTypeIDInst(value); if (typeID == OSTypeID(OSString)) { OSString *stringObj = OSDynamicCast(OSString, value); element->value_type = MAC_DATA_TYPE_PRIMITIVE; element->value_size = stringObj->getLength() + 1; DPRINTF(("osdict: string %s size %d\n", stringObj->getCStringNoCopy(), element->value_size)); memcpy(element->value, stringObj->getCStringNoCopy(), element->value_size); } else if (typeID == OSTypeID(OSNumber)) { OSNumber *numberObj = OSDynamicCast(OSNumber, value); element->value_type = MAC_DATA_TYPE_PRIMITIVE; element->value_size = sprintf(element->value, "%u", numberObj->unsigned32BitValue()) + 1; } else if (typeID == OSTypeID(OSBoolean)) { OSBoolean *boolObj = OSDynamicCast(OSBoolean, value); element->value_type = MAC_DATA_TYPE_PRIMITIVE; if (boolObj->isTrue()) { strcpy(element->value, "true"); element->value_size = 5; } else { strcpy(element->value, "false"); element->value_size = 6; } } else if (typeID == OSTypeID(OSData)) { OSData *dataObj = OSDynamicCast(OSData, value); element->value_type = MAC_DATA_TYPE_PRIMITIVE; element->value_size = dataObj->getLength(); DPRINTF(("osdict: data size %d\n", dataObj->getLength())); memcpy(element->value, dataObj->getBytesNoCopy(), element->value_size); } } /********************************************************************* * This function takes an OSDictionary and returns a struct mac_module_data * list. *********************************************************************/ struct mac_module_data * osdict_encode(OSDictionary *dict) { const OSMetaClass * typeID; // don't release OSString * key = NULL; // don't release OSCollectionIterator * keyIterator = 0; // must release struct mac_module_data * module_data = 0; struct mac_module_data_element * element; unsigned int strtabsize = 0; unsigned int listtabsize = 0; unsigned int dicttabsize = 0; unsigned int nkeys = 0; unsigned int datalen; char *strtab = NULL; char *listtab = NULL; char *dicttab = NULL; vm_offset_t data_addr; keyIterator = OSCollectionIterator::withCollection(dict); if (!keyIterator) goto finish; /* Iterate over OSModuleData to figure out total size */ while ( (key = OSDynamicCast(OSString, keyIterator->getNextObject())) ) { // Get the key's value and determine its type OSObject * value = dict->getObject(key); if (!value) continue; typeID = OSTypeIDInst(value); if (primitive_type(value)) { strtabsize += primitive_type_length(value); } else if (typeID == OSTypeID(OSArray)) { unsigned int k, cnt, nents; OSArray *arrayObj = OSDynamicCast(OSArray, value); nents = 0; cnt = arrayObj->getCount(); for (k = 0; k < cnt; k++) { value = arrayObj->getObject(k); typeID = OSTypeIDInst(value); if (primitive_type(value)) { listtabsize += primitive_type_length(value); nents++; } else if (typeID == OSTypeID(OSDictionary)) { unsigned int dents; OSDictionary *dictObj; OSString *dictkey; OSCollectionIterator *dictIterator; dents = 0; dictObj = OSDynamicCast(OSDictionary, value); dictIterator = OSCollectionIterator::withCollection(dictObj); if (!dictIterator) goto finish; while ((dictkey = OSDynamicCast(OSString, dictIterator->getNextObject()))) { OSObject *dictvalue; dictvalue = dictObj->getObject(dictkey); if (!dictvalue) continue; if (primitive_type(dictvalue)) { strtabsize += primitive_type_length(dictvalue); } else { continue; /* Only handle primitive types here. */ } /* * Allow for the "arraynnn/" prefix in the key length. */ strtabsize += dictkey->getLength() + 1; dents++; } dictIterator->release(); if (dents-- > 0) { dicttabsize += sizeof(struct mac_module_data_list) + dents * sizeof(struct mac_module_data_element); nents++; } } else { continue; /* Skip everything else. */ } } if (nents == 0) continue; listtabsize += sizeof(struct mac_module_data_list) + (nents - 1) * sizeof(struct mac_module_data_element); } else { continue; /* skip anything else */ } strtabsize += key->getLength() + 1; nkeys++; } if (nkeys == 0) goto finish; /* * Allocate and fill in the module data structures. */ datalen = sizeof(struct mac_module_data) + sizeof(mac_module_data_element) * (nkeys - 1) + strtabsize + listtabsize + dicttabsize; DPRINTF(("osdict: datalen %d strtabsize %d listtabsize %d dicttabsize %d\n", datalen, strtabsize, listtabsize, dicttabsize)); if (kmem_alloc(kernel_map, &data_addr, datalen) != KERN_SUCCESS) goto finish; module_data = (mac_module_data *)data_addr; module_data->base_addr = data_addr; module_data->size = datalen; module_data->count = nkeys; strtab = (char *)&module_data->data[nkeys]; listtab = strtab + strtabsize; dicttab = listtab + listtabsize; DPRINTF(("osdict: data_addr %p strtab %p listtab %p dicttab %p end %p\n", data_addr, strtab, listtab, dicttab, data_addr + datalen)); keyIterator->reset(); nkeys = 0; element = &module_data->data[0]; DPRINTF(("osdict: element %p\n", element)); while ( (key = OSDynamicCast(OSString, keyIterator->getNextObject())) ) { // Get the key's value and determine its type OSObject * value = dict->getObject(key); if (!value) continue; /* Store key */ DPRINTF(("osdict: element @%p\n", element)); element->key = strtab; element->key_size = key->getLength() + 1; DPRINTF(("osdict: key %s size %d @%p\n", key->getCStringNoCopy(), element->key_size, strtab)); memcpy(element->key, key->getCStringNoCopy(), element->key_size); typeID = OSTypeIDInst(value); if (primitive_type(value)) { /* Store value */ element->value = element->key + element->key_size; DPRINTF(("osdict: primitive element value %p\n", element->value)); primitive_type_collect(element, value); strtab += element->key_size + element->value_size; DPRINTF(("osdict: new strtab %p\n", strtab)); } else if (typeID == OSTypeID(OSArray)) { unsigned int k, cnt, nents; char *astrtab; struct mac_module_data_list *arrayhd; struct mac_module_data_element *ele; OSArray *arrayObj = OSDynamicCast(OSArray, value); element->value = listtab; DPRINTF(("osdict: array element value %p\n", element->value)); element->value_type = MAC_DATA_TYPE_ARRAY; arrayhd = (struct mac_module_data_list *)element->value; arrayhd->type = 0; DPRINTF(("osdict: arrayhd %p\n", arrayhd)); nents = 0; astrtab = strtab + element->key_size; ele = &(arrayhd->list[0]); cnt = arrayObj->getCount(); for (k = 0; k < cnt; k++) { value = arrayObj->getObject(k); DPRINTF(("osdict: array ele %d @%p\n", nents, ele)); ele->key = NULL; ele->key_size = 0; typeID = OSTypeIDInst(value); if (primitive_type(value)) { if (arrayhd->type != 0 && arrayhd->type != MAC_DATA_TYPE_PRIMITIVE) continue; arrayhd->type = MAC_DATA_TYPE_PRIMITIVE; ele->value = astrtab; primitive_type_collect(ele, value); astrtab += ele->value_size; DPRINTF(("osdict: array new astrtab %p\n", astrtab)); } else if (typeID == OSTypeID(OSDictionary)) { unsigned int dents; char *dstrtab; OSDictionary *dictObj; OSString *dictkey; OSCollectionIterator *dictIterator; struct mac_module_data_list *dicthd; struct mac_module_data_element *dele; if (arrayhd->type != 0 && arrayhd->type != MAC_DATA_TYPE_DICT) continue; dictObj = OSDynamicCast(OSDictionary, value); dictIterator = OSCollectionIterator::withCollection(dictObj); if (!dictIterator) goto finish; DPRINTF(("osdict: dict\n")); ele->value = dicttab; ele->value_type = MAC_DATA_TYPE_DICT; dicthd = (struct mac_module_data_list *)ele->value; DPRINTF(("osdict: dicthd %p\n", dicthd)); dstrtab = astrtab; dents = 0; while ((dictkey = OSDynamicCast(OSString, dictIterator->getNextObject()))) { OSObject *dictvalue; dictvalue = dictObj->getObject(dictkey); if (!dictvalue) continue; dele = &(dicthd->list[dents]); DPRINTF(("osdict: dict ele %d @%p\n", dents, dele)); if (primitive_type(dictvalue)) { dele->key = dstrtab; dele->key_size = dictkey->getLength() + 1; DPRINTF(("osdict: dictkey %s size %d @%p\n", dictkey->getCStringNoCopy(), dictkey->getLength(), dstrtab)); memcpy(dele->key, dictkey->getCStringNoCopy(), dele->key_size); dele->value = dele->key + dele->key_size; primitive_type_collect(dele, dictvalue); dstrtab += dele->key_size + dele->value_size; DPRINTF(("osdict: dict new dstrtab %p\n", dstrtab)); } else { continue; /* Only handle primitive types here. */ } dents++; } dictIterator->release(); if (dents == 0) continue; arrayhd->type = MAC_DATA_TYPE_DICT; ele->value_size = sizeof(struct mac_module_data_list) + (dents - 1) * sizeof(struct mac_module_data_element); DPRINTF(("osdict: dict ele size %d ents %d\n", ele->value_size, dents)); dicttab += ele->value_size; DPRINTF(("osdict: new dicttab %p\n", dicttab)); dicthd->count = dents; astrtab = dstrtab; } else { continue; /* Skip everything else. */ } nents++; ele++; } if (nents == 0) continue; element->value_size = sizeof(struct mac_module_data_list) + (nents - 1) * sizeof(struct mac_module_data_element); listtab += element->value_size; DPRINTF(("osdict: new listtab %p\n", listtab)); arrayhd->count = nents; strtab = astrtab; DPRINTF(("osdict: new strtab %p\n", strtab)); } else { continue; /* skip anything else */ } element++; } DPRINTF(("module_data list @%p, key %p value %p\n", module_data, module_data->data[0].key, module_data->data[0].value)); finish: if (keyIterator) keyIterator->release(); return(module_data); } /********************************************************************* * This function takes a plist and looks for an OSModuleData dictionary. * If it is found, an encoded copy is returned. *********************************************************************/ kmod_args_t get_module_data(OSDictionary * kextPlist, mach_msg_type_number_t * datalen) { OSDictionary * kextModuleData = 0; // don't release struct mac_module_data * module_data = 0; vm_map_copy_t copy = 0; kextModuleData = OSDynamicCast(OSDictionary, kextPlist->getObject("OSModuleData")); if (!kextModuleData) goto finish; module_data = osdict_encode(kextModuleData); if (!module_data) goto finish; *datalen = module_data->size; /* * Make a CoW copy of data and free the original. The copy is * consumed by a call to vm_map_copyout() in kmod_start_or_stop(). */ vm_map_copyin(kernel_map, (vm_offset_t)module_data, *datalen, FALSE, ©); kmem_free(kernel_map, (vm_offset_t)module_data, *datalen); DPRINTF(("get_module_data: copy @ %p\n", copy)); finish: return (kmod_args_t)copy; } #endif /* MAC */ static kern_return_t start_prelink_module(UInt32 moduleIndex) { kern_return_t kr = KERN_SUCCESS; UInt32 * togo; SInt32 count, where, end; UInt32 * prelink; SInt32 next, lastDep; OSData * data; OSString * str; OSDictionary * dict; OSArray * prelinkedModules = gIOPrelinkedModules; togo = IONew(UInt32, prelinkedModules->getCount()); togo[0] = moduleIndex; count = 1; for (next = 0; next < count; next++) { dict = (OSDictionary *) prelinkedModules->getObject(togo[next]); data = OSDynamicCast(OSData, dict->getObject("OSBundlePrelink")); if (!data) { // already started or no code if (togo[next] == moduleIndex) { kr = KERN_FAILURE; break; } continue; } prelink = (UInt32 *) data->getBytesNoCopy(); lastDep = OSReadBigInt32(prelink, 12); for (SInt32 idx = OSReadBigInt32(prelink, 8); idx < lastDep; idx += sizeof(UInt32)) { UInt32 depIdx = OSReadBigInt32(prelink, idx) - 1; for (where = next + 1; (where < count) && (togo[where] > depIdx); where++) {} if (where != count) { if (togo[where] == depIdx) continue; for (end = count; end != where; end--) togo[end] = togo[end - 1]; } count++; togo[where] = depIdx; } } if (KERN_SUCCESS != kr) return kr; for (next = (count - 1); next >= 0; next--) { dict = (OSDictionary *) prelinkedModules->getObject(togo[next]); data = OSDynamicCast(OSData, dict->getObject("OSBundlePrelink")); if (!data) continue; prelink = (UInt32 *) data->getBytesNoCopy(); kmod_t id; kmod_info_t * kmod_info = (kmod_info_t *) OSReadBigInt32(prelink, 0); kr = kmod_create_internal(kmod_info, &id); if (KERN_SUCCESS != kr) break; lastDep = OSReadBigInt32(prelink, 12); for (SInt32 idx = OSReadBigInt32(prelink, 8); idx < lastDep; idx += sizeof(UInt32)) { OSDictionary * depDict; kmod_info_t * depInfo; depDict = (OSDictionary *) prelinkedModules->getObject(OSReadBigInt32(prelink, idx) - 1); str = OSDynamicCast(OSString, depDict->getObject(kModuleKey)); depInfo = kmod_lookupbyname_locked(str->getCStringNoCopy()); if (depInfo) { kr = kmod_retain(KMOD_PACK_IDS(id, depInfo->id)); kfree(depInfo, sizeof(kmod_info_t)); } else IOLog("%s: NO DEP %s\n", kmod_info->name, str->getCStringNoCopy()); } dict->removeObject("OSBundlePrelink"); if (kmod_info->start) kr = kmod_start_or_stop(kmod_info->id, 1, 0, 0); } IODelete(togo, UInt32, prelinkedModules->getCount()); return kr; } /********************************************************************* * This is a function that IOCatalogue calls in order to load a kmod. *********************************************************************/ static kern_return_t kmod_load_from_cache_sym(const OSSymbol * kmod_name) { OSArray * prelinkedModules = gIOPrelinkedModules; kern_return_t result = KERN_FAILURE; OSDictionary * dict; OSObject * ident; UInt32 idx; if (!gIOPrelinkedModules) return KERN_FAILURE; for (idx = 0; (dict = (OSDictionary *) prelinkedModules->getObject(idx)); idx++) { if ((ident = dict->getObject(kModuleKey)) && kmod_name->isEqualTo(ident)) break; } if (dict) { if (kernelLinkerPresent && dict->getObject("OSBundleDefer")) { kmod_load_extension((char *) kmod_name->getCStringNoCopy()); result = kIOReturnOffline; } else result = start_prelink_module(idx); } return result; } extern "C" Boolean kmod_load_request(const char * moduleName, Boolean make_request) { bool ret, cacheMiss = false; kern_return_t kr; const OSSymbol * sym = 0; kmod_info_t * kmod_info; if (!moduleName) return false; /* To make sure this operation completes even if a bad extension needs * to be removed, take the kld lock for this whole block, spanning the * kmod_load_function() and remove_startup_extension_function() calls. */ IOLockLock(gIOKLDLock); do { // Is the module already loaded? ret = (0 != (kmod_info = kmod_lookupbyname_locked((char *)moduleName))); if (ret) { kfree(kmod_info, sizeof(kmod_info_t)); break; } sym = OSSymbol::withCString(moduleName); if (!sym) { ret = false; break; } kr = kmod_load_from_cache_sym(sym); ret = (kIOReturnSuccess == kr); cacheMiss = !ret; if (ret || !make_request || (kr == kIOReturnOffline)) break; // If the module hasn't been loaded, then load it. if (!kmod_load_function) { IOLog("IOCatalogue: %s cannot be loaded " "(kmod load function not set).\n", moduleName); ret = true; break; } kr = kmod_load_function((char *)moduleName); if (ret != kIOReturnSuccess) { IOLog("IOCatalogue: %s cannot be loaded.\n", moduleName); /* If the extension couldn't be loaded this time, * make it unavailable so that no more requests are * made in vain. This also enables other matching * extensions to have a chance. */ if (kernelLinkerPresent && remove_startup_extension_function) { (*remove_startup_extension_function)(moduleName); } ret = false; } else if (kernelLinkerPresent) { // If kern linker is here, the driver is actually loaded, // so return true. ret = true; } else { // kern linker isn't here, a request has been queued // but the module isn't necessarily loaded yet, so stall. ret = false; } } while (false); IOLockUnlock(gIOKLDLock); if (sym) { IOLockLock(gIOCatalogLock); gIOCatalogModuleRequests->setObject(sym); if (cacheMiss) gIOCatalogCacheMisses->setObject(sym); IOLockUnlock(gIOCatalogLock); } return ret; } extern "C" kern_return_t kmod_unload_cache(void) { OSArray * prelinkedModules = gIOPrelinkedModules; kern_return_t result = KERN_FAILURE; OSDictionary * dict; UInt32 idx; UInt32 * prelink; OSData * data; if (!gIOPrelinkedModules) return KERN_SUCCESS; IOLockLock(gIOKLDLock); for (idx = 0; (dict = (OSDictionary *) prelinkedModules->getObject(idx)); idx++) { data = OSDynamicCast(OSData, dict->getObject("OSBundlePrelink")); if (!data) continue; prelink = (UInt32 *) data->getBytesNoCopy(); kmod_info_t * kmod_info = (kmod_info_t *) OSReadBigInt32(prelink, 0); vm_offset_t virt = ml_static_ptovirt(kmod_info->address); if( virt) { ml_static_mfree(virt, kmod_info->size); } } gIOPrelinkedModules->release(); gIOPrelinkedModules = 0; IOLockUnlock(gIOKLDLock); return result; } extern "C" kern_return_t kmod_load_from_cache(const char * kmod_name) { kern_return_t kr; const OSSymbol * sym = OSSymbol::withCStringNoCopy(kmod_name); if (sym) { kr = kmod_load_from_cache_sym(sym); sym->release(); } else kr = kIOReturnNoMemory; return kr; } /********************************************************************* *********************************************************************/ static void UniqueProperties( OSDictionary * dict ) { OSString * data; data = OSDynamicCast( OSString, dict->getObject( gIOClassKey )); if( data) { const OSSymbol *classSymbol = OSSymbol::withString(data); dict->setObject( gIOClassKey, (OSSymbol *) classSymbol); classSymbol->release(); } data = OSDynamicCast( OSString, dict->getObject( gIOMatchCategoryKey )); if( data) { const OSSymbol *classSymbol = OSSymbol::withString(data); dict->setObject( gIOMatchCategoryKey, (OSSymbol *) classSymbol); classSymbol->release(); } } void IOCatalogue::initialize( void ) { OSArray * array; OSString * errorString; bool rc; extern const char * gIOKernelConfigTables; array = OSDynamicCast(OSArray, OSUnserialize(gIOKernelConfigTables, &errorString)); if (!array && errorString) { IOLog("KernelConfigTables syntax error: %s\n", errorString->getCStringNoCopy()); errorString->release(); } gIOClassKey = OSSymbol::withCStringNoCopy( kIOClassKey ); gIOProbeScoreKey = OSSymbol::withCStringNoCopy( kIOProbeScoreKey ); gIOModuleIdentifierKey = OSSymbol::withCStringNoCopy( kModuleKey ); gIOCatalogModuleRequests = OSSet::withCapacity(16); gIOCatalogCacheMisses = OSSet::withCapacity(16); gIOCatalogROMMkexts = OSSet::withCapacity(4); assert( array && gIOClassKey && gIOProbeScoreKey && gIOModuleIdentifierKey && gIOCatalogModuleRequests); gIOCatalogue = new IOCatalogue; assert(gIOCatalogue); rc = gIOCatalogue->init(array); assert(rc); array->release(); } // Initialize the IOCatalog object. bool IOCatalogue::init(OSArray * initArray) { OSDictionary * dict; if ( !super::init() ) return false; generation = 1; array = initArray; array->retain(); kernelTables = OSCollectionIterator::withCollection( array ); gIOCatalogLock = IOLockAlloc(); gIOKLDLock = IOLockAlloc(); lock = gIOCatalogLock; kld_lock = gIOKLDLock; kernelTables->reset(); while( (dict = (OSDictionary *) kernelTables->getNextObject())) { UniqueProperties(dict); if( 0 == dict->getObject( gIOClassKey )) IOLog("Missing or bad \"%s\" key\n", gIOClassKey->getCStringNoCopy()); } #if CATALOGTEST AbsoluteTime deadline; clock_interval_to_deadline( 1000, kMillisecondScale ); thread_call_func_delayed( ping, this, deadline ); #endif return true; } // Release all resources used by IOCatalogue and deallocate. // This will probably never be called. void IOCatalogue::free( void ) { if ( array ) array->release(); if ( kernelTables ) kernelTables->release(); super::free(); } #if CATALOGTEST static int hackLimit; enum { kDriversPerIter = 4 }; void IOCatalogue::ping( thread_call_param_t arg, thread_call_param_t) { IOCatalogue * self = (IOCatalogue *) arg; OSOrderedSet * set; OSDictionary * table; int newLimit; set = OSOrderedSet::withCapacity( 1 ); IOLockLock( &self->lock ); for( newLimit = 0; newLimit < kDriversPerIter; newLimit++) { table = (OSDictionary *) self->array->getObject( hackLimit + newLimit ); if( table) { set->setLastObject( table ); OSSymbol * sym = (OSSymbol *) table->getObject( gIOClassKey ); kprintf("enabling %s\n", sym->getCStringNoCopy()); } else { newLimit--; break; } } IOService::catalogNewDrivers( set ); hackLimit += newLimit; self->generation++; IOLockUnlock( &self->lock ); if( kDriversPerIter == newLimit) { AbsoluteTime deadline; clock_interval_to_deadline( 500, kMillisecondScale ); thread_call_func_delayed( ping, this, deadline ); } } #endif OSOrderedSet * IOCatalogue::findDrivers( IOService * service, SInt32 * generationCount ) { OSDictionary * nextTable; OSOrderedSet * set; OSString * imports; set = OSOrderedSet::withCapacity( 1, IOServiceOrdering, (void *)gIOProbeScoreKey ); if( !set ) return( 0 ); IOLockLock( lock ); kernelTables->reset(); #if CATALOGTEST int hackIndex = 0; #endif while( (nextTable = (OSDictionary *) kernelTables->getNextObject())) { #if CATALOGTEST if( hackIndex++ > hackLimit) break; #endif imports = OSDynamicCast( OSString, nextTable->getObject( gIOProviderClassKey )); if( imports && service->metaCast( imports )) set->setObject( nextTable ); } *generationCount = getGenerationCount(); IOLockUnlock( lock ); return( set ); } // Is personality already in the catalog? OSOrderedSet * IOCatalogue::findDrivers( OSDictionary * matching, SInt32 * generationCount) { OSDictionary * dict; OSOrderedSet * set; UniqueProperties(matching); set = OSOrderedSet::withCapacity( 1, IOServiceOrdering, (void *)gIOProbeScoreKey ); IOLockLock( lock ); kernelTables->reset(); while ( (dict = (OSDictionary *) kernelTables->getNextObject()) ) { /* This comparison must be done with only the keys in the * "matching" dict to enable general searches. */ if ( dict->isEqualTo(matching, matching) ) set->setObject(dict); } *generationCount = getGenerationCount(); IOLockUnlock( lock ); return set; } // Add a new personality to the set if it has a unique IOResourceMatchKey value. // XXX -- svail: This should be optimized. // esb - There doesn't seem like any reason to do this - it causes problems // esb - when there are more than one loadable driver matching on the same provider class static void AddNewImports( OSOrderedSet * set, OSDictionary * dict ) { set->setObject(dict); } // Add driver config tables to catalog and start matching process. bool IOCatalogue::addDrivers(OSArray * drivers, bool doNubMatching ) { OSCollectionIterator * iter; OSDictionary * dict; OSOrderedSet * set; OSArray * persons; OSString * moduleName; bool ret; ret = true; persons = OSDynamicCast(OSArray, drivers); if ( !persons ) return false; iter = OSCollectionIterator::withCollection( persons ); if (!iter ) return false; set = OSOrderedSet::withCapacity( 10, IOServiceOrdering, (void *)gIOProbeScoreKey ); if ( !set ) { iter->release(); return false; } IOLockLock( lock ); while ( (dict = (OSDictionary *) iter->getNextObject()) ) { if ((moduleName = OSDynamicCast(OSString, dict->getObject("OSBundleModuleDemand")))) { IOLockUnlock( lock ); ret = kmod_load_request(moduleName->getCStringNoCopy(), false); IOLockLock( lock ); ret = true; } else { SInt count; UniqueProperties( dict ); // Add driver personality to catalogue. count = array->getCount(); while ( count-- ) { OSDictionary * driver; // Be sure not to double up on personalities. driver = (OSDictionary *)array->getObject(count); /* Unlike in other functions, this comparison must be exact! * The catalogue must be able to contain personalities that * are proper supersets of others. * Do not compare just the properties present in one driver * pesonality or the other. */ if (dict->isEqualTo(driver)) break; } if (count >= 0) // its a dup continue; ret = array->setObject( dict ); if (!ret) break; AddNewImports( set, dict ); } } // Start device matching. if (doNubMatching && (set->getCount() > 0)) { IOService::catalogNewDrivers( set ); generation++; } IOLockUnlock( lock ); set->release(); iter->release(); return ret; } // Remove drivers from the catalog which match the // properties in the matching dictionary. bool IOCatalogue::removeDrivers( OSDictionary * matching, bool doNubMatching) { OSCollectionIterator * tables; OSDictionary * dict; OSOrderedSet * set; OSArray * arrayCopy; if ( !matching ) return false; set = OSOrderedSet::withCapacity(10, IOServiceOrdering, (void *)gIOProbeScoreKey); if ( !set ) return false; arrayCopy = OSArray::withCapacity(100); if ( !arrayCopy ) { set->release(); return false; } tables = OSCollectionIterator::withCollection(arrayCopy); arrayCopy->release(); if ( !tables ) { set->release(); return false; } UniqueProperties( matching ); IOLockLock( lock ); kernelTables->reset(); arrayCopy->merge(array); array->flushCollection(); tables->reset(); while ( (dict = (OSDictionary *)tables->getNextObject()) ) { /* This comparison must be done with only the keys in the * "matching" dict to enable general searches. */ if ( dict->isEqualTo(matching, matching) ) { AddNewImports( set, dict ); continue; } array->setObject(dict); } // Start device matching. if ( doNubMatching && (set->getCount() > 0) ) { IOService::catalogNewDrivers(set); generation++; } IOLockUnlock( lock ); set->release(); tables->release(); return true; } // Return the generation count. SInt32 IOCatalogue::getGenerationCount( void ) const { return( generation ); } bool IOCatalogue::isModuleLoaded( OSString * moduleName ) const { return isModuleLoaded(moduleName->getCStringNoCopy()); } bool IOCatalogue::isModuleLoaded( const char * moduleName ) const { return (kmod_load_request(moduleName, true)); } // Check to see if module has been loaded already. bool IOCatalogue::isModuleLoaded( OSDictionary * driver ) const { OSString * moduleName = NULL; if ( !driver ) return false; moduleName = OSDynamicCast(OSString, driver->getObject(gIOModuleIdentifierKey)); if ( moduleName ) return isModuleLoaded(moduleName); /* If a personality doesn't hold the "CFBundleIdentifier" key * it is assumed to be an "in-kernel" driver. */ return true; } // This function is called after a module has been loaded. void IOCatalogue::moduleHasLoaded( OSString * moduleName ) { OSDictionary * dict; dict = OSDictionary::withCapacity(2); dict->setObject(gIOModuleIdentifierKey, moduleName); startMatching(dict); dict->release(); } void IOCatalogue::moduleHasLoaded( const char * moduleName ) { OSString * name; name = OSString::withCString(moduleName); moduleHasLoaded(name); name->release(); } IOReturn IOCatalogue::unloadModule( OSString * moduleName ) const { kmod_info_t * k_info = 0; kern_return_t ret; const char * name; ret = kIOReturnBadArgument; if ( moduleName ) { name = moduleName->getCStringNoCopy(); k_info = kmod_lookupbyname_locked((char *)name); if ( k_info && (k_info->reference_count < 1) ) { if ( k_info->stop && !((ret = k_info->stop(k_info, 0)) == kIOReturnSuccess) ) { kfree(k_info, sizeof(kmod_info_t)); return ret; } ret = kmod_destroy(host_priv_self(), k_info->id); } } if (k_info) { kfree(k_info, sizeof(kmod_info_t)); } return ret; } static IOReturn _terminateDrivers( OSDictionary * matching ) { OSDictionary * dict; OSIterator * iter; IOService * service; IOReturn ret; if ( !matching ) return kIOReturnBadArgument; ret = kIOReturnSuccess; dict = 0; iter = IORegistryIterator::iterateOver(gIOServicePlane, kIORegistryIterateRecursively); if ( !iter ) return kIOReturnNoMemory; UniqueProperties( matching ); // terminate instances. do { iter->reset(); while( (service = (IOService *)iter->getNextObject()) ) { dict = service->getPropertyTable(); if ( !dict ) continue; /* Terminate only for personalities that match the matching dictionary. * This comparison must be done with only the keys in the * "matching" dict to enable general matching. */ if ( !dict->isEqualTo(matching, matching) ) continue; if ( !service->terminate(kIOServiceRequired|kIOServiceSynchronous) ) { ret = kIOReturnUnsupported; break; } } } while( !service && !iter->isValid()); iter->release(); return ret; } static IOReturn _removeDrivers( OSArray * array, OSDictionary * matching ) { OSCollectionIterator * tables; OSDictionary * dict; OSArray * arrayCopy; IOReturn ret = kIOReturnSuccess; // remove configs from catalog. arrayCopy = OSArray::withCapacity(100); if ( !arrayCopy ) return kIOReturnNoMemory; tables = OSCollectionIterator::withCollection(arrayCopy); arrayCopy->release(); if ( !tables ) return kIOReturnNoMemory; arrayCopy->merge(array); array->flushCollection(); tables->reset(); while ( (dict = (OSDictionary *)tables->getNextObject()) ) { /* Remove from the catalogue's array any personalities * that match the matching dictionary. * This comparison must be done with only the keys in the * "matching" dict to enable general matching. */ if ( dict->isEqualTo(matching, matching) ) continue; array->setObject(dict); } tables->release(); return ret; } IOReturn IOCatalogue::terminateDrivers( OSDictionary * matching ) { IOReturn ret; ret = _terminateDrivers(matching); IOLockLock( lock ); if (kIOReturnSuccess == ret) ret = _removeDrivers(array, matching); kernelTables->reset(); IOLockUnlock( lock ); return ret; } IOReturn IOCatalogue::terminateDriversForModule( OSString * moduleName, bool unload ) { IOReturn ret; OSDictionary * dict; dict = OSDictionary::withCapacity(1); if ( !dict ) return kIOReturnNoMemory; dict->setObject(gIOModuleIdentifierKey, moduleName); ret = _terminateDrivers(dict); IOLockLock( lock ); if (kIOReturnSuccess == ret) ret = _removeDrivers(array, dict); kernelTables->reset(); // Unload the module itself. if ( unload && ret == kIOReturnSuccess ) { // Do kmod stop first. ret = unloadModule(moduleName); } IOLockUnlock( lock ); dict->release(); return ret; } IOReturn IOCatalogue::terminateDriversForModule( const char * moduleName, bool unload ) { OSString * name; IOReturn ret; name = OSString::withCString(moduleName); if ( !name ) return kIOReturnNoMemory; ret = terminateDriversForModule(name, unload); name->release(); return ret; } bool IOCatalogue::startMatching( OSDictionary * matching ) { OSDictionary * dict; OSOrderedSet * set; if ( !matching ) return false; set = OSOrderedSet::withCapacity(10, IOServiceOrdering, (void *)gIOProbeScoreKey); if ( !set ) return false; IOLockLock( lock ); kernelTables->reset(); while ( (dict = (OSDictionary *)kernelTables->getNextObject()) ) { /* This comparison must be done with only the keys in the * "matching" dict to enable general matching. */ if ( dict->isEqualTo(matching, matching) ) AddNewImports(set, dict); } // Start device matching. if ( set->getCount() > 0 ) { IOService::catalogNewDrivers(set); generation++; } IOLockUnlock( lock ); set->release(); return true; } void IOCatalogue::reset(void) { IOLog("Resetting IOCatalogue.\n"); } bool IOCatalogue::serialize(OSSerialize * s) const { if ( !s ) return false; return super::serialize(s); } bool IOCatalogue::serializeData(IOOptionBits kind, OSSerialize * s) const { kern_return_t kr = kIOReturnSuccess; switch ( kind ) { case kIOCatalogGetContents: if (!array->serialize(s)) kr = kIOReturnNoMemory; break; case kIOCatalogGetModuleDemandList: IOLockLock( lock ); if (!gIOCatalogModuleRequests->serialize(s)) kr = kIOReturnNoMemory; IOLockUnlock( lock ); break; case kIOCatalogGetCacheMissList: IOLockLock( lock ); if (!gIOCatalogCacheMisses->serialize(s)) kr = kIOReturnNoMemory; IOLockUnlock( lock ); break; case kIOCatalogGetROMMkextList: IOLockLock( lock ); if (!gIOCatalogROMMkexts || !gIOCatalogROMMkexts->getCount()) kr = kIOReturnNoResources; else if (!gIOCatalogROMMkexts->serialize(s)) kr = kIOReturnNoMemory; if (gIOCatalogROMMkexts) { gIOCatalogROMMkexts->release(); gIOCatalogROMMkexts = 0; } IOLockUnlock( lock ); break; default: kr = kIOReturnBadArgument; break; } return kr; } bool IOCatalogue::recordStartupExtensions(void) { bool result = false; IOLockLock(kld_lock); if (kernelLinkerPresent && record_startup_extensions_function) { result = (*record_startup_extensions_function)(); } else { IOLog("Can't record startup extensions; " "kernel linker is not present.\n"); result = false; } IOLockUnlock(kld_lock); return result; } /********************************************************************* * This function operates on sections retrieved from the currently running * 32 bit mach kernel. *********************************************************************/ bool IOCatalogue::addExtensionsFromArchive(OSData * mkext) { OSData * copyData; bool result = false; bool prelinked; /* The mkext we've been handed (or the data it references) can go away, * so we need to make a local copy to keep around as long as it might * be needed. */ copyData = OSData::withData(mkext); if (copyData) { struct section * infosect; infosect = getsectbyname("__PRELINK", "__info"); prelinked = (infosect && infosect->addr && infosect->size); IOLockLock(kld_lock); if (gIOCatalogROMMkexts) gIOCatalogROMMkexts->setObject(copyData); if (prelinked) { result = true; } else if (kernelLinkerPresent && add_from_mkext_function) { result = (*add_from_mkext_function)(copyData); } else { IOLog("Can't add startup extensions from archive; " "kernel linker is not present.\n"); result = false; } IOLockUnlock(kld_lock); copyData->release(); } return result; } /********************************************************************* * This function clears out all references to the in-kernel linker, * frees the list of startup extensions in extensionDict, and * deallocates the kernel's __KLD segment to reclaim that memory. * * The segments it operates on are strictly 32 bit segments. *********************************************************************/ kern_return_t IOCatalogue::removeKernelLinker(void) { kern_return_t result = KERN_SUCCESS; struct segment_command * segmentLE, *segmentKLD; boolean_t keepsyms = FALSE; #if __ppc__ || __arm__ char * dt_segment_name; void * segment_paddress; int segment_size; #endif /* This must be the very first thing done by this function. */ IOLockLock(kld_lock); /* If the kernel linker isn't here, that's automatically * a success. */ if (!kernelLinkerPresent) { result = KERN_SUCCESS; goto finish; } PE_parse_boot_arg("keepsyms", &keepsyms); IOLog("Jettisoning kernel linker.\n"); kernelLinkerPresent = 0; /* Set the kmod_load_extension function as the means for loading * a kernel extension. */ kmod_load_function = &kmod_load_extension; record_startup_extensions_function = 0; add_from_mkext_function = 0; remove_startup_extension_function = 0; /* Invoke destructors for the __KLD and __LINKEDIT segments. * Do this for all segments before actually freeing their * memory so that any cross-dependencies (not that there * should be any) are handled. */ segmentKLD = getsegbyname("__KLD"); if (!segmentKLD) { IOLog("error removing kernel linker: can't find __KLD segment\n"); result = KERN_FAILURE; goto finish; } OSRuntimeUnloadCPPForSegment(segmentKLD); #if __ppc__ || __arm__ /* Free the memory that was set up by bootx. */ dt_segment_name = "Kernel-__KLD"; if (0 == IODTGetLoaderInfo(dt_segment_name, &segment_paddress, &segment_size)) { IODTFreeLoaderInfo(dt_segment_name, (void *)segment_paddress, (int)segment_size); } #elif __i386__ /* On x86, use the mapping data from the segment load command to * unload KLD directly, unless the keepsyms boot-arg was enabled. * This may invalidate any assumptions about "avail_start" * defining the lower bound for valid physical addresses. */ if (!keepsyms && segmentKLD->vmaddr && segmentKLD->vmsize) ml_static_mfree(segmentKLD->vmaddr, segmentKLD->vmsize); #else #error arch #endif struct section * sect; sect = getsectbyname("__PRELINK", "__symtab"); if (sect && sect->addr) { ml_static_mfree(sect->addr, sect->size); } finish: /* This must be the very last thing done before returning. */ IOLockUnlock(kld_lock); return result; } /********************************************************************* * This function stops the catalogue from making kextd requests during * shutdown. *********************************************************************/ void IOCatalogue::disableExternalLinker(void) { IOLockLock(gIOKLDLock); /* If kmod_load_extension (the kextd requester function) is in use, * disable new module requests. */ if (kmod_load_function == &kmod_load_extension) { kmod_load_function = NULL; } IOLockUnlock(gIOKLDLock); } extern "C" void jettison_kernel_linker(void) { if (gIOCatalogue != NULL) gIOCatalogue->removeKernelLinker(); } |