| 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 <utility> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/files/scoped_file.h" | 16 #include "base/files/scoped_file.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/memory/scoped_vector.h" | |
| 18 #include "base/posix/eintr_wrapper.h" | 18 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/posix/unix_domain_socket_linux.h" | 19 #include "base/posix/unix_domain_socket_linux.h" |
| 20 #include "base/process/process.h" | 20 #include "base/process/process.h" |
| 21 #include "sandbox/linux/services/syscall_wrappers.h" | 21 #include "sandbox/linux/services/syscall_wrappers.h" |
| 22 #include "sandbox/linux/tests/unit_tests.h" | 22 #include "sandbox/linux/tests/unit_tests.h" |
| 23 | 23 |
| 24 // Additional tests for base's UnixDomainSocket to make sure it behaves | 24 // Additional tests for base's UnixDomainSocket to make sure it behaves |
| 25 // correctly in the presence of sandboxing functionality (e.g., receiving | 25 // correctly in the presence of sandboxing functionality (e.g., receiving |
| 26 // PIDs across namespaces). | 26 // PIDs across namespaces). |
| 27 | 27 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 std::vector<base::ScopedFD> message_fds; |
| 99 ssize_t n = base::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 std::swap(*write_pipe, 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(base::UnixDomainSocket::EnableReceiveProcessId(recv_sock.get())); | 115 CHECK(base::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 |