| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sandbox/linux/syscall_broker/broker_process.h" | 5 #include "sandbox/linux/syscall_broker/broker_process.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <signal.h> | 8 #include <signal.h> |
| 9 #include <sys/socket.h> | |
| 10 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 11 #include <sys/syscall.h> | 10 #include <sys/syscall.h> |
| 12 #include <sys/types.h> | 11 #include <sys/types.h> |
| 13 #include <sys/wait.h> | 12 #include <sys/wait.h> |
| 14 #include <unistd.h> | 13 #include <unistd.h> |
| 15 | 14 |
| 16 #include <algorithm> | 15 #include <algorithm> |
| 17 #include <string> | 16 #include <string> |
| 18 #include <vector> | 17 #include <vector> |
| 19 | 18 |
| 20 #include "base/callback.h" | 19 #include "base/callback.h" |
| 21 #include "base/logging.h" | 20 #include "base/logging.h" |
| 22 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 23 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
| 24 #include "base/process/process_metrics.h" | 23 #include "base/process/process_metrics.h" |
| 25 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "sandbox/linux/syscall_broker/broker_channel.h" |
| 26 #include "sandbox/linux/syscall_broker/broker_client.h" | 26 #include "sandbox/linux/syscall_broker/broker_client.h" |
| 27 #include "sandbox/linux/syscall_broker/broker_host.h" | 27 #include "sandbox/linux/syscall_broker/broker_host.h" |
| 28 | 28 |
| 29 namespace sandbox { | 29 namespace sandbox { |
| 30 | 30 |
| 31 namespace syscall_broker { |
| 32 |
| 31 BrokerProcess::BrokerProcess(int denied_errno, | 33 BrokerProcess::BrokerProcess(int denied_errno, |
| 32 const std::vector<std::string>& allowed_r_files, | 34 const std::vector<std::string>& allowed_r_files, |
| 33 const std::vector<std::string>& allowed_w_files, | 35 const std::vector<std::string>& allowed_w_files, |
| 34 bool fast_check_in_client, | 36 bool fast_check_in_client, |
| 35 bool quiet_failures_for_tests) | 37 bool quiet_failures_for_tests) |
| 36 : initialized_(false), | 38 : initialized_(false), |
| 37 is_child_(false), | |
| 38 fast_check_in_client_(fast_check_in_client), | 39 fast_check_in_client_(fast_check_in_client), |
| 39 quiet_failures_for_tests_(quiet_failures_for_tests), | 40 quiet_failures_for_tests_(quiet_failures_for_tests), |
| 40 broker_pid_(-1), | 41 broker_pid_(-1), |
| 41 policy_(denied_errno, allowed_r_files, allowed_w_files), | 42 policy_(denied_errno, allowed_r_files, allowed_w_files) { |
| 42 ipc_socketpair_(-1) { | |
| 43 } | 43 } |
| 44 | 44 |
| 45 BrokerProcess::~BrokerProcess() { | 45 BrokerProcess::~BrokerProcess() { |
| 46 if (initialized_ && ipc_socketpair_ != -1) { | 46 if (initialized_) { |
| 47 // Closing the socket should be enough to notify the child to die, | 47 if (broker_client_.get()) { |
| 48 // unless it has been duplicated. | 48 // Closing the socket should be enough to notify the child to die, |
| 49 PCHECK(0 == IGNORE_EINTR(close(ipc_socketpair_))); | 49 // unless it has been duplicated. |
| 50 CloseChannel(); |
| 51 } |
| 50 PCHECK(0 == kill(broker_pid_, SIGKILL)); | 52 PCHECK(0 == kill(broker_pid_, SIGKILL)); |
| 51 siginfo_t process_info; | 53 siginfo_t process_info; |
| 52 // Reap the child. | 54 // Reap the child. |
| 53 int ret = HANDLE_EINTR(waitid(P_PID, broker_pid_, &process_info, WEXITED)); | 55 int ret = HANDLE_EINTR(waitid(P_PID, broker_pid_, &process_info, WEXITED)); |
| 54 PCHECK(0 == ret); | 56 PCHECK(0 == ret); |
| 55 } | 57 } |
| 56 } | 58 } |
| 57 | 59 |
| 58 bool BrokerProcess::Init( | 60 bool BrokerProcess::Init( |
| 59 const base::Callback<bool(void)>& broker_process_init_callback) { | 61 const base::Callback<bool(void)>& broker_process_init_callback) { |
| 60 CHECK(!initialized_); | 62 CHECK(!initialized_); |
| 61 int socket_pair[2]; | 63 BrokerChannel::EndPoint ipc_reader; |
| 62 // Use SOCK_SEQPACKET, because we need to preserve message boundaries | 64 BrokerChannel::EndPoint ipc_writer; |
| 63 // but we also want to be notified (recvmsg should return and not block) | 65 BrokerChannel::CreatePair(&ipc_reader, &ipc_writer); |
| 64 // when the connection has been broken (one of the processes died). | |
| 65 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, socket_pair)) { | |
| 66 LOG(ERROR) << "Failed to create socketpair"; | |
| 67 return false; | |
| 68 } | |
| 69 | 66 |
| 70 #if !defined(THREAD_SANITIZER) | 67 #if !defined(THREAD_SANITIZER) |
| 71 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); | 68 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); |
| 72 #endif | 69 #endif |
| 73 int child_pid = fork(); | 70 int child_pid = fork(); |
| 74 if (child_pid == -1) { | 71 if (child_pid == -1) { |
| 75 close(socket_pair[0]); | |
| 76 close(socket_pair[1]); | |
| 77 return false; | 72 return false; |
| 78 } | 73 } |
| 79 if (child_pid) { | 74 if (child_pid) { |
| 80 // We are the parent and we have just forked our broker process. | 75 // We are the parent and we have just forked our broker process. |
| 81 close(socket_pair[0]); | 76 ipc_reader.reset(); |
| 82 // We should only be able to write to the IPC channel. We'll always send | |
| 83 // a new file descriptor to receive the reply on. | |
| 84 shutdown(socket_pair[1], SHUT_RD); | |
| 85 ipc_socketpair_ = socket_pair[1]; | |
| 86 is_child_ = false; | |
| 87 broker_pid_ = child_pid; | 77 broker_pid_ = child_pid; |
| 88 broker_client_.reset( | 78 broker_client_.reset(new BrokerClient(policy_, ipc_writer.Pass(), |
| 89 new syscall_broker::BrokerClient(policy_, | 79 fast_check_in_client_, |
| 90 ipc_socketpair_, | 80 quiet_failures_for_tests_)); |
| 91 fast_check_in_client_, | |
| 92 quiet_failures_for_tests_)); | |
| 93 initialized_ = true; | 81 initialized_ = true; |
| 94 return true; | 82 return true; |
| 95 } else { | 83 } else { |
| 96 // We are the broker. | 84 // We are the broker process. Make sure to close the writer's end so that |
| 97 close(socket_pair[1]); | 85 // we get notified if the client disappears. |
| 98 // We should only be able to read from this IPC channel. We will send our | 86 ipc_writer.reset(); |
| 99 // replies on a new file descriptor attached to the requests. | |
| 100 shutdown(socket_pair[0], SHUT_WR); | |
| 101 ipc_socketpair_ = socket_pair[0]; | |
| 102 is_child_ = true; | |
| 103 CHECK(broker_process_init_callback.Run()); | 87 CHECK(broker_process_init_callback.Run()); |
| 104 syscall_broker::BrokerHost broker_host(policy_, ipc_socketpair_); | 88 BrokerHost broker_host(policy_, ipc_reader.Pass()); |
| 105 initialized_ = true; | |
| 106 for (;;) { | 89 for (;;) { |
| 107 broker_host.HandleRequest(); | 90 switch (broker_host.HandleRequest()) { |
| 91 case BrokerHost::RequestStatus::LOST_CLIENT: |
| 92 _exit(1); |
| 93 case BrokerHost::RequestStatus::SUCCESS: |
| 94 case BrokerHost::RequestStatus::FAILURE: |
| 95 continue; |
| 96 } |
| 108 } | 97 } |
| 109 _exit(1); | 98 _exit(1); |
| 110 } | 99 } |
| 111 NOTREACHED(); | 100 NOTREACHED(); |
| 112 } | 101 } |
| 113 | 102 |
| 103 void BrokerProcess::CloseChannel() { |
| 104 broker_client_.reset(); |
| 105 } |
| 106 |
| 114 int BrokerProcess::Access(const char* pathname, int mode) const { | 107 int BrokerProcess::Access(const char* pathname, int mode) const { |
| 115 RAW_CHECK(initialized_); | 108 RAW_CHECK(initialized_); |
| 116 return broker_client_->Access(pathname, mode); | 109 return broker_client_->Access(pathname, mode); |
| 117 } | 110 } |
| 118 | 111 |
| 119 int BrokerProcess::Open(const char* pathname, int flags) const { | 112 int BrokerProcess::Open(const char* pathname, int flags) const { |
| 120 RAW_CHECK(initialized_); | 113 RAW_CHECK(initialized_); |
| 121 return broker_client_->Open(pathname, flags); | 114 return broker_client_->Open(pathname, flags); |
| 122 } | 115 } |
| 123 | 116 |
| 117 } // namespace syscall_broker |
| 118 |
| 124 } // namespace sandbox. | 119 } // namespace sandbox. |
| OLD | NEW |