From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Richard Henderson" <richard.henderson@linaro.org>,
"Ed Maste" <emaste@freebsd.org>, "Thomas Huth" <thuth@redhat.com>,
"Alexandre Iooss" <erdnaxe@crans.org>,
"Markus Armbruster" <armbru@redhat.com>,
"Jagannathan Raman" <jag.raman@oracle.com>,
"John G Johnson" <john.g.johnson@oracle.com>,
"Mahmoud Mandour" <ma.mandourr@gmail.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Bandan Das" <bsd@redhat.com>,
"Elena Ufimtseva" <elena.ufimtseva@oracle.com>,
"Michael Roth" <michael.roth@amd.com>,
"Eduardo Habkost" <eduardo@habkost.net>,
"Beraldo Leal" <bleal@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Qiuhao Li" <Qiuhao.Li@outlook.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Alexander Bulekov" <alxndr@bu.edu>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"John Snow" <jsnow@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Darren Kenny" <darren.kenny@oracle.com>,
qemu-arm@nongnu.org, "Yanan Wang" <wangyanan55@huawei.com>,
"Cleber Rosa" <crosa@redhat.com>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
"Li-Wen Hsu" <lwhsu@freebsd.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Emilio Cota" <cota@braap.org>
Subject: [PATCH v2 26/35] cpu: free cpu->tb_jmp_cache with RCU
Date: Tue, 24 Jan 2023 18:01:18 +0000 [thread overview]
Message-ID: <20230124180127.1881110-27-alex.bennee@linaro.org> (raw)
In-Reply-To: <20230124180127.1881110-1-alex.bennee@linaro.org>
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>
---
accel/tcg/tb-jmp-cache.h | 1 +
accel/tcg/cpu-exec.c | 3 +--
cpu.c | 11 ++++++++++-
3 files changed, 12 insertions(+), 3 deletions(-)
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
next prev parent reply other threads:[~2023-01-24 18:05 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-24 18:00 [PATCH v2 00/35] maintainer tree pre-PR (testing/docs/semihosting/plugins) Alex Bennée
2023-01-24 18:00 ` [PATCH v2 01/35] scripts/ci: update gitlab-runner playbook to use latest runner Alex Bennée
2023-01-24 19:49 ` Richard Henderson
2023-01-24 18:00 ` [PATCH v2 02/35] gitlab: add FF_SCRIPT_SECTIONS for timings Alex Bennée
2023-01-25 12:09 ` Philippe Mathieu-Daudé
2023-01-25 12:15 ` Daniel P. Berrangé
2023-01-24 18:00 ` [PATCH v2 03/35] gitlab: just use plain --cc=clang for custom runner build Alex Bennée
2023-01-24 18:00 ` [PATCH v2 04/35] tests/unit: drop hacky race avoidance in test-io-channel-command Alex Bennée
2023-01-24 18:00 ` [PATCH v2 05/35] build-sys: fix crlf-ending C code Alex Bennée
2023-01-24 18:00 ` [PATCH v2 06/35] .gitlab-ci.d/windows: do not disable opengl Alex Bennée
2023-01-24 18:00 ` [PATCH v2 07/35] meson: replace Perl usage with Python Alex Bennée
2023-01-24 18:01 ` [PATCH v2 08/35] docs: drop texinfo options Alex Bennée
2023-01-24 18:01 ` [PATCH v2 09/35] gitlab: add lsan suppression file to workaround tcmalloc issues Alex Bennée
2023-01-25 7:44 ` Thomas Huth
2023-01-25 12:13 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 10/35] Update lcitool and fedora to 37 Alex Bennée
2023-01-24 18:01 ` [PATCH v2 11/35] lcitool: drop perl from QEMU project/dependencies Alex Bennée
2023-01-24 18:01 ` [PATCH v2 12/35] lcitool: drop texinfo " Alex Bennée
2023-01-24 18:01 ` [PATCH v2 13/35] tests/docker: Install flex in debian-tricore-cross Alex Bennée
2023-01-24 18:01 ` [PATCH v2 14/35] tests/docker: drop debian-tricore-cross's partial status Alex Bennée
2023-01-24 18:01 ` [PATCH v2 15/35] tests/tcg: skip the vma-pthread test on CI Alex Bennée
2023-01-24 18:01 ` [PATCH v2 16/35] tests/tcg: Use SIGKILL for timeout Alex Bennée
2023-01-25 12:14 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 17/35] gitlab: wrap up test results for custom runners Alex Bennée
2023-01-24 18:01 ` [PATCH v2 18/35] MAINTAINERS: Fix the entry for tests/tcg/nios2 Alex Bennée
2023-01-24 18:01 ` [PATCH v2 19/35] docs: add hotlinks to about preface text Alex Bennée
2023-01-25 12:16 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 20/35] docs: add a new section to outline emulation support Alex Bennée
2023-01-25 7:58 ` Thomas Huth
2023-01-25 12:18 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 21/35] semihosting: add semihosting section to the docs Alex Bennée
2023-01-24 18:01 ` [PATCH v2 22/35] docs: add an introduction to the system docs Alex Bennée
2023-01-25 7:53 ` Thomas Huth
2023-01-24 18:01 ` [PATCH v2 23/35] semihosting: Write back semihosting data before completion callback Alex Bennée
2023-01-25 23:21 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 24/35] semihosting: add O_BINARY flag in host_open for NT compatibility Alex Bennée
2023-01-24 18:01 ` [PATCH v2 25/35] tests/tcg: add memory-sve test for aarch64 Alex Bennée
2023-01-24 18:01 ` Alex Bennée [this message]
2023-01-24 18:01 ` [PATCH v2 27/35] util/qht: add missing atomic_set(hashes[i]) Alex Bennée
2023-01-24 18:01 ` [PATCH v2 28/35] thread: de-const qemu_spin_destroy Alex Bennée
2023-01-24 18:01 ` [PATCH v2 29/35] util/qht: use striped locks under TSAN Alex Bennée
2023-01-24 18:01 ` [PATCH v2 30/35] plugins: make qemu_plugin_user_exit's locking order consistent with fork_start's Alex Bennée
2023-01-24 18:01 ` [PATCH v2 31/35] plugins: fix optimization in plugin_gen_disable_mem_helpers Alex Bennée
2023-01-24 18:01 ` [PATCH v2 32/35] translator: always pair plugin_gen_insn_{start, end} calls Alex Bennée
2023-01-24 18:01 ` [PATCH v2 33/35] tcg: exclude non-memory effecting helpers from instrumentation Alex Bennée
2023-01-24 19:44 ` Richard Henderson
2023-01-24 18:01 ` [PATCH v2 34/35] cpu-exec: assert that plugin_mem_cbs is NULL after execution Alex Bennée
2023-01-24 19:46 ` Richard Henderson
2023-01-25 23:27 ` Philippe Mathieu-Daudé
2023-01-24 18:01 ` [PATCH v2 35/35] plugins: Iterate on cb_lists in qemu_plugin_user_exit Alex Bennée
2023-01-25 23:26 ` Philippe Mathieu-Daudé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230124180127.1881110-27-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=Qiuhao.Li@outlook.com \
--cc=alxndr@bu.edu \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=bleal@redhat.com \
--cc=bsd@redhat.com \
--cc=cota@braap.org \
--cc=crosa@redhat.com \
--cc=darren.kenny@oracle.com \
--cc=eduardo@habkost.net \
--cc=elena.ufimtseva@oracle.com \
--cc=emaste@freebsd.org \
--cc=erdnaxe@crans.org \
--cc=jag.raman@oracle.com \
--cc=john.g.johnson@oracle.com \
--cc=jsnow@redhat.com \
--cc=lwhsu@freebsd.org \
--cc=ma.mandourr@gmail.com \
--cc=marcandre.lureau@redhat.com \
--cc=marcel.apfelbaum@gmail.com \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=stefanha@redhat.com \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
--cc=wangyanan55@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).