iOS 8 通知操作:"Access to item attempted while keychain is locked" 访问 "didFinishLaunchingWithOptions" 中的钥匙串时出错

iOS 8 notification action: "Access to item attempted while keychain is locked" error when accessing keychain in "didFinishLaunchingWithOptions"

我观察到 SecItemCopyMatching 在锁定 phone 上处理 iOS 8 推送通知时抛出的设备控制台错误。详细复现步骤如下:

  1. 卸载所有以前版本的应用程序。在设备上构建应用程序的 Appstore 版本。强制退出应用程序。
  2. 增加内部版本号,并在设备上构建更新的版本。这模拟了应用程序更新流程。强制退出应用程序(在现实生活中,由于内存压力,应用程序可能会被 OS 杀死。强制退出模拟了这种行为)。
  3. 当 phone 被锁定时,向应用程序发送推送通知。
  4. 在 phone 锁定的情况下,向左滑动以查看操作按钮,然后按其中一个操作按钮。
  5. 应用程序被唤醒,didFinishLaunchingWithOptions 被调用,尝试访问钥匙串项目。 运行 SecItemCopyMatching 时,设备控制台中出现错误 Access to item attempted while keychain is locked

完整的错误日志如下所示。最后一行给出了特定于应用程序的错误消息。

ReportCrash[32481] <Error>: task_set_exception_ports(B07, 400, D03, 0, 0) failed with error (4: (os/kern) invalid argument)
ReportCrash[32481] <Notice>: ReportCrash acting against PID 31423
diagnosticd[32258] <Error>: error evaluating process info - pid: 31423, punique: 131317
ReportCrash[32481] <Notice>: Formulating crash report for process cfprefsd[31423]
com.apple.xpc.launchd[1] (com.apple.cfprefsd.xpc.daemon[31423]) <Notice>: Service exited due to signal: Bus error: 10
My App[32480] <Error>: assertion failed: 12F70: libxpc.dylib + 71768 [B870B51D-AA85-3686-A7D9-ACD48C5FE153]: 0x7d
Unknown[32480] <Error>: 
ReportCrash[32481] <Notice>: Saved report to /Library/Logs/CrashReporter/cfprefsd_2015-07-02-150139_Xianjing-Hus-iPhone.ips
securityd[32279] <Error>:  s3dl_query_row decode genp,rowid=8099 failed (-25308): The operation couldn’t be completed. (OSStatus error -25308 - ks_crypt: e00002e2 failed to     unwrap item (class 6, bag: 0) Access to item attempted while keychain is locked.)
securityd[32279] <Error>:  securityd_xpc_dictionary_handler Okta Verify[32480] copy_matching The operation couldn’t be completed. (OSStatus error -25308 - ks_crypt:     e00002e2 failed to unwrap item (class 6, bag: 0) Access to item attempted while keychain is locked.)
My App[32480] <Error>:  SecOSStatusWith error:[-25308] The operation couldn’t be completed. (OSStatus error -25308 - Remote error : The operation couldn't be completed. (OSStatus error -25308 - ks_crypt: e00002e2 failed to unwrap item (class 6, bag: 0) Access to item attempted while keychain is locked.))

几件事:

有没有人看到过类似的错误?如果有,您是如何解决这个问题的?感谢任何帮助。

更新:附加 cfprefsd 崩溃日志

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: unknown at 0x00000001007d4000
Triggered by Thread:  2

Thread 0 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 0:
0   libsystem_kernel.dylib          0x0000000197d88c24 kevent64 + 8
1   libdispatch.dylib               0x0000000197c6de6c _dispatch_mgr_invoke + 272
2   libdispatch.dylib               0x0000000197c5f998 _dispatch_mgr_thread + 48

Thread 1 name:  Dispatch queue: com.apple.root.default-qos.overcommit
Thread 1:
0   libsystem_kernel.dylib          0x0000000197da3984 __sigsuspend_nocancel + 8
1   libdispatch.dylib               0x0000000197c6921c _dispatch_sigsuspend + 24
2   libdispatch.dylib               0x0000000197c69200 _dispatch_sig_thread + 44

Thread 2 name:  Dispatch queue: src
Thread 2 Crashed:
0   libsystem_platform.dylib        0x0000000197e35300 _platform_memmove + 176
1   libxpc.dylib                    0x0000000197e6567c xpc_data_create + 84
2   CoreFoundation                  0x0000000185d5a9b8 -[CFPDSource acceptMessage:] + 1956
3   CoreFoundation                  0x0000000185dc0da8 __handle_synchronize_message_block_invoke103 + 172
4   CoreFoundation                  0x0000000185d57c58 __88+[CFPDSource withSourceForDomain:inContainer:user:byHost:managed:synchronously:perform:]_block_invoke_2 + 24
5   CoreFoundation                  0x0000000185d5955c __25-[CFPDSource lockedSync:]_block_invoke + 44
6   libdispatch.dylib               0x0000000197c5d950 _dispatch_client_callout + 12
7   libdispatch.dylib               0x0000000197c671e0 _dispatch_barrier_sync_f_invoke + 72
8   CoreFoundation                  0x0000000185d59520 -[CFPDSource lockedSync:] + 80
9   CoreFoundation                  0x0000000185d57c0c __88+[CFPDSource withSourceForDomain:inContainer:user:byHost:managed:synchronously:perform:]_block_invoke + 504
10  libdispatch.dylib               0x0000000197c5d950 _dispatch_client_callout + 12
11  libdispatch.dylib               0x0000000197c671e0 _dispatch_barrier_sync_f_invoke + 72
12  CoreFoundation                  0x0000000185d576fc +[CFPDSource withSourceForDomain:inContainer:user:byHost:managed:synchronously:perform:] + 364
13  CoreFoundation                  0x0000000185dc0508 handle_message + 1312
14  CoreFoundation                  0x0000000185dc081c __handle_multi_message_block_invoke_2 + 124
15  libxpc.dylib                    0x0000000197e657c0 xpc_array_apply + 76
16  CoreFoundation                  0x0000000185dc05f8 handle_message + 1552
17  CoreFoundation                  0x0000000185dbffd4 ____CFXPreferencesDaemon_main_block_invoke_5 + 132
18  libxpc.dylib                    0x0000000197e64cc8 _xpc_connection_call_event_handler + 64
19  libxpc.dylib                    0x0000000197e62bcc _xpc_connection_mach_event + 2156
20  libdispatch.dylib               0x0000000197c5da24 _dispatch_client_callout4 + 12
21  libdispatch.dylib               0x0000000197c6113c _dispatch_mach_msg_invoke + 488
22  libdispatch.dylib               0x0000000197c682d0 _dispatch_queue_drain + 2004
23  libdispatch.dylib               0x0000000197c60664 _dispatch_mach_invoke + 132
24  libdispatch.dylib               0x0000000197c6a314 _dispatch_root_queue_drain + 716
25  libdispatch.dylib               0x0000000197c6bc48 _dispatch_worker_thread3 + 104
26  libsystem_pthread.dylib         0x0000000197e3d228 _pthread_wqthread + 812
27  libsystem_pthread.dylib         0x0000000197e3ceec start_wqthread + 0

当您的设备上有密码并且设备被锁定时,对钥匙串的访问将被阻止。因此,如果您有密码,则无法在锁定屏幕上使用钥匙串执行任何操作。

此错误 肯定 是由于在设备仍处于锁定状态时尝试访问 kSecAttrAccessibleWhenUnlocked 的项目引起的。您可以通过查看您提供的日志的以下行来判断这一点:

securityd[32279] <Error>:  securityd_xpc_dictionary_handler Okta Verify[32480] copy_matching The operation couldn’t be completed. (OSStatus error -25308 - ks_crypt:     e00002e2 failed to unwrap item (class 6, bag: 0) Access to item attempted while keychain is locked.)

Class 6 是 kSecAttrAccessibleWhenUnlockedkSecAttrAccessibleAlways 是 class 8)——请参阅 this deck 的幻灯片 15 了解更多详情——所以你的行为预计会看到。

现在真正的问题是,为什么您认为它是 kSecAttrAccessibleAlways 而该项目最终却是 kSecAttrAccessibleWhenUnlocked。如果没有看到更多代码 and/or 有更多信息,很难判断,但这里有几点需要考虑:

  • 卸载应用程序时不会删除钥匙串项 - 它们会在应用程序 reinstallation/upgrade 中保留下来。因此,如果该应用程序的早期版本将项目创建为 kSecAttrAccessibleWhenUnlocked,它可能会继续执行。尝试删除该项目并重新创建它(并检查 SecItemDelete()SecItemAdd() 的 return 值以确保它已完成)。
  • 仔细检查 kSecAttrAccessibleAlways 是否已传递给 SecItemAdd(),以便 iOS 不会自行应用任何默认值。
  • 请注意,可访问性 class 必须在创建项目时传递(即传递给 SecItemAdd()),而不是在检索它时传递(即不传递给 SecItemCopyMatching())。这很明显,但重申一下也无妨。

如果以上 none 有帮助,请 post 显示项目创建方式和读取方式的相关代码。