From 814434f139b0f060668707caebda94a7a62ae375 Mon Sep 17 00:00:00 2001 From: Ariel Abreu Date: Thu, 19 May 2022 15:21:32 -0400 Subject: [PATCH] GetExecutablePath: Write the path out to the correct process We were writing out the path to the target process (i.e. the one we're looking up), but we should instead write it out to the process who made the call. --- src/call.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/call.cpp b/src/call.cpp index 1722fbb..b1cc330 100644 --- a/src/call.cpp +++ b/src/call.cpp @@ -950,17 +950,26 @@ void DarlingServer::Call::GetExecutablePath::processCall() { int code = 0; uint64_t fullLength; - if (auto maybeTargetProcess = processRegistry().lookupEntryByNSID(_body.pid)) { - auto targetProcess = *maybeTargetProcess; - auto path = targetProcess->executablePath(); - auto len = std::min(path.length() + 1, _body.buffer_size); - if (!targetProcess->writeMemory((uintptr_t)_body.buffer, path.data(), len, &code)) { - code = -code; + if (auto callingThread = _thread.lock()) { + if (auto callingProcess = callingThread->process()) { + if (auto maybeTargetProcess = processRegistry().lookupEntryByNSID(_body.pid)) { + auto targetProcess = *maybeTargetProcess; + auto path = targetProcess->executablePath(); + auto len = std::min(path.length() + 1, _body.buffer_size); + if (!callingProcess->writeMemory((uintptr_t)_body.buffer, path.c_str(), len, &code)) { + code = -code; + } + fullLength = path.length(); + } else { + // not negated because this is an acceptable case. + // e.g. the target process may have died before the call was processed. + code = ESRCH; + } + } else { + code = -ESRCH; } - fullLength = path.length(); - } else { - code = ESRCH; + code = -ESRCH; } _sendReply(code, fullLength);