-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathXPCListener.m
More file actions
308 lines (236 loc) · 7.89 KB
/
XPCListener.m
File metadata and controls
308 lines (236 loc) · 7.89 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
//
// file: XPCListener.m
// project: BlockBlock (launch daemon)
// description: XPC listener for connections for user components
//
// created by Patrick Wardle
// copyright (c) 2017 Objective-See. All rights reserved.
//
@import OSLog;
#import "consts.h"
#import "Rule.h"
#import "Rules.h"
#import "utilities.h"
#import "XPCDaemon.h"
#import "XPCListener.h"
#import "XPCUserProto.h"
#import "XPCDaemonProto.h"
#import <bsm/libbsm.h>
//signing auth
#define SIGNING_AUTH @"Developer ID Application: Objective-See, LLC (VBG97UB4TA)"
/* GLOBALS */
//log handle
extern os_log_t logHandle;
//interface for 'extension' to NSXPCConnection
// allows us to access the 'private' auditToken iVar
@interface ExtendedNSXPCConnection : NSXPCConnection
{
//private iVar
audit_token_t auditToken;
}
//private iVar
@property audit_token_t auditToken;
@end
//implementation for 'extension' to NSXPCConnection
// ->allows us to access the 'private' auditToken iVar
@implementation ExtendedNSXPCConnection
//private iVar
@synthesize auditToken;
@end
OSStatus SecTaskValidateForRequirement(SecTaskRef task, CFStringRef requirement);
//global rules obj
extern Rules* rules;
@implementation XPCListener
@synthesize client;
@synthesize listener;
//init
// create XPC listener
-(id)init
{
//init super
self = [super init];
if(nil != self)
{
//setup XPC listener
if(YES != [self initListener])
{
//unset
self = nil;
//bail
goto bail;
}
}
bail:
return self;
}
//setup XPC listener
-(BOOL)initListener
{
//result
BOOL result = NO;
//code signing requirement
NSString* requirement = nil;
//init listener
listener = [[NSXPCListener alloc] initWithMachServiceName:DAEMON_MACH_SERVICE];
if(nil == self.listener)
{
//err msg
os_log_error(logHandle, "ERROR: failed to create mach service %{public}@", DAEMON_MACH_SERVICE);
//bail
goto bail;
}
//macOS 13+
// set code signing requirement for clients via 'setConnectionCodeSigningRequirement'
if(@available(macOS 13.0, *)) {
//init requirement
// BB helper, v2.0+
requirement = [NSString stringWithFormat:@"anchor apple generic and identifier \"%@\" and certificate leaf [subject.CN] = \"%@\" and info [CFBundleShortVersionString] >= \"2.0.0\"", HELPER_ID, SIGNING_AUTH];
//set requirement
[self.listener setConnectionCodeSigningRequirement:requirement];
//dbg msg
os_log_debug(logHandle, "set XPC requirement %{public}@", requirement);
}
//dbg msg
os_log_debug(logHandle, "created mach service %{public}@", DAEMON_MACH_SERVICE);
//set delegate
self.listener.delegate = self;
//ready to accept connections
[self.listener resume];
//happy
result = YES;
bail:
return result;
}
#pragma mark -
#pragma mark NSXPCConnection method overrides
//automatically invoked
// allows NSXPCListener to configure/accept/resume a new incoming NSXPCConnection
// shoutout to writeup: https://blog.obdev.at/what-we-have-learned-from-a-vulnerability
-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
{
//flag
BOOL shouldAccept = NO;
//status
OSStatus status = !errSecSuccess;
//
NSOperatingSystemVersion macOS13 = {13,0,0};
//audit token
audit_token_t auditToken = {0};
//task ref
SecTaskRef taskRef = 0;
//code ref
SecCodeRef codeRef = NULL;
//code signing info
CFDictionaryRef csInfo = NULL;
//cs flags
uint32_t csFlags = 0;
//signing req string (main app)
NSString* requirement = nil;
//dbg msg
os_log_debug(logHandle, "'%s' invoked", __PRETTY_FUNCTION__);
//pre-macOS 13
// have to manually check client, as 'setConnectionCodeSigningRequirement' is not supported
if(YES != [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:macOS13])
{
//extract audit token
auditToken = ((ExtendedNSXPCConnection*)newConnection).auditToken;
//dbg msg
os_log_debug(logHandle, "received request to connect to XPC interface from: (%d)%{public}@", audit_token_to_pid(auditToken), getProcessPath(audit_token_to_pid(auditToken)));
//obtain dynamic code ref
status = SecCodeCopyGuestWithAttributes(NULL, (__bridge CFDictionaryRef _Nullable)(@{(__bridge NSString *)kSecGuestAttributeAudit : [NSData dataWithBytes:&auditToken length:sizeof(audit_token_t)]}), kSecCSDefaultFlags, &codeRef);
if(errSecSuccess != status)
{
//bail
goto bail;
}
//validate code
status = SecCodeCheckValidity(codeRef, kSecCSDefaultFlags, NULL);
if(errSecSuccess != status)
{
//bail
goto bail;
}
//get code signing info
status = SecCodeCopySigningInformation(codeRef, kSecCSDynamicInformation, &csInfo);
if(errSecSuccess != status)
{
//bail
goto bail;
}
//dbg msg
os_log_debug(logHandle, "client's code signing info: %{public}@", csInfo);
//extract flags
csFlags = [((__bridge NSDictionary *)csInfo)[(__bridge NSString *)kSecCodeInfoStatus] unsignedIntValue];
//gotta have hardened runtime
if( !(CS_VALID & csFlags) &&
!(CS_RUNTIME & csFlags) )
{
//bail
goto bail;
}
//init signing req string
requirement = [NSString stringWithFormat:@"anchor apple generic and identifier \"%@\" and certificate leaf [subject.CN] = \"%@\" and info [CFBundleShortVersionString] >= \"2.0.0\"", HELPER_ID, SIGNING_AUTH];
//step 1: create task ref
// uses NSXPCConnection's (private) 'auditToken' iVar
taskRef = SecTaskCreateWithAuditToken(NULL, ((ExtendedNSXPCConnection*)newConnection).auditToken);
if(NULL == taskRef)
{
//bail
goto bail;
}
//step 2: validate
// check that client is signed with Objective-See's and it's BlockBlock
status = SecTaskValidateForRequirement(taskRef, (__bridge CFStringRef)(requirement));
if(errSecSuccess != status)
{
//bail
goto bail;
}
}
//set the interface that the exported object implements
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XPCDaemonProtocol)];
//set object exported by connection
newConnection.exportedObject = [[XPCDaemon alloc] init];
//set type of remote object
// user (login item/main app) will set this object
newConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol: @protocol(XPCUserProtocol)];
//save
self.client = newConnection;
//in background
// notify that a new client connected
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
//notify
[[NSNotificationCenter defaultCenter] postNotificationName:USER_NOTIFICATION object:nil userInfo:nil];
});
//resume
[newConnection resume];
//dbg msg
os_log_debug(logHandle, "allowing XPC connection from client (pid: %d)", audit_token_to_pid(auditToken));
//happy
shouldAccept = YES;
bail:
//release task ref object
if(NULL != taskRef)
{
//release
CFRelease(taskRef);
taskRef = NULL;
}
//free cs info
if(NULL != csInfo)
{
//free
CFRelease(csInfo);
csInfo = NULL;
}
//free code ref
if(NULL != codeRef)
{
//free
CFRelease(codeRef);
codeRef = NULL;
}
return shouldAccept;
}
@end