Files
iOSGuard/tests/fixtures/poison_host.c
shenleiandClaude Opus 4.8 c4c9a6925a test(defender): 固化处置链宿主基线 + 真机验证协议(docs/19),修越狱聚合回归
P0 验真:真机验证前先把「检测→分级→数据毒化」闭环做成可复现宿主绿灯。

新增:
- tests/fixtures/poison_host.c + tests/test_poison_policy.py:poison/policy/
  anti_jailbreak 闭环自测 19/19(poison 恒等+坏key+不可逆、policy 分级+去重+
  mask一致、jailbreak 反向零误报+400+401),集成进 unittest 套件。
- docs/19_DISPOSITION_CHAIN_DEVICE_VERIFICATION.md:真机验证协议——R(非越狱)/
  J(越狱)两组、D1–D7 用例、验收门槛(反向零毒化为开毒化前提)、边界与进度。

修复(接越狱探针进 ig_defender_scan 聚合器时埋下的回归):
- test_defender_host.py SOURCES 漏 anti_jailbreak/IGAntiJailbreak.c → 链接缺符号。
- defender_host.c:reasons[] 由 256 扩到 512(400/401 越界漏计);clean 断言排除
  越狱文件命中(macOS 自带 Unix userland,按 iOS 标准宿主本身即“越狱”,非真实误报)。

全套 63 项宿主/工具测试通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-12 22:35:35 +09:00

170 lines
6.7 KiB
C
Raw Permalink 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.
/*
* poison_host.c — 处置链宿主自测(poison + policy + anti_jailbreak)。
*
* 固化此前只在 /tmp 临时跑过的「检测→分级→数据毒化」闭环为可复现基线,
* 作为真机验证(docs/19)之前的宿主绿灯。覆盖:
* A. poison:健康恒等、毒化破坏 key、mask 非分支语义、apply 逐字节扩散。
* B. policy:弱信号/UNAVAILABLE/CLEAR 不触发、单 MEDIUM 不触发、双 MEDIUM 去重后触发、
* 单 STRONG 触发——且触发后 mask 必非 0(真正接到了毒化)。
* C. anti_jailbreak:可注入路径版本——不存在路径零命中(反向零误报)、
* 存在路径命中 400、沙盒逃逸探测命中 401。
*
* 只用 ig_anti_jb_check_paths 的可注入入口,不依赖真机越狱特征文件,故宿主可跑。
* 退出码 0=全绿;非 0=有用例失败(stderr 打印失败项)。
*/
#include "IGDefender.h"
#include "poison/IGPoison.h"
#include "policy/IGPolicy.h"
#include "anti_jailbreak/IGAntiJailbreak.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int g_pass = 0, g_fail = 0;
static void ok(const char *name, int cond) {
if (cond) { g_pass++; printf(" PASS %s\n", name); }
else { g_fail++; fprintf(stderr, " FAIL %s\n", name); }
}
/* ---- 构造 policy sink 的输入:手工喂 finding 走 ig_policy_sink ---- */
static void feed(IGPolicyState *st, IGStatus status, int reason) {
IGFinding f;
memset(&f, 0, sizeof(f));
f.status = status;
f.reason = reason;
ig_policy_sink(&f, st);
}
/* 跑一组 finding,返回 evaluate 是否触发毒化(并断言 mask 的一致性)。 */
static int run_policy(void (*feeder)(IGPolicyState *)) {
ig_poison_init(); /* 每例从健康态起 */
IGPolicyState st;
ig_policy_reset(&st, NULL, NULL);
feeder(&st);
int triggered = ig_policy_evaluate_and_mark(&st);
return triggered;
}
/* ---- A. poison ---- */
static void test_poison(void) {
printf("[A] poison\n");
const uint8_t key[] = { 's','t','a','r','t','i','a','s','o','f','t', 0x11, 0x22 };
const size_t n = sizeof(key);
uint8_t out[sizeof(key)];
/* A1 健康:mask==0,apply 恒等 */
ig_poison_init();
ok("healthy_mask_zero", ig_poison_mask() == 0);
memset(out, 0xAB, n);
ig_poison_apply(key, out, n);
ok("healthy_apply_identity", memcmp(out, key, n) == 0);
/* A2 毒化:mask!=0,apply 破坏 key(至少一字节不同) */
ig_poison_mark(0xC0FFEE);
ok("poisoned_mask_nonzero", ig_poison_mask() != 0);
ig_poison_apply(key, out, n);
ok("poisoned_key_corrupted", memcmp(out, key, n) != 0);
/* A3 毒化不可逆回健康(再 mark 不会抵消;只有 init 能清) */
ig_poison_mark(0x1234);
ok("poisoned_still_nonzero_after_second_mark", ig_poison_mask() != 0);
ig_poison_init();
ok("init_restores_healthy", ig_poison_mask() == 0);
/* A4 entropy==0 仍生效(防“混 0 = 不生效”) */
ig_poison_init();
ig_poison_mark(0);
ok("zero_entropy_still_poisons", ig_poison_mask() != 0);
}
/* ---- B. policy 分级 ---- */
static void f_weak_only(IGPolicyState *st) {
feed(st, IG_DETECTED, IG_REASON_FOREIGN_IMAGE); /* 201 WEAK */
feed(st, IG_DETECTED, IG_REASON_FRIDA_PORT); /* 204 WEAK */
}
static void f_unavailable_clear(IGPolicyState *st) {
feed(st, IG_UNAVAILABLE, IG_REASON_INLINE_HOOK); /* STRONG reason 但 UNAVAILABLE→忽略 */
feed(st, IG_CLEAR, IG_REASON_GOT_REBIND); /* CLEAR→忽略 */
}
static void f_single_medium(IGPolicyState *st) {
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 单 MEDIUM */
}
static void f_medium_dup(IGPolicyState *st) {
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 */
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 重复 → 去重后仍算 1 */
}
static void f_two_medium(IGPolicyState *st) {
feed(st, IG_DETECTED, IG_REASON_OBJC_IMP_CHANGED); /* 110 */
feed(st, IG_DETECTED, IG_REASON_SYMBOL_PROVENANCE); /* 100 → 2 个不同 MEDIUM */
}
static void f_single_strong(IGPolicyState *st) {
feed(st, IG_DETECTED, IG_REASON_JAILBREAK_SANDBOX); /* 401 STRONG */
}
static void test_policy(void) {
printf("[B] policy\n");
ok("weak_only_no_trigger", run_policy(f_weak_only) == 0);
ok("unavailable_clear_no_trigger",run_policy(f_unavailable_clear) == 0);
ok("single_medium_no_trigger", run_policy(f_single_medium) == 0);
ok("medium_dup_no_trigger", run_policy(f_medium_dup) == 0); /* 去重:同 reason×2 ≠ 2 distinct */
ok("two_distinct_medium_trigger", run_policy(f_two_medium) == 1);
ok("single_strong_trigger", run_policy(f_single_strong) == 1);
/* 触发后 mask 必非 0(策略真的接到了毒化,不只是返回值) */
run_policy(f_single_strong);
ok("strong_trigger_sets_mask", ig_poison_mask() != 0);
ig_poison_init();
run_policy(f_weak_only);
ok("weak_leaves_mask_zero", ig_poison_mask() == 0);
}
/* ---- C. anti_jailbreak(可注入) ---- */
static int g_jb_hits;
static int jb_sink(const IGFinding *f, void *ctx) {
(void)ctx;
if (f->status == IG_DETECTED) g_jb_hits++;
return 0;
}
static void test_jailbreak(void) {
printf("[C] anti_jailbreak\n");
/* C1 反向:全是不存在的路径 + 不做逃逸探测 → 零命中(守误报底线) */
const char *absent[] = {
"/var/jb/__ig_absent_a", "/Applications/__ig_absent.app", "/usr/bin/__ig_absent"
};
g_jb_hits = 0;
ig_anti_jb_check_paths(absent, sizeof(absent)/sizeof(absent[0]), NULL, jb_sink, NULL);
ok("absent_paths_zero_hit", g_jb_hits == 0);
/* C2 正向 400:构造一个真实存在的临时文件冒充越狱特征文件 → 命中 */
char tmpl[] = "/tmp/ig_jb_fileXXXXXX";
int fd = mkstemp(tmpl);
ok("tmpfile_created", fd >= 0);
if (fd >= 0) close(fd);
const char *present[] = { tmpl };
g_jb_hits = 0;
ig_anti_jb_check_paths(present, 1, NULL, jb_sink, NULL);
ok("present_path_hits_400", g_jb_hits >= 1);
unlink(tmpl);
/* C3 正向 401:宿主上 /tmp 可写,沙盒逃逸探测应成功创建→命中(真机非越狱则失败=反向) */
char probe[] = "/tmp/ig_jb_probeXXXXXX";
int pfd = mkstemp(probe); if (pfd >= 0) { close(pfd); unlink(probe); } /* 腾出路径给探测自建 */
g_jb_hits = 0;
ig_anti_jb_check_paths(absent, sizeof(absent)/sizeof(absent[0]), probe, jb_sink, NULL);
ok("sandbox_probe_hits_401_on_host", g_jb_hits >= 1);
unlink(probe);
}
int main(void) {
printf("== poison_host self-test ==\n");
test_poison();
test_policy();
test_jailbreak();
printf("== %d pass, %d fail ==\n", g_pass, g_fail);
return g_fail == 0 ? 0 : 1;
}