feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固
- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
This commit is contained in:
@@ -1,30 +1,90 @@
|
||||
#define IG_ARCHIVE_IMPL 1
|
||||
#include "IGArchiveGuard.h"
|
||||
#if __has_include("../include/IGHookResponse.h")
|
||||
#include "../include/IGHookResponse.h"
|
||||
#define IG_ARCHIVE_HAS_RESPONSE 1
|
||||
#else
|
||||
#define IG_ARCHIVE_HAS_RESPONSE 0
|
||||
#endif
|
||||
#include "../resolve/IGResolve.h"
|
||||
#include "../anti_hook/IGAntiHook.h"
|
||||
#include "../primitives/IGInline.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <mach-o/loader.h>
|
||||
#include <mach-o/nlist.h>
|
||||
|
||||
/* 仅混淆常量,不作为秘密。SDK GOT 由 dyld 在独立镜像滑移后绑定。 */
|
||||
static const unsigned char names[4][6] = {
|
||||
{0x5a,0x53,0x4c,0x59,0x52}, {0x5a,0x4e,0x59,0x5d,0x58},
|
||||
{0x5a,0x4b,0x4e,0x55,0x48,0x59}, {0x5a,0x5f,0x50,0x53,0x4f,0x59}
|
||||
#if defined(__has_feature)
|
||||
# if __has_feature(ptrauth_calls)
|
||||
# include <ptrauth.h>
|
||||
# define IG_ARCHIVE_STRIP(p) ptrauth_strip((p), ptrauth_key_asia)
|
||||
# endif
|
||||
#endif
|
||||
#ifndef IG_ARCHIVE_STRIP
|
||||
# define IG_ARCHIVE_STRIP(p) (p)
|
||||
#endif
|
||||
|
||||
#if !IG_ARCHIVE_HAS_RESPONSE
|
||||
static inline int ig_response_capture(const IGFinding *finding, void *ctx) {
|
||||
if (finding->status == IG_DETECTED) *(int *)ctx = finding->reason;
|
||||
return 0;
|
||||
}
|
||||
static inline void ig_response_handle(IGStatus status, int reason) {
|
||||
(void)status;
|
||||
(void)reason;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 仅混淆常量,不作为秘密。SDK GOT/Lazy 槽由 dyld 在独立镜像滑移后绑定。 */
|
||||
static const unsigned char names[IG_ARCHIVE_FUNCTION_COUNT][6] = {
|
||||
{0x5a,0x53,0x4c,0x59,0x52},
|
||||
{0x5a,0x4e,0x59,0x5d,0x58},
|
||||
{0x5a,0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5a,0x5f,0x50,0x53,0x4f,0x59},
|
||||
{0x49,0x52,0x50,0x55,0x52,0x57},
|
||||
{0x53,0x4c,0x59,0x52},
|
||||
{0x4e,0x59,0x5d,0x58},
|
||||
{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59},
|
||||
{0x5b,0x59,0x48,0x4c,0x55,0x58},
|
||||
{0x59,0x44,0x55,0x48},
|
||||
{0x5a,0x53,0x4e,0x57},
|
||||
{0x58,0x50,0x53,0x4c,0x59,0x52},
|
||||
{0x58,0x50,0x4f,0x45,0x51}
|
||||
};
|
||||
static const uint8_t name_lengths[IG_ARCHIVE_FUNCTION_COUNT] = {
|
||||
5, 5, 6, 6, 6, 4, 4, 5, 5, 6, 4, 4, 6, 5
|
||||
};
|
||||
|
||||
enum { IG_ARCHIVE_MAX_SLOTS = 1024 };
|
||||
typedef struct {
|
||||
uintptr_t slot_addr;
|
||||
uintptr_t expected_fn;
|
||||
uintptr_t text_lo;
|
||||
uintptr_t text_hi;
|
||||
uint8_t is_lazy;
|
||||
} IGArchiveSlot;
|
||||
|
||||
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
||||
static _Thread_local int resolving;
|
||||
static _Atomic(uintptr_t) targets[4];
|
||||
static uintptr_t expected[4], bindingSlots[4];
|
||||
static unsigned char prefixes[4][16];
|
||||
static IGStatus initial[4];
|
||||
static _Atomic(uintptr_t) targets[IG_ARCHIVE_FUNCTION_COUNT];
|
||||
static uintptr_t expected[IG_ARCHIVE_FUNCTION_COUNT];
|
||||
static unsigned char prefixes[IG_ARCHIVE_FUNCTION_COUNT][16];
|
||||
static IGStatus initial[IG_ARCHIVE_FUNCTION_COUNT];
|
||||
static int initialReason[IG_ARCHIVE_FUNCTION_COUNT];
|
||||
static IGArchiveSlot trackedSlots[IG_ARCHIVE_MAX_SLOTS];
|
||||
static size_t trackedCount;
|
||||
static int bindingReady;
|
||||
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) counters[12];
|
||||
static _Atomic(uint64_t) counters[IG_ARCHIVE_FUNCTION_COUNT * 3];
|
||||
#define COUNT(i, k) atomic_fetch_add_explicit(&counters[(i)*3+(k)], 1, memory_order_relaxed)
|
||||
#else
|
||||
#define COUNT(i, k) ((void)0)
|
||||
@@ -36,20 +96,62 @@ static int read_bytes(uintptr_t address, void *out, size_t size) {
|
||||
&& copied == size;
|
||||
}
|
||||
|
||||
/* 仅支持打包工具已审核的非认证 arm64 GOT;缺槽/重复槽均标为不可验证。 */
|
||||
static int is_system_image(const char *path) {
|
||||
if (!path) return 0;
|
||||
return ig_strncmp(path, "/usr/lib/", 9) == 0 ||
|
||||
ig_strncmp(path, "/System/", 8) == 0 ||
|
||||
ig_strncmp(path, "/private/preboot/Cryptexes/", 27) == 0;
|
||||
}
|
||||
|
||||
static int has_suffix(const char *path, const char *suffix) {
|
||||
if (!path || !suffix) return 0;
|
||||
size_t len = ig_strlen(path), tail = ig_strlen(suffix);
|
||||
return len >= tail && ig_strcmp(path + len - tail, suffix) == 0;
|
||||
}
|
||||
|
||||
static uintptr_t expected_wrapper_for_symbol(const char *name) {
|
||||
struct { const char *sym; uintptr_t fn; } table[] = {
|
||||
{"_rd_a0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a0)},
|
||||
{"_rd_f", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_f)},
|
||||
{"_rd_a1", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a1)},
|
||||
{"_rd_a2", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a2)},
|
||||
{"_rd_a3", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a3)},
|
||||
{"_rd_a4", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a4)},
|
||||
{"_rd_u00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_u00)},
|
||||
{"_rd_o", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_o)},
|
||||
{"_rdo", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rdo)},
|
||||
{"_rd_r", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_r)},
|
||||
{"_rdr", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rdr)},
|
||||
{"_rd_w0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_w0)},
|
||||
{"_rd_w", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_w)},
|
||||
{"_rd_c0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_c0)},
|
||||
{"_rd_c", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_c)},
|
||||
{"_rd_p00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_p00)},
|
||||
{"_rd_p0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_p0)},
|
||||
{"_rd_e", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_e)},
|
||||
{"_rd_k", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_k)},
|
||||
{"_rd_l00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_l00)},
|
||||
{"_rd_s0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_s0)},
|
||||
{"_rd_s", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_s)},
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i++) {
|
||||
if (ig_strcmp(name, table[i].sym) == 0) return table[i].fn;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 扫描所有已加载非系统动态库的非认证 arm64 GOT/Lazy 槽;缺槽或表结构异常标为不可验证。 */
|
||||
static int locate_bindings(void) {
|
||||
unsigned found = 0, images = 0;
|
||||
for (uint32_t index = 0; index < _dyld_image_count(); index++) {
|
||||
trackedCount = 0;
|
||||
uint32_t image_total = _dyld_image_count();
|
||||
for (uint32_t index = 0; index < image_total; index++) {
|
||||
const char *path = _dyld_get_image_name(index);
|
||||
const char *suffix = "/SSZipArchive.framework/SSZipArchive";
|
||||
size_t length = path ? ig_strlen(path) : 0, tail = ig_strlen(suffix);
|
||||
if (length < tail || ig_strcmp(path + length - tail, suffix)) continue;
|
||||
if (++images != 1) return 0;
|
||||
if (!path || is_system_image(path) || has_suffix(path, "/RDCoreIO.framework/RDCoreIO")) continue;
|
||||
const struct mach_header_64 *header = (const void *)_dyld_get_image_header(index);
|
||||
if (!header || header->magic != MH_MAGIC_64 || header->cputype != CPU_TYPE_ARM64
|
||||
|| (header->cpusubtype & 0xffffff) != 0) return 0;
|
||||
uintptr_t slide = _dyld_get_image_vmaddr_slide(index);
|
||||
const struct segment_command_64 *linkedit = NULL;
|
||||
if (!header || header->magic != MH_MAGIC_64 || header->filetype != MH_DYLIB) continue;
|
||||
if (header->cputype != CPU_TYPE_ARM64 || (header->cpusubtype & 0xffffff) == 2) return 0;
|
||||
uintptr_t slide = (uintptr_t)_dyld_get_image_vmaddr_slide(index);
|
||||
const struct segment_command_64 *linkedit = NULL, *text_seg = NULL;
|
||||
const struct symtab_command *symtab = NULL;
|
||||
const struct dysymtab_command *dysymtab = NULL;
|
||||
const uint8_t *command = (const void *)(header + 1);
|
||||
@@ -57,23 +159,26 @@ static int locate_bindings(void) {
|
||||
for (uint32_t i = 0; i < header->ncmds; i++) {
|
||||
if (command + sizeof(struct load_command) > end) return 0;
|
||||
const struct load_command *lc = (const void *)command;
|
||||
if (lc->cmdsize < 8 || lc->cmdsize % 8 || lc->cmdsize > (size_t)(end-command)) return 0;
|
||||
if (lc->cmdsize < 8 || lc->cmdsize % 8 || lc->cmdsize > (size_t)(end - command)) return 0;
|
||||
if (lc->cmd == LC_SEGMENT_64) {
|
||||
if (lc->cmdsize < sizeof(struct segment_command_64)) return 0;
|
||||
const struct segment_command_64 *seg = (const void *)lc;
|
||||
if (sizeof(*seg) + (uint64_t)seg->nsects * sizeof(struct section_64) != lc->cmdsize) return 0;
|
||||
if (!ig_strcmp(seg->segname, SEG_LINKEDIT)) linkedit = seg;
|
||||
else if (!ig_strcmp(seg->segname, SEG_TEXT)) text_seg = seg;
|
||||
} else if (lc->cmd == LC_SYMTAB && lc->cmdsize == sizeof(*symtab)) symtab = (const void *)lc;
|
||||
else if (lc->cmd == LC_DYSYMTAB && lc->cmdsize == sizeof(*dysymtab)) dysymtab = (const void *)lc;
|
||||
command += lc->cmdsize;
|
||||
}
|
||||
if (!linkedit || !symtab || !dysymtab || command != end) return 0;
|
||||
if (!linkedit || !text_seg || !symtab || !dysymtab || command != end) continue;
|
||||
uint64_t lo = linkedit->fileoff, hi = lo + linkedit->filesize;
|
||||
if (hi < lo || symtab->symoff < lo || (uint64_t)symtab->symoff + (uint64_t)symtab->nsyms*16 > hi
|
||||
if (hi < lo || symtab->symoff < lo || (uint64_t)symtab->symoff + (uint64_t)symtab->nsyms * 16 > hi
|
||||
|| symtab->stroff < lo || (uint64_t)symtab->stroff + symtab->strsize > hi
|
||||
|| dysymtab->indirectsymoff < lo
|
||||
|| (uint64_t)dysymtab->indirectsymoff + (uint64_t)dysymtab->nindirectsyms*4 > hi) return 0;
|
||||
uintptr_t base = slide + linkedit->vmaddr - lo;
|
||||
|| (uint64_t)dysymtab->indirectsymoff + (uint64_t)dysymtab->nindirectsyms * 4 > hi) return 0;
|
||||
uintptr_t base = slide + (uintptr_t)(linkedit->vmaddr - lo);
|
||||
uintptr_t text_lo = slide + (uintptr_t)text_seg->vmaddr;
|
||||
uintptr_t text_hi = text_lo + (uintptr_t)text_seg->vmsize;
|
||||
const struct nlist_64 *symbols = (const void *)(base + symtab->symoff);
|
||||
const char *strings = (const void *)(base + symtab->stroff);
|
||||
const uint32_t *indirect = (const void *)(base + dysymtab->indirectsymoff);
|
||||
@@ -85,40 +190,49 @@ static int locate_bindings(void) {
|
||||
const struct section_64 *sections = (const void *)(seg + 1);
|
||||
for (uint32_t j = 0; j < seg->nsects; j++) {
|
||||
const struct section_64 *s = sections + j;
|
||||
if ((s->flags & SECTION_TYPE) != S_NON_LAZY_SYMBOL_POINTERS) continue;
|
||||
if (s->size % 8 || (uint64_t)s->reserved1 + s->size/8 > dysymtab->nindirectsyms) return 0;
|
||||
for (uint64_t k = 0; k < s->size/8; k++) {
|
||||
uint32_t stype = s->flags & SECTION_TYPE;
|
||||
if (stype != S_NON_LAZY_SYMBOL_POINTERS && stype != S_LAZY_SYMBOL_POINTERS) continue;
|
||||
if (s->size % 8 || (uint64_t)s->reserved1 + s->size / 8 > dysymtab->nindirectsyms) return 0;
|
||||
for (uint64_t k = 0; k < s->size / 8; k++) {
|
||||
uint32_t symbol = indirect[s->reserved1 + k];
|
||||
if (symbol & (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) continue;
|
||||
if (symbol >= symtab->nsyms) return 0;
|
||||
uint32_t strx = symbols[symbol].n_un.n_strx;
|
||||
if (strx >= symtab->strsize) return 0;
|
||||
const char *name = strings + strx;
|
||||
if (!memchr(name, 0, symtab->strsize-strx)) return 0;
|
||||
for (unsigned n = 0; n < 4; n++) {
|
||||
char expectedName[] = "_ig_a0";
|
||||
expectedName[5] += n;
|
||||
if (ig_strcmp(name, expectedName)) continue;
|
||||
if (found & (1u << n)) return 0;
|
||||
bindingSlots[n] = slide + s->addr + k*8;
|
||||
found |= 1u << n;
|
||||
}
|
||||
if (!memchr(name, 0, symtab->strsize - strx)) return 0;
|
||||
uintptr_t expected_fn = expected_wrapper_for_symbol(name);
|
||||
if (!expected_fn) continue;
|
||||
if (trackedCount >= IG_ARCHIVE_MAX_SLOTS) return 0;
|
||||
uintptr_t slot_addr = slide + (uintptr_t)s->addr + (uintptr_t)(k * 8);
|
||||
uintptr_t probe = 0;
|
||||
if (!read_bytes(slot_addr, &probe, sizeof(probe))) return 0;
|
||||
trackedSlots[trackedCount++] = (IGArchiveSlot){
|
||||
.slot_addr = slot_addr,
|
||||
.expected_fn = expected_fn,
|
||||
.text_lo = text_lo,
|
||||
.text_hi = text_hi,
|
||||
.is_lazy = (uint8_t)(stype == S_LAZY_SYMBOL_POINTERS)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
command += lc->cmdsize;
|
||||
}
|
||||
}
|
||||
return found == 15 && images == 1;
|
||||
return trackedCount > 0;
|
||||
}
|
||||
|
||||
static void initialize(void) {
|
||||
bindingReady = locate_bindings();
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
IGResolveResult result = ig_resolve_verified(NULL, names[i], i < 2 ? 5 : 6, NULL, NULL);
|
||||
for (unsigned i = 0; i < IG_ARCHIVE_FUNCTION_COUNT; i++) {
|
||||
IGResolveResult result = ig_resolve_verified(NULL, names[i], name_lengths[i], ig_response_capture, &initialReason[i]);
|
||||
initial[i] = result.status;
|
||||
if (result.status != IG_CLEAR || !result.address) continue;
|
||||
if (!read_bytes((uintptr_t)result.address, prefixes[i], 16)) { initial[i] = IG_UNAVAILABLE; continue; }
|
||||
if (!read_bytes((uintptr_t)IG_ARCHIVE_STRIP(result.address), prefixes[i], 16)) {
|
||||
initial[i] = IG_UNAVAILABLE;
|
||||
continue;
|
||||
}
|
||||
expected[i] = (uintptr_t)result.address;
|
||||
atomic_store_explicit(&targets[i], expected[i], memory_order_release);
|
||||
}
|
||||
@@ -132,95 +246,274 @@ static int prepare(void) {
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
static IGStatus validate(unsigned index, uintptr_t *address) {
|
||||
__attribute__((constructor))
|
||||
static void ig_archive_warmup(void) {
|
||||
(void)prepare();
|
||||
}
|
||||
|
||||
static IGStatus validate(unsigned index, uintptr_t *address, int *reason) {
|
||||
*reason = initialReason[index];
|
||||
if (initial[index] != IG_CLEAR) return initial[index];
|
||||
*reason = 107;
|
||||
uintptr_t current = atomic_load_explicit(&targets[index], memory_order_acquire);
|
||||
if (!current || current != expected[index]) return IG_DETECTED;
|
||||
IGStatus status = ig_verify_function((void *)current, "archive-target", NULL, NULL);
|
||||
if (status != IG_CLEAR) return status;
|
||||
unsigned char bytes[16];
|
||||
if (!read_bytes(current, bytes, 16)) return IG_UNAVAILABLE;
|
||||
if (!read_bytes((uintptr_t)IG_ARCHIVE_STRIP((const void *)current), bytes, 16)) return IG_UNAVAILABLE;
|
||||
if (ig_memcmp(bytes, prefixes[index], 16)) return IG_DETECTED;
|
||||
*reason = IG_REASON_VERIFICATION_UNAVAILABLE;
|
||||
IGStatus status = ig_verify_function((void *)current, IG_DIAG_STR("archive-target"), ig_response_capture, reason);
|
||||
if (status != IG_CLEAR) return status;
|
||||
*address = current;
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
static IGStatus check_bindings(void) {
|
||||
#ifdef IG_ARCHIVE_TESTING
|
||||
/* 宿主 ABI fixture 不装载 SSZipArchive;此豁免不能编入业务包。 */
|
||||
/* 宿主 ABI fixture 不装载外部动态 SDK;此豁免不能编入业务包。 */
|
||||
return IG_CLEAR;
|
||||
#else
|
||||
if (!bindingReady) return IG_UNAVAILABLE;
|
||||
uintptr_t correct[4] = {(uintptr_t)&ig_a0, (uintptr_t)&ig_a1, (uintptr_t)&ig_a2, (uintptr_t)&ig_a3};
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
uintptr_t current;
|
||||
if (!read_bytes(bindingSlots[i], ¤t, sizeof(current))) return IG_UNAVAILABLE;
|
||||
if (current != correct[i]) return IG_DETECTED;
|
||||
if (!bindingReady || trackedCount == 0) return IG_UNAVAILABLE;
|
||||
for (size_t i = 0; i < trackedCount; i++) {
|
||||
const IGArchiveSlot *slot = &trackedSlots[i];
|
||||
uintptr_t raw = *(const volatile uintptr_t *)slot->slot_addr;
|
||||
uintptr_t cur = (uintptr_t)IG_ARCHIVE_STRIP((const void *)raw);
|
||||
if (cur == slot->expected_fn) continue;
|
||||
if (slot->is_lazy && cur >= slot->text_lo && cur < slot->text_hi) continue;
|
||||
return IG_DETECTED;
|
||||
}
|
||||
return IG_CLEAR;
|
||||
#endif
|
||||
}
|
||||
|
||||
__attribute__((noinline))
|
||||
static uintptr_t checked(unsigned index) {
|
||||
COUNT(index, 0);
|
||||
if (!prepare()) { COUNT(index, 2); errno = EDEADLK; return 0; }
|
||||
uintptr_t address = 0;
|
||||
int reason = 107;
|
||||
IGStatus status = check_bindings();
|
||||
if (status == IG_CLEAR) status = validate(index, &address);
|
||||
if (status != IG_CLEAR) { COUNT(index, 2); errno = status == IG_DETECTED ? EACCES : ENOSYS; return 0; }
|
||||
if (status == IG_CLEAR) status = validate(index, &address, &reason);
|
||||
if (status != IG_CLEAR) {
|
||||
COUNT(index, 2);
|
||||
ig_response_handle(status, reason);
|
||||
errno = status == IG_DETECTED ? EACCES : ENOSYS;
|
||||
return 0;
|
||||
}
|
||||
COUNT(index, 1);
|
||||
return address;
|
||||
}
|
||||
|
||||
__attribute__((noinline)) FILE *ig_a0(const char *path, const char *mode) {
|
||||
__attribute__((noinline)) FILE *rd_a0(const char *path, const char *mode) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(0);
|
||||
if (!address) return NULL;
|
||||
errno = incoming;
|
||||
return ((FILE *(*)(const char *, const char *))address)(path, mode);
|
||||
}
|
||||
__attribute__((noinline)) size_t ig_a1(void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
__attribute__((noinline)) FILE *rd_f(const char *path, const char *mode) {
|
||||
return rd_a0(path, mode);
|
||||
}
|
||||
__attribute__((noinline)) size_t rd_a1(void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(1);
|
||||
if (!address) return 0;
|
||||
errno = incoming;
|
||||
return ((size_t (*)(void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
||||
}
|
||||
__attribute__((noinline)) size_t ig_a2(const void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
__attribute__((noinline)) size_t rd_a2(const void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(2);
|
||||
if (!address) return 0;
|
||||
errno = incoming;
|
||||
return ((size_t (*)(const void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
||||
}
|
||||
__attribute__((noinline)) int ig_a3(FILE *stream) {
|
||||
__attribute__((noinline)) int rd_a3(FILE *stream) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(3);
|
||||
if (!address) return EOF;
|
||||
errno = incoming;
|
||||
return ((int (*)(FILE *))address)(stream);
|
||||
}
|
||||
|
||||
int ig_archive_check(void) {
|
||||
__attribute__((noinline)) int rd_a4(const char *path) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(4);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((int (*)(const char *))address)(path);
|
||||
}
|
||||
__attribute__((noinline)) int rd_u00(const char *path) {
|
||||
return rd_a4(path);
|
||||
}
|
||||
|
||||
static int ig_open_impl(const char *path, int flags, int mode) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(5);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
int (*fn)(const char *, int, ...) = (void *)address;
|
||||
return (flags & O_CREAT) ? fn(path, flags, mode) : fn(path, flags);
|
||||
}
|
||||
__attribute__((noinline)) int rd_o(const char *path, int flags, ...) {
|
||||
int mode = 0;
|
||||
if (flags & O_CREAT) {
|
||||
va_list args;
|
||||
va_start(args, flags);
|
||||
mode = va_arg(args, int);
|
||||
va_end(args);
|
||||
}
|
||||
return ig_open_impl(path, flags, mode);
|
||||
}
|
||||
__attribute__((noinline)) int rdo(const char *path, int flags, ...) {
|
||||
int mode = 0;
|
||||
if (flags & O_CREAT) {
|
||||
va_list args;
|
||||
va_start(args, flags);
|
||||
mode = va_arg(args, int);
|
||||
va_end(args);
|
||||
}
|
||||
return ig_open_impl(path, flags, mode);
|
||||
}
|
||||
|
||||
__attribute__((noinline)) ssize_t rd_r(int fd, void *buffer, size_t count) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(6);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((ssize_t (*)(int, void *, size_t))address)(fd, buffer, count);
|
||||
}
|
||||
__attribute__((noinline)) ssize_t rdr(int fd, void *buffer, size_t count) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(6);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((ssize_t (*)(int, void *, size_t))address)(fd, buffer, count);
|
||||
}
|
||||
__attribute__((noinline)) ssize_t rd_w0(int fd, const void *buffer, size_t count) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(7);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((ssize_t (*)(int, const void *, size_t))address)(fd, buffer, count);
|
||||
}
|
||||
__attribute__((noinline)) ssize_t rd_w(int fd, const void *buffer, size_t count) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(7);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((ssize_t (*)(int, const void *, size_t))address)(fd, buffer, count);
|
||||
}
|
||||
__attribute__((noinline)) int rd_c0(int fd) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(8);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((int (*)(int))address)(fd);
|
||||
}
|
||||
__attribute__((noinline)) int rd_c(int fd) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(8);
|
||||
if (!address) return -1;
|
||||
errno = incoming;
|
||||
return ((int (*)(int))address)(fd);
|
||||
}
|
||||
__attribute__((noinline)) pid_t rd_p00(void) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(9);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno = incoming;
|
||||
return ((pid_t (*)(void))address)();
|
||||
}
|
||||
__attribute__((noinline)) pid_t rd_p0(void) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(9);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno = incoming;
|
||||
return ((pid_t (*)(void))address)();
|
||||
}
|
||||
__attribute__((noinline, noreturn)) void rd_e(int status) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(10);
|
||||
if (address) {
|
||||
errno = incoming;
|
||||
((void (*)(int))address)(status);
|
||||
}
|
||||
_exit(status);
|
||||
__builtin_trap();
|
||||
}
|
||||
__attribute__((noinline)) pid_t rd_k(void) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(11);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno = incoming;
|
||||
return ((pid_t (*)(void))address)();
|
||||
}
|
||||
__attribute__((noinline)) void *rd_l00(const char *path, int mode) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(12);
|
||||
if (!address) return NULL;
|
||||
errno = incoming;
|
||||
return ((void *(*)(const char *, int))address)(path, mode);
|
||||
}
|
||||
__attribute__((noinline)) void *rd_s0(void *handle, const char *symbol) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(13);
|
||||
if (!address) return NULL;
|
||||
errno = incoming;
|
||||
return ((void *(*)(void *, const char *))address)(handle, symbol);
|
||||
}
|
||||
__attribute__((noinline)) void *rd_s(void *handle, const char *symbol) {
|
||||
int incoming = errno;
|
||||
uintptr_t address = checked(13);
|
||||
if (!address) return NULL;
|
||||
errno = incoming;
|
||||
return ((void *(*)(void *, const char *))address)(handle, symbol);
|
||||
}
|
||||
|
||||
/* 返回具体原因,避免把启动时的跳转形态启发式误升级为已确认篡改。 */
|
||||
int rd_io_ev(void) {
|
||||
int incoming = errno, result = 0;
|
||||
if (!prepare()) { errno = incoming; return -1; }
|
||||
IGStatus status = check_bindings();
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
IGStatus binding = check_bindings();
|
||||
if (binding == IG_DETECTED) result = 107;
|
||||
else if (binding == IG_UNAVAILABLE) result = -1;
|
||||
for (unsigned i = 0; i < IG_ARCHIVE_FUNCTION_COUNT; i++) {
|
||||
uintptr_t address;
|
||||
IGStatus current = validate(i, &address);
|
||||
if (current == IG_DETECTED || (current == IG_UNAVAILABLE && status == IG_CLEAR)) status = current;
|
||||
int reason;
|
||||
IGStatus current = validate(i, &address, &reason);
|
||||
if (current == IG_DETECTED) {
|
||||
if (reason == 100 || reason == 107) result = reason;
|
||||
else if (result <= 0) result = 101;
|
||||
} else if (current == IG_UNAVAILABLE && result == 0) result = -1;
|
||||
}
|
||||
errno = incoming;
|
||||
return status == IG_CLEAR ? 0 : status == IG_DETECTED ? 1 : -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
int rd_io_chk(void) {
|
||||
int result = rd_io_ev();
|
||||
return result > 0 ? 1 : result;
|
||||
}
|
||||
|
||||
void rd_io_cfg(int enabled, int fd) {
|
||||
#if IG_ARCHIVE_HAS_RESPONSE
|
||||
ig_response_configure(enabled, fd);
|
||||
#else
|
||||
(void)enabled;
|
||||
(void)fd;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef IG_OBSERVATION
|
||||
uint64_t ig_archive_counter(unsigned index) {
|
||||
return index < 12 ? atomic_load_explicit(&counters[index], memory_order_relaxed) : 0;
|
||||
return index < IG_ARCHIVE_FUNCTION_COUNT * 3
|
||||
? atomic_load_explicit(&counters[index], memory_order_relaxed) : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef IG_ARCHIVE_TESTING
|
||||
void ig_archive_test_override(unsigned index, void *address) {
|
||||
if (index < 4 && prepare()) atomic_store_explicit(&targets[index], (uintptr_t)address, memory_order_release);
|
||||
if (index < IG_ARCHIVE_FUNCTION_COUNT && prepare())
|
||||
atomic_store_explicit(&targets[index], (uintptr_t)address, memory_order_release);
|
||||
}
|
||||
void ig_archive_test_reentry(int enabled) { resolving = enabled; }
|
||||
#endif
|
||||
|
||||
@@ -2,13 +2,38 @@
|
||||
#define IG_ARCHIVE_GUARD_H
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
enum { IG_ARCHIVE_FUNCTION_COUNT = 14 };
|
||||
#define IG_ARCHIVE_EXPORT __attribute__((visibility("default")))
|
||||
IG_ARCHIVE_EXPORT FILE *ig_a0(const char *path, const char *mode);
|
||||
IG_ARCHIVE_EXPORT size_t ig_a1(void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT size_t ig_a2(const void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT int ig_a3(FILE *stream);
|
||||
|
||||
IG_ARCHIVE_EXPORT FILE *rd_a0(const char *path, const char *mode);
|
||||
IG_ARCHIVE_EXPORT FILE *rd_f(const char *path, const char *mode);
|
||||
IG_ARCHIVE_EXPORT size_t rd_a1(void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT size_t rd_a2(const void *ptr, size_t size, size_t count, FILE *stream);
|
||||
IG_ARCHIVE_EXPORT int rd_a3(FILE *stream);
|
||||
IG_ARCHIVE_EXPORT int rd_a4(const char *path);
|
||||
IG_ARCHIVE_EXPORT int rd_u00(const char *path);
|
||||
IG_ARCHIVE_EXPORT int rd_o(const char *path, int flags, ...);
|
||||
IG_ARCHIVE_EXPORT int rdo(const char *path, int flags, ...);
|
||||
IG_ARCHIVE_EXPORT ssize_t rd_r(int fd, void *buffer, size_t count);
|
||||
IG_ARCHIVE_EXPORT ssize_t rdr(int fd, void *buffer, size_t count);
|
||||
IG_ARCHIVE_EXPORT ssize_t rd_w0(int fd, const void *buffer, size_t count);
|
||||
IG_ARCHIVE_EXPORT ssize_t rd_w(int fd, const void *buffer, size_t count);
|
||||
IG_ARCHIVE_EXPORT int rd_c0(int fd);
|
||||
IG_ARCHIVE_EXPORT int rd_c(int fd);
|
||||
IG_ARCHIVE_EXPORT pid_t rd_p00(void);
|
||||
IG_ARCHIVE_EXPORT pid_t rd_p0(void);
|
||||
IG_ARCHIVE_EXPORT __attribute__((noreturn)) void rd_e(int status);
|
||||
IG_ARCHIVE_EXPORT pid_t rd_k(void);
|
||||
IG_ARCHIVE_EXPORT void *rd_l00(const char *path, int mode);
|
||||
IG_ARCHIVE_EXPORT void *rd_s0(void *handle, const char *symbol);
|
||||
IG_ARCHIVE_EXPORT void *rd_s(void *handle, const char *symbol);
|
||||
|
||||
/* 0=正常,1=检测异常,-1=不可验证;保留 errno。 */
|
||||
IG_ARCHIVE_EXPORT int ig_archive_check(void);
|
||||
IG_ARCHIVE_EXPORT void rd_io_cfg(int enabled, int fd);
|
||||
IG_ARCHIVE_EXPORT int rd_io_chk(void);
|
||||
/* 0=正常,-1=不可用,正值=具体原因;101 为启发式,不触发退出。 */
|
||||
IG_ARCHIVE_EXPORT int rd_io_ev(void);
|
||||
#ifdef IG_OBSERVATION
|
||||
/* index/3 为函数,index%3 为 attempted/dispatched/rejected。 */
|
||||
IG_ARCHIVE_EXPORT uint64_t ig_archive_counter(unsigned index);
|
||||
@@ -17,4 +42,32 @@ IG_ARCHIVE_EXPORT uint64_t ig_archive_counter(unsigned index);
|
||||
void ig_archive_test_override(unsigned index, void *address);
|
||||
void ig_archive_test_reentry(int enabled);
|
||||
#endif
|
||||
|
||||
#ifndef IG_ARCHIVE_IMPL
|
||||
#define ig_a0 rd_a0
|
||||
#define ig_f rd_f
|
||||
#define ig_a1 rd_a1
|
||||
#define ig_a2 rd_a2
|
||||
#define ig_a3 rd_a3
|
||||
#define ig_a4 rd_a4
|
||||
#define ig_u00 rd_u00
|
||||
#define ig_o rd_o
|
||||
#define igo rdo
|
||||
#define ig_r rd_r
|
||||
#define igr rdr
|
||||
#define ig_w0 rd_w0
|
||||
#define ig_w rd_w
|
||||
#define ig_c0 rd_c0
|
||||
#define ig_c rd_c
|
||||
#define ig_p00 rd_p00
|
||||
#define ig_p0 rd_p0
|
||||
#define ig_e rd_e
|
||||
#define ig_k rd_k
|
||||
#define ig_l00 rd_l00
|
||||
#define ig_s0 rd_s0
|
||||
#define ig_s rd_s
|
||||
#define ig_archive_response_configure rd_io_cfg
|
||||
#define ig_archive_check rd_io_chk
|
||||
#define ig_archive_assess rd_io_ev
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+145
-13
@@ -6,19 +6,42 @@
|
||||
#include <stdatomic.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <mach/mach.h>
|
||||
|
||||
/* 编码只减少常量区明文,不作为秘密或强加密。缓存与基线仍可被同进程攻击者修改。 */
|
||||
static const unsigned char kNames[2][5] = {{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59}};
|
||||
enum { IG_DISPATCH_COUNT = 11 };
|
||||
static const unsigned char kNames[IG_DISPATCH_COUNT][6] = {
|
||||
{0x4b,0x4e,0x55,0x48,0x59},
|
||||
{0x5f,0x50,0x53,0x4f,0x59},
|
||||
{0x4e,0x59,0x5d,0x58},
|
||||
{0x53,0x4c,0x59,0x52},
|
||||
{0x49,0x52,0x50,0x55,0x52,0x57},
|
||||
{0x5b,0x59,0x48,0x4c,0x55,0x58},
|
||||
{0x5a,0x53,0x4c,0x59,0x52},
|
||||
{0x59,0x44,0x55,0x48},
|
||||
{0x5a,0x53,0x4e,0x57},
|
||||
{0x58,0x50,0x53,0x4c,0x59,0x52},
|
||||
{0x58,0x50,0x4f,0x45,0x51},
|
||||
};
|
||||
static const size_t kLengths[IG_DISPATCH_COUNT] = {5,5,4,4,6,6,5,4,4,6,5};
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
static const char *const kLabels[IG_DISPATCH_COUNT] = {
|
||||
"dispatch-write", "dispatch-close", "dispatch-read",
|
||||
"dispatch-open", "dispatch-unlink", "dispatch-getpid", "dispatch-fopen",
|
||||
"dispatch-exit", "dispatch-fork", "dispatch-dlopen", "dispatch-dlsym"
|
||||
};
|
||||
#endif
|
||||
static pthread_once_t gOnce=PTHREAD_ONCE_INIT;
|
||||
static _Atomic(uintptr_t) gSlots[2];
|
||||
static uintptr_t gExpected[2];
|
||||
static unsigned char gPrefixes[2][16];
|
||||
static IGStatus gInitialStatus[2]={IG_UNAVAILABLE,IG_UNAVAILABLE};
|
||||
static _Atomic(uintptr_t) gSlots[IG_DISPATCH_COUNT];
|
||||
static uintptr_t gExpected[IG_DISPATCH_COUNT];
|
||||
static unsigned char gPrefixes[IG_DISPATCH_COUNT][16];
|
||||
static IGStatus gInitialStatus[IG_DISPATCH_COUNT];
|
||||
static _Thread_local int gResolving;
|
||||
#ifdef IG_OBSERVATION
|
||||
static _Atomic(uint64_t) gAttempted[2], gDispatched[2];
|
||||
static _Atomic(uint64_t) gAttempted[IG_DISPATCH_COUNT], gDispatched[IG_DISPATCH_COUNT];
|
||||
#define IG_COUNT(slot,index) atomic_fetch_add_explicit(&(slot)[index],1,memory_order_relaxed)
|
||||
#else
|
||||
#define IG_COUNT(slot,index) ((void)0)
|
||||
@@ -31,8 +54,8 @@ static int read_prefix(uintptr_t address, unsigned char out[16]) {
|
||||
}
|
||||
|
||||
static void initialize(void) {
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],5,NULL,NULL);
|
||||
for (unsigned i=0;i<IG_DISPATCH_COUNT;i++) {
|
||||
IGResolveResult r=ig_resolve_verified(NULL,kNames[i],kLengths[i],NULL,NULL);
|
||||
gInitialStatus[i]=r.status;
|
||||
if (r.status!=IG_CLEAR || !r.address) continue;
|
||||
uintptr_t address=(uintptr_t)r.address;
|
||||
@@ -61,7 +84,7 @@ static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
||||
*reason=IG_REASON_DISPATCH_TAMPER;
|
||||
return IG_DETECTED;
|
||||
}
|
||||
IGStatus status=ig_verify_function((void *)current,"dispatch-target",NULL,NULL);
|
||||
IGStatus status=ig_verify_function((void *)current,IG_DIAG_STR("dispatch-target"),NULL,NULL);
|
||||
if (status!=IG_CLEAR) {
|
||||
*reason=status==IG_DETECTED ? IG_REASON_INLINE_HOOK : IG_REASON_VERIFICATION_UNAVAILABLE;
|
||||
return status;
|
||||
@@ -79,6 +102,7 @@ static IGStatus validate(unsigned i, uintptr_t *address, int *reason) {
|
||||
return IG_CLEAR;
|
||||
}
|
||||
|
||||
__attribute__((noinline))
|
||||
static uintptr_t checked_address(unsigned i) {
|
||||
if (!prepare()) { errno=EDEADLK; return 0; }
|
||||
uintptr_t address=0; int reason=0;
|
||||
@@ -87,6 +111,11 @@ static uintptr_t checked_address(unsigned i) {
|
||||
return address;
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void ig_dispatch_warmup(void) {
|
||||
(void)prepare();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
ssize_t IG_dispatch_write(int fd, const void *buffer, size_t count) {
|
||||
int incoming=errno;
|
||||
@@ -111,17 +140,120 @@ int IG_dispatch_close(int fd) {
|
||||
return function(fd);
|
||||
}
|
||||
|
||||
/* Each typed entry preserves libc results and errno; no retry or short-I/O loop.
|
||||
* Not async-signal-safe: validation uses pthread_once/dlsym/Mach APIs. */
|
||||
__attribute__((used,noinline))
|
||||
ssize_t IG_dispatch_read(int fd, void *buffer, size_t count) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(2);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
ssize_t (*function)(int,void *,size_t)=(void *)address;
|
||||
return function(fd,buffer,count);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
int IG_dispatch_open(const char *path, int flags, ...) {
|
||||
int incoming=errno;
|
||||
/* Darwin mode_t is promoted to int in the variadic argument list.
|
||||
* Do not read a nonexistent third argument for ordinary two-argument open. */
|
||||
int mode=0;
|
||||
if (flags & O_CREAT) {
|
||||
va_list args; va_start(args,flags); mode=va_arg(args,int); va_end(args);
|
||||
}
|
||||
uintptr_t address=checked_address(3);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
int (*function)(const char *,int,...)=(void *)address;
|
||||
return (flags & O_CREAT) ? function(path,flags,mode) : function(path,flags);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
int IG_dispatch_unlink(const char *path) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(4);
|
||||
if (!address) return -1;
|
||||
errno=incoming;
|
||||
int (*function)(const char *)=(void *)address;
|
||||
return function(path);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
pid_t IG_dispatch_getpid(void) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(5);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno=incoming;
|
||||
pid_t (*function)(void)=(void *)address;
|
||||
return function();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
FILE *IG_dispatch_fopen(const char *path, const char *mode) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(6);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
FILE *(*function)(const char *,const char *)=(void *)address;
|
||||
return function(path,mode);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline,noreturn))
|
||||
void IG_dispatch_exit(int status) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(7);
|
||||
if (address) {
|
||||
errno=incoming;
|
||||
void (*function)(int)=(void *)address;
|
||||
function(status);
|
||||
}
|
||||
_exit(status);
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
pid_t IG_dispatch_fork(void) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(8);
|
||||
if (!address) return (pid_t)-1;
|
||||
errno=incoming;
|
||||
pid_t (*function)(void)=(void *)address;
|
||||
return function();
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
void *IG_dispatch_dlopen(const char *path, int mode) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(9);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
void *(*function)(const char *,int)=(void *)address;
|
||||
return function(path,mode);
|
||||
}
|
||||
|
||||
__attribute__((used,noinline))
|
||||
void *IG_dispatch_dlsym(void *handle, const char *symbol) {
|
||||
int incoming=errno;
|
||||
uintptr_t address=checked_address(10);
|
||||
if (!address) return NULL;
|
||||
errno=incoming;
|
||||
void *(*function)(void *,const char *)=(void *)address;
|
||||
return function(handle,symbol);
|
||||
}
|
||||
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx) {
|
||||
int saved=errno, hits=0;
|
||||
int ready=prepare();
|
||||
for (unsigned i=0;i<2;i++) {
|
||||
for (unsigned i=0;i<IG_DISPATCH_COUNT;i++) {
|
||||
uintptr_t address=0; int reason=IG_REASON_DISPATCH_UNAVAILABLE;
|
||||
IGStatus status=ready ? validate(i,&address,&reason) : IG_UNAVAILABLE;
|
||||
if (status==IG_CLEAR) continue;
|
||||
IGFinding f={0}; f.status=status; f.reason=reason;
|
||||
const char *label=i==0 ? "dispatch-write" : "dispatch-close";
|
||||
#if IG_DIAGNOSTIC_STRINGS
|
||||
const char *label=kLabels[i];
|
||||
strncpy(f.subject,label,sizeof(f.subject)-1);
|
||||
strncpy(f.evidence,"调度地址、提供方或代码前缀校验未通过",sizeof(f.evidence)-1);
|
||||
#endif
|
||||
if (status==IG_DETECTED) hits++;
|
||||
if (sink && sink(&f,ctx)!=0) break;
|
||||
}
|
||||
@@ -143,7 +275,7 @@ IGDispatchObservation ig_dispatch_observation(void) {
|
||||
|
||||
#ifdef IG_DISPATCH_TESTING
|
||||
void ig_dispatch_test_override(unsigned i, void *address) {
|
||||
if (i<2 && prepare()) atomic_store_explicit(&gSlots[i],(uintptr_t)address,memory_order_release);
|
||||
if (i<IG_DISPATCH_COUNT && prepare()) atomic_store_explicit(&gSlots[i],(uintptr_t)address,memory_order_release);
|
||||
}
|
||||
void ig_dispatch_test_reentry(int enabled) { gResolving=enabled; }
|
||||
#endif
|
||||
|
||||
@@ -1,18 +1,34 @@
|
||||
#ifndef IG_DISPATCH_H
|
||||
#define IG_DISPATCH_H
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/IGDefender.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* 固定签名试点:首次按线程安全方式解析;异常时不调用未经校验的地址。 */
|
||||
/* 线程安全解析并缓存目标;每次调用前校验,失败不调用目标。
|
||||
* 检查依赖 pthread/dlsym/Mach,不可用于异步信号处理器或 fork 后的子进程。
|
||||
* 仅供逐调用点接入,不能作为全局 libc interpose 实现。 */
|
||||
ssize_t IG_dispatch_write(int fd, const void *buffer, size_t count);
|
||||
int IG_dispatch_close(int fd);
|
||||
/* Only redirect reviewed ordinary call sites, never signal handlers or post-fork
|
||||
* child paths. getpid returns -1 with errno if validation fails (unlike libc).
|
||||
* These declarations do not automatically redirect existing IPA imports. */
|
||||
ssize_t IG_dispatch_read(int fd, void *buffer, size_t count);
|
||||
int IG_dispatch_open(const char *path, int flags, ...);
|
||||
int IG_dispatch_unlink(const char *path);
|
||||
pid_t IG_dispatch_getpid(void);
|
||||
FILE *IG_dispatch_fopen(const char *path, const char *mode);
|
||||
__attribute__((noreturn)) void IG_dispatch_exit(int status);
|
||||
pid_t IG_dispatch_fork(void);
|
||||
void *IG_dispatch_dlopen(const char *path, int mode);
|
||||
void *IG_dispatch_dlsym(void *handle, const char *symbol);
|
||||
int ig_dispatch_check(IGFindingSink sink, void *ctx);
|
||||
|
||||
#ifdef IG_OBSERVATION
|
||||
/* 仅观察构建导出只读计数,不提供地址覆盖或关闭校验的接口。 */
|
||||
/* 保留原 write/close 试点计数 ABI;新增入口不计入这四个字段。
|
||||
* 仅观察构建导出只读计数,不提供地址覆盖或关闭校验的接口。 */
|
||||
typedef struct {
|
||||
uint64_t write_attempted, write_dispatched;
|
||||
uint64_t close_attempted, close_dispatched;
|
||||
|
||||
Reference in New Issue
Block a user