Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a null pointer reference bug #2090

Open
wnxd opened this issue Jan 20, 2025 · 6 comments
Open

There is a null pointer reference bug #2090

wnxd opened this issue Jan 20, 2025 · 6 comments
Labels
Milestone

Comments

@wnxd
Copy link

wnxd commented Jan 20, 2025

Null pointer dereference when uc_context_reg_write writes to UC_ARM_REG_PC register.

qemu/target/arm/unicorn_arm.c -> reg_write

    CHECK_REG_TYPE(uint32_t);
    env->pc = (*(uint32_t *)value & ~1);
    env->thumb = (*(uint32_t *)value & 1);
 -> env->uc->thumb = (*(uint32_t *)value & 1);
    env->regs[15] = (*(uint32_t *)value & ~1);
    *setpc = 1;
    break;

I don't quite understand why arm has a custom uc_arm_context_save
It seems that there is no copy of the uc structure pointer to env->uc in the code

@wtdcode
Copy link
Member

wtdcode commented Jan 20, 2025

How did you construct the context object? Or could you post the full reproduction?

@wnxd
Copy link
Author

wnxd commented Jan 20, 2025

uc_engine *uc;
uc_open(UC_ARCH_ARM, UC_MODE_ARM, &uc);
uc_context *ctx;
uc_context_alloc(uc, &ctx);
uc_context_save(uc, ctx);
uint32_t pc = 0x1000;
uc_context_reg_write(ctx, UC_ARM_REG_PC, &pc);

Similar to the code above

@wnxd
Copy link
Author

wnxd commented Jan 20, 2025

Of course my original code is written in go.
Since arm64 is normal, only arm reports an error.
So I traced the call stack.
Only PC register writing will result in an error.

@wtdcode
Copy link
Member

wtdcode commented Jan 20, 2025

This usage was well tested here:

static void test_arm_context_save(void)

@wtdcode
Copy link
Member

wtdcode commented Jan 20, 2025

Oh I see, you are writing to PC register.

Yeah, this is a bug. We left out uc pointer here.

@wnxd
Copy link
Author

wnxd commented Jan 20, 2025

It seems that arm's context is not saved using memcpy. It only copies the data related to the register, and does not copy env->uc.

@wtdcode wtdcode added the bug label Jan 20, 2025
@wtdcode wtdcode added this to the Unicorn 2.1.2 milestone Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants