From 2e43ff6da25b13f684ce8f0d78bce5b3cc76b74b Mon Sep 17 00:00:00 2001 From: shenlei Date: Fri, 13 Nov 2026 11:25:30 +0900 Subject: [PATCH] =?UTF-8?q?harden(poison):=20=E6=96=AD=E8=BE=B9=E5=8E=9F?= =?UTF-8?q?=E8=AF=AD=20IGPoisonInline.h=E2=80=94=E2=80=94=E6=AF=92?= =?UTF-8?q?=E5=8C=96=E5=86=85=E8=81=94=E8=BF=9B=E8=A7=A3=E5=AF=86=20TU?= =?UTF-8?q?=EF=BC=8C=E5=88=A0=E6=8E=89=E5=8F=AF=20NOP=20=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E8=BE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- defender/include/IGVisibility.h | 7 ++ defender/poison/IGPoison.c | 111 ++++++++---------------- defender/poison/IGPoison.h | 5 ++ defender/poison/IGPoisonInline.h | 107 +++++++++++++++++++++++ docs/18_READOOR_DEFENDER_INTEGRATION.md | 18 +++- tests/fixtures/poison_inline_caller.c | 17 ++++ tests/fixtures/poison_inline_host.c | 58 +++++++++++++ tests/test_poison_inline.py | 81 +++++++++++++++++ 8 files changed, 326 insertions(+), 78 deletions(-) create mode 100644 defender/poison/IGPoisonInline.h create mode 100644 tests/fixtures/poison_inline_caller.c create mode 100644 tests/fixtures/poison_inline_host.c create mode 100644 tests/test_poison_inline.py diff --git a/defender/include/IGVisibility.h b/defender/include/IGVisibility.h index 55e4d4a..05bd75f 100644 --- a/defender/include/IGVisibility.h +++ b/defender/include/IGVisibility.h @@ -25,9 +25,16 @@ #if defined(__GNUC__) || defined(__clang__) #define IG_PUBLIC __attribute__((visibility("default"))) #define IG_INTERNAL __attribute__((visibility("hidden"))) +/* + * IG_ALWAYS_INLINE:强制内联。用于「删调用图的边」——把毒化读取/应用的核心做成 + * always_inline,接入方在解密 TU 内联后反汇编里不留 `bl ig_poison_*` 调用边可 NOP。 + * 见 defender/poison/IGPoisonInline.h 与 docs/18 §7。 + */ +#define IG_ALWAYS_INLINE static inline __attribute__((always_inline)) #else #define IG_PUBLIC #define IG_INTERNAL +#define IG_ALWAYS_INLINE static inline #endif #endif /* IG_VISIBILITY_H */ diff --git a/defender/poison/IGPoison.c b/defender/poison/IGPoison.c index 82ac516..4ac49ff 100644 --- a/defender/poison/IGPoison.c +++ b/defender/poison/IGPoison.c @@ -1,74 +1,38 @@ /* * IGPoison.c — 毒化状态机实现。见 IGPoison.h 的设计与边界声明。 * - * 实现要点: - * - 三副本冗余、写法各异、均 volatile(防编译器把 (copy ^ BASELINE) 常量折叠回 0)。 - * - 掩码由三副本与基线异或后按位或得出:健康时每副本==基线 → 全 0;毒化时非 0。 - * - 没有任何 `if (poisoned)` 控制流;处置是纯算术数据流。 + * 本文件只负责:① 定义跨 TU 共享的匿名状态存储 ig_pstate[];② 写入侧(init/mark, + * 由检测线程调用,不在解密热路径);③ out-of-line 公共入口 ig_poison_mask/apply。 + * 读取/应用的**算术核心**(baseline / lane_k / spread / mask / apply)统一来自 + * IGPoisonInline.h —— **单一真源**,库路径与接入方内联路径永不漂移。 * - * §7 硬化(本次已落地): - * - §7.4 基线不再明文驻留:IG_POISON_BASELINE 宏删除,改 ig_poison_baseline() 运行期 - * 由分片种子派生,volatile 屏障阻止折叠回单一 64bit 魔数写入常量区。 - * - §7.2 三副本分散:真副本+诱饵散在一个「看起来普通」的匿名数组 g_state[] 的非相邻下标里。 - * **刻意不用自定义命名 section**:命名 section 会原样留在 Mach-O load command 里, - * `strip` 也抹不掉,等于在 header 上挂牌「毒化副本在 __ig_pX」——比匿名 BSS 更糟 - * (攻击者 otool -l 一把定位)。改匿名数组 + per-lane 掩码后:全数组进匿名 __bss/__data, - * 各槽值互不相同(健康态也不是「几个相同的字」这种一眼可辨的 tell),哪几槽真正喂掩码不可见。 + * §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 编译钩子,默认构建不启用。 + * - mark / mask / apply 用 OLLVM(Fla+BCF) + 常量加密盖住;预留 IG_OBFUSCATOR 钩子。 */ #include "IGPoison.h" +#include "poison/IGPoisonInline.h" /* - * §7.4 基线运行期派生:具体 bit 无语义要求(健康态三副本==基线即可,mask=copy^基线), - * 只要全程一致且非零。volatile 种子强制运行期求值,阻止整条链折叠回单一常量写入 __const。 + * §7.2 分散存储:一个看起来普通的匿名状态数组。跨 TU 共享(接入方内联 mask/apply 时直读), + * 故为非 static 的 hidden 全局(中性名、无 poison 字样,strip 后无符号名)。 + * 进程级(非 TLS)——毒化态须跨线程一致:检测线程 mark 后解密线程读到的掩码必须同为非零, + * TLS 会各线程各看各的、破坏处置闭环,故**刻意不用 TLS**。下标/长度定义在 IGPoisonInline.h。 */ -static uint64_t ig_poison_baseline(void) { - volatile uint64_t seed = 0x243F6A88ULL; /* 分片种子,非最终值 */ - uint64_t x = (uint64_t)seed; - x ^= 0x85A308D3ULL; - x *= 0xFF51AFD7ED558CCDULL; - x ^= x >> 33; - x += 0x2545F4914F6CDD1DULL; - x ^= x >> 29; - return x | 1ULL; /* 保证非零 */ -} - -/* - * §7.2 分散存储:一个看起来普通的匿名状态数组(无命名 section、无单独符号)。 - * 三路真副本散在非相邻下标 IG_LANE[0..2],诱饵在 IG_DECOY,其余槽是陪衬(init/mark 照写、 - * mask 不读)。进程级全局(非 TLS)——毒化态须跨线程一致:检测线程 mark 后解密线程读到的 - * 掩码必须同为非零,TLS 会各线程各看各的、破坏处置闭环,故**刻意不用 TLS**。 - */ -#define IG_STATE_N 12 -static volatile uint64_t g_state[IG_STATE_N]; -/* 真副本下标(非相邻)、诱饵下标。改这些值需与 ig_poison_mask 的读取保持一致。 */ -static const unsigned IG_LANE[3] = { 2u, 7u, 11u }; -#define IG_DECOY 5u - -/* per-槽掩码常量:健康态存 (baseline ^ lane_k(i)),使各槽值互不相同, - * 消除「数组里几个相同的字=一组健康值」这种可一眼框定的静态特征。 */ -static uint64_t ig_lane_k(unsigned i) { - return 0x9E3779B97F4A7C15ULL * (uint64_t)(i * 2u + 1u); -} - -/* 把 entropy 扩散成三个互不相同、且必非 0 的偏移量(保证任一副本都会偏离基线)。 */ -static uint64_t ig_spread(uint64_t e, unsigned lane) { - e ^= 0x9E3779B97F4A7C15ULL * (lane + 1u); /* 每路不同常量 */ - e ^= e >> 30; e *= 0xBF58476D1CE4E5B9ULL; - e ^= e >> 27; e *= 0x94D049BB133111EBULL; - e ^= e >> 31; - return e | 1ULL; /* 置最低位,确保非 0 */ -} +volatile uint64_t ig_pstate[IG_PSTATE_N]; void ig_poison_init(void) { - uint64_t b = ig_poison_baseline(); + uint64_t b = ig_pbaseline_inline(); /* 全数组都填成「健康值」表示 (b ^ lane_k(i)):真副本、诱饵、陪衬槽写法一致, * 各槽值互不相同,静态看不出哪几槽是喂掩码的真副本。 */ - for (unsigned i = 0; i < IG_STATE_N; ++i) { - g_state[i] = b ^ ig_lane_k(i); + for (unsigned i = 0; i < IG_PSTATE_N; ++i) { + ig_pstate[i] = b ^ ig_plane_k_inline(i); } } @@ -77,32 +41,25 @@ __attribute__((constructor)) static void ig_poison_ctor(void) { ig_poison_init(); } void ig_poison_mark(uint64_t entropy) { - /* entropy 为 0 时仍强制非 0,避免“混入 0 = 不生效”。 */ + /* entropy 为 0 时仍强制非 0,避免“混入 0 = 不生效”。写入侧不内联:由检测线程调用, + * 不在解密热路径,无需删边。 */ uint64_t e = entropy ? entropy : 0xD1B54A32D192ED03ULL; - /* 三路真副本各自偏离健康值(任一偏离即令掩码非零)。 */ - g_state[IG_LANE[0]] ^= ig_spread(e, 0); - g_state[IG_DECOY] ^= ig_spread(e, 3); /* 诱饵也动,混淆“哪几槽生效” */ - g_state[IG_LANE[1]] ^= ig_spread(e, 1); - g_state[IG_LANE[2]] ^= ig_spread(e, 2); + /* 三路真副本各自偏离健康值(任一偏离即令掩码非零);诱饵也动,混淆“哪几槽生效”。 */ + 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) { - /* 健康:每槽存的是 (b ^ lane_k(i)),解回 ^lane_k^b == 0 → 或起来为 0。 - * 毒化:至少一路非 0。无分支。只读三路真副本,诱饵/陪衬不参与—— - * 攻击者把诱饵或某陪衬槽写回也不影响掩码。 */ - uint64_t b = ig_poison_baseline(); - uint64_t m = (g_state[IG_LANE[0]] ^ ig_lane_k(IG_LANE[0]) ^ b) - | (g_state[IG_LANE[1]] ^ ig_lane_k(IG_LANE[1]) ^ b) - | (g_state[IG_LANE[2]] ^ ig_lane_k(IG_LANE[2]) ^ b); - return m; + return ig_poison_mask_inline(); } void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n) { - if (!in || !out) return; - uint64_t m = ig_poison_mask(); - for (size_t i = 0; i < n; ++i) { - /* 健康 m==0 → 扰动字节恒 0 → out[i]==in[i]。毒化 → 每字节被 m 的对应字节异或。 */ - uint8_t d = (uint8_t)((m >> ((i & 7u) * 8u)) & 0xFFu); - out[i] = (uint8_t)(in[i] ^ d); - } + ig_poison_apply_inline(in, out, n); } diff --git a/defender/poison/IGPoison.h b/defender/poison/IGPoison.h index 37ae182..be67992 100644 --- a/defender/poison/IGPoison.h +++ b/defender/poison/IGPoison.h @@ -48,6 +48,11 @@ uint64_t ig_poison_mask(void); * 数据依赖处置原语:把 in[0..n) 揉以当前掩码写入 out[0..n)(允许 in==out 原地)。 * 健康时 out 与 in 逐字节相等(掩码 0,异或 0);毒化时每字节被掩码扰动。 * 业务用法:对解密 key 的字节调用本函数后再传给解密器——健康照常、毒化解出乱码。 + * + * 【断边】本函数是一条可被 NOP 的调用边(`bl ig_poison_apply`)。要删掉这条边, + * 接入方改 #include "IGPoisonInline.h",在解密 TU 内直接调用 always_inline 的 + * ig_poison_mix_key_inline(key, n)——编译器把毒化揉进解密函数自身,反汇编里不留调用边, + * 且毒化落在 key schedule(比后置改输出更难摘)。两条路径共用同一真源,语义等价。见 docs/18 §7。 */ IG_PUBLIC void ig_poison_apply(const uint8_t *in, uint8_t *out, size_t n); diff --git a/defender/poison/IGPoisonInline.h b/defender/poison/IGPoisonInline.h new file mode 100644 index 0000000..9f8a9a0 --- /dev/null +++ b/defender/poison/IGPoisonInline.h @@ -0,0 +1,107 @@ +/* + * IGPoisonInline.h — 毒化「读取/应用」核心(always_inline,单一真源)。 + * + * 为什么存在(docs/18 §7「断边」): + * 符号隐藏只抹掉节点名字,调用图的边(`bl ig_poison_apply` / `bl ig_poison_mask`) + * 仍在——攻击者靠交叉引用定位到这条边,NOP 掉调用、或把被调函数 stub 成恒等,毒化即失效。 + * 本头把「读健康值 → 推掩码 → 扰动密钥」整段做成 always_inline:接入方(readoor 解密 TU) + * #include 后在密钥派生处调用 ig_poison_mix_key_inline(),编译器把这段揉进解密函数自身, + * 反汇编里**没有这条调用边**可 NOP,毒化藏进 key schedule,与真实密钥推导混在一起。 + * + * 单一真源:库内 IGPoison.c 的 out-of-line 公共入口(ig_poison_mask/apply)也复用本头的 + * core,baseline / lane_k / 下标只此一处定义,内联路径与库路径永不漂移。 + * + * 跨 TU 唯一共享的是「数据」ig_pstate[](中性名匿名数组,hidden,最终 strip 后无符号名), + * 不是「调用边」——读它是 data 重定位,不是 call。即便漏 strip,名字也不含 poison 字样、不 telegraph。 + * + * 注意:写入侧(init / mark)不在本头内联——它们由检测线程调用、不在解密热路径, + * 无需删边,留在 IGPoison.c。本头只内联**读取/应用**侧。 + */ +#ifndef IG_POISON_INLINE_H +#define IG_POISON_INLINE_H + +#include +#include + +#include "IGVisibility.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* 匿名状态数组长度与真副本/诱饵下标(非相邻)。写入侧(IGPoison.c)须与此一致。 */ +#define IG_PSTATE_N 12 +#define IG_PSTATE_L0 2u +#define IG_PSTATE_L1 7u +#define IG_PSTATE_L2 11u +#define IG_PSTATE_DECOY 5u + +/* 存储定义在 IGPoison.c。hidden + 中性名:最终 strip 后无符号名,漏 strip 也不 telegraph。 */ +__attribute__((visibility("hidden"))) +extern volatile uint64_t ig_pstate[IG_PSTATE_N]; + +/* 基线运行期派生(与 IGPoison.c 删除的 IG_POISON_BASELINE 魔数等价,但不明文驻留)。 */ +IG_ALWAYS_INLINE uint64_t ig_pbaseline_inline(void) { + volatile uint64_t seed = 0x243F6A88ULL; /* 分片种子,非最终值 */ + uint64_t x = (uint64_t)seed; + x ^= 0x85A308D3ULL; + x *= 0xFF51AFD7ED558CCDULL; + x ^= x >> 33; + x += 0x2545F4914F6CDD1DULL; + x ^= x >> 29; + return x | 1ULL; /* 保证非零 */ +} + +/* per-槽掩码常量:使各槽健康值互不相同,消除「数组里几个相同的字=一组健康值」的 tell。 */ +IG_ALWAYS_INLINE uint64_t ig_plane_k_inline(unsigned i) { + return 0x9E3779B97F4A7C15ULL * (uint64_t)(i * 2u + 1u); +} + +/* 把 entropy 扩散成每路互不相同、必非 0 的偏移(写入侧用;放这里供单一真源)。 */ +IG_ALWAYS_INLINE uint64_t ig_pspread_inline(uint64_t e, unsigned lane) { + e ^= 0x9E3779B97F4A7C15ULL * (lane + 1u); + e ^= e >> 30; e *= 0xBF58476D1CE4E5B9ULL; + e ^= e >> 27; e *= 0x94D049BB133111EBULL; + e ^= e >> 31; + return e | 1ULL; +} + +/* + * 掩码核心:健康=0,毒化=非 0,无分支。只读三真副本(诱饵/陪衬槽不读)。 + * 健康态每真副本存 (baseline ^ lane_k(i)),解回 ^lane_k ^baseline == 0。 + */ +IG_ALWAYS_INLINE uint64_t ig_poison_mask_inline(void) { + uint64_t b = ig_pbaseline_inline(); + return (ig_pstate[IG_PSTATE_L0] ^ ig_plane_k_inline(IG_PSTATE_L0) ^ b) + | (ig_pstate[IG_PSTATE_L1] ^ ig_plane_k_inline(IG_PSTATE_L1) ^ b) + | (ig_pstate[IG_PSTATE_L2] ^ ig_plane_k_inline(IG_PSTATE_L2) ^ b); +} + +/* + * 应用核心(等价 ig_poison_apply):健康 out==in(异或 0),毒化逐字节被掩码扰动。 + * 允许 in==out 原地。 + */ +IG_ALWAYS_INLINE void ig_poison_apply_inline(const uint8_t *in, uint8_t *out, size_t n) { + if (!in || !out) return; + uint64_t m = ig_poison_mask_inline(); + for (size_t i = 0; i < n; ++i) { + uint8_t d = (uint8_t)((m >> ((i & 7u) * 8u)) & 0xFFu); + out[i] = (uint8_t)(in[i] ^ d); + } +} + +/* + * 推荐接入形态:把掩码揉进密钥字节(原地)。健康 → 密钥不变 → 解密正常; + * 毒化 → 密钥被扰动 → 解出乱码。比后置改输出更难摘:毒化落在 key schedule, + * 攻击者要在不破坏正常解密的前提下摘掉它,得先逆清整条密钥推导。 + * 用法:在解密 TU 取得明文密钥后、喂给解密器前,对 key 调用一次。 + */ +IG_ALWAYS_INLINE void ig_poison_mix_key_inline(uint8_t *key, size_t n) { + ig_poison_apply_inline(key, key, n); +} + +#ifdef __cplusplus +} +#endif + +#endif /* IG_POISON_INLINE_H */ diff --git a/docs/18_READOOR_DEFENDER_INTEGRATION.md b/docs/18_READOOR_DEFENDER_INTEGRATION.md index e3070dc..ff4330c 100644 --- a/docs/18_READOOR_DEFENDER_INTEGRATION.md +++ b/docs/18_READOOR_DEFENDER_INTEGRATION.md @@ -173,7 +173,23 @@ func poisonedKey(_ key: String) -> String { 4. ✅ 毒化基线魔数运行期派生。删除硬编码 `0xA5C3F10E7B2D4968`,改 `ig_poison_baseline()` 由拆分种子经 `volatile` 屏障派生(阻止常量折叠回单一魔数);值本身不影响行为,只需进程内自洽。`otool -tV` 已无旧魔数立即数。 5. ⛔ 越狱探针真机正向验证:越狱设备实测命中 400/401,且非越狱设备零 STRONG。**仍阻塞**(无越狱设备)。D8 `svc_access_live` 已在非越狱 iPhone 证了 svc 直发探针活性,残留缺口=真越狱文件 400 + 沙盒逃逸 401 的正向命中。 -> **尚未做、留到 readoor 接入时的「断边」深化**:把 `ig_poison_apply` 内联进 epub 解密函数(删掉 `bl ig_poison_apply` 这条调用边),以及调用点改间接跳转(`blr` + 运行期解析目标)——符号隐藏只去掉「按名定位」,这两项才去掉静态调用图里的边本身。 +**6. ✅ 断边原语(C 侧已就绪,留 readoor 接入时挂点)。** 符号隐藏只去掉「按名定位」,调用图的边(`bl ig_poison_apply/mask`)仍在、可被 NOP。本轮提供 `defender/poison/IGPoisonInline.h`:把「读健康值→推掩码→扰动密钥」做成 `always_inline` 核心,接入方在**解密 TU 内联**后反汇编**不留 `bl ig_poison_*` 调用边**(已用 `tests/test_poison_inline.py` 的 `test_no_call_edge_after_inline` 对 arm64 `-O2` 产物断言:`rd_derive_key` 内零 `bl`,对 `ig_pstate` 仅 data 引用)。库内 `IGPoison.c` 的 out-of-line 入口复用同一真源,内联路径与库路径语义等价(`test_inline_equals_outofline` 逐例校验),不会漂移。 + +### 7.6 断边接入形态(readoor 解密 TU) + +推荐把毒化揉进 **key 派生**(落在 key schedule,比后置改输出更难摘),而不是改解密输出: + +```c +#include "poison/IGPoisonInline.h" // dist/include/poison/IGPoisonInline.h + +// readoor 取得明文 epub 解密 key 后、喂给解密器之前: +ig_poison_mix_key_inline(key, key_len); // 健康→key 不变→正常解密;毒化→key 被扰动→乱码 +// 随后照常 AES/chacha 解密(key, …) +``` + +编译后 `ig_poison_mix_key_inline` 整段内联进 readoor 的解密函数:没有可 NOP 的调用边,基线派生+三副本读取+异或全部化进解密函数自身的指令流,与真实 key schedule 混在一起。唯一跨 TU 符号是**数据** `ig_pstate`(中性名、hidden、strip 后无名),非调用边。 + +> **仍未做**:调用点级别的间接跳转(`blr` + 运行期解析目标)——内联已消除热路径的调用边,间接跳转是针对「仍保留 out-of-line 入口」场景的额外一层,价值低于内联,按需再做。 --- diff --git a/tests/fixtures/poison_inline_caller.c b/tests/fixtures/poison_inline_caller.c new file mode 100644 index 0000000..5d18a85 --- /dev/null +++ b/tests/fixtures/poison_inline_caller.c @@ -0,0 +1,17 @@ +/* + * poison_inline_caller.c — 模拟接入方(readoor)解密 TU 的「断边」验证夹具。 + * + * rd_derive_key 代表解密前取得明文密钥、喂给解密器前揉入毒化的那一步。它 #include + * IGPoisonInline.h 并调用 always_inline 的 ig_poison_mix_key_inline——编译后反汇编本函数 + * 应**不含任何 `bl ig_poison_*` 调用边**(原语已内联进来),只有对数据 ig_pstate 的读取。 + * 由 tests/test_poison_inline.py 反汇编断言。 + */ +#include +#include + +#include "poison/IGPoisonInline.h" + +/* 非 static extern:保证不被优化掉,反汇编里能定位到本函数。 */ +void rd_derive_key(uint8_t *key, size_t n) { + ig_poison_mix_key_inline(key, n); +} diff --git a/tests/fixtures/poison_inline_host.c b/tests/fixtures/poison_inline_host.c new file mode 100644 index 0000000..717fc8e --- /dev/null +++ b/tests/fixtures/poison_inline_host.c @@ -0,0 +1,58 @@ +/* + * poison_inline_host.c — 验证 IGPoisonInline.h 内联核心与 IGPoison.c out-of-line 公共入口 + * 语义等价(单一真源不漂移),且推荐接入形态 ig_poison_mix_key_inline 行为正确: + * 健康→密钥不变(解密正常),毒化→密钥被扰动(解出乱码)。 + * 退出码 0=全部用例通过。由 tests/test_poison_inline.py 编译运行。 + */ +#include +#include +#include + +#include "poison/IGPoison.h" +#include "poison/IGPoisonInline.h" + +static int fails = 0; +static void check(int cond, const char *msg) { + printf("%s: %s\n", cond ? "ok" : "FAIL", msg); + if (!cond) fails++; +} + +int main(void) { + ig_poison_init(); + + /* —— 健康态 —— */ + check(ig_poison_mask() == 0, "healthy: public mask==0"); + check(ig_poison_mask_inline() == 0, "healthy: inline mask==0"); + check(ig_poison_mask_inline() == ig_poison_mask(), "healthy: inline mask==public mask"); + + uint8_t ref[16]; + for (int i = 0; i < 16; ++i) ref[i] = (uint8_t)(i * 7 + 1); + + uint8_t key[16]; + memcpy(key, ref, 16); + ig_poison_mix_key_inline(key, 16); + check(memcmp(key, ref, 16) == 0, "healthy: mix_key 恒等(密钥不变→解密正常)"); + + uint8_t o1[16], o2[16]; + ig_poison_apply(ref, o1, 16); + ig_poison_apply_inline(ref, o2, 16); + check(memcmp(o1, o2, 16) == 0, "healthy: inline apply == public apply"); + check(memcmp(o1, ref, 16) == 0, "healthy: apply 恒等"); + + /* —— 毒化态 —— */ + ig_poison_mark(0xDEADBEEF12345678ULL); + check(ig_poison_mask() != 0, "poisoned: public mask!=0"); + check(ig_poison_mask_inline() == ig_poison_mask(), "poisoned: inline mask==public mask"); + + memcpy(key, ref, 16); + ig_poison_mix_key_inline(key, 16); + check(memcmp(key, ref, 16) != 0, "poisoned: mix_key 扰动密钥(→解出乱码)"); + + ig_poison_apply(ref, o1, 16); + ig_poison_apply_inline(ref, o2, 16); + check(memcmp(o1, o2, 16) == 0, "poisoned: inline apply == public apply"); + check(memcmp(o1, ref, 16) != 0, "poisoned: apply 扰动输出"); + + printf("%d fail\n", fails); + return fails ? 1 : 0; +} diff --git a/tests/test_poison_inline.py b/tests/test_poison_inline.py new file mode 100644 index 0000000..d77d7c7 --- /dev/null +++ b/tests/test_poison_inline.py @@ -0,0 +1,81 @@ +"""断边原语(docs/18 §7「断边」):IGPoisonInline.h 的 always_inline 核心。 + +两条验收: + 1. 语义等价(host/macosx):内联 mask/apply 与 IGPoison.c 的 out-of-line 公共入口逐例一致, + 推荐接入形态 ig_poison_mix_key_inline 健康恒等、毒化扰动。证「单一真源不漂移」。 + 2. 断边(iphoneos,gate 于 SDK):模拟解密 TU 调用 ig_poison_mix_key_inline,-O2 编译后 + 反汇编**不含任何 `bl ig_poison_*` 调用边**(原语已内联进调用者),对 ig_pstate 只是 + data 引用而非 call——攻击者没有可 NOP 的调用边,毒化揉进 key schedule。 +""" +from pathlib import Path +import os +import shutil +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +@unittest.skipUnless(sys.platform == 'darwin' and shutil.which('xcrun'), '需要 Xcode/macOS') +class PoisonInlineEquivalenceTests(unittest.TestCase): + """语义等价:内联核心 == 库入口(单一真源)。host 原生跑,不需设备。""" + + def test_inline_equals_outofline(self): + env = dict(os.environ) + env.pop('DYLD_INSERT_LIBRARIES', None) + with tempfile.TemporaryDirectory() as d: + exe = Path(d) / 'poison-inline-host' + # 内联核心只依赖 IGPoison.c(定义 ig_pstate + init/mark),无其它 TU 依赖。 + args = ['xcrun', '--sdk', 'macosx', 'clang', '-O2', '-Wall', '-Wextra', + '-I', str(ROOT / 'defender/include'), '-I', str(ROOT / 'defender'), + str(ROOT / 'tests/fixtures/poison_inline_host.c'), + str(ROOT / 'defender/poison/IGPoison.c'), + '-o', str(exe)] + b = subprocess.run(args, capture_output=True, text=True, env=env) + self.assertEqual(b.returncode, 0, b.stdout + b.stderr) + r = subprocess.run([str(exe)], capture_output=True, text=True, env=env) + print(r.stdout.strip(), flush=True) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertIn('0 fail', r.stdout) + + +def have_ios_sdk(): + if sys.platform != 'darwin' or not shutil.which('xcrun'): + return False + r = subprocess.run(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], + capture_output=True, text=True) + return r.returncode == 0 and r.stdout.strip() != '' + + +@unittest.skipUnless(have_ios_sdk(), '需要 Xcode + iphoneos SDK') +class PoisonInlineEdgeDeletionTests(unittest.TestCase): + """断边:接入方内联后反汇编无 `bl ig_poison_*` 调用边。""" + + def test_no_call_edge_after_inline(self): + with tempfile.TemporaryDirectory() as d: + obj = Path(d) / 'caller.o' + b = subprocess.run( + ['xcrun', '--sdk', 'iphoneos', 'clang', '-arch', 'arm64', + '-miphoneos-version-min=15.0', '-O2', + '-I', str(ROOT / 'defender'), '-I', str(ROOT / 'defender/include'), + '-c', str(ROOT / 'tests/fixtures/poison_inline_caller.c'), '-o', str(obj)], + capture_output=True, text=True) + self.assertEqual(b.returncode, 0, b.stdout + b.stderr) + dis = subprocess.run(['xcrun', '--sdk', 'iphoneos', 'otool', '-tV', str(obj)], + capture_output=True, text=True).stdout + # rd_derive_key 内不得有任何 bl 调用边(原语已 always_inline)。 + calls = [ln.strip() for ln in dis.splitlines() if '\tbl\t' in ln] + self.assertEqual(calls, [], f'内联后仍残留调用边:{calls}') + # 且应以 data 引用(非 call)触达 ig_pstate:undefined 符号里有 _ig_pstate。 + syms = subprocess.run(['xcrun', '--sdk', 'iphoneos', 'nm', str(obj)], + capture_output=True, text=True).stdout + self.assertIn('_ig_pstate', syms, '未见对 ig_pstate 的数据引用,内联读取可能未生效') + # 符号表里不应把 ig_poison_mask/apply 作为被调用符号引入(无调用边的佐证)。 + self.assertNotIn('_ig_poison_mask', syms, '残留对 ig_poison_mask 的符号引用') + self.assertNotIn('_ig_poison_apply', syms, '残留对 ig_poison_apply 的符号引用') + + +if __name__ == '__main__': + unittest.main()