Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: sandbox/linux/syscall_broker/broker_process.cc

Issue 717673002: Linux sandbox: introduce BrokerChannel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 { 31 namespace syscall_broker {
32 32
33 BrokerProcess::BrokerProcess(int denied_errno, 33 BrokerProcess::BrokerProcess(int denied_errno,
34 const std::vector<std::string>& allowed_r_files, 34 const std::vector<std::string>& allowed_r_files,
35 const std::vector<std::string>& allowed_w_files, 35 const std::vector<std::string>& allowed_w_files,
36 bool fast_check_in_client, 36 bool fast_check_in_client,
37 bool quiet_failures_for_tests) 37 bool quiet_failures_for_tests)
38 : initialized_(false), 38 : initialized_(false),
39 is_child_(false),
40 fast_check_in_client_(fast_check_in_client), 39 fast_check_in_client_(fast_check_in_client),
41 quiet_failures_for_tests_(quiet_failures_for_tests), 40 quiet_failures_for_tests_(quiet_failures_for_tests),
42 broker_pid_(-1), 41 broker_pid_(-1),
43 policy_(denied_errno, allowed_r_files, allowed_w_files), 42 policy_(denied_errno, allowed_r_files, allowed_w_files) {
44 ipc_socketpair_(-1) {
45 } 43 }
46 44
47 BrokerProcess::~BrokerProcess() { 45 BrokerProcess::~BrokerProcess() {
48 if (initialized_) { 46 if (initialized_) {
49 if (ipc_socketpair_ != -1) { 47 if (broker_client_.get()) {
50 // Closing the socket should be enough to notify the child to die, 48 // Closing the socket should be enough to notify the child to die,
51 // unless it has been duplicated. 49 // unless it has been duplicated.
52 CloseChannel(); 50 CloseChannel();
53 } 51 }
54 PCHECK(0 == kill(broker_pid_, SIGKILL)); 52 PCHECK(0 == kill(broker_pid_, SIGKILL));
55 siginfo_t process_info; 53 siginfo_t process_info;
56 // Reap the child. 54 // Reap the child.
57 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));
58 PCHECK(0 == ret); 56 PCHECK(0 == ret);
59 } 57 }
60 } 58 }
61 59
62 bool BrokerProcess::Init( 60 bool BrokerProcess::Init(
63 const base::Callback<bool(void)>& broker_process_init_callback) { 61 const base::Callback<bool(void)>& broker_process_init_callback) {
64 CHECK(!initialized_); 62 CHECK(!initialized_);
65 int socket_pair[2]; 63 BrokerChannel::EndPoint ipc_reader;
66 // Use SOCK_SEQPACKET, because we need to preserve message boundaries 64 BrokerChannel::EndPoint ipc_writer;
67 // but we also want to be notified (recvmsg should return and not block) 65 BrokerChannel::GetPair(&ipc_reader, &ipc_writer);
68 // when the connection has been broken (one of the processes died).
69 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, socket_pair)) {
70 LOG(ERROR) << "Failed to create socketpair";
71 return false;
72 }
73 66
74 #if !defined(THREAD_SANITIZER) 67 #if !defined(THREAD_SANITIZER)
75 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); 68 DCHECK_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
76 #endif 69 #endif
77 int child_pid = fork(); 70 int child_pid = fork();
78 if (child_pid == -1) { 71 if (child_pid == -1) {
79 close(socket_pair[0]);
80 close(socket_pair[1]);
81 return false; 72 return false;
82 } 73 }
83 if (child_pid) { 74 if (child_pid) {
84 // 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.
85 close(socket_pair[0]); 76 ipc_reader.reset();
86 // We should only be able to write to the IPC channel. We'll always send
87 // a new file descriptor to receive the reply on.
88 shutdown(socket_pair[1], SHUT_RD);
89 ipc_socketpair_ = socket_pair[1];
90 is_child_ = false;
91 broker_pid_ = child_pid; 77 broker_pid_ = child_pid;
92 broker_client_.reset(new BrokerClient(policy_, ipc_socketpair_, 78 broker_client_.reset(new BrokerClient(policy_, ipc_writer.Pass(),
93 fast_check_in_client_, 79 fast_check_in_client_,
94 quiet_failures_for_tests_)); 80 quiet_failures_for_tests_));
95 initialized_ = true; 81 initialized_ = true;
96 return true; 82 return true;
97 } else { 83 } else {
98 // We are the broker. 84 // We are the broker process. Make sure to close the writer's end so that
99 close(socket_pair[1]); 85 // we get notified if the client disappears.
100 // We should only be able to read from this IPC channel. We will send our 86 ipc_writer.reset();
101 // replies on a new file descriptor attached to the requests.
102 shutdown(socket_pair[0], SHUT_WR);
103 ipc_socketpair_ = socket_pair[0];
104 is_child_ = true;
105 CHECK(broker_process_init_callback.Run()); 87 CHECK(broker_process_init_callback.Run());
106 BrokerHost broker_host(policy_, ipc_socketpair_); 88 BrokerHost broker_host(policy_, ipc_reader.Pass());
107 initialized_ = true;
108 for (;;) { 89 for (;;) {
109 switch (broker_host.HandleRequest()) { 90 switch (broker_host.HandleRequest()) {
110 case BrokerHost::RequestStatus::LOST_CLIENT: 91 case BrokerHost::RequestStatus::LOST_CLIENT:
111 _exit(1); 92 _exit(1);
112 case BrokerHost::RequestStatus::SUCCESS: 93 case BrokerHost::RequestStatus::SUCCESS:
113 case BrokerHost::RequestStatus::FAILURE: 94 case BrokerHost::RequestStatus::FAILURE:
114 continue; 95 continue;
115 } 96 }
116 } 97 }
117 _exit(1); 98 _exit(1);
118 } 99 }
119 NOTREACHED(); 100 NOTREACHED();
120 } 101 }
121 102
122 void BrokerProcess::CloseChannel() { 103 void BrokerProcess::CloseChannel() {
123 CHECK_NE(-1, ipc_socketpair_); 104 broker_client_.reset();
124 PCHECK(0 == IGNORE_EINTR(close(ipc_socketpair_)));
125 ipc_socketpair_ = -1;
126 } 105 }
127 106
128 int BrokerProcess::Access(const char* pathname, int mode) const { 107 int BrokerProcess::Access(const char* pathname, int mode) const {
129 RAW_CHECK(initialized_); 108 RAW_CHECK(initialized_);
130 return broker_client_->Access(pathname, mode); 109 return broker_client_->Access(pathname, mode);
131 } 110 }
132 111
133 int BrokerProcess::Open(const char* pathname, int flags) const { 112 int BrokerProcess::Open(const char* pathname, int flags) const {
134 RAW_CHECK(initialized_); 113 RAW_CHECK(initialized_);
135 return broker_client_->Open(pathname, flags); 114 return broker_client_->Open(pathname, flags);
136 } 115 }
137 116
138 } // namespace syscall_broker 117 } // namespace syscall_broker
139 118
140 } // namespace sandbox. 119 } // namespace sandbox.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698