Chromium Code Reviews| 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" | |
| 18 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/posix/unix_domain_socket_linux.h" | 18 #include "base/posix/unix_domain_socket_linux.h" |
| 20 #include "base/process/process.h" | 19 #include "base/process/process.h" |
| 21 #include "sandbox/linux/services/syscall_wrappers.h" | 20 #include "sandbox/linux/services/syscall_wrappers.h" |
| 22 #include "sandbox/linux/tests/unit_tests.h" | 21 #include "sandbox/linux/tests/unit_tests.h" |
| 23 | 22 |
| 24 // Additional tests for base's UnixDomainSocket to make sure it behaves | 23 // Additional tests for base's UnixDomainSocket to make sure it behaves |
| 25 // correctly in the presence of sandboxing functionality (e.g., receiving | 24 // correctly in the presence of sandboxing functionality (e.g., receiving |
| 26 // PIDs across namespaces). | 25 // PIDs across namespaces). |
| 27 | 26 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 // the descriptor is closed. | 87 // the descriptor is closed. |
| 89 // (Implementation details: SendHello allocates a new pipe, sends us the writing | 88 // (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 | 89 // end alongside the "hello" message, and then blocks until we close the writing |
| 91 // end of the pipe.) | 90 // end of the pipe.) |
| 92 void RecvHello(int fd, | 91 void RecvHello(int fd, |
| 93 base::ProcessId* sender_pid, | 92 base::ProcessId* sender_pid, |
| 94 base::ScopedFD* write_pipe = NULL) { | 93 base::ScopedFD* write_pipe = NULL) { |
| 95 // Extra receiving buffer space to make sure we really received only | 94 // 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. | 95 // sizeof(kHello) bytes and it wasn't just truncated to fit the buffer. |
| 97 char buf[sizeof(kHello) + 1]; | 96 char buf[sizeof(kHello) + 1]; |
| 98 ScopedVector<base::ScopedFD> message_fds; | 97 std::vector<base::ScopedFD> message_fds; |
| 99 ssize_t n = base::UnixDomainSocket::RecvMsgWithPid( | 98 ssize_t n = base::UnixDomainSocket::RecvMsgWithPid( |
| 100 fd, buf, sizeof(buf), &message_fds, sender_pid); | 99 fd, buf, sizeof(buf), &message_fds, sender_pid); |
| 101 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); | 100 CHECK_EQ(sizeof(kHello), static_cast<size_t>(n)); |
| 102 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); | 101 CHECK_EQ(0, memcmp(buf, kHello, sizeof(kHello))); |
| 103 CHECK_EQ(1U, message_fds.size()); | 102 CHECK_EQ(1U, message_fds.size()); |
| 104 if (write_pipe) | 103 if (write_pipe) |
| 105 write_pipe->swap(*message_fds[0]); | 104 write_pipe->swap(message_fds.at(0)); |
|
danakj
2015/12/08 23:44:48
more at() paranoia? or wanna drop a comment why at
mdempsky
2015/12/09 00:45:37
Reverted.
| |
| 106 } | 105 } |
| 107 | 106 |
| 108 // Check that receiving PIDs works across a fork(). | 107 // Check that receiving PIDs works across a fork(). |
| 109 SANDBOX_TEST(UnixDomainSocketTest, Fork) { | 108 SANDBOX_TEST(UnixDomainSocketTest, Fork) { |
| 110 int fds[2]; | 109 int fds[2]; |
| 111 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); | 110 CHECK_EQ(0, socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds)); |
| 112 base::ScopedFD recv_sock(fds[0]); | 111 base::ScopedFD recv_sock(fds[0]); |
| 113 base::ScopedFD send_sock(fds[1]); | 112 base::ScopedFD send_sock(fds[1]); |
| 114 | 113 |
| 115 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 114 CHECK(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 | 257 |
| 259 // Parent process. | 258 // Parent process. |
| 260 recv_sock.reset(); | 259 recv_sock.reset(); |
| 261 SendHello(send_sock.get()); | 260 SendHello(send_sock.get()); |
| 262 WaitForExit(pid); | 261 WaitForExit(pid); |
| 263 } | 262 } |
| 264 | 263 |
| 265 } // namespace | 264 } // namespace |
| 266 | 265 |
| 267 } // namespace sandbox | 266 } // namespace sandbox |
| OLD | NEW |