Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
jln (very slow on Chromium)
2014/04/24 18:32:58
No "(c)" in new files.
mdempsky
2014/04/24 19:00:34
Done. (But this file will be moved to a separate
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <netinet/in.h> | |
| 6 #include <sched.h> | |
| 7 #include <sys/socket.h> | |
| 8 #include <sys/syscall.h> | |
| 9 #include <unistd.h> | |
| 10 | |
| 11 #include "base/files/scoped_file.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/posix/unix_domain_socket_linux.h" | |
| 14 #include "sandbox/linux/tests/unit_tests.h" | |
| 15 | |
| 16 // TODO(mdempsky): These tests logically belong in base/posix with the other | |
| 17 // UnixDomainSocket tests, but they rely on sandbox test infrastructure for | |
| 18 // fork-safe testing. | |
| 19 | |
| 20 namespace sandbox { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 const char kHello[] = "hello"; | |
| 25 | |
| 26 // If the calling process isn't root, then try using unshare(CLONE_NEWUSER) | |
| 27 // to fake it. | |
| 28 void FakeRoot() { | |
| 29 // If we're already root, then allow test to proceed. | |
| 30 if (geteuid() == 0) | |
| 31 return; | |
| 32 | |
| 33 // Otherwise hope the kernel supports unprivileged namespaces. | |
| 34 if (unshare(CLONE_NEWUSER) == 0) | |
| 35 return; | |
| 36 | |
| 37 printf("Permission to use CLONE_NEWPID missing; skipping test.\n"); | |
| 38 UnitTests::IgnoreThisTest(); | |
| 39 } | |
| 40 | |
| 41 void WaitForExit(pid_t pid) { | |
| 42 int status; | |
| 43 CHECK_EQ(pid, waitpid(pid, &status, 0)); | |
| 44 CHECK(WIFEXITED(status)); | |
| 45 CHECK_EQ(0, WEXITSTATUS(status)); | |
| 46 } | |
| 47 | |
| 48 base::ProcessId GetParentProcessId(base::ProcessId pid) { | |
| 49 // base::GetParentProcessId() is defined as taking a ProcessHandle instead of | |
| 50 // a ProcessId, even though it's a POSIX-only function and IDs and Handles | |
| 51 // are both simply pid_t on POSIX... :/ | |
| 52 base::ProcessHandle handle; | |
| 53 CHECK(base::OpenProcessHandle(pid, &handle)); | |
| 54 base::ProcessId ret = base::GetParentProcessId(pid); | |
| 55 base::CloseProcessHandle(handle); | |
| 56 return ret; | |
| 57 } | |
| 58 | |
| 59 void SendHello(int fd) { | |
| 60 int pipe_fds[2]; | |
| 61 CHECK_EQ(0, pipe(pipe_fds)); | |
| 62 base::ScopedFD read_pipe(pipe_fds[0]); | |
| 63 base::ScopedFD write_pipe(pipe_fds[1]); | |
| 64 | |
| 65 std::vector<int> send_fds; | |
| 66 send_fds.push_back(write_pipe.get()); | |
| 67 CHECK(UnixDomainSocket::SendMsg(fd, kHello, sizeof(kHello), send_fds)); | |
| 68 | |
| 69 write_pipe.reset(); | |
| 70 | |
| 71 // Block until receiver closes their end of the pipe. | |
| 72 char ch; | |
| 73 CHECK_EQ(0, read(read_pipe.get(), &ch, 1)); | |
| 74 } | |
| 75 | |
| 76 void RecvHello(int fd, | |
| 77 base::ProcessId* sender_pid, | |
| 78 base::ScopedFD* write_pipe = NULL) { | |
| 79 char buf[sizeof(kHello) + 1]; | |
| 80 std::vector<int> message_fds; | |
| 81 ssize_t n = UnixDomainSocket::RecvMsgWithPid( | |
| 82 fd, buf, sizeof(buf), &message_fds, sender_pid); | |
| 83 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); | |
| 84 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | |
| 85 CHECK_EQ(1U, message_fds.size()); | |
| 86 base::ScopedFD message_fd(message_fds[0]); | |
| 87 if (write_pipe) | |
| 88 write_pipe->swap(message_fd); | |
| 89 } | |
| 90 | |
| 91 // Check that receiving PIDs works across a fork(). | |
| 92 SANDBOX_TEST(UnixDomainSocketTest, Fork) { | |
| 93 int fds[2]; | |
| 94 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 95 base::ScopedFD recv_sock(fds[0]); | |
| 96 base::ScopedFD send_sock(fds[1]); | |
| 97 | |
| 98 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 99 | |
| 100 const pid_t pid = fork(); | |
| 101 CHECK_NE(-1, pid); | |
| 102 if (pid == 0) { | |
| 103 // Child process. | |
| 104 recv_sock.reset(); | |
| 105 SendHello(send_sock.get()); | |
| 106 _exit(0); | |
| 107 } | |
| 108 | |
| 109 // Parent process. | |
| 110 send_sock.reset(); | |
| 111 | |
| 112 base::ProcessId sender_pid; | |
| 113 RecvHello(recv_sock.get(), &sender_pid); | |
| 114 CHECK_EQ(pid, sender_pid); | |
| 115 | |
| 116 WaitForExit(pid); | |
| 117 } | |
| 118 | |
| 119 // Similar to Fork above, but forking the child into a new pid namespace. | |
| 120 SANDBOX_TEST(UnixDomainSocketTest, Namespace) { | |
| 121 FakeRoot(); | |
| 122 | |
| 123 int fds[2]; | |
| 124 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 125 base::ScopedFD recv_sock(fds[0]); | |
| 126 base::ScopedFD send_sock(fds[1]); | |
| 127 | |
| 128 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 129 | |
| 130 const pid_t pid = syscall(__NR_clone, CLONE_NEWPID | SIGCHLD, 0, 0, 0); | |
| 131 CHECK_NE(-1, pid); | |
| 132 if (pid == 0) { | |
| 133 // Child process. | |
| 134 recv_sock.reset(); | |
| 135 | |
| 136 // Check that we think we're pid 1 in our new namespace. | |
| 137 CHECK_EQ(1, syscall(__NR_getpid)); | |
| 138 | |
| 139 SendHello(send_sock.get()); | |
| 140 _exit(0); | |
| 141 } | |
| 142 | |
| 143 // Parent process. | |
| 144 send_sock.reset(); | |
| 145 | |
| 146 base::ProcessId sender_pid; | |
| 147 RecvHello(recv_sock.get(), &sender_pid); | |
| 148 CHECK_EQ(pid, sender_pid); | |
| 149 | |
| 150 WaitForExit(pid); | |
| 151 } | |
| 152 | |
| 153 // Again similar to Fork, but now with nested PID namespaces. | |
| 154 SANDBOX_TEST(UnixDomainSocketTest, DoubleNamespace) { | |
| 155 FakeRoot(); | |
| 156 | |
| 157 int fds[2]; | |
| 158 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 159 base::ScopedFD recv_sock(fds[0]); | |
| 160 base::ScopedFD send_sock(fds[1]); | |
| 161 | |
| 162 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 163 | |
| 164 const pid_t pid = syscall(__NR_clone, CLONE_NEWPID | SIGCHLD, 0, 0, 0); | |
| 165 CHECK_NE(-1, pid); | |
| 166 if (pid == 0) { | |
| 167 // Child process. | |
| 168 recv_sock.reset(); | |
| 169 | |
| 170 const pid_t pid2 = syscall(__NR_clone, CLONE_NEWPID | SIGCHLD, 0, 0, 0); | |
| 171 CHECK_NE(-1, pid2); | |
| 172 | |
| 173 if (pid2 != 0) { | |
| 174 // Wait for grand child to run to completion; see comments below. | |
| 175 WaitForExit(pid2); | |
| 176 } | |
| 177 | |
| 178 // Check that we think we're pid 1. | |
| 179 CHECK_EQ(1, syscall(__NR_getpid)); | |
| 180 | |
| 181 SendHello(send_sock.get()); | |
| 182 _exit(0); | |
| 183 } | |
| 184 | |
| 185 // Parent process. | |
| 186 send_sock.reset(); | |
| 187 | |
| 188 // We have two messages to receive: first from the grand-child, | |
| 189 // then from the child. | |
| 190 | |
| 191 for (unsigned iteration = 0; iteration < 2; ++iteration) { | |
| 192 base::ProcessId sender_pid; | |
| 193 base::ScopedFD pipe_fd; | |
| 194 RecvHello(recv_sock.get(), &sender_pid, &pipe_fd); | |
| 195 | |
| 196 // We need our child and grand child processes to both be alive for | |
| 197 // GetParentProcessId() to return a valid pid, hence the pipe trickery. | |
| 198 // (On the first iteration, grand child is blocked reading from the pipe | |
| 199 // until we close it, and child is blocked waiting for grand child to exit.) | |
| 200 | |
| 201 switch (iteration) { | |
| 202 case 0: // Grand child's message | |
| 203 CHECK_EQ(pid, GetParentProcessId(sender_pid)); | |
| 204 break; | |
| 205 case 1: // Child's message | |
| 206 CHECK_EQ(pid, sender_pid); | |
| 207 break; | |
| 208 default: | |
| 209 NOTREACHED(); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 WaitForExit(pid); | |
| 214 } | |
| 215 | |
| 216 // Tests that GetPeerPid() returns 0 if the peer does not exist in caller's | |
| 217 // namespace. | |
| 218 SANDBOX_TEST(UnixDomainSocketTest, ImpossiblePid) { | |
| 219 FakeRoot(); | |
| 220 | |
| 221 int fds[2]; | |
| 222 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | |
| 223 base::ScopedFD send_sock(fds[0]); | |
| 224 base::ScopedFD recv_sock(fds[1]); | |
| 225 | |
| 226 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | |
| 227 | |
| 228 const pid_t pid = syscall(__NR_clone, CLONE_NEWPID | SIGCHLD, 0, 0, 0); | |
| 229 CHECK_NE(-1, pid); | |
| 230 if (pid == 0) { | |
| 231 // Child process. | |
| 232 send_sock.reset(); | |
| 233 | |
| 234 base::ProcessId sender_pid; | |
| 235 RecvHello(recv_sock.get(), &sender_pid); | |
| 236 CHECK_EQ(0, sender_pid); | |
| 237 _exit(0); | |
| 238 } | |
| 239 | |
| 240 // Parent process. | |
| 241 recv_sock.reset(); | |
| 242 SendHello(send_sock.get()); | |
| 243 WaitForExit(pid); | |
| 244 } | |
| 245 | |
| 246 } // namespace | |
| 247 | |
| 248 } // namespace sandbox | |
| OLD | NEW |