Skip to content

Commit bb4bee8

Browse files
committed
Fix pidfd reuse race condition
1 parent cadb3cd commit bb4bee8

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/processes.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ bool ProcessManager::addProcess(Pin<ProcessObject> po) {
106106
if (!po) {
107107
return false;
108108
}
109+
pid_t pid;
109110
int pidfd;
110111
{
111112
std::lock_guard lk(po->m);
113+
pid = po->pid;
112114
pidfd = po->pidfd;
113115
if (pidfd < 0) {
114116
return false;
@@ -128,7 +130,7 @@ bool ProcessManager::addProcess(Pin<ProcessObject> po) {
128130
std::lock_guard lk(m);
129131
mReg.emplace(pidfd, std::move(po));
130132
}
131-
DEBUG_LOG("ProcessManager: tracking pidfd %d\n", pidfd);
133+
DEBUG_LOG("ProcessManager: registered pid %d with pidfd %d\n", pid, pidfd);
132134
wake();
133135
return true;
134136
}
@@ -181,7 +183,6 @@ void ProcessManager::checkPidfd(int pidfd) {
181183
return;
182184
}
183185
epoll_ctl(mEpollFd, EPOLL_CTL_DEL, pidfd, nullptr);
184-
close(pidfd);
185186
}
186187

187188
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) {
190191
{
191192
std::shared_lock lk(m);
192193
auto it = mReg.find(pidfd);
193-
if (it == mReg.end()) {
194-
return;
194+
if (it != mReg.end()) {
195+
po = std::move(it->second);
196+
mReg.erase(it);
195197
}
196-
po = it->second.clone();
198+
}
199+
close(pidfd);
200+
if (!po) {
201+
return;
197202
}
198203
{
199204
std::lock_guard lk(po->m);
@@ -205,14 +210,6 @@ void ProcessManager::checkPidfd(int pidfd) {
205210
}
206211
po->cv.notify_all();
207212
po->notifyWaiters(false);
208-
209-
{
210-
std::lock_guard lk(m);
211-
auto it = mReg.find(pidfd);
212-
if (it != mReg.end()) {
213-
mReg.erase(it);
214-
}
215-
}
216213
}
217214

218215
ProcessManager &processes() {

0 commit comments

Comments
 (0)