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> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_vector.h" |
17 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
18 #include "base/posix/unix_domain_socket_linux.h" | 19 #include "base/posix/unix_domain_socket_linux.h" |
19 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
20 #include "sandbox/linux/tests/unit_tests.h" | 21 #include "sandbox/linux/tests/unit_tests.h" |
21 | 22 |
22 // Additional tests for base's UnixDomainSocket to make sure it behaves | 23 // Additional tests for base's UnixDomainSocket to make sure it behaves |
23 // correctly in the presence of sandboxing functionality (e.g., receiving | 24 // correctly in the presence of sandboxing functionality (e.g., receiving |
24 // PIDs across namespaces). | 25 // PIDs across namespaces). |
25 | 26 |
26 namespace sandbox { | 27 namespace sandbox { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // the descriptor is closed. | 88 // the descriptor is closed. |
88 // (Implementation details: SendHello allocates a new pipe, sends us the writing | 89 // (Implementation details: SendHello allocates a new pipe, sends us the writing |
89 // 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 |
90 // end of the pipe.) | 91 // end of the pipe.) |
91 void RecvHello(int fd, | 92 void RecvHello(int fd, |
92 base::ProcessId* sender_pid, | 93 base::ProcessId* sender_pid, |
93 base::ScopedFD* write_pipe = NULL) { | 94 base::ScopedFD* write_pipe = NULL) { |
94 // Extra receiving buffer space to make sure we really received only | 95 // Extra receiving buffer space to make sure we really received only |
95 // 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. |
96 char buf[sizeof(kHello) + 1]; | 97 char buf[sizeof(kHello) + 1]; |
97 std::vector<int> message_fds; | 98 ScopedVector<base::ScopedFD> message_fds; |
98 ssize_t n = UnixDomainSocket::RecvMsgWithPid( | 99 ssize_t n = UnixDomainSocket::RecvMsgWithPid( |
99 fd, buf, sizeof(buf), &message_fds, sender_pid); | 100 fd, buf, sizeof(buf), &message_fds, sender_pid); |
100 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); | 101 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); |
101 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 102 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
102 CHECK_EQ(1U, message_fds.size()); | 103 CHECK_EQ(1U, message_fds.size()); |
103 base::ScopedFD message_fd(message_fds[0]); | |
104 if (write_pipe) | 104 if (write_pipe) |
105 write_pipe->swap(message_fd); | 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(UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |