Files
iOSGuard/defender/poison/IGPoison.c
T
shenleiandClaude Opus 4.8 2e43ff6da2 harden(poison): 断边原语 IGPoisonInline.h——毒化内联进解密 TU,删掉可 NOP 的调用边
docs/18 §7「断边」:符号隐藏只抹掉节点名字,调用图的边 bl ig_poison_apply/mask 仍在、
可被攻击者 NOP 或把被调函数 stub 成恒等。本提交提供能内联进接入方解密 TU 的 always_inline 核心。

- defender/poison/IGPoisonInline.h(单一真源):把「读健康值→推掩码→扰动密钥」做成
  always_inline。接入方在解密 TU #include 后调用 ig_poison_mix_key_inline(key,n),编译器
  把这段揉进解密函数自身——反汇编里没有 bl ig_poison_* 调用边可 NOP,毒化藏进 key schedule
  (比后置改输出更难摘)。推荐形态:扰动 key 派生(健康→key 不变→正常解密;毒化→乱码)。
- 跨 TU 唯一共享的是数据 ig_pstate[](中性名、hidden、strip 后无符号名),是 data 重定位
  非调用边。
- IGPoison.c 重构为复用该头的 core(baseline/lane_k/spread/mask/apply 只此一处定义),
  库 out-of-line 入口与接入方内联路径语义等价、永不漂移;g_state 改名 ig_pstate 并提升为
  hidden 全局供跨 TU 内联直读。IGVisibility.h 加 IG_ALWAYS_INLINE。

验证(tests/test_poison_inline.py,+2):
- test_inline_equals_outofline(host/macosx):内联 mask/apply == 库入口,mix_key 健康恒等、
  毒化扰动,逐例 0 fail。
- test_no_call_edge_after_inline(iphoneos,gate SDK):模拟解密 TU rd_derive_key 调用
  mix_key,-O2 反汇编断言零 bl、仅对 ig_pstate 的 data 引用,无 ig_poison_mask/apply 符号引用。

全量 72 测试通过;poison_device.m 全模块 iphoneos 交叉编译 arm64 通过。docs/18 §7 补 7.6 接入形态。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-13 11:25:30 +09:00

66 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* IGPoison.c — 毒化状态机实现。见 IGPoison.h 的设计与边界声明。
*
* 本文件只负责:① 定义跨 TU 共享的匿名状态存储 ig_pstate[];② 写入侧(init/mark,
* 由检测线程调用,不在解密热路径);③ out-of-line 公共入口 ig_poison_mask/apply。
* 读取/应用的**算术核心**(baseline / lane_k / spread / mask / apply)统一来自
* IGPoisonInline.h —— **单一真源**,库路径与接入方内联路径永不漂移。
*
* §7 硬化(已落地):
* - §7.4 基线运行期派生(见 IGPoisonInline.h ig_pbaseline_inline,不明文驻留)。
* - §7.2 匿名数组分散存储(无命名 section——命名 section 会留在 load command、strip 抹不掉,
* 等于挂牌毒化副本位置;见 IGPoisonInline.h 顶部说明)。
* - §7「断边」:apply/mask 核心做成 always_inline,接入方在解密 TU 内联后不留
* `bl ig_poison_*` 调用边;推荐用 ig_poison_mix_key_inline 把毒化揉进 key schedule。
*
* §7 硬化(仍 TODO,需额外工具链,见 docs/18 §7.3):
* - mark / mask / apply 用 OLLVM(Fla+BCF) + 常量加密盖住;预留 IG_OBFUSCATOR 钩子。
*/
#include "IGPoison.h"
#include "poison/IGPoisonInline.h"
/*
* §7.2 分散存储:一个看起来普通的匿名状态数组。跨 TU 共享(接入方内联 mask/apply 时直读),
* 故为非 static 的 hidden 全局(中性名、无 poison 字样,strip 后无符号名)。
* 进程级(非 TLS)——毒化态须跨线程一致:检测线程 mark 后解密线程读到的掩码必须同为非零,
* TLS 会各线程各看各的、破坏处置闭环,故**刻意不用 TLS**。下标/长度定义在 IGPoisonInline.h。
*/
volatile uint64_t ig_pstate[IG_PSTATE_N];
void ig_poison_init(void) {
uint64_t b = ig_pbaseline_inline();
/* 全数组都填成「健康值」表示 (b ^ lane_k(i)):真副本、诱饵、陪衬槽写法一致,
* 各槽值互不相同,静态看不出哪几槽是喂掩码的真副本。 */
for (unsigned i = 0; i < IG_PSTATE_N; ++i) {
ig_pstate[i] = b ^ ig_plane_k_inline(i);
}
}
/* 构造期兜底初始化(若宿主未显式调用 ig_poison_init)。 */
__attribute__((constructor))
static void ig_poison_ctor(void) { ig_poison_init(); }
void ig_poison_mark(uint64_t entropy) {
/* entropy 为 0 时仍强制非 0,避免“混入 0 = 不生效”。写入侧不内联:由检测线程调用,
* 不在解密热路径,无需删边。 */
uint64_t e = entropy ? entropy : 0xD1B54A32D192ED03ULL;
/* 三路真副本各自偏离健康值(任一偏离即令掩码非零);诱饵也动,混淆“哪几槽生效”。 */
ig_pstate[IG_PSTATE_L0] ^= ig_pspread_inline(e, 0);
ig_pstate[IG_PSTATE_DECOY] ^= ig_pspread_inline(e, 3);
ig_pstate[IG_PSTATE_L1] ^= ig_pspread_inline(e, 1);
ig_pstate[IG_PSTATE_L2] ^= ig_pspread_inline(e, 2);
}
/*
* out-of-line 公共入口:直接委托给单一真源的内联核心。
* 接入方若想删掉对这两个函数的调用边,改 #include "IGPoisonInline.h" 调用
* ig_poison_mask_inline / ig_poison_mix_key_inline(见 docs/18 §7)。
*/
uint64_t ig_poison_mask(void) {
return ig_poison_mask_inline();
}
void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n) {
ig_poison_apply_inline(in, out, n);
}