Changeset View
Changeset View
Standalone View
Standalone View
contrib/capsicum-test/procdesc.cc
Show First 20 Lines • Show All 516 Lines • ▼ Show 20 Lines | TEST_F(PipePdfork, CloseLast) { | ||||
EXPECT_FALSE(had_signal[SIGCHLD]); | EXPECT_FALSE(had_signal[SIGCHLD]); | ||||
signal(SIGCHLD, original); | signal(SIGCHLD, original); | ||||
} | } | ||||
FORK_TEST(Pdfork, OtherUserIfRoot) { | FORK_TEST(Pdfork, OtherUserIfRoot) { | ||||
GTEST_SKIP_IF_NOT_ROOT(); | GTEST_SKIP_IF_NOT_ROOT(); | ||||
int pd; | int pd; | ||||
int status; | |||||
pid_t pid = pdfork(&pd, 0); | pid_t pid = pdfork(&pd, 0); | ||||
EXPECT_OK(pid); | EXPECT_OK(pid); | ||||
if (pid == 0) { | if (pid == 0) { | ||||
// Child process: loop forever. | // Child process: loop forever. | ||||
while (true) usleep(100000); | while (true) usleep(100000); | ||||
} | } | ||||
usleep(100); | usleep(100); | ||||
// Now that the second process has been pdfork()ed, change euid. | // Now that the second process has been pdfork()ed, change euid. | ||||
ASSERT_NE(0u, other_uid) << "other_uid not initialized correctly, " | ASSERT_NE(0u, other_uid) << "other_uid not initialized correctly, " | ||||
"please pass the -u <uid> flag."; | "please pass the -u <uid> flag."; | ||||
EXPECT_EQ(0, setuid(other_uid)); | EXPECT_EQ(0, setuid(other_uid)); | ||||
EXPECT_EQ(other_uid, getuid()); | EXPECT_EQ(other_uid, getuid()); | ||||
if (verbose) fprintf(stderr, "uid=%d euid=%d\n", getuid(), geteuid()); | if (verbose) fprintf(stderr, "uid=%d euid=%d\n", getuid(), geteuid()); | ||||
// Fail to kill child with normal PID operation. | // Fail to kill child with normal PID operation. | ||||
EXPECT_EQ(-1, kill(pid, SIGKILL)); | EXPECT_EQ(-1, kill(pid, SIGKILL)); | ||||
EXPECT_EQ(EPERM, errno); | EXPECT_EQ(EPERM, errno); | ||||
EXPECT_PID_ALIVE(pid); | EXPECT_PID_ALIVE(pid); | ||||
// Succeed with pdkill though. | // Succeed with pdkill though. | ||||
#ifdef __FreeBSD__ | |||||
// Can't send a signal to a process with another UID unless we are root | |||||
EXPECT_EQ(-1, 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 | |||||
EXPECT_OK(pdkill(pd, SIGKILL)); | EXPECT_OK(pdkill(pd, SIGKILL)); | ||||
EXPECT_PID_ZOMBIE(pid); | EXPECT_PID_ZOMBIE(pid); | ||||
int status; | |||||
int rc = pdwait4_(pd, &status, WNOHANG, NULL); | int rc = pdwait4_(pd, &status, WNOHANG, NULL); | ||||
EXPECT_OK(rc); | EXPECT_OK(rc); | ||||
EXPECT_EQ(pid, rc); | EXPECT_EQ(pid, rc); | ||||
EXPECT_TRUE(WIFSIGNALED(status)); | EXPECT_TRUE(WIFSIGNALED(status)); | ||||
#endif | |||||
} | } | ||||
TEST_F(PipePdfork, WaitPidThenPd) { | TEST_F(PipePdfork, WaitPidThenPd) { | ||||
TerminateChild(); | TerminateChild(); | ||||
int status; | int status; | ||||
// If we waitpid(pid) first... | // If we waitpid(pid) first... | ||||
int rc = waitpid(pid_, &status, __WALL); | int rc = waitpid(pid_, &status, __WALL); | ||||
EXPECT_OK(rc); | EXPECT_OK(rc); | ||||
▲ Show 20 Lines • Show All 419 Lines • Show Last 20 Lines |