OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "sandbox/linux/syscall_broker/broker_process.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <signal.h> |
| 9 #include <sys/socket.h> |
| 10 #include <sys/stat.h> |
| 11 #include <sys/syscall.h> |
| 12 #include <sys/types.h> |
| 13 #include <sys/wait.h> |
| 14 #include <unistd.h> |
| 15 |
| 16 #include <algorithm> |
| 17 #include <string> |
| 18 #include <vector> |
| 19 |
| 20 #include "base/callback.h" |
| 21 #include "base/logging.h" |
| 22 #include "base/memory/scoped_ptr.h" |
| 23 #include "base/posix/eintr_wrapper.h" |
| 24 #include "base/process/process_metrics.h" |
| 25 #include "build/build_config.h" |
| 26 #include "sandbox/linux/syscall_broker/broker_client.h" |
| 27 #include "sandbox/linux/syscall_broker/broker_host.h" |
| 28 |
| 29 namespace sandbox { |
| 30 |
| 31 BrokerProcess::BrokerProcess(int denied_errno, |
| 32 const std::vector<std::string>& allowed_r_files, |
| 33 const std::vector<std::string>& allowed_w_files, |
| 34 bool fast_check_in_client, |
| 35 bool quiet_failures_for_tests) |
| 36 : initialized_(false), |
| 37 is_child_(false), |
| 38 fast_check_in_client_(fast_check_in_client), |
| 39 quiet_failures_for_tests_(quiet_failures_for_tests), |
| 40 broker_pid_(-1), |
| 41 policy_(denied_errno, allowed_r_files, allowed_w_files), |
| 42 ipc_socketpair_(-1) { |
| 43 } |
| 44 |
| 45 BrokerProcess::~BrokerProcess() { |
| 46 if (initialized_ && ipc_socketpair_ != -1) { |
| 47 // Closing the socket should be enough to notify the child to die, |
| 48 // unless it has been duplicated. |
| 49 PCHECK(0 == IGNORE_EINTR(close(ipc_socketpair_))); |
| 50 PCHECK(0 == kill(broker_pid_, SIGKILL)); |
| 51 siginfo_t process_info; |
| 52 // Reap the child. |
| 53 int ret = HANDLE_EINTR(waitid(P_PID, broker_pid_, &process_info, WEXITED)); |
| 54 PCHECK(0 == ret); |
| 55 } |
| 56 } |
| 57 |
| 58 bool BrokerProcess::Init( |
| 59 const base::Callback<bool(void)>& broker_process_init_callback) { |
| 60 CHECK(!initialized_); |
| 61 int socket_pair[2]; |
| 62 // Use SOCK_SEQPACKET, because we need to preserve message boundaries |
| 63 // but we also want to be notified (recvmsg should return and not block) |
| 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 |
| 70 #if !defined(THREAD_SANITIZER) |
| 71 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); |
| 72 #endif |
| 73 int child_pid = fork(); |
| 74 if (child_pid == -1) { |
| 75 close(socket_pair[0]); |
| 76 close(socket_pair[1]); |
| 77 return false; |
| 78 } |
| 79 if (child_pid) { |
| 80 // We are the parent and we have just forked our broker process. |
| 81 close(socket_pair[0]); |
| 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; |
| 88 broker_client_.reset( |
| 89 new syscall_broker::BrokerClient(policy_, |
| 90 ipc_socketpair_, |
| 91 fast_check_in_client_, |
| 92 quiet_failures_for_tests_)); |
| 93 initialized_ = true; |
| 94 return true; |
| 95 } else { |
| 96 // We are the broker. |
| 97 close(socket_pair[1]); |
| 98 // We should only be able to read from this IPC channel. We will send our |
| 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()); |
| 104 syscall_broker::BrokerHost broker_host(policy_, ipc_socketpair_); |
| 105 initialized_ = true; |
| 106 for (;;) { |
| 107 broker_host.HandleRequest(); |
| 108 } |
| 109 _exit(1); |
| 110 } |
| 111 NOTREACHED(); |
| 112 } |
| 113 |
| 114 int BrokerProcess::Access(const char* pathname, int mode) const { |
| 115 RAW_CHECK(initialized_); |
| 116 return broker_client_->Access(pathname, mode); |
| 117 } |
| 118 |
| 119 int BrokerProcess::Open(const char* pathname, int flags) const { |
| 120 RAW_CHECK(initialized_); |
| 121 return broker_client_->Open(pathname, flags); |
| 122 } |
| 123 |
| 124 } // namespace sandbox. |
OLD | NEW |