1 /* Copyright (c) 2012-2014 Apple Inc. All Rights Reserved. */
3 #include "securityd_service.h"
4 #include "securityd_service_client.h"
10 #include <xpc/private.h>
11 #include <dispatch/dispatch.h>
12 #include <sys/types.h>
20 #include <uuid/uuid.h>
21 #include <bsm/libbsm.h>
23 #include <AssertMacros.h>
24 #include <Security/Security.h>
25 #include <Security/SecKeychainPriv.h>
27 #include <IOKit/IOKitLib.h>
28 #include <Kernel/IOKit/crypto/AppleFDEKeyStoreDefs.h>
31 #define LOG(...) syslog(LOG_ERR, ##__VA_ARGS__);
36 static pid_t
get_caller_pid(audit_token_t
* token
)
40 audit_token_to_au32(*token
, NULL
, NULL
, NULL
, NULL
, NULL
, &pid
, NULL
, NULL
);
45 // exported from libaks.a
46 kern_return_t
aks_register_for_notifications(mach_port_t server_port
, uintptr_t message_id
);
47 kern_return_t
aks_stash_escrow(keybag_handle_t handle
, bool create
, const void * secret
, int secret_len
, const void * in_data
, int in_data_len
, void ** out_data
, int * out_data_len
);
49 const char * kb_home_path
= "Library/Keychains";
50 const char * kb_user_bag
= "user.kb";
51 const char * kb_stash_bag
= "stash.kb";
53 #define HEXBUF_LEN 2048
60 } service_user_record_t
;
70 io_registry_entry_t service
;
74 service
= IOServiceGetMatchingService(kIOMasterPortDefault
, IOServiceMatching(kAppleFDEKeyStoreServiceName
));
75 if (service
== IO_OBJECT_NULL
)
76 return IO_OBJECT_NULL
;
78 kr
= IOServiceOpen(service
, mach_task_self(), 0, &conn
);
79 if (kr
!= KERN_SUCCESS
)
80 return IO_OBJECT_NULL
;
82 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStoreUserClientOpen
, NULL
, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
83 if (kr
!= KERN_SUCCESS
) {
85 return IO_OBJECT_NULL
;
92 closeiodev(io_connect_t conn
)
95 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStoreUserClientClose
, NULL
, 0, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
96 if (kr
!= KERN_SUCCESS
)
101 static dispatch_queue_t
102 _kb_service_get_dispatch_queue()
104 static dispatch_once_t onceToken
= 0;
105 static dispatch_queue_t connection_queue
= NULL
;
107 dispatch_once(&onceToken
, ^{
108 connection_queue
= dispatch_queue_create("kb-service-queue", DISPATCH_QUEUE_SERIAL
);
111 return connection_queue
;
114 static service_user_record_t
* get_user_record(uid_t uid
)
116 service_user_record_t
* ur
= NULL
;
118 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1) {
122 struct passwd pwbuf
, *pw
= NULL
;
123 if ((getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
) == 0) && pw
!= NULL
) {
124 ur
= calloc(1u, sizeof(service_user_record_t
));
126 ur
->uid
= pw
->pw_uid
;
127 ur
->gid
= pw
->pw_gid
;
128 ur
->home
= strdup(pw
->pw_dir
);
129 ur
->name
= strdup(pw
->pw_name
);
131 syslog(LOG_ERR
, "failed to lookup user record for uid: %d", uid
);
138 static void free_user_record(service_user_record_t
* ur
)
151 static const char * get_host_uuid()
153 static uuid_string_t hostuuid
= {};
154 static dispatch_once_t onceToken
;
155 dispatch_once(&onceToken
, ^{
156 struct timespec timeout
= {30, 0};
158 if (gethostuuid(uuid
, &timeout
) == 0) {
159 uuid_unparse(uuid
, hostuuid
);
161 syslog(LOG_ERR
, "failed to get host uuid");
169 _kb_copy_bag_filename(service_user_record_t
* ur
, kb_bag_type_t type
)
171 char * bag_file
= NULL
;
172 const char * name
= NULL
;
176 case kb_bag_type_user
:
179 case kb_bag_type_stash
:
186 bag_file
= calloc(1u, PATH_MAX
);
187 require(bag_file
, done
);
189 snprintf(bag_file
, PATH_MAX
, "%s/%s/%s/%s", ur
->home
, kb_home_path
, get_host_uuid(), name
);
196 _kb_verify_create_path(service_user_record_t
* ur
)
198 bool created
= false;
199 struct stat st_info
= {};
200 char new_path
[PATH_MAX
] = {};
201 char kb_path
[PATH_MAX
] = {};
202 snprintf(kb_path
, sizeof(kb_path
), "%s/%s/%s", ur
->home
, kb_home_path
, get_host_uuid());
203 if (lstat(kb_path
, &st_info
) == 0) {
204 if (S_ISDIR(st_info
.st_mode
)) {
207 syslog(LOG_ERR
, "invalid directory at '%s' moving aside", kb_path
);
208 snprintf(new_path
, sizeof(new_path
), "%s-invalid", kb_path
);
210 if (rename(kb_path
, new_path
) != 0) {
211 syslog(LOG_ERR
, "failed to rename file: %s (%s)", kb_path
, strerror(errno
));
217 require_action(mkpath_np(kb_path
, 0700) == 0, done
, syslog(LOG_ERR
, "could not create path: %s (%s)", kb_path
, strerror(errno
)));
226 _set_thread_credentials(service_user_record_t
* ur
)
228 int rc
= pthread_setugid_np(ur
->uid
, ur
->gid
);
229 if (rc
) { syslog(LOG_ERR
, "failed to set thread credential: %i (%s)", errno
, strerror(errno
)); }
231 rc
= initgroups(ur
->name
, ur
->gid
);
232 if (rc
) { syslog(LOG_ERR
, "failed to initgroups: %i", rc
); }
236 _clear_thread_credentials()
238 int rc
= pthread_setugid_np(KAUTH_UID_NONE
, KAUTH_GID_NONE
);
239 if (rc
) { syslog(LOG_ERR
, "failed to reset thread credential: %i (%s)", errno
, strerror(errno
)); }
243 _kb_bag_exists(service_user_record_t
* ur
, const char * bag_file
)
246 struct stat st_info
= {};
247 char new_file
[PATH_MAX
] = {};
251 _set_thread_credentials(ur
);
252 if (lstat(bag_file
, &st_info
) == 0) {
253 if (S_ISREG(st_info
.st_mode
)) {
256 syslog(LOG_ERR
, "invalid file at '%s' moving aside", bag_file
);
257 snprintf(new_file
, sizeof(new_file
), "%s-invalid", bag_file
);
259 if (rename(bag_file
, new_file
) != 0) {
260 syslog(LOG_ERR
, "failed to rename file: %s (%s)", bag_file
, strerror(errno
));
266 _clear_thread_credentials();
271 _kb_save_bag_to_disk(service_user_record_t
* ur
, const char * bag_file
, void * data
, size_t length
)
276 require(bag_file
, done
);
278 _set_thread_credentials(ur
);
279 require(_kb_verify_create_path(ur
), done
);
281 fd
= open(bag_file
, O_CREAT
| O_TRUNC
| O_WRONLY
| O_NOFOLLOW
, 0600);
282 require_action(fd
!= -1, done
, syslog(LOG_ERR
, "could not create file: %s (%s)", bag_file
, strerror(errno
)));
283 require_action(write(fd
, data
, length
) != -1, done
, syslog(LOG_ERR
, "failed to write keybag to disk %s", strerror(errno
)));
288 if (fd
!= -1) { close(fd
); }
289 _clear_thread_credentials();
294 _kb_load_bag_from_disk(service_user_record_t
* ur
, const char * bag_file
, uint8_t ** data
, size_t * length
)
298 uint8_t * buf
= NULL
;
300 struct stat st_info
= {};
302 require(bag_file
, done
);
304 _set_thread_credentials(ur
);
305 require(_kb_verify_create_path(ur
), done
);
306 require_quiet(lstat(bag_file
, &st_info
) == 0, done
);
307 require_action(S_ISREG(st_info
.st_mode
), done
, syslog(LOG_ERR
, "failed to load, not a file: %s", bag_file
));
308 buf_size
= (size_t)st_info
.st_size
;
310 fd
= open(bag_file
, O_RDONLY
| O_NOFOLLOW
);
311 require_action(fd
!= -1, done
, syslog(LOG_ERR
, "could not open file: %s (%s)", bag_file
, strerror(errno
)));
313 buf
= (uint8_t *)calloc(1u, buf_size
);
314 require(buf
!= NULL
, done
);
315 require(read(fd
, buf
, buf_size
) == buf_size
, done
);
323 if (fd
!= -1) { close(fd
); }
324 if (buf
) { free(buf
); }
325 _clear_thread_credentials();
330 _kb_rename_bag_on_disk(service_user_record_t
* ur
, const char * bag_file
)
332 char new_file
[PATH_MAX
] = {};
334 _set_thread_credentials(ur
);
335 snprintf(new_file
, sizeof(new_file
), "%s-invalid", bag_file
);
337 rename(bag_file
, new_file
);
338 _clear_thread_credentials();
343 _kb_delete_bag_on_disk(service_user_record_t
* ur
, const char * bag_file
)
346 _set_thread_credentials(ur
);
348 _clear_thread_credentials();
353 _kb_get_session_handle(service_context_t
* context
, keybag_handle_t
* handle_out
)
355 int rc
= KB_BagNotLoaded
;
356 keybag_handle_t session_handle
= bad_keybag_handle
;
357 require_noerr_quiet(aks_get_system(context
->s_uid
, &session_handle
), done
);
359 *handle_out
= session_handle
;
366 static void update_keybag_handle(keybag_handle_t handle
)
368 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
369 uid_t uid
= abs(handle
);
370 uint8_t * buf
= NULL
;
372 service_user_record_t
* ur
= NULL
;
373 char * bag_file
= NULL
;
375 require_noerr(aks_save_bag(handle
, (void**)&buf
, (int*)&buf_size
), done
);
376 require(ur
= get_user_record(uid
), done
);
377 require(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
);
378 require(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
);
380 syslog(LOG_NOTICE
, "successfully updated handle %d", handle
);
384 if (ur
) free_user_record(ur
);
385 if (bag_file
) free(bag_file
);
390 service_kb_create(service_context_t
* context
, const void * secret
, int secret_len
)
392 __block
int rc
= KB_GeneralError
;
394 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
395 uint8_t * buf
= NULL
;
397 keybag_handle_t session_handle
= bad_keybag_handle
;
398 service_user_record_t
* ur
= get_user_record(context
->s_uid
);
399 char * bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
);
401 require(bag_file
, done
);
403 // check for the existance of the bagfile
404 require_action(!_kb_bag_exists(ur
, bag_file
), done
, rc
= KB_BagExists
);
406 require_noerr(rc
= aks_create_bag(secret
, secret_len
, kAppleKeyStoreDeviceBag
, &session_handle
), done
);
407 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
408 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
409 require_noerr(rc
= aks_set_system(session_handle
, context
->s_uid
), done
);
410 aks_unload_bag(session_handle
);
411 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
413 if (secret
&& rc
== KB_Success
) {
414 aks_unlock_bag(session_handle
, secret
, secret_len
);
419 if (bag_file
) { free(bag_file
); }
420 if (ur
) free_user_record(ur
);
427 service_kb_load(service_context_t
* context
)
429 __block
int rc
= KB_GeneralError
;
431 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
432 uint8_t * buf
= NULL
;
434 keybag_handle_t session_handle
= bad_keybag_handle
;
435 service_user_record_t
* ur
= NULL
;
436 char * bag_file
= NULL
;
438 rc
= aks_get_system(context
->s_uid
, &session_handle
);
439 if (rc
== kIOReturnNotFound
) {
440 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
441 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
442 require_action_quiet(_kb_load_bag_from_disk(ur
, bag_file
, &buf
, &buf_size
), done
, rc
= KB_BagNotFound
);
443 rc
= aks_load_bag(buf
, (int)buf_size
, &session_handle
);
444 if (rc
== kIOReturnNotPermitted
) {
445 syslog(LOG_ERR
, "error loading keybag for uid (%i) in session (%i)", context
->s_uid
, context
->s_id
);
446 _kb_rename_bag_on_disk(ur
, bag_file
);
449 require_noerr(rc
, done
);
450 require_noerr(rc
= aks_set_system(session_handle
, context
->s_uid
), done
);
451 aks_unload_bag(session_handle
);
453 require(rc
== KB_Success
, done
);
457 if (ur
) free_user_record(ur
);
458 if (bag_file
) free(bag_file
);
465 service_kb_save(service_context_t
* context
)
467 __block
int rc
= KB_GeneralError
;
468 keybag_handle_t session_handle
;
469 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
471 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
472 uint8_t * buf
= NULL
;
474 service_user_record_t
* ur
= NULL
;
475 char * bag_file
= NULL
;
477 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
478 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
479 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
480 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
486 if (ur
) free_user_record(ur
);
487 if (bag_file
) free(bag_file
);
496 service_kb_unlock(service_context_t
* context
, const void * secret
, int secret_len
)
498 int rc
= KB_GeneralError
;
499 keybag_handle_t session_handle
;
500 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
502 rc
= aks_unlock_bag(session_handle
, secret
, secret_len
);
509 service_kb_lock(service_context_t
* context
)
511 int rc
= KB_GeneralError
;
512 keybag_handle_t session_handle
;
513 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
515 rc
= aks_lock_bag(session_handle
);
522 service_kb_change_secret(service_context_t
* context
, const void * secret
, int secret_len
, const void * new_secret
, int new_secret_len
)
524 __block
int rc
= KB_GeneralError
;
525 keybag_handle_t session_handle
;
526 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
528 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
529 uint8_t * buf
= NULL
;
531 service_user_record_t
* ur
= NULL
;
532 char * bag_file
= NULL
;
534 require_noerr(rc
= aks_change_secret(session_handle
, secret
, secret_len
, new_secret
, new_secret_len
, NULL
, NULL
), done
);
535 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
536 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
537 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
538 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
544 if (ur
) free_user_record(ur
);
545 if (bag_file
) free(bag_file
);
554 service_kb_reset(service_context_t
* context
, const void * secret
, int secret_len
)
556 __block
int rc
= KB_GeneralError
;
557 service_user_record_t
* ur
= NULL
;
558 char * bag_file
= NULL
;
560 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
561 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_user
), done
, rc
= KB_GeneralError
);
563 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
564 uint8_t * buf
= NULL
;
566 keybag_handle_t session_handle
= bad_keybag_handle
;
568 syslog(LOG_ERR
, "resetting keybag for uid (%i) in session (%i)", context
->s_uid
, context
->s_id
);
569 _kb_rename_bag_on_disk(ur
, bag_file
);
571 require_noerr(rc
= aks_create_bag(secret
, secret_len
, kAppleKeyStoreDeviceBag
, &session_handle
), done
);
572 require_noerr(rc
= aks_save_bag(session_handle
, (void**)&buf
, (int*)&buf_size
), done
);
573 require_action(_kb_save_bag_to_disk(ur
, bag_file
, buf
, buf_size
), done
, rc
= KB_BagError
);
574 require_noerr(rc
= aks_set_system(session_handle
, context
->s_uid
), done
);
575 aks_unload_bag(session_handle
);
576 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
578 if (secret
&& rc
== KB_Success
) {
579 aks_unlock_bag(session_handle
, secret
, secret_len
);
588 if (ur
) free_user_record(ur
);
589 if (bag_file
) free(bag_file
);
594 service_kb_is_locked(service_context_t
* context
, xpc_object_t reply
)
596 int rc
= KB_GeneralError
;
597 keybag_state_t state
;
598 keybag_handle_t session_handle
;
599 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
601 require_noerr(rc
= aks_get_lock_state(session_handle
, &state
), done
);
603 xpc_dictionary_set_bool(reply
, SERVICE_XPC_LOCKED
, state
& keybag_state_locked
);
604 xpc_dictionary_set_bool(reply
, SERVICE_XPC_NO_PIN
, state
& keybag_state_no_pin
);
611 service_kb_stash_create(service_context_t
* context
, const void * key
, unsigned key_size
)
613 int rc
= KB_GeneralError
;
614 char * bag_file
= NULL
;
615 keybag_handle_t session_handle
;
616 service_user_record_t
* ur
= NULL
;
617 void * stashbag
= NULL
;
618 int stashbag_size
= 0;
619 __block
bool saved
= false;
622 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
623 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
624 require_noerr(rc
= aks_stash_escrow(session_handle
, true, key
, key_size
, NULL
, 0, (void**)&stashbag
, &stashbag_size
), done
);
625 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_stash
), done
, rc
= KB_GeneralError
);
627 // sync writing the bag to disk
628 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
629 saved
= _kb_save_bag_to_disk(ur
, bag_file
, stashbag
, stashbag_size
);
631 require_action(saved
, done
, rc
= KB_BagError
);
635 if (stashbag
) { free(stashbag
); }
636 if (bag_file
) { free(bag_file
); }
637 if (ur
) free_user_record(ur
);
642 service_kb_stash_load(service_context_t
* context
, const void * key
, unsigned key_size
, bool nondestructive
)
644 __block
int rc
= KB_GeneralError
;
645 char * bag_file
= NULL
;
646 keybag_handle_t session_handle
;
647 service_user_record_t
* ur
= NULL
;
648 __block
uint8_t * stashbag
= NULL
;
649 __block
size_t stashbag_size
= 0;
652 require_noerr(rc
= _kb_get_session_handle(context
, &session_handle
), done
);
653 require_action(ur
= get_user_record(context
->s_uid
), done
, rc
= KB_GeneralError
);
654 require_action(bag_file
= _kb_copy_bag_filename(ur
, kb_bag_type_stash
), done
, rc
= KB_GeneralError
);
656 // sync loading the bag from disk
657 dispatch_sync(_kb_service_get_dispatch_queue(), ^{
658 if (!_kb_load_bag_from_disk(ur
, bag_file
, &stashbag
, &stashbag_size
)) {
662 require_noerr(rc
, done
);
664 require_noerr(rc
= aks_stash_escrow(session_handle
, false, key
, key_size
, stashbag
, (int)stashbag_size
, NULL
, NULL
), done
);
668 if (stashbag
) { free(stashbag
); }
669 if ((bag_file
) && (!nondestructive
)) {
670 _kb_delete_bag_on_disk(ur
, bag_file
);
673 if (ur
) free_user_record(ur
);
678 // Get the keychain master key from the AppleFDEKeyStore.
679 // Note that this is a one-time call - the master key is
680 // removed from the keystore after it is returned.
681 // Requires the entitlement: com.apple.private.securityd.keychain
683 OSStatus
service_stash_get_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
685 getStashKey_InStruct_t inStruct
;
686 getStashKey_OutStruct_t outStruct
;
687 size_t outSize
= sizeof(outStruct
);
688 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
690 io_connect_t conn
= openiodev();
692 inStruct
.type
= kAppleFDEKeyStoreStash_master
;
694 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_getStashKey
,
696 &inStruct
, sizeof(inStruct
),
698 &outStruct
, &outSize
);
700 if (kr
== KERN_SUCCESS
) {
701 xpc_dictionary_set_data(reply
, SERVICE_XPC_KEY
, outStruct
.outBuf
.key
.key
, outStruct
.outBuf
.key
.keysize
);
702 service_kb_stash_load(context
, outStruct
.outBuf
.key
.key
, outStruct
.outBuf
.key
.keysize
, false);
713 // Stash the keychain master key in the AppleFDEKeyStore and
714 // flag it as the keychain master key to be added to the
715 // reboot NVRAM blob.
716 // This requires two calls to the AKS: the first to store the
717 // key and get its uuid. The second uses the uuid to flag the
718 // key for blob inclusion.
720 OSStatus
service_stash_set_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
722 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
723 size_t keydata_len
= 0;
726 io_connect_t conn
= openiodev();
729 // Store the key in the keystore and get its uuid
730 setKeyGetUUID_InStruct_t inStruct1
;
731 uuid_OutStruct_t outStruct1
;
734 const uint8_t *keydata
= xpc_dictionary_get_data(event
, SERVICE_XPC_KEY
, &keydata_len
);
735 require(keydata
, done
);
737 memcpy(&inStruct1
.inKey
.key
.key
, keydata
, keydata_len
);
738 inStruct1
.inKey
.key
.keysize
= (cryptosize_t
) keydata_len
;
739 len
= sizeof(outStruct1
);
740 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_setKeyGetUUID
,
742 &inStruct1
, sizeof(inStruct1
),
745 require(kr
== KERN_SUCCESS
, done
);
747 // Now using the uuid stash it as the master key
748 setStashKey_InStruct_t inStruct2
;
749 memcpy(&inStruct2
.uuid
, &outStruct1
.uuid
, sizeof(outStruct1
.uuid
));
750 inStruct2
.type
= kAppleFDEKeyStoreStash_master
;
752 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_setStashKey
,
754 &inStruct2
, sizeof(inStruct2
),
758 if (kr
== KERN_SUCCESS
) {
759 service_kb_stash_create(context
, keydata
, (unsigned)keydata_len
);
769 // Load the master stash key
771 OSStatus
service_stash_load_key(service_context_t
* context
, xpc_object_t event
, xpc_object_t reply
)
773 kern_return_t kr
= KERN_SUCCESS
;
774 size_t keydata_len
= 0;
776 const uint8_t *keydata
= xpc_dictionary_get_data(event
, SERVICE_XPC_KEY
, &keydata_len
);
777 require(keydata
, done
);
779 kr
= service_kb_stash_load(context
, keydata
, (cryptosize_t
) keydata_len
, true);
786 // Signal the AppleFDEKeyStore to take the tagged FDE key
787 // and keychain master key, stash them in an encrypted
788 // blob structure and write the blob to NVRAM. The random
789 // encryption key is written to the SMC.
792 OSStatus
service_stash_blob(xpc_object_t event
, xpc_object_t reply
)
794 kern_return_t kr
= KERN_INVALID_ARGUMENT
;
796 io_connect_t conn
= openiodev();
799 kr
= IOConnectCallMethod(conn
, kAppleFDEKeyStore_commitStash
,
812 bool peer_has_entitlement(xpc_connection_t peer
, const char * entitlement
)
814 bool entitled
= false;
816 xpc_object_t value
= xpc_connection_copy_entitlement_value(peer
, entitlement
);
817 if (value
&& (xpc_get_type(value
) == XPC_TYPE_BOOL
)) {
818 entitled
= xpc_bool_get_value(value
);
821 if (value
) xpc_release(value
);
825 static char * sel_to_char(uint64_t sel
)
828 case SERVICE_STASH_SET_KEY
:
830 case SERVICE_STASH_GET_KEY
:
832 case SERVICE_STASH_BLOB
:
834 case SERVICE_KB_LOAD
:
836 case SERVICE_KB_SAVE
:
838 case SERVICE_KB_UNLOCK
:
840 case SERVICE_KB_LOCK
:
842 case SERVICE_KB_CHANGE_SECRET
:
843 return "kb_change_secret";
844 case SERVICE_KB_CREATE
:
846 case SERVICE_KB_IS_LOCKED
:
847 return "kb_is_locked";
848 case SERVICE_KB_RESET
:
855 static char * err_to_char(int err
)
860 case KB_GeneralError
:
861 return "general error";
863 return "bag not found";
866 case KB_BagNotLoaded
:
867 return "bag not loaded";
870 case KB_InvalidSession
:
871 return "invalid session";
877 void service_peer_event_handler(xpc_connection_t connection
, xpc_object_t event
)
879 xpc_type_t type
= xpc_get_type(event
);
881 if (type
== XPC_TYPE_ERROR
) {
882 if (event
== XPC_ERROR_CONNECTION_INVALID
) {
885 assert(type
== XPC_TYPE_DICTIONARY
);
887 int rc
= KB_GeneralError
;
888 uint64_t request
= 0;
889 const uint8_t * secret
= NULL
, * new_secret
= NULL
;
890 size_t secret_len
= 0, new_secret_len
= 0, data_len
= 0;
891 service_context_t
* context
= NULL
;
894 xpc_object_t reply
= xpc_dictionary_create_reply(event
);
896 data
= xpc_dictionary_get_data(event
, SERVICE_XPC_CONTEXT
, &data_len
);
898 require(data_len
== sizeof(service_context_t
), done
);
899 context
= (service_context_t
*)data
;
901 request
= xpc_dictionary_get_uint64(event
, SERVICE_XPC_REQUEST
);
903 require_action(context
->s_id
!= AU_DEFAUDITSID
, done
, rc
= KB_InvalidSession
);
904 require_action(context
->s_uid
!= AU_DEFAUDITID
, done
, rc
= KB_InvalidSession
); // we only want to work in actual user sessions.
907 case SERVICE_KB_CREATE
:
908 // if (kb_service_has_entitlement(peer, "com.apple.keystore.device")) {
909 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
910 rc
= service_kb_create(context
, secret
, (int)secret_len
);
913 case SERVICE_KB_LOAD
:
914 rc
= service_kb_load(context
);
916 case SERVICE_KB_SAVE
:
917 rc
= service_kb_save(context
);
919 case SERVICE_KB_UNLOCK
:
920 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
921 rc
= service_kb_unlock(context
, secret
, (int)secret_len
);
923 case SERVICE_KB_LOCK
:
924 rc
= service_kb_lock(context
);
926 case SERVICE_KB_CHANGE_SECRET
:
927 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
928 new_secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET_NEW
, &new_secret_len
);
929 rc
= service_kb_change_secret(context
, secret
, (int)secret_len
, new_secret
, (int)new_secret_len
);
931 case SERVICE_KB_RESET
:
932 secret
= xpc_dictionary_get_data(event
, SERVICE_XPC_SECRET
, &secret_len
);
933 rc
= service_kb_reset(context
, secret
, (int)secret_len
);
935 case SERVICE_KB_IS_LOCKED
:
936 rc
= service_kb_is_locked(context
, reply
);
938 case SERVICE_STASH_GET_KEY
:
939 rc
= service_stash_get_key(context
, event
, reply
);
941 case SERVICE_STASH_SET_KEY
:
942 rc
= service_stash_set_key(context
, event
, reply
);
944 case SERVICE_STASH_LOAD_KEY
:
945 rc
= service_stash_load_key(context
, event
, reply
);
948 case SERVICE_STASH_BLOB
:
949 rc
= service_stash_blob(event
, reply
);
953 LOG("unknown service type");
959 LOG("selector: %s (%llu), error: %s (%x), sid: %d, suid: %d, pid: %d", sel_to_char(request
), request
, err_to_char(rc
), rc
, context
? context
->s_id
: 0, context
? context
->s_uid
: 0, context
? get_caller_pid(&context
->procToken
) : 0);
962 syslog(LOG_NOTICE
, "selector: %s (%llu), error: %s (%x), sid: %d, suid: %d, pid: %d", sel_to_char(request
), request
, err_to_char(rc
), rc
, context
? context
->s_id
: 0, context
? context
->s_uid
: 0, context
? get_caller_pid(&context
->procToken
) : 0);
965 xpc_dictionary_set_int64(reply
, SERVICE_XPC_RC
, rc
);
966 xpc_connection_send_message(connection
, reply
);
971 bool check_signature(xpc_connection_t connection
)
973 CFStringRef reqStr
= CFSTR("identifier com.apple.securityd and anchor apple");
974 SecRequirementRef requirement
= NULL
;
975 SecCodeRef codeRef
= NULL
;
976 CFMutableDictionaryRef codeDict
= NULL
;
977 CFNumberRef codePid
= NULL
;
978 pid_t pid
= xpc_connection_get_pid(connection
);
980 OSStatus status
= SecRequirementCreateWithString(reqStr
, kSecCSDefaultFlags
, &requirement
);
981 require_action(status
== errSecSuccess
, done
, LOG("failed to create requirement"));
983 codeDict
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
984 codePid
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &pid
);
985 CFDictionarySetValue(codeDict
, kSecGuestAttributePid
, codePid
);
986 status
= SecCodeCopyGuestWithAttributes(NULL
, codeDict
, kSecCSDefaultFlags
, &codeRef
);
987 require_action(status
== errSecSuccess
, done
, LOG("failed to get code ref"));
989 status
= SecCodeCheckValidity(codeRef
, kSecCSDefaultFlags
,
990 #if DEBUG || RC_BUILDIT_YES
995 require_action(status
== errSecSuccess
, done
, syslog(LOG_ERR
, "pid %d, does not satisfy code requirment (%d)", pid
, status
));
998 if (codeRef
) CFRelease(codeRef
);
999 if (requirement
) CFRelease(requirement
);
1000 if (codeDict
) CFRelease(codeDict
);
1001 if (codePid
) CFRelease(codePid
);
1003 return (status
== errSecSuccess
);
1006 static void register_for_notifications()
1008 __block kern_return_t kr
;
1009 static mach_port_t mp
= MACH_PORT_NULL
;
1011 static dispatch_once_t onceToken
= 0;
1012 dispatch_once(&onceToken
, ^{
1013 kr
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &mp
);
1014 if (kr
== KERN_SUCCESS
) {
1015 dispatch_source_t mach_src
= dispatch_source_create(DISPATCH_SOURCE_TYPE_MACH_RECV
, mp
, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0));
1016 dispatch_source_set_event_handler(mach_src
, ^{
1017 mach_msg_return_t mr
;
1018 uint8_t buf
[sizeof(aks_notification_msg_t
) + MAX_TRAILER_SIZE
] = {};
1019 aks_notification_msg_t
* msg
= (aks_notification_msg_t
*)buf
;
1020 mr
= mach_msg((mach_msg_header_t
*)&buf
, MACH_RCV_MSG
, 0, sizeof(buf
), mp
, MACH_MSG_TIMEOUT_NONE
, MACH_PORT_NULL
);
1021 if (mr
== MACH_MSG_SUCCESS
&& msg
->hdr
.msgh_id
== AKS_NOTIFICATION_MSGID
) {
1023 } else if (mr
== MACH_MSG_SUCCESS
&& msg
->hdr
.msgh_id
== AKS_NOTIFICATION_WRITE_SYSTEM_KEYBAG
) {
1024 syslog(LOG_NOTICE
, "request to update handle %d", msg
->handle
);
1025 update_keybag_handle(msg
->handle
);
1027 syslog(LOG_ERR
, "mach_msg error: %x", mr
);
1030 dispatch_resume(mach_src
);
1032 syslog(LOG_NOTICE
, "failed to create notification port");
1037 kr
= aks_register_for_notifications(mp
, AKS_NOTIFICATION_WRITE_SYSTEM_KEYBAG
);
1038 if (kr
== KERN_SUCCESS
) {
1039 syslog(LOG_NOTICE
, "registered for notifications");
1041 syslog(LOG_NOTICE
, "failed to register for notifications %d", kr
);
1045 int main(int argc
, const char * argv
[])
1048 if (sandbox_init(SECURITYD_SERVICE_NAME
, SANDBOX_NAMED
, &errorbuf
) != 0) {
1049 syslog(LOG_ERR
, "sandbox_init failed %s", errorbuf
);
1050 sandbox_free_error(errorbuf
);
1056 register_for_notifications();
1058 xpc_connection_t listener
= xpc_connection_create_mach_service(SECURITYD_SERVICE_NAME
, NULL
, XPC_CONNECTION_MACH_SERVICE_LISTENER
);
1059 xpc_connection_set_event_handler(listener
, ^(xpc_object_t peer
) {
1060 // It is safe to cast 'peer' to xpc_connection_t assuming
1061 // we have a correct configuration in our launchd.plist.
1063 if (xpc_connection_get_euid(peer
) != 0) {
1064 xpc_connection_cancel(peer
);
1068 if (!check_signature(peer
)) {
1069 xpc_connection_cancel(peer
);
1073 xpc_connection_set_event_handler(peer
, ^(xpc_object_t event
) {
1074 vproc_transaction_t transaction
= vproc_transaction_begin(NULL
);
1075 service_peer_event_handler(peer
, event
);
1076 vproc_transaction_end(NULL
, transaction
);
1078 xpc_connection_resume(peer
);
1080 xpc_connection_resume(listener
);