Skip to content

Commit 088c066

Browse files
committed
Remove hack to copy function body
1 parent 4dc5f1e commit 088c066

File tree

1 file changed

+5
-36
lines changed

1 file changed

+5
-36
lines changed

tests/unit/test_ctl.c

+5-36
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ static void test_uc_ctl_arm_cpu(void)
262262
static void test_uc_hook_cached_cb(uc_engine *uc, uint64_t addr, size_t size,
263263
void *user_data)
264264
{
265-
// Don't add any TEST_CHECK here since we can't refer to the global variable
266-
// here.
267265
uint64_t *p = (uint64_t *)user_data;
268266
(*p)++;
269267
return;
@@ -276,26 +274,10 @@ static void test_uc_hook_cached_uaf(void)
276274
char code[] = "\x41\x4a\xeb\x00\x90";
277275
uc_hook h;
278276
uint64_t count = 0;
279-
#ifndef _WIN32
280-
// Apple Silicon does not allow RWX pages.
281-
void *callback = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
282-
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
283-
TEST_CHECK(callback != (void *)-1);
284-
#else
285-
void *callback = VirtualAlloc(NULL, 4096, MEM_RESERVE | MEM_COMMIT,
286-
PAGE_EXECUTE_READWRITE);
287-
TEST_CHECK(callback != NULL);
288-
#endif
289-
290-
memcpy(callback, (void *)test_uc_hook_cached_cb, 4096);
291-
292-
#ifndef _WIN32
293-
TEST_CHECK(mprotect(callback, 4096, PROT_READ | PROT_EXEC) == 0);
294-
#endif
295277

296278
uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_32, code, sizeof(code) - 1);
297279

298-
OK(uc_hook_add(uc, &h, UC_HOOK_CODE, (void *)callback, (void *)&count, 1,
280+
OK(uc_hook_add(uc, &h, UC_HOOK_CODE, (void *)test_uc_hook_cached_cb, (void *)&count, 1,
299281
0));
300282

301283
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
@@ -306,28 +288,15 @@ static void test_uc_hook_cached_uaf(void)
306288
// This will clear deleted hooks and SHOULD clear cache.
307289
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
308290

309-
#ifndef _WIN32
310-
TEST_CHECK(mprotect(callback, 4096, PROT_READ | PROT_WRITE) == 0);
311-
#endif
312-
313-
memset(callback, 0, 4096);
314-
315-
#ifndef _WIN32
316-
TEST_CHECK(mprotect(callback, 4096, PROT_READ | PROT_EXEC) == 0);
317-
#endif
318-
319-
// Now hooks are deleted and thus this will trigger a UAF
291+
// Now hooks are deleted and thus this _should not_ call test_uc_hook_cached_cb anymore.
292+
// If the hook is allocated like from malloc, and the code region is free-ed, this call _shall not_
293+
// call the hook anymore to avoid UAF.
320294
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
321295

296+
// Only 4 calls
322297
TEST_CHECK(count == 4);
323298

324299
OK(uc_close(uc));
325-
326-
#ifndef _WIN32
327-
munmap(callback, 4096);
328-
#else
329-
VirtualFree(callback, 0, MEM_RELEASE);
330-
#endif
331300
}
332301

333302
static void test_uc_emu_stop_set_ip_callback(uc_engine *uc, uint64_t address,

0 commit comments

Comments
 (0)