Skip to content

Commit a903fa1

Browse files
committed
Avoid null ptr deref when writing to arm context pc register
1 parent 48fb28d commit a903fa1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

qemu/target/arm/unicorn_arm.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ uc_err reg_write(void *_env, int mode, unsigned int regid, const void *value,
433433
CHECK_REG_TYPE(uint32_t);
434434
env->pc = (*(uint32_t *)value & ~1);
435435
env->thumb = (*(uint32_t *)value & 1);
436-
env->uc->thumb = (*(uint32_t *)value & 1);
436+
if (env->uc) {
437+
// This can be NULL if env is a context
438+
env->uc->thumb = (*(uint32_t *)value & 1);
439+
}
437440
env->regs[15] = (*(uint32_t *)value & ~1);
438441
*setpc = 1;
439442
break;
@@ -754,7 +757,8 @@ static uc_err uc_arm_context_restore(struct uc_struct *uc, uc_context *context)
754757
ARM_ENV_RESTORE(env->sau.rlar)
755758

756759
#undef ARM_ENV_RESTORE
757-
760+
// Overwrite uc to our uc
761+
env->uc = uc;
758762
return UC_ERR_OK;
759763
}
760764

tests/unit/test_arm.c

+3
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,15 @@ static void test_arm_context_save(void)
757757
uc_engine *uc2;
758758
char code[] = "\x83\xb0"; // sub sp, #0xc
759759
uc_context *ctx;
760+
uint32_t pc;
760761

761762
uc_common_setup(&uc, UC_ARCH_ARM, UC_MODE_THUMB, code, sizeof(code) - 1,
762763
UC_CPU_ARM_CORTEX_R5);
763764

764765
OK(uc_context_alloc(uc, &ctx));
765766
OK(uc_context_save(uc, ctx));
767+
OK(uc_context_reg_read(ctx, UC_ARM_REG_PC, (void*)&pc));
768+
OK(uc_context_reg_write(ctx, UC_ARM_REG_PC, (void*)&pc));
766769
OK(uc_context_restore(uc, ctx));
767770

768771
uc_common_setup(&uc2, UC_ARCH_ARM, UC_MODE_THUMB, code, sizeof(code) - 1,

0 commit comments

Comments
 (0)