Skip to content

Commit 9e61dc2

Browse files
committed
fixed write_mem calling mach_vm_protect_trap on ios
1 parent 2507772 commit 9e61dc2

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

.github/workflows/c-cpp.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ jobs:
1111
runs-on: macos-latest
1212

1313
steps:
14-
- uses: actions/checkout@v4
14+
- uses: actions/checkout@v6
1515

1616
- name: build macos libraries
1717
run: ./build.sh -t macosx -v 10.15
1818

1919
- name: upload build artifact
20-
uses: actions/upload-artifact@v4
20+
uses: actions/upload-artifact@v6
2121
with:
2222
name: tinyhook-macosx
2323
path: |
@@ -30,7 +30,7 @@ jobs:
3030
run: ./build.sh -t iphoneos -v 12.0
3131

3232
- name: upload build artifact
33-
uses: actions/upload-artifact@v4
33+
uses: actions/upload-artifact@v6
3434
with:
3535
name: tinyhook-iphoneos
3636
path: |
@@ -51,7 +51,7 @@ jobs:
5151
runs-on: ${{ matrix.os }}
5252

5353
steps:
54-
- uses: actions/checkout@v4
54+
- uses: actions/checkout@v6
5555

5656
- name: run test
5757
run: make test

src/memory.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
#include <mach/mach_error.h> // mach_error_string()
77
#endif
88

9+
#if TARGET_OS_OSX
10+
#define mach_vm_protect mach_vm_protect_trap
11+
912
__attribute__((naked)) static kern_return_t mach_vm_protect_trap(mach_port_name_t task, mach_vm_address_t address,
1013
mach_vm_size_t size, boolean_t set_maximum,
1114
vm_prot_t new_protection) {
12-
#ifdef __aarch64__
15+
#ifdef __aarch64__
1316
asm volatile("mov x16, #-0xe\n"
1417
"svc #0x80\n"
1518
"ret");
16-
#elif __x86_64__
19+
#elif __x86_64__
1720
asm volatile(".intel_syntax\n"
1821
"mov r10, rcx\n"
1922
"mov eax, 0x100000e\n"
2023
"syscall\n"
2124
"ret");
22-
#endif
25+
#endif
2326
}
27+
#endif
2428

2529
static inline void copy_mem(void *destination, const void *source, size_t len) {
2630
unsigned char *dst = (unsigned char *)destination;
@@ -65,13 +69,13 @@ int write_mem(void *destination, const void *source, size_t len) {
6569
ARG_CHECK(len != 0);
6670
mach_port_t task = mach_task_self();
6771
mach_vm_address_t dst = (mach_vm_address_t)destination;
68-
int kr = mach_vm_protect_trap(task, dst, len, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
72+
int kr = mach_vm_protect(task, dst, len, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
6973
if (kr != 0) {
7074
LOG_ERROR("mach_vm_protect failed for address %p: %s", destination, mach_error_string(kr));
7175
return kr;
7276
}
7377
copy_mem(destination, source, len);
74-
mach_vm_protect_trap(task, dst, len, FALSE, VM_PROT_READ | VM_PROT_EXECUTE);
78+
mach_vm_protect(task, dst, len, FALSE, VM_PROT_READ | VM_PROT_EXECUTE);
7579
// might fail when editing __DATA, but not a big deal
7680
return 0;
7781
}

src/tinyhook.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static inline int a64_movz_movk(uint16_t rd, uint64_t imm64, uint32_t **outputp)
2121
bool cleaned = false;
2222
uint32_t *output = *outputp;
2323
for (int i = 0; imm64; imm64 >>= 16, i++) {
24-
uint64_t imm16 = imm64 & 0xffff;
24+
uint16_t imm16 = imm64 & 0xffff;
2525
if (imm16) {
2626
uint32_t insn = (i << 21) | (imm16 << 5) | rd;
2727
if (cleaned)
@@ -53,8 +53,8 @@ static int calc_jump(void *output, void *src, void *dst, bool link) {
5353
// adrp x17, imm
5454
// add x17, x17, imm ; x17 -> dst
5555
jump_size = 8 + 4;
56-
*outcode++ = AARCH64_ADRP | ((gap & 0x3) << 29) | ((gap & 0x1ffffc) << 3);
57-
*outcode++ = AARCH64_ADD | ((int64_t)dst & 0xfff) << 10;
56+
*outcode++ = AARCH64_ADRP | (uint32_t)((gap & 0x3) << 29) | (uint32_t)((gap & 0x1ffffc) << 3);
57+
*outcode++ = AARCH64_ADD | (uint32_t)((int64_t)dst & 0xfff) << 10;
5858
}
5959
else {
6060
// movz x17, lowbit
@@ -64,11 +64,11 @@ static int calc_jump(void *output, void *src, void *dst, bool link) {
6464
*outcode = (link ? AARCH64_BLR : AARCH64_BR);
6565
}
6666
#elif __x86_64__
67-
if (gap <= INT32_MAX && gap >= INT32_MIN) { // 32 bit imm
67+
if (gap - 5 <= INT32_MAX && gap - 5 >= INT32_MIN) { // 32 bit imm
6868
// jmp/call imm ; go to dst
6969
jump_size = 5;
7070
*(uint8_t *)output = (link ? X86_64_CALL : X86_64_JMP);
71-
*(int32_t *)(output + 1) = gap - 5;
71+
*(int32_t *)(output + 1) = (int32_t)gap - 5;
7272
}
7373
else {
7474
// jmp [rip]
@@ -100,7 +100,7 @@ static inline void save_header(void **src_p, void **dst_p, int min_len) {
100100
if (gap <= 0x100000 - 1 && gap >= -0x100000) { // 21 bit imm
101101
// modify the immediate (len: 4 -> 4)
102102
insn &= 0x9f00001f; // clean the immediate
103-
*dst++ = insn | ((gap & 0x3) << 29) | ((gap & 0x1ffffc) << 3);
103+
*dst++ = insn | (uint32_t)((gap & 0x3) << 29) | (uint32_t)((gap & 0x1ffffc) << 3);
104104
}
105105
else {
106106
// use movz + movk to get the address (len: 4 -> 16)

0 commit comments

Comments
 (0)