[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 26/35] cpu: free cpu->tb_jmp_cache with RCU
From: |
Alex Bennée |
Subject: |
[PULL 26/35] cpu: free cpu->tb_jmp_cache with RCU |
Date: |
Thu, 26 Jan 2023 11:22:41 +0000 |
From: Emilio Cota <cota@braap.org>
Fixes the appended use-after-free. The root cause is that
during tb invalidation we use CPU_FOREACH, and therefore
to safely free a vCPU we must wait for an RCU grace period
to elapse.
$ x86_64-linux-user/qemu-x86_64 tests/tcg/x86_64-linux-user/munmap-pthread
=================================================================
==1800604==ERROR: AddressSanitizer: heap-use-after-free on address
0x62d0005f7418 at pc 0x5593da6704eb bp 0x7f4961a7ac70 sp 0x7f4961a7ac60
READ of size 8 at 0x62d0005f7418 thread T2
#0 0x5593da6704ea in tb_jmp_cache_inval_tb ../accel/tcg/tb-maint.c:244
#1 0x5593da6704ea in do_tb_phys_invalidate ../accel/tcg/tb-maint.c:290
#2 0x5593da670631 in tb_phys_invalidate__locked ../accel/tcg/tb-maint.c:306
#3 0x5593da670631 in tb_invalidate_phys_page_range__locked
../accel/tcg/tb-maint.c:542
#4 0x5593da67106d in tb_invalidate_phys_range ../accel/tcg/tb-maint.c:614
#5 0x5593da6a64d4 in target_munmap ../linux-user/mmap.c:766
#6 0x5593da6dba05 in do_syscall1 ../linux-user/syscall.c:10105
#7 0x5593da6f564c in do_syscall ../linux-user/syscall.c:13329
#8 0x5593da49e80c in cpu_loop ../linux-user/x86_64/../i386/cpu_loop.c:233
#9 0x5593da6be28c in clone_func ../linux-user/syscall.c:6633
#10 0x7f496231cb42 in start_thread nptl/pthread_create.c:442
#11 0x7f49623ae9ff (/lib/x86_64-linux-gnu/libc.so.6+0x1269ff)
0x62d0005f7418 is located 28696 bytes inside of 32768-byte region
[0x62d0005f0400,0x62d0005f8400)
freed by thread T148 here:
#0 0x7f49627b6460 in __interceptor_free
../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
#1 0x5593da5ac057 in cpu_exec_unrealizefn ../cpu.c:180
#2 0x5593da81f851 (/home/cota/src/qemu/build/qemu-x86_64+0x484851)
Signed-off-by: Emilio Cota <cota@braap.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230111151628.320011-2-cota@braap.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20230124180127.1881110-27-alex.bennee@linaro.org>
diff --git a/accel/tcg/tb-jmp-cache.h b/accel/tcg/tb-jmp-cache.h
index ff5ffc8fc2..b3f6e78835 100644
--- a/accel/tcg/tb-jmp-cache.h
+++ b/accel/tcg/tb-jmp-cache.h
@@ -18,6 +18,7 @@
* a load_acquire/store_release to 'tb'.
*/
struct CPUJumpCache {
+ struct rcu_head rcu;
struct {
TranslationBlock *tb;
#if TARGET_TB_PCREL
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
index 04cd1f3092..25ec73ef9a 100644
--- a/accel/tcg/cpu-exec.c
+++ b/accel/tcg/cpu-exec.c
@@ -1064,13 +1064,12 @@ void tcg_exec_realizefn(CPUState *cpu, Error **errp)
/* undo the initializations in reverse order */
void tcg_exec_unrealizefn(CPUState *cpu)
{
- qemu_plugin_vcpu_exit_hook(cpu);
#ifndef CONFIG_USER_ONLY
tcg_iommu_free_notifier_list(cpu);
#endif /* !CONFIG_USER_ONLY */
tlb_destroy(cpu);
- g_free(cpu->tb_jmp_cache);
+ g_free_rcu(cpu->tb_jmp_cache, rcu);
}
#ifndef CONFIG_USER_ONLY
diff --git a/cpu.c b/cpu.c
index 4a7d865427..21cf809614 100644
--- a/cpu.c
+++ b/cpu.c
@@ -176,11 +176,20 @@ void cpu_exec_unrealizefn(CPUState *cpu)
vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
}
#endif
+
+ /* Call the plugin hook before clearing cpu->cpu_index in cpu_list_remove
*/
if (tcg_enabled()) {
- tcg_exec_unrealizefn(cpu);
+ qemu_plugin_vcpu_exit_hook(cpu);
}
cpu_list_remove(cpu);
+ /*
+ * Now that the vCPU has been removed from the RCU list, we can call
+ * tcg_exec_unrealizefn, which may free fields using call_rcu.
+ */
+ if (tcg_enabled()) {
+ tcg_exec_unrealizefn(cpu);
+ }
}
/*
--
2.34.1
- [PULL 18/35] MAINTAINERS: Fix the entry for tests/tcg/nios2, (continued)
- [PULL 18/35] MAINTAINERS: Fix the entry for tests/tcg/nios2, Alex Bennée, 2023/01/26
- [PULL 14/35] tests/docker: drop debian-tricore-cross's partial status, Alex Bennée, 2023/01/26
- [PULL 28/35] thread: de-const qemu_spin_destroy, Alex Bennée, 2023/01/26
- [PULL 13/35] tests/docker: Install flex in debian-tricore-cross, Alex Bennée, 2023/01/26
- [PULL 15/35] tests/tcg: skip the vma-pthread test on CI, Alex Bennée, 2023/01/26
- [PULL 20/35] docs: add a new section to outline emulation support, Alex Bennée, 2023/01/26
- [PULL 29/35] util/qht: use striped locks under TSAN, Alex Bennée, 2023/01/26
- [PULL 09/35] gitlab: add lsan suppression file to workaround tcmalloc issues, Alex Bennée, 2023/01/26
- [PULL 16/35] tests/tcg: Use SIGKILL for timeout, Alex Bennée, 2023/01/26
- [PULL 30/35] plugins: make qemu_plugin_user_exit's locking order consistent with fork_start's, Alex Bennée, 2023/01/26
- [PULL 26/35] cpu: free cpu->tb_jmp_cache with RCU,
Alex Bennée <=
- [PULL 34/35] cpu-exec: assert that plugin_mem_cbs is NULL after execution, Alex Bennée, 2023/01/26
- [PULL 32/35] translator: always pair plugin_gen_insn_{start, end} calls, Alex Bennée, 2023/01/26
- [PULL 31/35] plugins: fix optimization in plugin_gen_disable_mem_helpers, Alex Bennée, 2023/01/26
- [PULL 24/35] semihosting: add O_BINARY flag in host_open for NT compatibility, Alex Bennée, 2023/01/26
- [PULL 23/35] semihosting: Write back semihosting data before completion callback, Alex Bennée, 2023/01/26
- [PULL 19/35] docs: add hotlinks to about preface text, Alex Bennée, 2023/01/26
- [PULL 35/35] plugins: Iterate on cb_lists in qemu_plugin_user_exit, Alex Bennée, 2023/01/26
- [PULL 27/35] util/qht: add missing atomic_set(hashes[i]), Alex Bennée, 2023/01/26
- [PULL 21/35] semihosting: add semihosting section to the docs, Alex Bennée, 2023/01/26
- [PULL 33/35] tcg: exclude non-memory effecting helpers from instrumentation, Alex Bennée, 2023/01/26