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>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
e3d2a48d56
commit
2e43ff6da2
+17
@@ -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 <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "poison/IGPoisonInline.h"
|
||||
|
||||
/* 非 static extern:保证不被优化掉,反汇编里能定位到本函数。 */
|
||||
void rd_derive_key(uint8_t *key, size_t n) {
|
||||
ig_poison_mix_key_inline(key, n);
|
||||
}
|
||||
Vendored
+58
@@ -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 <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user