Skip to content

afl-bridge: fix race between main thread and a vCPU thread #102

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

Merged
merged 2 commits into from
Mar 14, 2025

Conversation

lorc
Copy link
Contributor

@lorc lorc commented Feb 11, 2025

In some cases qemu_main_loop() can exit before libafl_sync_exit_cpu() completes. This will case race between Rust code that restarts QEMU and vCPU thread that updates last_exit_reason. What I observed is

libafl_exit_signal_vm_start() from a new iteration cleared last_exit_reason.cpu before libafl_sync_exit_cpu() tried to access *last_exit_reason.cpu. This caused NULL pointer dereference.

Fix this by not setting cpu->exit in prepare_qemu_exit() and updating it only in rr_cpu_thread_fn(). This will ensure that qemu_main_loop() waits for vCPU thread to actually stop before returning control to Rust code.

@lorc
Copy link
Contributor Author

lorc commented Feb 13, 2025

Hi, @rmalmain,

Could you please take a look?

reformat code with more typedefs.
@lorc
Copy link
Contributor Author

lorc commented Mar 7, 2025

@rmalmain, @domenukk, @vanhauser-thc , @hexcoder-, @andreafioraldi, @tokatoka,

May I ask you to take a look at this PR? We really want to use LibAFL for Xen fuzzing on our CI server, but there is an issue that crashes the whole fuzzer. This PR fixes it.

If you need more information, please let me know.

@tokatoka
Copy link
Member

tokatoka commented Mar 7, 2025

I'll tell romain to check it.
but he'll be back only after next Wednesday

@lorc
Copy link
Contributor Author

lorc commented Mar 7, 2025

Thanks! I waited almost a month, so I'll wait couple days more :)

@domenukk domenukk requested a review from rmalmain March 9, 2025 19:05
Copy link
Member

@rmalmain rmalmain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sorry for the delay, didn't have much time lately.
the fix looks good to me, thanks for the pr!

@@ -273,6 +279,12 @@ static void *rr_cpu_thread_fn(void *arg)
bql_lock();
break;
}
//// --- Begin LibAFL code ---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also propagate this change to accel/tcg/tcg-accel-ops-mttcg.c please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do this today.

@rmalmain
Copy link
Member

rmalmain commented Mar 13, 2025 via email

@rmalmain rmalmain self-requested a review March 13, 2025 13:02
In some cases qemu_main_loop() can exit before libafl_sync_exit_cpu()
completes. This will case race between Rust code that restarts QEMU
and vCPU thread that updates last_exit_reason. What I observed is

libafl_exit_signal_vm_start() from a new iteration cleared
last_exit_reason.cpu before libafl_sync_exit_cpu() tried to
access *last_exit_reason.cpu. This caused NULL pointer dereference.

Fix this by not setting cpu->exit in prepare_qemu_exit() and updating
it only in rr_cpu_thread_fn() and MTTCG counterpart. This will ensure
that qemu_main_loop() waits for vCPU thread to actually stop before
returning control to the Rust code.

Signed-off-by: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
@lorc
Copy link
Contributor Author

lorc commented Mar 13, 2025

Okay, I've updated the PR by adding the complementary change into MTTR. But I didn't tested MTTR case, though.

@rmalmain
Copy link
Member

it should behave similarly.
i'll re-run the tests on the libafl pr.

@rmalmain rmalmain merged commit 0b9d826 into AFLplusplus:main Mar 14, 2025
1 check passed
@lorc lorc deleted the pr_fix_race branch March 14, 2025 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants