From 47a7acc7410e5623acc4a57ee1cd9bc08e35a2c1 Mon Sep 17 00:00:00 2001 From: shenlei Date: Tue, 24 Nov 2026 16:37:52 +0900 Subject: [PATCH] =?UTF-8?q?feat(llvm):=20=E5=8A=A0=E5=9B=BA=20SD=20?= =?UTF-8?q?=E7=AD=BE=E5=90=8D=E4=B8=8E=E5=86=85=E8=B4=AD=E4=BA=A4=E6=98=93?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/ig_llvm_policy.json | 8 ++++++++ tests/test_indirect_branch_pass.py | 13 ++++++++++++- tools/llvm_obf/build_obfuscated_ipa.py | 3 ++- tools/llvm_obf/verify_macho_indirect_branches.py | 9 +++++++++ tools/xcode_archive/audit_exported_ipa.py | 8 +++++++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/config/ig_llvm_policy.json b/config/ig_llvm_policy.json index c611270..d697eee 100644 --- a/config/ig_llvm_policy.json +++ b/config/ig_llvm_policy.json @@ -688,6 +688,9 @@ { "id": "swift_injection_det_ibr", "path_contains": "RDInjectionDetector.swift" }, { "id": "swift_jailbreak_det_ibr", "path_contains": "RDJailbreakDetector.swift" }, { "id": "swift_sd_signer_ibr", "path_contains": "RDSDAuthV32Signer.swift" }, + { "id": "swift_sd_request_format_ibr", "path_contains": "RDRequestParamsFormat.swift" }, + { "id": "swift_sd_private_key_manager_ibr", "path_contains": "RDSDPrivateKeyManager.swift" }, + { "id": "swift_iap_transaction_gate_ibr", "path_contains": "RDIAPPurchaseManager.swift", "function_contains": "RDIAPTransactionGate" }, { "id": "swift_iap_handler_ibr", "path_contains": "RDIAPHandler.swift" }, { "id": "objc_iap_verify_handler_ibr", "path_contains": "STSIAPVerifyHandler.m" }, { "id": "objc_pay_ext_ibr", "path_contains": "STSBaseViewController+STSPayExtension.m" }, @@ -696,6 +699,11 @@ { "id": "vendor_pdf_trial_ibr", "path_contains": "Vendor/RDPDFReaderView/Sources/RDPDFReaderTrial.swift" }, { "id": "vendor_epub_core_ibr", "path_contains": "Vendor/RDEpubReaderView/EPUBCore/" } ], + "required_indirect_branch_sources": [ + "RDRequestParamsFormat.swift", + "RDSDPrivateKeyManager.swift", + "RDIAPPurchaseManager.swift" + ], "string_encrypt_stack_targets": [ { "id": "c_rd_sd_keybox_sstr", "path_contains": "RDSDKeyBox.c" }, { "id": "c_rd_epub_keybox_sstr", "path_contains": "RDEpubKeyBox.c" }, diff --git a/tests/test_indirect_branch_pass.py b/tests/test_indirect_branch_pass.py index 123e073..33f76ea 100644 --- a/tests/test_indirect_branch_pass.py +++ b/tests/test_indirect_branch_pass.py @@ -177,9 +177,20 @@ int main(void) { exe1, dwarf1, audit_wrapper, require_topology='' ) self.assertEqual(star_res['failed_count'], 0) - ibr_res = verify_macho_indirect_branches.verify_binary_indirect_branches(exe1, dwarf1, audit_wrapper) + ibr_res = verify_macho_indirect_branches.verify_binary_indirect_branches( + exe1, dwarf1, audit_wrapper, required_sources=[src_c.name] + ) self.assertEqual(ibr_res['failed_count'], 0) self.assertGreaterEqual(ibr_res['verified_ibr_count'], 2) + self.assertEqual(ibr_res['required_sources_verified'], {src_c.name: ibr_res['verified_ibr_count']}) + audit_with_unlinked = {'module_audits': [ + *audit_wrapper['module_audits'], + {'source_path': 'required.c', 'indirect_branches_functions': ['unlinked_func']}, + ]} + with self.assertRaisesRegex(AssertionError, r'required\.c'): + verify_macho_indirect_branches.verify_binary_indirect_branches( + exe1, dwarf1, audit_with_unlinked, required_sources=['required.c'] + ) nm_exe = subprocess.check_output(['xcrun', 'nm', str(exe1)], text=True) self.assertNotIn('dead_unused_helper', nm_exe) diff --git a/tools/llvm_obf/build_obfuscated_ipa.py b/tools/llvm_obf/build_obfuscated_ipa.py index 7360928..993d672 100644 --- a/tools/llvm_obf/build_obfuscated_ipa.py +++ b/tools/llvm_obf/build_obfuscated_ipa.py @@ -964,7 +964,8 @@ def build_ipa(scheme: str, output_dir: Path, seed: str, expected_binaries: int, ) assert icall_gate['gate_passed'], f"IndirectCallGate failed: {icall_gate['violations']}" ibr_gate = verify_macho_indirect_branches.verify_binary_indirect_branches( - main_bin, dwarf_bin, obf_audit + main_bin, dwarf_bin, obf_audit, + required_sources=policy.get('required_indirect_branch_sources') or (), ) print( f"[IndirectBranchGate] verified={ibr_gate['verified_ibr_count']}/{ibr_gate['linked_checked_count']} " diff --git a/tools/llvm_obf/verify_macho_indirect_branches.py b/tools/llvm_obf/verify_macho_indirect_branches.py index 6460086..88a7819 100644 --- a/tools/llvm_obf/verify_macho_indirect_branches.py +++ b/tools/llvm_obf/verify_macho_indirect_branches.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 """Verify that every indirect-branch obfuscated function emits ARM64 `br xN` instructions in Mach-O.""" import argparse +from collections import Counter +from collections.abc import Iterable from concurrent.futures import ThreadPoolExecutor import json from pathlib import Path @@ -37,6 +39,7 @@ def verify_binary_indirect_branches( obf_audit: dict, *, fail_on_missing: bool = False, + required_sources: Iterable[str] = (), ) -> dict: """Disassemble every audited IBR function in main_bin and require >=1 `br xN`.""" ranges_map = parse_dwarf_text_ranges(dwarf_bin) @@ -86,7 +89,10 @@ def verify_binary_indirect_branches( **range_results[0], }) + verified_per_source = Counter(item['source'] for item in verified) + required_source_counts = {source: verified_per_source[source] for source in required_sources} summary = { + 'required_sources_verified': required_source_counts, 'total_audited': len(verified) + len(failed) + (0 if fail_on_missing else len(dead_stripped)), 'linked_checked_count': len(verified) + len(failed), 'verified_ibr_count': len(verified), @@ -100,6 +106,9 @@ def verify_binary_indirect_branches( raise AssertionError( f'[IndirectBranchGate] {len(failed)} function(s) failed br xN check:\n{preview}' ) + missing_sources = [source for source, count in required_source_counts.items() if not count] + if missing_sources: + raise AssertionError('[IndirectBranchGate] 缺少已链接的间接分支保护: ' + ', '.join(missing_sources)) return summary diff --git a/tools/xcode_archive/audit_exported_ipa.py b/tools/xcode_archive/audit_exported_ipa.py index 6d0b704..c179531 100755 --- a/tools/xcode_archive/audit_exported_ipa.py +++ b/tools/xcode_archive/audit_exported_ipa.py @@ -375,6 +375,12 @@ def audit_ipa( raise ValueError(f'IPA 静态卫生门禁失败: {failed_keys}') policy = _load_policy(policy_path) + required_ibr_sources = policy.get('required_indirect_branch_sources') or () + if archive_path and required_ibr_sources and not skip_macho_gates: + if not Path(archive_path).is_dir(): + raise ValueError(f'核心间接分支门禁缺少 xcarchive: {archive_path}') + if not audit_json_path or not Path(audit_json_path).is_file(): + raise ValueError('核心间接分支门禁缺少本次构建的 LLVM 审计日志') obf_audit = None if audit_json_path and Path(audit_json_path).is_file(): obf_audit = json.loads(Path(audit_json_path).read_text(encoding='utf-8')) @@ -415,7 +421,7 @@ def audit_ipa( raise ValueError('StarCFGGate verified_star_count 为 0') ibr_gate = verify_macho_indirect_branches.verify_binary_indirect_branches( - main_bin, dwarf_bin, obf_audit + main_bin, dwarf_bin, obf_audit, required_sources=required_ibr_sources ) report['indirect_branch_gate'] = ibr_gate if policy.get('indirect_branch_targets') and ibr_gate['verified_ibr_count'] <= 0: