From 11c48eb54708a0b4f833ecd308185aba180104cf Mon Sep 17 00:00:00 2001 From: Sun Yuechi Date: Tue, 30 Jun 2026 05:06:53 -0700 Subject: [PATCH] crimson/mon/MonClient: fix use-after-free in run_command run_command() captured a reference to the just-emplaced mon_commands element into the send_message() continuation. mon_commands is a std::vector, so a concurrent run_command() could reallocate it and invalidate the reference before the continuation ran, dereferencing freed memory. No caller issues concurrent commands today, but the pattern is unsafe. Take the result future before issuing the message, then coroutinize the method so it is awaited directly instead of being captured into a continuation lambda. Signed-off-by: Sun Yuechi --- src/crimson/mon/MonClient.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/crimson/mon/MonClient.cc b/src/crimson/mon/MonClient.cc index fb60645f8688..b6aeb5a1ae08 100644 --- a/src/crimson/mon/MonClient.cc +++ b/src/crimson/mon/MonClient.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -1076,10 +1077,10 @@ Client::run_command(std::string&& cmd, m->set_tid(tid); m->cmd = {std::move(cmd)}; m->set_data(std::move(bl)); - auto& command = mon_commands.emplace_back(crimson::make_message(*m)); - return send_message(std::move(m)).then([&result=command.result] { - return result.get_future(); - }); + auto fut = mon_commands.emplace_back(crimson::make_message(*m)) + .result.get_future(); + co_await send_message(std::move(m)); + co_return co_await std::move(fut); } seastar::future<> Client::send_message(MessageURef m) -- 2.47.3