@@ -262,8 +262,6 @@ static void test_uc_ctl_arm_cpu(void)
262
262
static void test_uc_hook_cached_cb (uc_engine * uc , uint64_t addr , size_t size ,
263
263
void * user_data )
264
264
{
265
- // Don't add any TEST_CHECK here since we can't refer to the global variable
266
- // here.
267
265
uint64_t * p = (uint64_t * )user_data ;
268
266
(* p )++ ;
269
267
return ;
@@ -276,26 +274,10 @@ static void test_uc_hook_cached_uaf(void)
276
274
char code [] = "\x41\x4a\xeb\x00\x90" ;
277
275
uc_hook h ;
278
276
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
295
277
296
278
uc_common_setup (& uc , UC_ARCH_X86 , UC_MODE_32 , code , sizeof (code ) - 1 );
297
279
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 ,
299
281
0 ));
300
282
301
283
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)
306
288
// This will clear deleted hooks and SHOULD clear cache.
307
289
OK (uc_emu_start (uc , code_start , code_start + sizeof (code ) - 1 , 0 , 0 ));
308
290
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.
320
294
OK (uc_emu_start (uc , code_start , code_start + sizeof (code ) - 1 , 0 , 0 ));
321
295
296
+ // Only 4 calls
322
297
TEST_CHECK (count == 4 );
323
298
324
299
OK (uc_close (uc ));
325
-
326
- #ifndef _WIN32
327
- munmap (callback , 4096 );
328
- #else
329
- VirtualFree (callback , 0 , MEM_RELEASE );
330
- #endif
331
300
}
332
301
333
302
static void test_uc_emu_stop_set_ip_callback (uc_engine * uc , uint64_t address ,
0 commit comments