]> git.saurik.com Git - apple/security.git/blob - OSX/authd/authtoken.c
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / authd / authtoken.c
1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
2
3 #include "authtoken.h"
4 #include "authd_private.h"
5 #include "process.h"
6 #include "authitems.h"
7 #include "debugging.h"
8 #include "authutilities.h"
9 #include "server.h"
10
11 #include <CommonCrypto/CommonRandomSPI.h>
12 #include <Security/Authorization.h>
13 #include <Security/SecBase.h>
14 #include <sandbox.h>
15
16 AUTHD_DEFINE_LOG
17
18 static Boolean AuthTokenEqualCallBack(const void *value1, const void *value2)
19 {
20 return (*(uint64_t*)value1) == (*(uint64_t*)value2);
21 }
22
23 static CFHashCode AuthTokenHashCallBack(const void *value)
24 {
25 // CFHashCode hash;
26 // AuthorizationBlob* blob = (AuthorizationBlob*)value;
27 // hash = blob->data[1];
28 // hash <<= 32;
29 // hash |= blob->data[0];
30 // return hash;
31 //quick 64 bit aligned version
32 return *((CFHashCode*)((AuthorizationBlob*)value)->data);
33 }
34
35 const CFDictionaryKeyCallBacks kAuthTokenKeyCallBacks = {
36 .version = 0,
37 .retain = NULL,
38 .release = NULL,
39 .copyDescription = NULL,
40 .equal = &AuthTokenEqualCallBack,
41 .hash = &AuthTokenHashCallBack
42 };
43
44 struct _auth_token_s {
45 __AUTH_BASE_STRUCT_HEADER__;
46
47 AuthorizationBlob blob;
48 auth_token_state_t state;
49 audit_info_s auditInfo;
50 dispatch_queue_t dispatch_queue;
51
52 CFMutableSetRef processes;
53
54 session_t session;
55 process_t creator; // weak reference, used for entitlement checking
56 mach_port_t creator_bootstrap_port;
57
58 auth_items_t context;
59
60 CFMutableSetRef credentials;
61 CFMutableSetRef authorized_rights;
62
63 CFMutableDataRef encryption_key;
64
65 bool least_privileged;
66 bool appleSigned;
67 bool firstPartySigned;
68
69 bool sandboxed;
70 char * code_url;
71
72 credential_t credential;
73 };
74
75 static void
76 _auth_token_finalize(CFTypeRef value)
77 {
78 auth_token_t auth = (auth_token_t)value;
79 os_log_debug(AUTHD_LOG, "authtoken: finalizing");
80
81 dispatch_barrier_sync(auth->dispatch_queue, ^{});
82
83 dispatch_release(auth->dispatch_queue);
84 CFReleaseNull(auth->session);
85 CFReleaseNull(auth->processes);
86 CFReleaseNull(auth->context);
87 CFReleaseNull(auth->credentials);
88 CFReleaseNull(auth->authorized_rights);
89 free_safe(auth->code_url);
90 CFReleaseNull(auth->credential);
91 CFReleaseNull(auth->encryption_key);
92
93 if (auth->creator_bootstrap_port != MACH_PORT_NULL) {
94 mach_port_deallocate(mach_task_self(), auth->creator_bootstrap_port);
95 auth->creator_bootstrap_port = MACH_PORT_NULL;
96 }
97 }
98
99 static Boolean
100 _auth_token_equal(CFTypeRef value1, CFTypeRef value2)
101 {
102 auth_token_t auth1 = (auth_token_t)value1;
103 auth_token_t auth2 = (auth_token_t)value2;
104
105 return memcmp(&auth1->blob, &auth2->blob, sizeof(AuthorizationBlob)) == 0;
106 }
107
108 static CFStringRef
109 _auth_token_copy_description(CFTypeRef value)
110 {
111 auth_token_t auth = (auth_token_t)value;
112 return CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("auth_token: uid=%i, pid=%i, processes=%li least_privileged=%i"),
113 auth->auditInfo.euid, auth->auditInfo.pid, CFSetGetCount(auth->processes), auth->least_privileged);
114 }
115
116 static CFHashCode
117 _auth_token_hash(CFTypeRef value)
118 {
119 auth_token_t auth = (auth_token_t)value;
120 return *(CFHashCode*)&auth->blob;
121 }
122
123 AUTH_TYPE_INSTANCE(auth_token,
124 .init = NULL,
125 .copy = NULL,
126 .finalize = _auth_token_finalize,
127 .equal = _auth_token_equal,
128 .hash = _auth_token_hash,
129 .copyFormattingDesc = NULL,
130 .copyDebugDesc = _auth_token_copy_description
131 );
132
133 static CFTypeID auth_token_get_type_id() {
134 static CFTypeID type_id = _kCFRuntimeNotATypeID;
135 static dispatch_once_t onceToken;
136
137 dispatch_once(&onceToken, ^{
138 type_id = _CFRuntimeRegisterClass(&_auth_type_auth_token);
139 });
140
141 return type_id;
142 }
143
144 static auth_token_t
145 _auth_token_create(const audit_info_s * auditInfo, bool operateAsLeastPrivileged)
146 {
147 #if __LLP64__
148 __Check_Compile_Time(sizeof(CFHashCode) == sizeof(AuthorizationBlob));
149 #endif
150
151 auth_token_t auth = (auth_token_t)_CFRuntimeCreateInstance(kCFAllocatorDefault, auth_token_get_type_id(), AUTH_CLASS_SIZE(auth_token), NULL);
152 require(auth != NULL, done);
153
154 if (CCRandomCopyBytes(kCCRandomDefault, auth->blob.data, sizeof(auth->blob.data)) != kCCSuccess) {
155 os_log_error(AUTHD_LOG, "authtoken: failed to generate blob (PID %d)", auditInfo->pid);
156 CFReleaseNull(auth);
157 goto done;
158 }
159
160 auth->context = auth_items_create();
161 auth->auditInfo = *auditInfo;
162 auth->least_privileged = operateAsLeastPrivileged;
163
164 auth->dispatch_queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
165 check(auth->dispatch_queue != NULL);
166
167 auth->credentials = CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks);
168 auth->authorized_rights = CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks);
169 auth->processes = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
170 auth->creator_bootstrap_port = MACH_PORT_NULL;
171
172 if (sandbox_check_by_audit_token(auth->auditInfo.opaqueToken, "authorization-right-obtain", SANDBOX_CHECK_NO_REPORT) != 0)
173 auth->sandboxed = true;
174 else
175 auth->sandboxed = false;
176
177 size_t key_length = kCCKeySizeAES256;
178 auth->encryption_key = CFDataCreateMutable(kCFAllocatorDefault, key_length);
179 if (auth->encryption_key) {
180 int rv = CCRandomCopyBytes(kCCRandomDefault, CFDataGetMutableBytePtr(auth->encryption_key), key_length);
181 if (rv != kCCSuccess) {
182 CFReleaseNull(auth->encryption_key)
183 }
184 CFDataSetLength(auth->encryption_key, key_length);
185 }
186
187 #if DEBUG
188 CFHashCode code = AuthTokenHashCallBack(&auth->blob);
189 if (memcmp(&code, auth->blob.data, sizeof(auth->blob.data)) != 0) {
190 os_log_debug(AUTHD_LOG, "authtoken[%i]: blob = %x%01x", auth->auditInfo.pid, auth->blob.data[1], auth->blob.data[0]);
191 os_log_debug(AUTHD_LOG, "authtoken[%i]: hash = %lx", auth->auditInfo.pid, code);
192 assert(false);
193 }
194 #endif
195
196 done:
197 return auth;
198 }
199
200 auth_token_t
201 auth_token_create(process_t proc, bool operateAsLeastPrivileged)
202 {
203 auth_token_t auth = NULL;
204 require(proc != NULL, done);
205
206 auth = _auth_token_create(process_get_audit_info(proc), operateAsLeastPrivileged);
207 require(auth != NULL, done);
208
209 auth->creator = proc;
210 auth->session = (session_t)CFRetain(process_get_session(proc));
211 auth->code_url = _copy_string(process_get_code_url(proc));
212 auth->appleSigned = process_apple_signed(proc);
213 auth->creator_bootstrap_port = process_get_bootstrap(proc);
214 // This line grabs a reference to the send right to the bootstrap (our right to send to the bootstrap)
215 // This makes it critical to use the same call in reverse as we are only getting a ref to one right,
216 // but deallocate will free a ref to all 5 rights.
217 if (auth->creator_bootstrap_port != MACH_PORT_NULL) {
218 kern_return_t error_code = mach_port_mod_refs(mach_task_self(), auth->creator_bootstrap_port, MACH_PORT_RIGHT_SEND, 1);
219 if (error_code != KERN_SUCCESS) {
220 // If no reference to the mach port right can be obtained, we don't hold the copy, so mark it NULL again!
221 auth->creator_bootstrap_port = MACH_PORT_NULL;
222 }
223 }
224
225 os_log_debug(AUTHD_LOG, "authtoken: created for PID %d", auth->auditInfo.pid);
226
227 done:
228 return auth;
229 }
230
231 bool
232 auth_token_get_sandboxed(auth_token_t auth)
233 {
234 return auth->sandboxed;
235 }
236
237 const char *
238 auth_token_get_code_url(auth_token_t auth)
239 {
240 return auth->code_url;
241 }
242
243 const void *
244 auth_token_get_key(auth_token_t auth)
245 {
246 return &auth->blob;
247 }
248
249 auth_items_t
250 auth_token_get_context(auth_token_t auth)
251 {
252 return auth->context;
253 }
254
255 bool
256 auth_token_least_privileged(auth_token_t auth)
257 {
258 return auth->least_privileged;
259 }
260
261 uid_t
262 auth_token_get_uid(auth_token_t auth)
263 {
264 assert(auth); // marked non-null
265 return auth->auditInfo.euid;
266 }
267
268 pid_t
269 auth_token_get_pid(auth_token_t auth)
270 {
271 assert(auth); // marked non-null
272 return auth->auditInfo.pid;
273 }
274
275 session_t
276 auth_token_get_session(auth_token_t auth)
277 {
278 return auth->session;
279 }
280
281 const AuthorizationBlob *
282 auth_token_get_blob(auth_token_t auth)
283 {
284 return &auth->blob;
285 }
286
287 const audit_info_s *
288 auth_token_get_audit_info(auth_token_t auth)
289 {
290 return &auth->auditInfo;
291 }
292
293 mach_port_t
294 auth_token_get_creator_bootstrap(auth_token_t auth)
295 {
296 return auth->creator_bootstrap_port;
297 }
298
299 CFIndex
300 auth_token_add_process(auth_token_t auth, process_t proc)
301 {
302 __block CFIndex count = 0;
303 dispatch_sync(auth->dispatch_queue, ^{
304 CFSetAddValue(auth->processes, proc);
305 count = CFSetGetCount(auth->processes);
306 });
307 return count;
308 }
309
310 CFIndex
311 auth_token_remove_process(auth_token_t auth, process_t proc)
312 {
313 __block CFIndex count = 0;
314 dispatch_sync(auth->dispatch_queue, ^{
315 if (auth->creator == proc) {
316 auth->creator = NULL;
317 }
318 CFSetRemoveValue(auth->processes, proc);
319 count = CFSetGetCount(auth->processes);
320 });
321 return count;
322 }
323
324 CFIndex
325 auth_token_get_process_count(auth_token_t auth)
326 {
327 __block CFIndex count = 0;
328 dispatch_sync(auth->dispatch_queue, ^{
329 count = CFSetGetCount(auth->processes);
330 });
331 return count;
332 }
333
334 void
335 auth_token_set_credential(auth_token_t auth, credential_t cred)
336 {
337 dispatch_sync(auth->dispatch_queue, ^{
338 CFSetSetValue(auth->credentials, cred);
339 });
340 }
341
342 bool
343 auth_token_credentials_iterate(auth_token_t auth, credential_iterator_t iter)
344 {
345 __block bool result = false;
346
347 dispatch_sync(auth->dispatch_queue, ^{
348 CFIndex count = CFSetGetCount(auth->credentials);
349 if (count > 128) { // <rdar://problem/38179345> Variable Length Arrays; AuthD
350 // auth_token usually contains 0 or 1 credential
351 count = 128;
352 }
353 CFTypeRef values[count];
354 CFSetGetValues(auth->credentials, values);
355 for (CFIndex i = 0; i < count; i++) {
356 credential_t cred = (credential_t)values[i];
357 result = iter(cred);
358 if (!result) {
359 break;
360 }
361 }
362 });
363
364 return result;
365 }
366
367 void
368 auth_token_set_right(auth_token_t auth, credential_t right)
369 {
370 dispatch_sync(auth->dispatch_queue, ^{
371 CFSetSetValue(auth->authorized_rights, right);
372 });
373 }
374
375 CFTypeRef
376 auth_token_copy_entitlement_value(auth_token_t auth, const char * entitlement)
377 {
378 __block CFTypeRef value = NULL;
379 dispatch_sync(auth->dispatch_queue, ^{
380 if (auth->creator) {
381 value = process_copy_entitlement_value(auth->creator, entitlement);
382 }
383 });
384
385 return value;
386 }
387
388 bool
389 auth_token_has_entitlement(auth_token_t auth, const char * entitlement)
390 {
391 __block bool entitled = false;
392
393 dispatch_sync(auth->dispatch_queue, ^{
394 if (auth->creator) {
395 entitled = process_has_entitlement(auth->creator, entitlement);
396 }
397 });
398
399 return entitled;
400 }
401
402 bool
403 auth_token_has_entitlement_for_right(auth_token_t auth, const char * right)
404 {
405 __block bool entitled = false;
406
407 dispatch_sync(auth->dispatch_queue, ^{
408 if (auth->creator) {
409 entitled = process_has_entitlement_for_right(auth->creator, right);
410 }
411 });
412
413 return entitled;
414 }
415
416 credential_t
417 auth_token_get_credential(auth_token_t auth)
418 {
419 dispatch_sync(auth->dispatch_queue, ^{
420 if (auth->credential == NULL) {
421 auth->credential = credential_create(auth->auditInfo.euid);
422 }
423 });
424
425 return auth->credential;
426 }
427
428 bool
429 auth_token_apple_signed(auth_token_t auth)
430 {
431 return auth->appleSigned;
432 }
433
434 bool auth_token_is_creator(auth_token_t auth, process_t proc)
435 {
436 assert(proc); // marked non-null
437 __block bool creator = false;
438 dispatch_sync(auth->dispatch_queue, ^{
439 if (auth->creator == proc) {
440 creator = true;
441 }
442 });
443 return creator;
444 }
445
446 void auth_token_set_state(auth_token_t auth, auth_token_state_t state)
447 {
448 auth->state |= state;
449 }
450
451 void auth_token_clear_state(auth_token_t auth, auth_token_state_t state)
452 {
453 auth->state &= ~state;
454 }
455
456 auth_token_state_t auth_token_get_state(auth_token_t auth)
457 {
458 return auth->state;
459 }
460
461 bool auth_token_check_state(auth_token_t auth, auth_token_state_t state)
462 {
463 if (state) {
464 return (auth->state & state) != 0;
465 } else {
466 return auth->state == 0;
467 }
468 }
469
470 CFDataRef auth_token_get_encryption_key(auth_token_t auth)
471 {
472 return auth->encryption_key;
473 }
474