-
Notifications
You must be signed in to change notification settings - Fork 3
/
kextutil_main.c
2460 lines (2150 loc) · 86.2 KB
/
kextutil_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* kextutil_main.c
* kext_tools
*
* Created by Nik Gervae on 4/24/08.
* Copyright 2008 Apple Inc. All rights reserved.
*
*/
#include "kextutil_main.h"
#include "kext_tools_util.h"
#include "security.h"
#include "bootcaches.h"
#include <libc.h>
#include <sysexits.h>
#include <IOKit/kext/OSKext.h>
#include <IOKit/kext/OSKextPrivate.h>
#include <IOKit/kext/KextManager.h>
#include <IOKit/kext/KextManagerPriv.h>
#include <IOKit/kext/kextmanager_types.h>
#include <servers/bootstrap.h> // bootstrap mach ports
#include <bootfiles.h>
#pragma mark Constants
/*******************************************************************************
* Constants
*******************************************************************************/
#define BAD_ADDRESS_SPEC "Address format is <bundle-id@address>, " \
"with nonzero hexadecimal address."
#pragma mark Global/Static Variables
/*******************************************************************************
* Global/Static Variables
*******************************************************************************/
const char * progname = "(unknown)";
#define LOCK_MAXTRIES 90
#define LOCK_DELAY 1
static mach_port_t sKextdPort = MACH_PORT_NULL;
static mach_port_t sLockPort = MACH_PORT_NULL; // kext loading lock
static int sLockStatus = 0;
static bool sLockTaken = false;
#pragma mark Main Routine
/*******************************************************************************
* Global variables.
*******************************************************************************/
ExitStatus
main(int argc, char * const * argv)
{
ExitStatus result = EX_SOFTWARE;
ExitStatus processResult = EX_SOFTWARE;
Boolean fatal = false;
CFArrayRef allKexts = NULL; // must release
CFArrayRef kextsToProcess = NULL; // must release
KextutilArgs toolArgs;
/*****
* Find out what the program was invoked as.
*/
progname = rindex(argv[0], '/');
if (progname) {
progname++; // go past the '/'
} else {
progname = (char *)argv[0];
}
/* Set the OSKext log callback right away and hook up to get any
* warnings & errors out of the kernel.
*/
OSKextSetLogOutputFunction(&tool_log);
OSKextSetLogFilter(kOSKextLogWarningLevel | kOSKextLogVerboseFlagsMask,
/* kernel? */ true);
/*****
* Process args & check for permission to load.
*/
result = readArgs(argc, argv, &toolArgs);
if (result != EX_OK) {
if (result == kKextutilExitHelp) {
result = EX_OK;
}
goto finish;
}
result = checkArgs(&toolArgs);
if (result != EX_OK) {
goto finish;
}
/* From here on out the default exit status is ok.
*/
result = EX_OK;
/*****
* Turn on recording of all diagnostics.
*/
OSKextSetRecordsDiagnostics(kOSKextDiagnosticsFlagAll);
/*****
* Create the set of kexts we'll be working from.
*/
allKexts = OSKextCreateKextsFromURLs(kCFAllocatorDefault, toolArgs.scanURLs);
if (!allKexts || !CFArrayGetCount(allKexts)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"No kernel extensions found.");
result = EX_SOFTWARE;
goto finish;
}
if (result != EX_OK) {
goto finish;
}
/*****
* Figure out which kexts we're actually loading/generating symbols for.
* This must be done after the kext objects are created.
*/
result = createKextsToProcess(&toolArgs, &kextsToProcess, &fatal);
if (fatal) {
goto finish;
}
if (!serializeLoad(&toolArgs, toolArgs.doLoad)) {
result = EX_OSERR;
goto finish;
}
processResult = processKexts(kextsToProcess, &toolArgs);
if (result == EX_OK) {
result = processResult;
}
finish:
/*****
* Clean everything up. The mach stuff should be done even though we're
* exiting so we don't mess up other processes.
*/
if (sLockTaken) {
kextmanager_unlock_kextload(sKextdPort, sLockPort);
}
if (sKextdPort != MACH_PORT_NULL) {
mach_port_deallocate(mach_task_self(), sKextdPort);
}
if (sLockPort != MACH_PORT_NULL) {
mach_port_mod_refs(mach_task_self(), sLockPort, MACH_PORT_RIGHT_RECEIVE, -1);
}
/* We're actually not going to free anything else because we're exiting!
*/
exit(result);
SAFE_RELEASE(allKexts);
SAFE_RELEASE(toolArgs.loadAddresses);
SAFE_RELEASE(toolArgs.kextIDs);
SAFE_RELEASE(toolArgs.personalityNames);
SAFE_RELEASE(toolArgs.dependencyURLs);
SAFE_RELEASE(toolArgs.repositoryURLs);
SAFE_RELEASE(toolArgs.kextURLs);
SAFE_RELEASE(toolArgs.scanURLs);
SAFE_RELEASE(toolArgs.kernelURL);
SAFE_RELEASE(toolArgs.kernelFile);
SAFE_RELEASE(toolArgs.symbolDirURL);
return result;
}
#pragma mark Major Subroutines
/*******************************************************************************
* Major Subroutines
*******************************************************************************/
ExitStatus
readArgs(
int argc,
char * const * argv,
KextutilArgs * toolArgs)
{
ExitStatus result = EX_USAGE;
ExitStatus scratchResult = EX_USAGE;
int optchar;
int longindex;
CFStringRef scratchString = NULL; // must release
CFNumberRef scratchNumber = NULL; // must release
CFNumberRef existingAddress = NULL; // do not release
CFURLRef scratchURL = NULL; // must release
uint32_t i;
bzero(toolArgs, sizeof(*toolArgs));
/* Set up default arg values.
*/
toolArgs->useRepositoryCaches = true;
toolArgs->useSystemExtensions = true;
toolArgs->overwriteSymbols = true;
toolArgs->interactiveLevel = kOSKextExcludeNone;
toolArgs->doLoad = true;
toolArgs->doStartMatching = true;
// toolArgs->archInfo must remain NULL before checkArgs
/*****
* Allocate collection objects.
*/
if (!createCFMutableDictionary(&toolArgs->loadAddresses) ||
!createCFMutableArray(&toolArgs->kextIDs, &kCFTypeArrayCallBacks) ||
!createCFMutableArray(&toolArgs->personalityNames, &kCFTypeArrayCallBacks) ||
!createCFMutableArray(&toolArgs->dependencyURLs, &kCFTypeArrayCallBacks) ||
!createCFMutableArray(&toolArgs->repositoryURLs, &kCFTypeArrayCallBacks) ||
!createCFMutableArray(&toolArgs->kextURLs, &kCFTypeArrayCallBacks) ||
!createCFMutableArray(&toolArgs->scanURLs, &kCFTypeArrayCallBacks)) {
result = EX_OSERR;
OSKextLogMemError();
exit(result);
}
while ((optchar = getopt_long_only(argc, (char * const *)argv,
kOptChars, sOptInfo, &longindex)) != -1) {
char * address_string = NULL; // don't free
uint64_t address;
SAFE_RELEASE_NULL(scratchString);
SAFE_RELEASE_NULL(scratchNumber);
SAFE_RELEASE_NULL(scratchURL);
switch (optchar) {
case kOptHelp:
usage(kUsageLevelFull);
result = kKextutilExitHelp;
goto finish;
break;
case kOptBundleIdentifier:
scratchString = CFStringCreateWithCString(kCFAllocatorDefault,
optarg, kCFStringEncodingUTF8);
if (!scratchString) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
CFArrayAppendValue(toolArgs->kextIDs, scratchString);
break;
case kOptPersonality:
scratchString = CFStringCreateWithCString(kCFAllocatorDefault,
optarg, kCFStringEncodingUTF8);
if (!scratchString) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
CFArrayAppendValue(toolArgs->personalityNames, scratchString);
break;
case kOptKernel:
if (toolArgs->kernelURL) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogGeneralFlag,
"Warning: multiple use of -%s (-%c); using last.",
kOptNameKernel, kOptKernel);
SAFE_RELEASE_NULL(toolArgs->kernelURL);
}
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)optarg, strlen(optarg), true);
if (!scratchURL) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
goto finish;
}
toolArgs->kernelURL = CFRetain(scratchURL);
break;
case kOptDependency:
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)optarg, strlen(optarg), true);
if (!scratchURL) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
goto finish;
}
CFArrayAppendValue(toolArgs->dependencyURLs, scratchURL);
break;
case kOptRepository:
scratchResult = checkPath(optarg, /* suffix */ NULL,
/* directoryRequired */ TRUE, /* writableRequired */ FALSE);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)optarg, strlen(optarg), true);
if (!scratchURL) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
goto finish;
}
CFArrayAppendValue(toolArgs->repositoryURLs, scratchURL);
break;
case kOptNoCaches:
toolArgs->useRepositoryCaches = false;
break;
case kOptNoLoadedCheck:
toolArgs->checkLoadedForDependencies = false;
break;
case kOptNoSystemExtensions:
toolArgs->useSystemExtensions = false;
break;
case kOptInteractive:
if (toolArgs->interactiveLevel) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogGeneralFlag,
"Warning: multiple use of -%s (-%c) or -%s (-%c); using last.",
kOptNameInteractive, kOptInteractive,
kOptNameInteractiveAll, kOptInteractiveAll);
}
toolArgs->overwriteSymbols = false;
toolArgs->interactiveLevel = kOSKextExcludeKext;
break;
case kOptInteractiveAll:
if (toolArgs->interactiveLevel) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogGeneralFlag,
"Warning: multiple use of -%s (-%c) or -%s (-%c); using last.",
kOptNameInteractive, kOptInteractive,
kOptNameInteractiveAll, kOptInteractiveAll);
}
toolArgs->overwriteSymbols = false;
toolArgs->interactiveLevel = kOSKextExcludeAll;
break;
case kOptLoadOnly:
toolArgs->flag_l = 1;
break;
case kOptMatchOnly:
toolArgs->flag_m = 1;
break;
case kOptNoLoad:
toolArgs->flag_n = 1;
break;
case kOptSymbolsDirectory:
if (toolArgs->symbolDirURL) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogGeneralFlag,
"Warning: multiple use of -%s (-%c); using last",
kOptNameSymbolsDirectory, kOptSymbolsDirectory);
SAFE_RELEASE_NULL(toolArgs->symbolDirURL);
}
scratchResult = checkPath(optarg, /* suffix */ NULL,
/* directoryRequired? */ TRUE, /* writable? */ TRUE);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)optarg, strlen(optarg), true);
if (!scratchURL) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
goto finish;
}
toolArgs->symbolDirURL = CFRetain(scratchURL);
break;
case kOptAddress:
toolArgs->flag_n = 1; // -a implies -n
address_string = index(optarg, '@');
if (!address_string) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
BAD_ADDRESS_SPEC);
goto finish;
}
address_string[0] = '\0';
address_string++;
/* Read a 64-bit int here; we'll check at load time
* whether the address is too big.
*/
address = strtoull(address_string, NULL, 16);
if (!address) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
BAD_ADDRESS_SPEC);
goto finish;
}
scratchNumber = CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt64Type, &address);
if (!scratchNumber) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
scratchString = CFStringCreateWithCString(kCFAllocatorDefault,
optarg, kCFStringEncodingUTF8);
if (!scratchString) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
existingAddress = CFDictionaryGetValue(toolArgs->loadAddresses,
scratchString);
if (existingAddress && !CFEqual(scratchNumber, existingAddress)) {
OSKextLog(/* kext */ NULL, kOSKextLogWarningLevel,
"Warning: multiple addresses specified for %s; using last.",
optarg);
}
CFDictionarySetValue(toolArgs->loadAddresses, scratchString,
scratchNumber);
break;
case kOptUseKernelAddresses:
toolArgs->flag_n = 1; // -A implies -n
toolArgs->getAddressesFromKernel = true;
break;
case kOptQuiet:
beQuiet();
toolArgs->logFilterChanged = true;
break;
case kOptVerbose:
scratchResult = setLogFilterForOpt(argc, argv, /* forceOnFlags */ 0);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
toolArgs->logFilterChanged = true;
break;
case kOptTests:
toolArgs->printDiagnostics = true;
break;
case kOptSafeBoot:
toolArgs->safeBootMode = true;
toolArgs->useRepositoryCaches = false; // -x implies -c
break;
case kOptNoAuthentication:
toolArgs->skipAuthentication = true;
break;
case kOptNoResolveDependencies:
toolArgs->skipDependencies = true;
break;
case 0:
switch (longopt) {
case kLongOptArch:
if (toolArgs->archInfo) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogGeneralFlag,
"Warning: multiple use of -%s; using last.",
kOptNameArch);
}
toolArgs->archInfo = NXGetArchInfoFromName(optarg);
if (!toolArgs->archInfo) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Unknown architecture %s.", optarg);
goto finish;
}
break;
default:
/* getopt_long_only() prints an error message for us. */
goto finish;
break;
}
break;
default:
/* getopt_long_only() prints an error message for us. */
goto finish;
break;
} /* switch (optchar) */
} /* while (optchar = getopt_long_only(...) */
/*****
* Record the kext names from the command line.
*/
for (i = optind; i < argc; i++) {
SAFE_RELEASE_NULL(scratchURL);
scratchResult = checkPath(argv[i], kOSKextBundleExtension,
/* directoryRequired */ TRUE, /* writableRequired */ FALSE);
if (scratchResult != EX_OK) {
result = scratchResult;
goto finish;
}
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)argv[i], strlen(argv[i]), true);
if (!scratchURL) {
result = EX_OSERR;
OSKextLogMemError();
goto finish;
}
CFArrayAppendValue(toolArgs->kextURLs, scratchURL);
}
result = EX_OK;
finish:
SAFE_RELEASE(scratchString);
SAFE_RELEASE(scratchNumber);
SAFE_RELEASE(scratchURL);
if (result == EX_USAGE) {
usage(kUsageLevelBrief);
}
return result;
}
/*******************************************************************************
*******************************************************************************/
ExitStatus
checkArgs(KextutilArgs * toolArgs)
{
ExitStatus result = EX_USAGE;
char kernelPathCString[PATH_MAX];
const NXArchInfo * kernelArchInfo = OSKextGetRunningKernelArchitecture();
/* We don't need this for everything, but it's unlikely to fail.
*/
if (!kernelArchInfo) {
result = EX_OSERR;
goto finish;
}
/*****
* Check for bad combinations of arguments and options.
*/
if (toolArgs->flag_l + toolArgs->flag_m + toolArgs->flag_n > 1) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Only one of -%s (-%c), -%s (-%c), or -%s (-%c) is allowed;\n"
"-%s (-%c) and -%s (-%c) imply -%s (-%c).",
kOptNameLoadOnly, kOptLoadOnly,
kOptNameMatchOnly, kOptMatchOnly,
kOptNameNoLoad, kOptNoLoad,
kOptNameAddress, kOptAddress,
kOptNameUseKernelAddresses, kOptUseKernelAddresses,
kOptNameNoLoad, kOptNoLoad);
goto finish;
} else if (toolArgs->flag_l) {
toolArgs->doLoad = true;
toolArgs->doStartMatching = false;
} else if (toolArgs->flag_m) {
toolArgs->doLoad = false;
toolArgs->doStartMatching = true;
} else if (toolArgs->flag_n) {
toolArgs->doLoad = false;
toolArgs->doStartMatching = false;
}
if ((toolArgs->interactiveLevel != kOSKextExcludeNone) &&
!toolArgs->doLoad && !toolArgs->doStartMatching) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Use interactive modes only when loading or matching.");
goto finish;
}
/* If running in quiet mode and the invocation might require
* interaction, just exit with an error. Interaction required when:
*
* - Explicitly flagged with -i/-I.
* - Saving symbols w/o loading, no addresses specified,
* not getting from kernel.
*/
if (OSKextGetLogFilter(/* kernel? */ false) == kOSKextLogSilentFilter) {
Boolean interactive = (toolArgs->interactiveLevel != kOSKextExcludeNone);
Boolean needAddresses = (toolArgs->symbolDirURL &&
!toolArgs->doLoad &&
CFDictionaryGetCount(toolArgs->loadAddresses) == 0 &&
!toolArgs->getAddressesFromKernel);
if (interactive || needAddresses) {
result = kKextutilExitLoadFailed;
goto finish;
}
}
/* Disallow -address(-a) with any load or getting addresses from kernel.
*/
if (CFDictionaryGetCount(toolArgs->loadAddresses) > 0 &&
(toolArgs->doLoad || toolArgs->getAddressesFromKernel)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Don't use -%s (-%c) when loading, or with -%s (-%c).",
kOptNameAddress, kOptAddress,
kOptNameUseKernelAddresses, kOptUseKernelAddresses);
goto finish;
}
/* If sending anything into the kernel, don't use a kernel file
* and refuse to skip authentication.
*/
if (toolArgs->doLoad || toolArgs->doStartMatching) {
if (toolArgs->kernelURL) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"-%s (-%c) is allowed only when not loading "
"or sending personalities.",
kOptNameKernel, kOptKernel);
goto finish;
}
if (toolArgs->skipAuthentication) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"-%s (-%c) is allowed only when not loading "
"or sending personalities.",
kOptNameNoAuthentication, kOptNoAuthentication);
goto finish;
}
}
/* If we aren't sending anything to the kernel and not doing full
* tests, then don't bother authenticating. This lets developers
* generate symbols more conveniently & allows basic checks with -n alone.
*/
if (!toolArgs->doLoad &&
!toolArgs->doStartMatching &&
!toolArgs->printDiagnostics) {
toolArgs-> skipAuthentication = true;
}
/****
* If we're getting addresses from the kernel we have to call
* down there, so we might as well check what's loaded before
* resolving dependencies too (-A overrides -D). If we're not
* performing a load, then don't check (-n/-m implies -D).
*/
if (toolArgs->getAddressesFromKernel) {
toolArgs->checkLoadedForDependencies = true;
}
/*****
* -no-resolve-dependencies/-Z is only allowed
* if you don't need to resolve dependencies (duh).
* You need to resolve if loading or saving symbols.
*/
if (toolArgs->skipDependencies) {
if (toolArgs->doLoad ||
toolArgs->symbolDirURL) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Use -%s (-%c) only with -%s (-%c) or %s (-%c), "
"and not with -%s (-%c).",
kOptNameNoResolveDependencies, kOptNoResolveDependencies,
kOptNameNoLoad, kOptNoLoad,
kOptNameMatchOnly, kOptMatchOnly,
kOptNameSymbolsDirectory, kOptSymbolsDirectory);
goto finish;
}
}
if (!CFArrayGetCount(toolArgs->kextURLs) &&
!CFArrayGetCount(toolArgs->kextIDs) &&
!CFDictionaryGetCount(toolArgs->loadAddresses)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"No kernel extensions specified; name kernel extension bundles\n"
" following options, or use -%s (-%c) and -%s (-%c).",
kOptNameBundleIdentifier, kOptBundleIdentifier,
kOptNameAddress, kOptAddress);
goto finish;
}
/*****
* Check whether the user has permission to load into the kernel.
*/
if ((toolArgs->doLoad || toolArgs->doStartMatching) && geteuid() != 0) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"You must be running as root to load kexts "
"or send personalities into the kernel.");
result = EX_NOPERM;
goto finish;
}
/*****
* Set up which arch to work with and sanity-check it.
*/
if (toolArgs->archInfo) {
/* If loading into or getting addresses form the kernel,
* any specified architecture must match that of the running kernel.
*/
if (toolArgs->doLoad || toolArgs->getAddressesFromKernel) {
if (kernelArchInfo != OSKextGetArchitecture()) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Specified architecture %s does not match "
"running kernel architecture %s.",
toolArgs->archInfo->name, kernelArchInfo->name);
goto finish;
}
}
/* All good; set the default OSKext arch & safe boot mode.
*/
OSKextSetArchitecture(toolArgs->archInfo);
OSKextSetSimulatedSafeBoot(toolArgs->safeBootMode);
}
/* Give a notice to the user in case they're doing cross-arch work.
*/
if (toolArgs->symbolDirURL && !toolArgs->archInfo &&
!toolArgs->getAddressesFromKernel) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogLinkFlag,
"Notice: Using running kernel architecture %s to generate symbols.",
kernelArchInfo->name);
/* not an error! */
}
/* Give a notice to the user if safe boot mode is actually on.
*/
if (OSKextGetActualSafeBoot() && !toolArgs->safeBootMode) {
OSKextLog(/* kext */ NULL,
kOSKextLogWarningLevel | kOSKextLogLoadFlag,
"Notice: system is in safe boot mode; kernel may refuse loads.");
/* not an error! */
}
/*****
* Assemble the list of URLs to scan, in this order (the OSKext lib inverts it
* for last-opened-wins semantics):
* 1. System repository directories (-no-system-extensions/-e skips).
* 2. Named kexts (always given after -repository & -dependency on command line).
* 3. Named repository directories (-repository/-r).
* 4. Named dependencies get priority (-dependency/-d).
*/
if (toolArgs->useSystemExtensions) {
CFArrayRef sysExtFolders = OSKextGetSystemExtensionsFolderURLs();
// xxx - check it
CFArrayAppendArray(toolArgs->scanURLs,
sysExtFolders, RANGE_ALL(sysExtFolders));
}
CFArrayAppendArray(toolArgs->scanURLs, toolArgs->kextURLs,
RANGE_ALL(toolArgs->kextURLs));
CFArrayAppendArray(toolArgs->scanURLs, toolArgs->repositoryURLs,
RANGE_ALL(toolArgs->repositoryURLs));
CFArrayAppendArray(toolArgs->scanURLs, toolArgs->dependencyURLs,
RANGE_ALL(toolArgs->dependencyURLs));
if ( (CFArrayGetCount(toolArgs->kextIDs) ||
CFDictionaryGetCount(toolArgs->loadAddresses)) &&
!CFArrayGetCount(toolArgs->scanURLs)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"No kexts to search for bundle IDs; "
"-%s (-%c) requires kexts or repository directories "
"be named by path.",
kOptNameBundleIdentifier, kOptBundleIdentifier);
goto finish;
}
/* If no explicit kernel image was provided by the user, default it
* to KernelPath from /usr/standalone/bootcaches.plist
* <= 10.9 /mach_kernel
* > 10.9 /System/Library/Kernels/kernel
*/
if (!toolArgs->kernelURL) {
CFURLRef scratchURL = NULL;
// use KernelPath from /usr/standalone/bootcaches.plist
if (getKernelPathForURL(NULL,
kernelPathCString,
sizeof(kernelPathCString)) == FALSE) {
// no bootcaches.plist? Forced to hardwire...
strlcpy(kernelPathCString, "/System/Library/Kernels/kernel",
sizeof(kernelPathCString));
}
if (useDevelopmentKernel(kernelPathCString)) {
if (strlen(kernelPathCString) + strlen(kDefaultDevKernelSuffix) + 1 < sizeof(kernelPathCString)) {
strlcat(kernelPathCString,
kDefaultDevKernelSuffix,
sizeof(kernelPathCString));
}
}
scratchURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
(const UInt8 *)kernelPathCString,
strlen(kernelPathCString),
false );
if (!scratchURL) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
goto finish;
}
toolArgs->kernelURL = scratchURL;
OSKextLog(/* kext */ NULL,
kOSKextLogBasicLevel | kOSKextLogLoadFlag,
"Defaulting to kernel file '%s'",
kernelPathCString);
}
if (toolArgs->kernelURL) {
/* create and fill our CFData object for toolArgs->kernelFile
*/
if (!CFURLGetFileSystemRepresentation(toolArgs->kernelURL,
true,
(uint8_t *)kernelPathCString,
sizeof(kernelPathCString))) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSFILE;
goto finish;
}
if (!createCFDataFromFile(&toolArgs->kernelFile,
kernelPathCString)) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogFileAccessFlag,
"Can't read kernel file '%s'",
kernelPathCString);
result = EX_OSFILE;
goto finish;
}
}
if (!toolArgs->doLoad || (toolArgs->interactiveLevel != kOSKextExcludeNone)) {
adjustLogFilterForInteractive(toolArgs);
}
result = EX_OK;
finish:
if (result == EX_USAGE) {
usage(kUsageLevelBrief);
}
return result;
}
/*******************************************************************************
*******************************************************************************/
void adjustLogFilterForInteractive(KextutilArgs * toolArgs)
{
if (!toolArgs->logFilterChanged) {
OSKextSetLogFilter(kDefaultServiceLogFilter, /* kernel? */ false);
OSKextSetLogFilter(kDefaultServiceLogFilter, /* kernel? */ true);
}
}
/*******************************************************************************
*******************************************************************************/
ExitStatus
createKextsToProcess(
KextutilArgs * toolArgs,
CFArrayRef * outArray,
Boolean * fatal)
{
ExitStatus result = EX_OK;
CFMutableArrayRef kextsToProcess = NULL; // returned by ref.
CFURLRef kextURL = NULL; // do not release
char kextPathCString[PATH_MAX];
CFStringRef * addressKeys = NULL; // must free
char * kextIDCString = NULL; // must free
OSKextRef theKext = NULL; // do not release
CFIndex kextCount, idCount, kextIndex, idIndex;
if (!createCFMutableArray(&kextsToProcess, &kCFTypeArrayCallBacks)) {
result = EX_OSERR;
goto finish;
}
/*****
* If we got kext bundle names, then create kexts for them. Kexts named
* by path always have priority so we're adding them first.
*/
kextCount = CFArrayGetCount(toolArgs->kextURLs);
for (kextIndex = 0; kextIndex < kextCount; kextIndex++) {
kextURL = (CFURLRef)CFArrayGetValueAtIndex(
toolArgs->kextURLs, kextIndex);
if (!CFURLGetFileSystemRepresentation(kextURL,
/* resolveToBase */ false,
(u_char *)kextPathCString, sizeof(kextPathCString))) {
OSKextLogStringError(/* kext */ NULL);
result = EX_OSERR;
*fatal = true;
goto finish;
}
OSKextLog(/* kext */ NULL,
kOSKextLogStepLevel | kOSKextLogKextBookkeepingFlag,
"Looking up extension with URL %s.",
kextPathCString);
/* Use OSKextGetKextWithURL() to avoid double open error messages,
* because we already tried to open all kexts in main(). That means
* we don't log here if we don't find the kext.
*/
theKext = OSKextGetKextWithURL(kextURL);
if (!theKext) {
result = kKextutilExitNotFound;
// keep going
continue;
}
addToArrayIfAbsent(kextsToProcess, theKext);
/* Since we're running a developer tool on this kext, let's go ahead
* and enable per-kext logging for it regardless of the in-bundle setting.
* Would be nice to do this for all dependencies but the user can set
* the verbose filter's 0x8 bit for that. It will log for all kexts
* but that's not so bad.
*/
OSKextSetLoggingEnabled(theKext, true);
}
/*****
* If we got load addresses on the command line, add their bundle IDs
* to the list of kext IDs to load.
*/
idCount = CFDictionaryGetCount(toolArgs->loadAddresses);
if (idCount) {
addressKeys = (CFStringRef *)malloc(idCount * sizeof(CFStringRef));
if (!addressKeys) {
OSKextLogMemError();
result = EX_OSERR;
*fatal = true;
goto finish;
}
CFDictionaryGetKeysAndValues(toolArgs->loadAddresses,
(void *)addressKeys, /* values */ NULL);
for (idIndex = 0; idIndex < idCount; idIndex++) {
if (kCFNotFound == CFArrayGetFirstIndexOfValue(toolArgs->kextIDs,
RANGE_ALL(toolArgs->kextIDs), addressKeys[idIndex])) {
CFArrayAppendValue(toolArgs->kextIDs, addressKeys[idIndex]);
}
}
}
/*****
* If we have CFBundleIdentifiers, then look them up. We just add to the
* kextsToProcess array here.
*/
idCount = CFArrayGetCount(toolArgs->kextIDs);
for (idIndex = 0; idIndex < idCount; idIndex++) {
CFStringRef thisKextID = (CFStringRef)
CFArrayGetValueAtIndex(toolArgs->kextIDs, idIndex);
SAFE_FREE_NULL(kextIDCString);
kextIDCString = createUTF8CStringForCFString(thisKextID);
if (!kextIDCString) {
OSKextLogMemError();
result = EX_OSERR;
goto finish;
}
/* First see if we already have this kext in the list of kexts
* to process. Only if we don't have it by name do we look it up
* by identifier. This should allow kexts named by path on the command
* line to take precendence over any bundle ID lookups; for example,
* if the user is working with an older version of the kext, an
* identifier lookup would find the newer.
*/
kextCount = CFArrayGetCount(kextsToProcess);