Repository navigation
Expand file tree
/
Copy pathdriverkit.m
More file actions
464 lines (396 loc) · 15.3 KB
/
Copy pathdriverkit.m
File metadata and controls
464 lines (396 loc) · 15.3 KB
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
#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>
#import <CoreFoundation/CFXPCBridge.h>
#include <IOKit/kext/OSKext.h>
#include <IOKit/kext/OSKextPrivate.h>
#include <launch_priv.h>
#include <Security/Authorization.h>
#include <SystemExtensions/SystemExtensions.h>
#include <SystemExtensions/SystemExtensions_Private.h>
#include <xpc/private.h>
#include "driverkit.h"
#include "signposts.h"
#include "security.h"
#include "kext_tools_util.h"
#include "paths.h"
#define DEXT_DEBUG_ENTITLEMENT "get-task-allow"
#define LAUNCH_KEY_PROCESSTYPE_DRIVER "Driver"
Boolean isDextSignedForDebugging(OSKextRef aDext);
Boolean isDextStandaloneExecutable(OSKextRef aDext);
AuthOptions_t KextdAuthenticationOptions = {};
@implementation OSSystemExtensionClient
- (instancetype)init {
self = [super init];
return self;
}
- (BOOL)checkExtension:(NSString *)bundleID
teamID:(NSString *)teamID
teamIDPlatformBinary:(BOOL)teamIDPlatformBinary
teamIDNone:(BOOL)teamIDNone
error:(NSError **)error
{
// Always pretend success
return YES;
}
@end
static OSSystemExtensionClient *gSysextClient = nil;
/*
* This function takes a dext's identifier and executable path, and wraps
* them up in the necessary data structures to make launchd happy. It starts
* a dext daemon with the bundle id as its service name.
* This function started out using CF things, but the dictionary verbosity
* got too unwieldy. Trying to contain the ObjC in this file to just this function...
*/
bool submitJob(CFStringRef serverName,
CFNumberRef serverTag,
CFURLRef executableURL,
mach_port_t checkInPort,
CFDictionaryRef extraEnvVars,
Boolean enableCoreDump,
Boolean asDriver)
{
int error = 0;
bool result = false;
NSArray *executableArgs = nil;
NSString *bundlePath = nil;
NSString *serverTagString = nil;
NSString *serverNameAndTag = nil;
xpc_object_t xjob = NULL;
xpc_object_t ldrequest = NULL;
NSMutableDictionary *jobDict = nil;
NSMutableDictionary *envVars = nil;
bundlePath = [(__bridge NSURL *)executableURL path];
if (!bundlePath) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create string path to bundle.");
goto finish;
}
serverTagString = [NSString stringWithFormat:@"0x%llx", [(__bridge NSNumber *)serverTag unsignedLongLongValue]];
if (!serverTagString) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not get unique tag for DriverKit daemon.");
goto finish;
}
executableArgs = @[ bundlePath, (__bridge NSString *)serverName, serverTagString ];
if (!executableArgs) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create array for DriverKit daemon arguments.");
OSKextLogMemError();
goto finish;
}
serverNameAndTag = [NSString stringWithFormat:@"%@-(%@)", (__bridge NSString *)serverName, serverTagString];
if (!serverNameAndTag) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create dext service label!");
goto finish;
}
envVars = [@{
@"LIBTRACE_DRIVERKIT" : @"1",
} mutableCopy];
if (!envVars) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create environment variables!");
goto finish;
}
if (!asDriver) {
envVars[@"MallocNanoZone"] = @"1";
}
if (extraEnvVars && CFGetTypeID(extraEnvVars) == CFDictionaryGetTypeID()) {
[envVars addEntriesFromDictionary:(__bridge NSDictionary *)extraEnvVars];
}
jobDict = [@{
@"Label" : serverNameAndTag,
@"ProgramArguments" : executableArgs,
@"ProcessType" : asDriver ? @(LAUNCH_KEY_PROCESSTYPE_DRIVER) : @"Interactive",
@"RunAtLoad" : @YES,
@"LaunchOnlyOnce" : @YES,
@"EnvironmentVariables" : envVars,
@"SandboxProfile" : @"com.apple.dext",
@"UserName" : @"_driverkit",
@"_NullBootstrapPort" : @YES,
} mutableCopy];
if (!jobDict) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create dictionary for DriverKit daemon job submission.");
OSKextLogMemError();
goto finish;
}
if (enableCoreDump) {
[jobDict addEntriesFromDictionary:@{
@"HardResourceLimits" : @{ @"Core" : @(RLIM_INFINITY) },
@"SoftResourceLimits" : @{ @"Core" : @(RLIM_INFINITY) },
}];
}
os_signpost_event_emit(get_signpost_log(), OS_SIGNPOST_ID_EXCLUSIVE, SIGNPOST_KEXTD_DEXT_LAUNCH, "%s - %s",
[bundlePath UTF8String],
[serverNameAndTag UTF8String]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" // needed for SMJobSubmit()
xjob = _CFXPCCreateXPCObjectFromCFObject((__bridge CFDictionaryRef)jobDict);
if (!xjob) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create launchd job for dext.");
goto finish;
}
xpc_dictionary_set_mach_send(xjob, "_DextCheckInPort", checkInPort);
ldrequest = xpc_dictionary_create(NULL, NULL, 0);
if (!ldrequest) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create launchd request dictionary for dext.");
goto finish;
}
xpc_dictionary_set_value(ldrequest, LAUNCH_KEY_SUBMITJOB, xjob);
// wham-cast. (See SMShared.c in ServiceManagement)
launch_data_t ldreply = launch_msg((__bridge void *)ldrequest);
if (!ldreply) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Could not create reply message from launchd.");
goto finish;
}
error = launch_data_get_errno(ldreply);
launch_data_free(ldreply);
if (error) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogIPCFlag,
"Error %d (%s) in reply received from launchd!", error, strerror(error));
goto finish;
}
#pragma clang diagnostic pop
result = true; /* success */
finish:
return result;
}
/* This function starts the userland driver portion of a dext by reading a key
* in the dext's Info.plist which specifies an executable path. This path is
* handed over to launchd, which starts the driver as a daemon.
*
* This function is called by kextdProcessDaemonLaunchRequest
* in kextd_request.c. It should only be called
* AFTER all security and staging has taken place.
*/
bool startUserExtension(OSKextRef aDext, CFStringRef serverName, CFNumberRef serverTag, mach_port_t checkInPort)
{
bool result = false;
bool coreDumps = false;
bool asDriver = true; // XXX: Can we delete this soon
CFURLRef executableURL = NULL; // do not release
CFURLRef absoluteURL = NULL; // release
CFStringRef absolutePath = NULL; // release
CFDictionaryRef envVars = NULL; // do not release
CFBooleanRef coreDumpsRef = NULL; // do not release
if (!serverName) {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogIPCFlag | kOSKextLogGeneralFlag,
"Could not get dext service name.");
goto finish;
}
executableURL = OSKextGetUserExecutableURL(aDext);
if (!executableURL) {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogIPCFlag | kOSKextLogGeneralFlag,
"Could not get URL to executable.");
goto finish;
}
absoluteURL = CFURLCopyAbsoluteURL(OSKextGetURL(aDext));
if (!absoluteURL) {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogIPCFlag | kOSKextLogGeneralFlag,
"Could not get URL to dext.");
goto finish;
}
/*
* Only allow dexts to run as interactive daemons if they're Apple dexts
* that are running out of /S/L/DE. Otherwise, they need the DriverKit
* entitlement to run under the driver process type.
*/
if (!checkEntitlementAtURL(
absoluteURL,
CFSTR(DEXT_LAUNCH_ENTITLEMENT),
KextdAuthenticationOptions.allowNetwork)) {
absolutePath = CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
if (!absolutePath) {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogIPCFlag | kOSKextLogGeneralFlag,
"Could not get path to dext.");
goto finish;
}
if (_OSKextIdentifierHasApplePrefix(aDext) &&
CFStringHasPrefix(absolutePath, CFSTR(_kOSKextSystemLibraryDriverExtensionsFolder))) {
asDriver = false;
} else {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogIPCFlag | kOSKextLogGeneralFlag,
"Dext executable is not signed with the %s entitlement.", DEXT_LAUNCH_ENTITLEMENT);
goto finish;
}
}
if (checkEntitlementAtURL(
absoluteURL,
CFSTR(DEXT_DEBUG_ENTITLEMENT),
KextdAuthenticationOptions.allowNetwork)) {
OSKextLog(aDext,
kOSKextLogDebugLevel | kOSKextLogGeneralFlag,
"Dext with server name %s is configured for debugging.",
CFStringGetCStringPtr(serverName, kCFStringEncodingUTF8));
envVars = OSKextGetValueForInfoDictionaryKey(aDext, CFSTR("EnvironmentVariables"));
coreDumpsRef = OSKextGetValueForInfoDictionaryKey(aDext, CFSTR("EnableCoreDumps"));
coreDumps = (coreDumpsRef && CFEqual(coreDumpsRef, kCFBooleanTrue));
}
if (!submitJob(serverName, serverTag, executableURL, checkInPort, envVars, coreDumps, asDriver)) {
OSKextLog(aDext,
kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"Unable to start user extension daemon.");
goto finish;
}
result = true; /* success */
finish:
SAFE_RELEASE(absoluteURL);
SAFE_RELEASE(absolutePath);
return result;
}
Boolean addCDHashToDextPersonality(OSKextRef aDext, CFMutableDictionaryRef personality)
{
Boolean result = FALSE;
CFURLRef executableURL = NULL; // do not release
CFStringRef bundleID = NULL; // do not release
CFStringRef cdHashString = NULL; // must release
/* We don't care about old-school kexts here, so early-out with success */
if (!OSKextDeclaresUserExecutable(aDext)) {
result = TRUE;
goto finish;
}
bundleID = OSKextGetIdentifier(aDext);
if (!bundleID) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogIPCFlag,
"Unable to get bundleID for extension?");
goto finish;
}
executableURL = OSKextGetUserExecutableURL(aDext);
if (!executableURL) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogIPCFlag,
"Unable to get executableURL for extension.");
goto finish;
}
cdHashString = copyCDHashFromURL(executableURL);
if (!cdHashString) {
OSKextLog(/* kext */ NULL,
kOSKextLogErrorLevel | kOSKextLogIPCFlag,
"Unable to get cdhash from binary.");
goto finish;
}
OSKextLogCFString(aDext,
kOSKextLogDetailLevel | kOSKextLogGeneralFlag,
CFSTR("Adding IOUserServerCDHash %@ for dext %@."),
cdHashString, bundleID);
CFDictionaryAddValue(personality, CFSTR("IOUserServerCDHash"), cdHashString);
result = TRUE;
finish:
SAFE_RELEASE(cdHashString);
return result;
}
bool copyDextTeamID(CFURLRef kextURL, CFStringRef *teamIDp)
{
bool result = false;
SecStaticCodeRef code = NULL; // must release
CFDictionaryRef information = NULL; // must release
CFStringRef teamID = NULL; // do not release
OSStatus status = noErr;
if (!kextURL) {
goto finish;
}
if (SecStaticCodeCreateWithPath(kextURL,
kSecCSDefaultFlags,
&code) != 0
|| (code == NULL)) {
OSKextLogMemError();
goto finish;
}
status = SecCodeCopySigningInformation(code,
kSecCSSigningInformation,
&information);
if (status != noErr) {
goto finish;
}
/*
* It's possible to have a non-existant team ID. That's okay, but make
* sure we don't accidentally retain something that's a NULL pointer.
*/
teamID = CFDictionaryGetValue(information, kSecCodeInfoTeamIdentifier);
if (teamID) {
CFRetain(teamID);
}
if (teamIDp) {
*teamIDp = teamID;
}
result = true;
finish:
SAFE_RELEASE(code);
SAFE_RELEASE(information);
return result;
}
bool isDextAllowed(OSKextRef aDext)
{
CFURLRef kextURL = NULL; // must release
CFStringRef teamID = NULL; // must release
CFStringRef bundleID = NULL; // do not release
NSError *error = NULL; // must release
bool teamIDPlatformBinary = false;
bool teamIDNone = false;
bool approved = false;
// SystemExtensions.framework is weak-linked, so be careful
if (![OSSystemExtensionClient class]) {
return true;
}
if (!gSysextClient) {
gSysextClient = [[OSSystemExtensionClient alloc] init];
}
bundleID = OSKextGetIdentifier(aDext);
kextURL = CFURLCopyAbsoluteURL(OSKextGetURL(aDext));
if (!kextURL) {
OSKextLogMemError();
goto finish;
}
// XXX - sysextd has no opinions on first-party dexts for now.
if (_OSKextIdentifierHasApplePrefix(aDext)) {
// teamIDPlatformBinary = true;
approved = true;
goto finish;
} else {
if (!copyDextTeamID(kextURL, &teamID)) {
OSKextLogCFString(aDext, kOSKextLogErrorLevel | kOSKextLogFileAccessFlag,
CFSTR("Encountered error while trying to copy team ID at %@"),
kextURL);
goto finish;
}
if (!teamID) {
OSKextLog(aDext, kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
"No team ID found for dext binary.");
teamIDNone = true;
}
}
approved = [gSysextClient checkExtension:(__bridge NSString *)bundleID
teamID:(__bridge NSString *)teamID
teamIDPlatformBinary:teamIDPlatformBinary
teamIDNone:teamIDNone
error:&error];
if (error) {
OSKextLogCFString(aDext, kOSKextLogErrorLevel | kOSKextLogGeneralFlag,
CFSTR("Error encountered during approval check: %@"), error);
approved = false;
}
finish:
SAFE_RELEASE(teamID);
SAFE_RELEASE(kextURL);
return approved;
}