fix(defender): ObjC 方法基线改用构建期签名清单,修复误报 110 导致的延迟闪退
- 构建期生成随包签名的 IGMethods.bin(仅主程序),运行时按镜像 UUID、 入口偏移与函数边界内指令摘要校验;导出审计复核清单与最终镜像一致 - 系统函数、调度与解包保护的代码前缀按 LC_FUNCTION_STARTS 限定在函数边界内 - 边界内识别 B / LDR+BR / ADRP+ADD+BR / MOVK+BR 蹦床并确认目标归属,避免系统尾调用误报 - 登记前 IMP 已落入匿名内存判为改写;清单读取改走 IG_dispatch_open - 函数边界长度按入口缓存;镜像未重新链接时复用基线;Debug 附加调试器时不按入口摘要判定改写 - 补充宿主测试;删除已过时的 README 与 docs 历史文档,保留 THIRD_PARTY_NOTICES
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/* 构建时由 objc_method_baseline.py 从签名前的镜像生成。 */
|
||||
#ifndef IG_METHOD_MANIFEST_H
|
||||
#define IG_METHOD_MANIFEST_H
|
||||
#include "../dispatch/IGDispatch.h"
|
||||
#include <CommonCrypto/CommonDigest.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct {
|
||||
unsigned char key[32], uuid[16];
|
||||
uint64_t offset;
|
||||
uint32_t length;
|
||||
unsigned char digest[CC_SHA256_DIGEST_LENGTH], reserved[4];
|
||||
} IGMethodRecord;
|
||||
_Static_assert(sizeof(IGMethodRecord) == 96, "IGMethods row layout");
|
||||
|
||||
static const IGMethodRecord *ig_records;
|
||||
static size_t ig_record_count;
|
||||
static pthread_once_t ig_manifest_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
static const uint8_t *ig_method_image_uuid(const struct mach_header *header) {
|
||||
if (!header || header->magic != MH_MAGIC_64) return NULL;
|
||||
const uint8_t *p = (const uint8_t *)header + sizeof(struct mach_header_64);
|
||||
const uint8_t *end = p + header->sizeofcmds;
|
||||
for (uint32_t i = 0; i < header->ncmds && (size_t)(end-p) >= sizeof(struct load_command); ++i) {
|
||||
const struct load_command *cmd = (const void *)p;
|
||||
if (cmd->cmdsize < sizeof(*cmd) || cmd->cmdsize > (size_t)(end-p)) return NULL;
|
||||
if (cmd->cmd == LC_UUID && cmd->cmdsize == sizeof(struct uuid_command))
|
||||
return ((const struct uuid_command *)cmd)->uuid;
|
||||
p += cmd->cmdsize;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ig_load_method_manifest(void) {
|
||||
char path[PATH_MAX];
|
||||
uint32_t size = sizeof(path);
|
||||
if (_NSGetExecutablePath(path, &size)) return;
|
||||
char *slash = strrchr(path, '/');
|
||||
if (!slash || sizeof("IGMethods.bin") > sizeof(path)-(size_t)(slash+1-path)) return;
|
||||
memcpy(slash+1, "IGMethods.bin", sizeof("IGMethods.bin"));
|
||||
int fd = IG_dispatch_open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
|
||||
if (fd < 0) return;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size >= 24 &&
|
||||
(st.st_size-24) % sizeof(IGMethodRecord) == 0) {
|
||||
size_t len = (size_t)st.st_size;
|
||||
void *mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (mem != MAP_FAILED) {
|
||||
const uint8_t *bytes = mem;
|
||||
const uint8_t *uuid = ig_method_image_uuid(_dyld_get_image_header(0));
|
||||
if (uuid && !memcmp(bytes, "IGMB0001", 8) && !memcmp(bytes+8, uuid, 16)) {
|
||||
ig_records = (const void *)(bytes+24);
|
||||
ig_record_count = (len-24) / sizeof(IGMethodRecord);
|
||||
} else {
|
||||
munmap(mem, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
IG_dispatch_close(fd);
|
||||
}
|
||||
|
||||
static int ig_compare_method_key(const void *key, const void *record) {
|
||||
return memcmp(key, ((const IGMethodRecord *)record)->key, 32);
|
||||
}
|
||||
|
||||
static const IGMethodRecord *ig_expected_method(Class cls, SEL sel, int class_method, Method method) {
|
||||
pthread_once(&ig_manifest_once, ig_load_method_manifest);
|
||||
if (!ig_records) return NULL;
|
||||
Class owner = class_method ? object_getClass((id)cls) : cls;
|
||||
Class parent;
|
||||
while ((parent = class_getSuperclass(owner)) &&
|
||||
class_getInstanceMethod(parent, sel) == method)
|
||||
owner = parent;
|
||||
char name[1024];
|
||||
int n = snprintf(name, sizeof(name), "%c%s %s", class_method ? '+' : '-',
|
||||
class_getName(owner), sel_getName(sel));
|
||||
if (n < 0 || (size_t)n >= sizeof(name)) return NULL;
|
||||
unsigned char key[CC_SHA256_DIGEST_LENGTH];
|
||||
CC_SHA256(name, (CC_LONG)n, key);
|
||||
return bsearch(key, ig_records, ig_record_count, sizeof(*ig_records), ig_compare_method_key);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user