| 6 |
| #include <sys/mman.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <string.h> |
| 10 | #include "inline.h" |
| 11 | #include "common.h" |
| 12 |
| 13 | #ifdef DEBUG_HOOK_SELF |
| 14 |
| 15 | #include <syscall.h> |
| 16 | #include <unistd.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include "whale/whale.h" |
| 19 |
| 20 | static long faccessat_replace(int dirfd, const char *pathname, int mode, int flags) { |
| 21 | return syscall(__NR_faccessat, dirfd, pathname, mode, flags); |
| 22 | } |
| 23 |
| 24 | static long fchownat_replace(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags) { |
| 25 | return syscall(__NR_fchownat, dirfd, pathname, owner, group, flags); |
| 26 | } |
| 27 |
| 28 | static int futimens_replace(int fd, const struct timespec times[2]) { |
| 29 | return utimensat(fd, NULL, times, 0); |
| 30 | } |
| 31 |
| 32 | #if defined(__arm__) || defined(__aarch64__) |
| 33 |
| 34 | void check_inline_hook_hookzz() { |
| 35 | void *backup; |
| 36 | zz_disable_arm_arm64_b_branch(); |
| 37 |
| 38 | ZzReplace(&fchownat, &fchownat_replace, &backup); |
| 39 | LOGI("hookzz fchownat: %p, replace: %p, hooked: %d", &fchownat, &fchownat_replace, |
| 40 | isInlineHooked(&fchownat)); |
| 41 |
| 42 | ZzReplace(&faccessat, &faccessat_replace, &backup); |
| 43 | LOGI("hookzz faccessat: %p, repalce: %p, hooked: %d", &faccessat, &faccessat_replace, |
| 44 | isInlineHooked(&faccessat)); |
| 45 |
| 46 | ZzReplace(&futimens, &futimens_replace, &backup); |
| 47 | LOGI("hookzz futimens: %p, replace: %p, hooked: %d", &futimens, &futimens_replace, |
| 48 | isInlineHooked(&futimens)); |
| 49 | } |
| 50 |
| 51 | void check_inline_hook_hookzz_b() { |
| 52 | void *backup; |
| 53 | zz_enable_arm_arm64_b_branch(); |
| 54 |
| 55 | ZzReplace(&fchownat, &fchownat_replace, &backup); |
| 56 | LOGI("hookzz-b fchownat: %p, replace: %p, hooked: %d", &fchownat, &fchownat_replace, |
| 57 | isInlineHooked(&fchownat)); |
| 58 |
| 59 | ZzReplace(&faccessat, &faccessat_replace, &backup); |
| 60 | LOGI("hookzz-b faccessat: %p, repalce: %p, hooked: %d", &faccessat, &faccessat_replace, |
| 61 | isInlineHooked(&faccessat)); |
| 62 |
| 63 | ZzReplace(&futimens, &futimens_replace, &backup); |
| 64 | LOGI("hookzz-b futimens: %p, replace: %p, hooked: %d", &futimens, &futimens_replace, |
| 65 | isInlineHooked(&futimens)); |
| 66 | } |
| 67 |
| 68 | #endif |
| 69 |
| 70 | void check_inline_hook_whale() { |
| 71 | WInlineHookFunction(&fchownat, &fchownat_replace, NULL); |
| 72 | LOGI("whale fchownat: %p, replace: %p, hooked: %d", &fchownat, &fchownat_replace, |
| 73 | isInlineHooked(&fchownat)); |
| 74 |
| 75 | WInlineHookFunction(&faccessat, &faccessat_replace, NULL); |
| 76 | LOGI("whale faccessat: %p, repalce: %p, hooked: %d", &faccessat, &faccessat_replace, |
| 77 | isInlineHooked(&faccessat)); |
| 78 |
| 79 | WInlineHookFunction(&futimens, &futimens_replace, NULL); |
| 80 | LOGI("whale futimens: %p, replace: %p, hooked: %d", &futimens, &futimens_replace, |
| 81 | isInlineHooked(&futimens)); |
| 82 | } |
| 83 |
| 84 | #if defined(__arm__) || defined(DEBUG_HOOK_IDE) |
| 85 |
| 86 | #include "substrate/CydiaSubstrate.h" |
| 87 |
| 88 | void check_inline_hook_substrate() { |
| 89 | MSHookFunction(&fchownat, &fchownat_replace, NULL); |
| 90 | LOGI("substrate fchownat: %p, replace: %p, hooked: %d", &fchownat, &fchownat_replace, |
| 91 | isInlineHooked(&fchownat)); |
| 92 |
| 93 | MSHookFunction(&faccessat, &faccessat_replace, NULL); |
| 94 | LOGI("substrate faccessat: %p, repalce: %p, hooked: %d", &faccessat, &faccessat_replace, |
| 95 | isInlineHooked(&faccessat)); |
| 96 |
| 97 | MSHookFunction(&futimens, &futimens_replace, NULL); |
| 98 | LOGI("substrate futimens: %p, replace: %p, hooked: %d", &futimens, &futimens_replace, |
| 99 | isInlineHooked(&futimens)); |
| 100 | } |
| 101 |
| 102 | #endif |
| 103 |
| 104 | #endif |
| 105 |
| 106 | bool isInlineHooked(void *symbol) { |
| 107 | if (symbol == NULL) { |
| 108 | return false; |
| 109 | } |
| 110 | #if defined(__arm__) || defined(DEBUG_HOOK_IDE) |
| 111 |
| 112 | // https://developer.arm.com/docs/ddi0597/b/base-instructions-alphabetic-order/ldr-literal-load-register-literal |
| 113 | // A1, !(P == 0 && W == 1), we don't check P and W |
| 114 | // cond 010P U0W1 1111 _Rt_ xxxx xxxx xxxx |
| 115 | #define IS_LDR_PC_A1(x) (((x) & 0xfe5ff000u) == 0xe41ff000u) |
| 116 | // T2 |
| 117 | // 1111 1000 U101 1111 | _Rt_ xxxx xxxx xxxx |
| 118 | #define IS_LDR_PC_T2(x) (((x) & 0xf000ff7fu) == 0xf000f85fu) |
| 119 |
| 120 | // https://developer.arm.com/docs/ddi0597/b/base-instructions-alphabetic-order/b-branch |
| 121 | // A1 |
| 122 | // cond 100 xxxx xxxx xxxx xxxx xxxx xxxx |
| 123 | #define IS_B_A1(x) (((x) & 0xff000000u) == 0xea000000u) |
| 124 | // T2 |
| 125 | // 1110 0xxx xxxx xxxx |
| 126 | #define IS_B_T2(x) (((x) & 0xf800u) == 0xe000u) |
| 127 | // T4 |
| 128 | // 1111 0Sxx xxxx xxxx | 10J1 Jxxx xxxx xxxx |
| 129 | // -- imm10 --- | --- imm11 --- |
| 130 | #define IS_B_T4(x) (((x) & 0xd000f800u) == 0x9000f000u) |
| 131 |
| 132 | // https://developer.arm.com/docs/ddi0597/b/base-instructions-alphabetic-order/nop-no-operation |
| 133 | // T1, hint should be 0000, we don't check |
| 134 | // 1011 1111 hint 0000 |
| 135 | #define IS_NOP_T1(x) (((x) & 0xff0fu) == 0xbf00u) |
| 136 |
| 137 | // https://developer.arm.com/docs/ddi0597/b/base-instructions-alphabetic-order/mov-movs-register-move-register |
| 138 | // cydia use `mov r8, r8` for Nop |
| 139 | // T1, Mmmm is Rm, Dddd is Rd |
| 140 | // 0100 0110 DMmm mddd |
| 141 | #define _IS_MOV_T1(x) (((x) & 0xff00u) == 0x4600u) |
| 142 | #define _RM_MOV_T1(x) ((((x) & 0x78u) >> 3u)) |
| 143 | #define _RD_MOV_T1(x) ((((x) & 0x80u) >> 4u) | ((x) & 7u)) |
| 144 | #define IS_MOV_T1_RR(x) (_IS_MOV_T1(x) && _RM_MOV_T1(x) == _RD_MOV_T1(x)) |
| 145 |
| 146 | // https://developer.arm.com/docs/ddi0597/b/base-instructions-alphabetic-order/bx-branch-and-exchange |
| 147 | // cydia use `bx` |
| 148 | // T1 |
| 149 | // 0100 0111 0Rmm m000 |
| 150 | #define IS_BX_T1(x) (((x) & 0xff87u) == 0x4700u) |
| 151 | #define RM_BX_T1(x) (((x) & 0x0078u) >> 3u) |
| 152 | #define IS_BX_PC_T1(x) ((x) == 0x4778u) |
| 153 |
| 154 | uintptr_t address = (uintptr_t) symbol; |
| 155 | if ((address & 1U) == 0) { |
| 156 | uint32_t *value32 = (uint32_t *) address; |
| 157 | if (IS_LDR_PC_A1(*value32)) { |
| 158 | #ifdef DEBUG_HOOK |
| 159 | LOGW("(arm ldr pc) symbol: %p, value: %08x", symbol, *value32); |
| 160 | #endif |
| 161 | return true; |
| 162 | } |
| 163 | if (IS_B_A1(*value32)) { |
| 164 | #ifdef DEBUG_HOOK |
| 165 | LOGW("(arm b) symbol: %p, value: %08x", symbol, *value32); |
| 166 | #endif |
| 167 | return true; |
| 168 | } |
| 169 | #ifdef DEBUG |
| 170 | LOGI("(arm) symbol: %p, value: %08x", symbol, *value32); |
| 171 | #endif |
| 172 | } else { |
| 173 | address = address & ~1U; |
| 174 | uint16_t *value16 = (uint16_t *) address; |
| 175 | uint32_t *value32 = (uint32_t *) address; |
| 176 | if (IS_LDR_PC_T2(*value32)) { |
| 177 | #ifdef DEBUG_HOOK |
| 178 | LOGW("(thumb ldr pc) symbol: %p, address: %p, value: %08x", |
| 179 | symbol, address, *value32); |
| 180 | #endif |
| 181 | return true; |
| 182 | } |
| 183 | if (IS_B_T4(*value32)) { |
| 184 | #ifdef DEBUG_HOOK |
| 185 | LOGW("(thumb b) symbol: %p, address: %p, value: %08x", |
| 186 | symbol, address, *value32); |
| 187 | #endif |
| 188 | return true; |
| 189 | } |
| 190 | if (IS_B_T2(*value16)) { |
| 191 | #ifdef DEBUG_HOOK |
| 192 | LOGW("(thumb b) symbol: %p, address: %p, value: %04x", |
| 193 | symbol, address, *value16); |
| 194 | #endif |
| 195 | return true; |
| 196 | } |
| 197 | if (IS_NOP_T1(*value16) || IS_MOV_T1_RR(*value16)) { |
| 198 | #ifdef DEBUG_HOOK |
| 199 | LOGW("(thumb nop) symbol: %p, address: %p, value: %04x", |
| 200 | symbol, address, *value16); |
| 201 | #endif |
| 202 | address += 2; |
| 203 | value16 = (uint16_t *) address; |
| 204 | value32 = (uint32_t *) address; |
| 205 | } |
| 206 | if (IS_LDR_PC_T2(*value32)) { |
| 207 | #ifdef DEBUG_HOOK |
| 208 | LOGW("(thumb ldr pc) symbol: %p, address: %p, value: %08x", |
| 209 | symbol, address, *value32); |
| 210 | #endif |
| 211 | return true; |
| 212 | } |
| 213 | if (IS_BX_PC_T1(*value16) && IS_LDR_PC_A1(*(value32 + 1))) { |
| 214 | #ifdef DEBUG_HOOK |
| 215 | LOGW("(thumb bx + arm ldr pc) symbol: %p, address: %p, value: %08x %08x", |
| 216 | symbol, address, *value32, *(value32 + 1)); |
| 217 | #endif |
| 218 | return true; |
| 219 | } |
| 220 | #ifdef DEBUG |
| 221 | LOGI("(thumb) symbol: %p, address: %p, value: %08x %08x", |
| 222 | symbol, address, *value32, *(value32 + 1)); |
| 223 | #endif |
| 224 | } |
| 225 | #endif |
| 226 | #if defined(__aarch64__) || defined(DEBUG_HOOK_IDE) |
| 227 |
| 228 | // https://developer.arm.com/docs/ddi0596/latest/base-instructions-alphabetic-order/b-branch |
| 229 | // 0001 01xx xxxx xxxx xxxx xxxx xxxx xxxx |
| 230 | // ------------ imm26 ------------- |
| 231 | #define IS_B(x) (((x) & 0xfc000000u) == 0x14000000u) |
| 232 |
| 233 | // https://developer.arm.com/docs/ddi0596/latest/base-instructions-alphabetic-order/ldr-literal-load-register-literal |
| 234 | // 0101 1000 xxxx xxxx xxxx xxxx xxxR tttt |
| 235 | // -------- imm19 -------- |
| 236 | #define IS_LDR_X(x) (((x) & 0xff000000u) == 0x58000000u) |
| 237 | #define X_LDR(x) ((x) & 0x1fu) |
| 238 |
| 239 | // https://developer.arm.com/docs/ddi0596/latest/base-instructions-alphabetic-order/adrp-form-pc-relative-address-to-4kb-page |
| 240 | // 1xx1 0000 xxxx xxxx xxxx xxxx xxxR dddd |
| 241 | // lo -------- immhi -------- |
| 242 | #define IS_ADRP_X(x) (((x) & 0x9f000000u) == 0x90000000u) |
| 243 | #define X_ADRP(x) ((x) & 0x1fu) |
| 244 |
| 245 | // https://developer.arm.com/docs/ddi0596/latest/base-instructions-alphabetic-order/br-branch-to-register |
| 246 | // 1101 0110 0001 1111 0000 00Rn nnn0 0000 |
| 247 | #define IS_BR_X(x) (((x) & 0xfffffc0f) == 0xd61f0000u) |
| 248 | #define X_BR(x) (((x) & 0x3e0u) >> 0x5u) |
| 249 |
| 250 | // https://developer.arm.com/docs/ddi0596/latest/base-instructions-alphabetic-order/movz-move-wide-with-zero |
| 251 | // 1op1 0010 1hwx xxxx xxxx xxxx xxxR dddd |
| 252 | // ------ imm16 ------- |
| 253 | // for op, 00 -> MOVN, 10 -> MOVZ, 11 -> MOVK |
| 254 | #define IS_MOV_X(x) (((x) & 0x9f800000u) == 0x92800000u) |
| 255 | #define X_MOV(x) ((x) & 0x1fu) |
| 256 |
| 257 | uint32_t *value32 = symbol; |
| 258 | if (IS_B(*value32)) { |
| 259 | #ifdef DEBUG_HOOK |
| 260 | LOGW("(arm64 b) symbol: %p, value: %08x", symbol, *value32); |
| 261 | #endif |
| 262 | return true; |
| 263 | } |
| 264 | if (IS_LDR_X(*value32) && IS_BR_X(*(value32 + 1))) { |
| 265 | uint32_t x = X_LDR(*value32); |
| 266 | if (x == X_BR(*(value32 + 1))) { |
| 267 | #ifdef DEBUG_HOOK |
| 268 | LOGW("(arm64 ldr+br x%d) symbol: %p, value: %08x %08x", |
| 269 | x, symbol, *value32, *(value32 + 1)); |
| 270 | #endif |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | if (IS_ADRP_X(*value32) && IS_BR_X(*(value32 + 1))) { |
| 275 | uint32_t x = X_ADRP(*value32); |
| 276 | if (x == X_BR(*(value32 + 1))) { |
| 277 | #ifdef DEBUG_HOOK |
| 278 | LOGW("(arm64 adrp+br x%d) symbol: %p, value: %08x %08x", |
| 279 | x, symbol, *value32, *(value32 + 1)); |
| 280 | #endif |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | if (IS_MOV_X(*value32)) { |
| 285 | uint32_t x = X_MOV(*value32); |
| 286 | for (int i = 1; i <= 4; ++i) { |
| 287 | if (IS_BR_X(*(value32 + i))) { |
| 288 | if (x != X_BR(*(value32 + i))) { |
| 289 | break; |
| 290 | } |
| 291 | #ifdef DEBUG_HOOK |
| 292 | for (int k = 0; k < i; ++k) { |
| 293 | LOGW("(arm64 mov x%d) symbol: %p, value: %08x", |
| 294 | x, symbol + sizeof(uint32_t) * k, *(value32 + k)); |
| 295 | } |
| 296 | LOGW("(arm64 br x%d) symbol: %p, value: %08x", |
| 297 | x, symbol + sizeof(uint32_t) * i, *(value32 + i)); |
| 298 | #endif |
| 299 | return true; |
| 300 | } else if (IS_MOV_X(*(value32 + i))) { |
| 301 | if (x != X_MOV(*(value32 + i))) { |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | #ifdef DEBUG |
| 308 | LOGI("(arm64) symbol: %p, value: %08x %08x", symbol, *value32, *(value32 + 1)); |
| 309 | #endif |
| 310 | #endif |
| 311 | return false; |
| 312 | } |
| 313 |
| 314 | #ifdef DEBUG_HOOK_IO |
| 315 | bool setRead(void *symbol) { |
| 316 | uintptr_t address = (uintptr_t) symbol; |
| 317 | uintptr_t page_size = (uintptr_t) getpagesize(); |
| 318 | uintptr_t base = address & ~(page_size - 1); |
| 319 | // inline check read at most 20 bytes |
| 320 | uintptr_t end = (address + 20 + page_size - 1) & -page_size; |
| 321 | #ifdef DEBUG |
| 322 | LOGI("set r+x from %p to %p", base, end); |
| 323 | #endif |
| 324 | if (mprotect((void *) base, end - base, PROT_READ | PROT_EXEC)) { |
| 325 | #ifdef DEBUG |
| 326 | LOGW("cannot mprotect: %s", strerror(errno)); |
| 327 | #endif |
| 328 | return false; |
| 329 | } else { |
| 330 | return true; |
| 331 | } |
| 332 | } |
| 333 | #endif |
| 334 |