Skip to content

Commit ee5f82b

Browse files
happyCoder92copybara-github
authored andcommitted
UnotifyMonitor: Refactor killing the sandboxee/init
This is to avoid log spam - init can already be dead and waited when cleanup runs. Additionally remove risk of killing wrong process when the pid is recycled - keeping reference to the proc dir keeps the pid around. PiperOrigin-RevId: 903812678 Change-Id: Ia75c290c99dfcbb017ea369ba175b01dc9edfef5
1 parent 51a36db commit ee5f82b

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

sandboxed_api/sandbox2/monitor_unotify.cc

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ absl::Status WaitForTaskToStop(pid_t pid) {
126126
: absl::InternalError("task did not stop");
127127
}
128128

129+
void KillProcess(pid_t pid) {
130+
VLOG(1) << "Sending SIGKILL to the PID: " << pid;
131+
if (kill(pid, SIGKILL) != 0) {
132+
if (errno != ESRCH) {
133+
PLOG(ERROR) << "Could not send SIGKILL to PID " << pid;
134+
} else {
135+
VLOG(1) << "Process " << pid << " already dead";
136+
}
137+
}
138+
}
139+
129140
} // namespace
130141

131142
UnotifyMonitor::UnotifyMonitor(Executor* executor, Policy* policy,
@@ -261,7 +272,15 @@ void UnotifyMonitor::Run() {
261272
OnDone();
262273
};
263274
absl::Cleanup setup_notify = [this] { setup_notification_.Notify(); };
264-
absl::Cleanup process_cleanup = [this] { KillInit(); };
275+
absl::Cleanup process_cleanup =
276+
[this, init_pidfd = FDCloser(open(
277+
absl::StrCat("/proc/", process_.init_pid).c_str(), O_PATH))] {
278+
KillInit();
279+
};
280+
// Keep reference to the main pid to avoid killing a random process recycling
281+
// the pid.
282+
FDCloser main_pidfd(
283+
open(absl::StrCat("/proc/", process_.main_pid).c_str(), O_PATH));
265284
if (!InitSetupUnotify()) {
266285
SetExitStatusCode(Result::SETUP_ERROR, Result::FAILED_NOTIFY);
267286
return;
@@ -426,21 +445,9 @@ void UnotifyMonitor::NotifyMonitor() {
426445
sizeof(value));
427446
}
428447

429-
bool UnotifyMonitor::KillSandboxee() {
430-
VLOG(1) << "Sending SIGKILL to the PID: " << process_.main_pid;
431-
if (kill(process_.main_pid, SIGKILL) != 0) {
432-
PLOG(ERROR) << "Could not send SIGKILL to PID " << process_.main_pid;
433-
return false;
434-
}
435-
return true;
436-
}
448+
void UnotifyMonitor::KillSandboxee() { KillProcess(process_.main_pid); }
437449

438-
void UnotifyMonitor::KillInit() {
439-
VLOG(1) << "Sending SIGKILL to the PID: " << process_.init_pid;
440-
if (kill(process_.init_pid, SIGKILL) != 0) {
441-
PLOG(ERROR) << "Could not send SIGKILL to PID " << process_.init_pid;
442-
}
443-
}
450+
void UnotifyMonitor::KillInit() { KillProcess(process_.init_pid); }
444451

445452
void UnotifyMonitor::Join() {
446453
absl::MutexLock lock(notify_mutex_);

sandboxed_api/sandbox2/monitor_unotify.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class UnotifyMonitor : public MonitorBase {
7777
bool InitSetupUnotify();
7878
bool InitSetupNotifyEventFd();
7979
// Kills the main traced PID with SIGKILL.
80-
// Returns false if an error occurred and process could not be killed.
81-
bool KillSandboxee();
80+
void KillSandboxee();
8281
void KillInit();
8382

8483
void AllowSyscallViaUnotify(seccomp_notif req);

0 commit comments

Comments
 (0)