Fix pidfd reuse race condition

This commit is contained in:
Luke Street 2025-10-09 16:52:12 -06:00
parent cadb3cd00e
commit bb4bee83b7

View File

@ -106,9 +106,11 @@ bool ProcessManager::addProcess(Pin<ProcessObject> po) {
if (!po) { if (!po) {
return false; return false;
} }
pid_t pid;
int pidfd; int pidfd;
{ {
std::lock_guard lk(po->m); std::lock_guard lk(po->m);
pid = po->pid;
pidfd = po->pidfd; pidfd = po->pidfd;
if (pidfd < 0) { if (pidfd < 0) {
return false; return false;
@ -128,7 +130,7 @@ bool ProcessManager::addProcess(Pin<ProcessObject> po) {
std::lock_guard lk(m); std::lock_guard lk(m);
mReg.emplace(pidfd, std::move(po)); mReg.emplace(pidfd, std::move(po));
} }
DEBUG_LOG("ProcessManager: tracking pidfd %d\n", pidfd); DEBUG_LOG("ProcessManager: registered pid %d with pidfd %d\n", pid, pidfd);
wake(); wake();
return true; return true;
} }
@ -181,7 +183,6 @@ void ProcessManager::checkPidfd(int pidfd) {
return; return;
} }
epoll_ctl(mEpollFd, EPOLL_CTL_DEL, pidfd, nullptr); epoll_ctl(mEpollFd, EPOLL_CTL_DEL, pidfd, nullptr);
close(pidfd);
} }
DEBUG_LOG("ProcessManager: pidfd %d exited: code=%d status=%d\n", pidfd, si.si_code, si.si_status); DEBUG_LOG("ProcessManager: pidfd %d exited: code=%d status=%d\n", pidfd, si.si_code, si.si_status);
@ -190,10 +191,14 @@ void ProcessManager::checkPidfd(int pidfd) {
{ {
std::shared_lock lk(m); std::shared_lock lk(m);
auto it = mReg.find(pidfd); auto it = mReg.find(pidfd);
if (it == mReg.end()) { if (it != mReg.end()) {
return; po = std::move(it->second);
mReg.erase(it);
} }
po = it->second.clone(); }
close(pidfd);
if (!po) {
return;
} }
{ {
std::lock_guard lk(po->m); std::lock_guard lk(po->m);
@ -205,14 +210,6 @@ void ProcessManager::checkPidfd(int pidfd) {
} }
po->cv.notify_all(); po->cv.notify_all();
po->notifyWaiters(false); po->notifyWaiters(false);
{
std::lock_guard lk(m);
auto it = mReg.find(pidfd);
if (it != mReg.end()) {
mReg.erase(it);
}
}
} }
ProcessManager &processes() { ProcessManager &processes() {