Skip to content

Commit

Permalink
Update Pdfork.OtherUserIfRootForked for actual FreeBSD behaviour (#53)
Browse files Browse the repository at this point in the history
FreeBSD does not currently allow sending signals via pdkill to a process
that is running with a different UID (unless we are root).
This test is currently failing for me on FreeBSD 12.2 and 13. We can still
send a SIGKILL via close(), but pdkill() returns EBADF.
  • Loading branch information
arichardson committed Feb 25, 2021
1 parent b1862b6 commit 906d578
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions procdesc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ TEST_F(PipePdfork, CloseLast) {
FORK_TEST(Pdfork, OtherUserIfRoot) {
GTEST_SKIP_IF_NOT_ROOT();
int pd;
int status;
pid_t pid = pdfork(&pd, 0);
EXPECT_OK(pid);
if (pid == 0) {
Expand All @@ -542,15 +543,29 @@ FORK_TEST(Pdfork, OtherUserIfRoot) {
EXPECT_EQ(EPERM, errno);
EXPECT_PID_ALIVE(pid);

// Succeed with pdkill though.
// Ideally, we should be able to send signals via a process descriptor even
// if it's owned by another user, but this is not implementated on FreeBSD.
#ifdef __FreeBSD__
// On FreeBSD, pdkill() still performs all the same checks that kill() does
// and therefore cannot be used to send a signal to a process with another
// UID unless we are root.
EXPECT_SYSCALL_FAIL(EBADF, pdkill(pid, SIGKILL));
EXPECT_PID_ALIVE(pid);
// However, the process will be killed when we close the process descriptor.
EXPECT_OK(close(pd));
EXPECT_PID_GONE(pid);
// Can't pdwait4() after close() since close() reparents the child to a reaper (init)
EXPECT_SYSCALL_FAIL(EBADF, pdwait4_(pd, &status, WNOHANG, NULL));
#else
// Sending a signal with pdkill() should be permitted though.
EXPECT_OK(pdkill(pd, SIGKILL));
EXPECT_PID_ZOMBIE(pid);

int status;
int rc = pdwait4_(pd, &status, WNOHANG, NULL);
EXPECT_OK(rc);
EXPECT_EQ(pid, rc);
EXPECT_TRUE(WIFSIGNALED(status));
#endif
}

TEST_F(PipePdfork, WaitPidThenPd) {
Expand Down

0 comments on commit 906d578

Please sign in to comment.