| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sched.h> | 5 #include <sched.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 #include <sys/syscall.h> | 9 #include <sys/syscall.h> |
| 10 #include <sys/wait.h> | 10 #include <sys/wait.h> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // SendHello sends a "hello" to socket fd, and then blocks until the recipient | 66 // SendHello sends a "hello" to socket fd, and then blocks until the recipient |
| 67 // acknowledges it by calling RecvHello. | 67 // acknowledges it by calling RecvHello. |
| 68 void SendHello(int fd) { | 68 void SendHello(int fd) { |
| 69 int pipe_fds[2]; | 69 int pipe_fds[2]; |
| 70 CHECK_EQ(0, pipe(pipe_fds)); | 70 CHECK_EQ(0, pipe(pipe_fds)); |
| 71 base::ScopedFD read_pipe(pipe_fds[0]); | 71 base::ScopedFD read_pipe(pipe_fds[0]); |
| 72 base::ScopedFD write_pipe(pipe_fds[1]); | 72 base::ScopedFD write_pipe(pipe_fds[1]); |
| 73 | 73 |
| 74 std::vector<int> send_fds; | 74 std::vector<int> send_fds; |
| 75 send_fds.push_back(write_pipe.get()); | 75 send_fds.push_back(write_pipe.get()); |
| 76 CHECK(UnixDomainSocket::SendMsg(fd, kHello, sizeof(kHello), send_fds)); | 76 CHECK(base::UnixDomainSocket::SendMsg(fd, kHello, sizeof(kHello), send_fds)); |
| 77 | 77 |
| 78 write_pipe.reset(); | 78 write_pipe.reset(); |
| 79 | 79 |
| 80 // Block until receiver closes their end of the pipe. | 80 // Block until receiver closes their end of the pipe. |
| 81 char ch; | 81 char ch; |
| 82 CHECK_EQ(0, HANDLE_EINTR(read(read_pipe.get(), &ch, 1))); | 82 CHECK_EQ(0, HANDLE_EINTR(read(read_pipe.get(), &ch, 1))); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // RecvHello receives and acknowledges a "hello" on socket fd, and returns the | 85 // RecvHello receives and acknowledges a "hello" on socket fd, and returns the |
| 86 // process ID of the sender in sender_pid. Optionally, write_pipe can be used | 86 // process ID of the sender in sender_pid. Optionally, write_pipe can be used |
| 87 // to return a file descriptor, and the acknowledgement will be delayed until | 87 // to return a file descriptor, and the acknowledgement will be delayed until |
| 88 // the descriptor is closed. | 88 // the descriptor is closed. |
| 89 // (Implementation details: SendHello allocates a new pipe, sends us the writing | 89 // (Implementation details: SendHello allocates a new pipe, sends us the writing |
| 90 // end alongside the "hello" message, and then blocks until we close the writing | 90 // end alongside the "hello" message, and then blocks until we close the writing |
| 91 // end of the pipe.) | 91 // end of the pipe.) |
| 92 void RecvHello(int fd, | 92 void RecvHello(int fd, |
| 93 base::ProcessId* sender_pid, | 93 base::ProcessId* sender_pid, |
| 94 base::ScopedFD* write_pipe = NULL) { | 94 base::ScopedFD* write_pipe = NULL) { |
| 95 // Extra receiving buffer space to make sure we really received only | 95 // Extra receiving buffer space to make sure we really received only |
| 96 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. | 96 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 97 char buf[sizeof(kHello) + 1]; | 97 char buf[sizeof(kHello) + 1]; |
| 98 ScopedVector<base::ScopedFD> message_fds; | 98 ScopedVector<base::ScopedFD> message_fds; |
| 99 ssize_t n = UnixDomainSocket::RecvMsgWithPid( | 99 ssize_t n = base::UnixDomainSocket::RecvMsgWithPid( |
| 100 fd, buf, sizeof(buf), &message_fds, sender_pid); | 100 fd, buf, sizeof(buf), &message_fds, sender_pid); |
| 101 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); | 101 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); |
| 102 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 102 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 103 CHECK_EQ(1U, message_fds.size()); | 103 CHECK_EQ(1U, message_fds.size()); |
| 104 if (write_pipe) | 104 if (write_pipe) |
| 105 write_pipe->swap(*message_fds[0]); | 105 write_pipe->swap(*message_fds[0]); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Check that receiving PIDs works across a fork(). | 108 // Check that receiving PIDs works across a fork(). |
| 109 SANDBOX_TEST(UnixDomainSocketTest, Fork) { | 109 SANDBOX_TEST(UnixDomainSocketTest, Fork) { |
| 110 int fds[2]; | 110 int fds[2]; |
| 111 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 111 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 112 base::ScopedFD recv_sock(fds[0]); | 112 base::ScopedFD recv_sock(fds[0]); |
| 113 base::ScopedFD send_sock(fds[1]); | 113 base::ScopedFD send_sock(fds[1]); |
| 114 | 114 |
| 115 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 115 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 116 | 116 |
| 117 const pid_t pid = fork(); | 117 const pid_t pid = fork(); |
| 118 CHECK_NE(-1, pid); | 118 CHECK_NE(-1, pid); |
| 119 if (pid == 0) { | 119 if (pid == 0) { |
| 120 // Child process. | 120 // Child process. |
| 121 recv_sock.reset(); | 121 recv_sock.reset(); |
| 122 SendHello(send_sock.get()); | 122 SendHello(send_sock.get()); |
| 123 _exit(0); | 123 _exit(0); |
| 124 } | 124 } |
| 125 | 125 |
| 126 // Parent process. | 126 // Parent process. |
| 127 send_sock.reset(); | 127 send_sock.reset(); |
| 128 | 128 |
| 129 base::ProcessId sender_pid; | 129 base::ProcessId sender_pid; |
| 130 RecvHello(recv_sock.get(), &sender_pid); | 130 RecvHello(recv_sock.get(), &sender_pid); |
| 131 CHECK_EQ(pid, sender_pid); | 131 CHECK_EQ(pid, sender_pid); |
| 132 | 132 |
| 133 WaitForExit(pid); | 133 WaitForExit(pid); |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Similar to Fork above, but forking the child into a new pid namespace. | 136 // Similar to Fork above, but forking the child into a new pid namespace. |
| 137 SANDBOX_TEST(UnixDomainSocketTest, Namespace) { | 137 SANDBOX_TEST(UnixDomainSocketTest, Namespace) { |
| 138 FakeRoot(); | 138 FakeRoot(); |
| 139 | 139 |
| 140 int fds[2]; | 140 int fds[2]; |
| 141 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 141 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 142 base::ScopedFD recv_sock(fds[0]); | 142 base::ScopedFD recv_sock(fds[0]); |
| 143 base::ScopedFD send_sock(fds[1]); | 143 base::ScopedFD send_sock(fds[1]); |
| 144 | 144 |
| 145 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 145 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 146 | 146 |
| 147 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); | 147 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); |
| 148 CHECK_NE(-1, pid); | 148 CHECK_NE(-1, pid); |
| 149 if (pid == 0) { | 149 if (pid == 0) { |
| 150 // Child process. | 150 // Child process. |
| 151 recv_sock.reset(); | 151 recv_sock.reset(); |
| 152 | 152 |
| 153 // Check that we think we're pid 1 in our new namespace. | 153 // Check that we think we're pid 1 in our new namespace. |
| 154 CHECK_EQ(1, sys_getpid()); | 154 CHECK_EQ(1, sys_getpid()); |
| 155 | 155 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 169 | 169 |
| 170 // Again similar to Fork, but now with nested PID namespaces. | 170 // Again similar to Fork, but now with nested PID namespaces. |
| 171 SANDBOX_TEST(UnixDomainSocketTest, DoubleNamespace) { | 171 SANDBOX_TEST(UnixDomainSocketTest, DoubleNamespace) { |
| 172 FakeRoot(); | 172 FakeRoot(); |
| 173 | 173 |
| 174 int fds[2]; | 174 int fds[2]; |
| 175 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 175 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 176 base::ScopedFD recv_sock(fds[0]); | 176 base::ScopedFD recv_sock(fds[0]); |
| 177 base::ScopedFD send_sock(fds[1]); | 177 base::ScopedFD send_sock(fds[1]); |
| 178 | 178 |
| 179 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 179 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 180 | 180 |
| 181 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); | 181 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); |
| 182 CHECK_NE(-1, pid); | 182 CHECK_NE(-1, pid); |
| 183 if (pid == 0) { | 183 if (pid == 0) { |
| 184 // Child process. | 184 // Child process. |
| 185 recv_sock.reset(); | 185 recv_sock.reset(); |
| 186 | 186 |
| 187 const pid_t pid2 = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); | 187 const pid_t pid2 = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); |
| 188 CHECK_NE(-1, pid2); | 188 CHECK_NE(-1, pid2); |
| 189 | 189 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 // Tests that GetPeerPid() returns 0 if the peer does not exist in caller's | 235 // Tests that GetPeerPid() returns 0 if the peer does not exist in caller's |
| 236 // namespace. | 236 // namespace. |
| 237 SANDBOX_TEST(UnixDomainSocketTest, ImpossiblePid) { | 237 SANDBOX_TEST(UnixDomainSocketTest, ImpossiblePid) { |
| 238 FakeRoot(); | 238 FakeRoot(); |
| 239 | 239 |
| 240 int fds[2]; | 240 int fds[2]; |
| 241 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 241 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 242 base::ScopedFD send_sock(fds[0]); | 242 base::ScopedFD send_sock(fds[0]); |
| 243 base::ScopedFD recv_sock(fds[1]); | 243 base::ScopedFD recv_sock(fds[1]); |
| 244 | 244 |
| 245 CHECK(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 245 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| 246 | 246 |
| 247 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); | 247 const pid_t pid = sys_clone(CLONE_NEWPID | SIGCHLD, 0, 0, 0, 0); |
| 248 CHECK_NE(-1, pid); | 248 CHECK_NE(-1, pid); |
| 249 if (pid == 0) { | 249 if (pid == 0) { |
| 250 // Child process. | 250 // Child process. |
| 251 send_sock.reset(); | 251 send_sock.reset(); |
| 252 | 252 |
| 253 base::ProcessId sender_pid; | 253 base::ProcessId sender_pid; |
| 254 RecvHello(recv_sock.get(), &sender_pid); | 254 RecvHello(recv_sock.get(), &sender_pid); |
| 255 CHECK_EQ(0, sender_pid); | 255 CHECK_EQ(0, sender_pid); |
| 256 _exit(0); | 256 _exit(0); |
| 257 } | 257 } |
| 258 | 258 |
| 259 // Parent process. | 259 // Parent process. |
| 260 recv_sock.reset(); | 260 recv_sock.reset(); |
| 261 SendHello(send_sock.get()); | 261 SendHello(send_sock.get()); |
| 262 WaitForExit(pid); | 262 WaitForExit(pid); |
| 263 } | 263 } |
| 264 | 264 |
| 265 } // namespace | 265 } // namespace |
| 266 | 266 |
| 267 } // namespace sandbox | 267 } // namespace sandbox |
| OLD | NEW |