/* * IGInline.h — 强制内联的自实现 str/mem 原语(落地方案第一优先级)。 * * 目的:核心校验/鉴权/加解密逻辑里避免直接调用 strcmp/memcmp/memcpy/strstr/getenv 等 * 被攻击者重点监控的 libc 函数——这些调用会在 __la_symbol_ptr/__got 留下符号引用, * 既是 Fishhook rebind 的靶点,也是静态分析的锚点。 * * 本头提供 __attribute__((always_inline)) 版本:使用处直接展开为离散汇编指令, * 不生成 bl 跳转、不产生导入表项,从而从导入表中抹除这些符号。 * * 用法:在安全逻辑中用 ig_memcmp/ig_strcmp/... 替换 libc 同名函数。纯头文件,零依赖。 * 注意:这是“隐藏特征”,非“更安全的实现”——不做常量时间比较;需防时序侧信道时另行处理。 */ #ifndef IG_INLINE_H #define IG_INLINE_H #include #include #define IG_AINLINE static inline __attribute__((always_inline)) IG_AINLINE size_t ig_strlen(const char *s) { const char *p = s; while (*p) ++p; return (size_t)(p - s); } IG_AINLINE int ig_memcmp(const void *a, const void *b, size_t n) { const unsigned char *x = (const unsigned char *)a; const unsigned char *y = (const unsigned char *)b; for (size_t i = 0; i < n; ++i) { if (x[i] != y[i]) return (int)x[i] - (int)y[i]; } return 0; } IG_AINLINE void *ig_memcpy(void *dst, const void *src, size_t n) { unsigned char *d = (unsigned char *)dst; const unsigned char *s = (const unsigned char *)src; for (size_t i = 0; i < n; ++i) d[i] = s[i]; return dst; } IG_AINLINE void *ig_memset(void *dst, int c, size_t n) { unsigned char *d = (unsigned char *)dst; for (size_t i = 0; i < n; ++i) d[i] = (unsigned char)c; return dst; } IG_AINLINE int ig_strcmp(const char *a, const char *b) { while (*a && (*a == *b)) { ++a; ++b; } return (int)(unsigned char)*a - (int)(unsigned char)*b; } IG_AINLINE int ig_strncmp(const char *a, const char *b, size_t n) { for (size_t i = 0; i < n; ++i) { unsigned char ca = (unsigned char)a[i], cb = (unsigned char)b[i]; if (ca != cb) return (int)ca - (int)cb; if (ca == 0) break; } return 0; } /* 朴素子串匹配,避免引用 libc strstr。 */ IG_AINLINE const char *ig_strstr(const char *hay, const char *needle) { if (!needle[0]) return hay; for (; *hay; ++hay) { const char *h = hay, *n = needle; while (*h && *n && (*h == *n)) { ++h; ++n; } if (!*n) return hay; } return (const char *)0; } #endif /* IG_INLINE_H */