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

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

Issue 11557025: Linux sandbox: add a new low-level broker process mechanism. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Jorge's comments. Created 8 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 #include "sandbox/linux/services/broker_process.h"
2
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12
13 #include "base/logging.h"
14 #include "base/pickle.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/posix/unix_domain_socket.h"
17
18 namespace {
19
20 static const int kCommandOpen = 'O';
21 static const size_t kMaxMessageLength = 4096;
22
23 // Check whether |requested_filename| is in |allowed_file_names|.
24 // For paranoia, if file_to_open is not NULL, we will return the matching
25 // string from the white list.
26 // Even if an attacker managed to fool the string comparison mechanism, we
27 // would not open an attacker-controlled file name.
28 bool GetFileNameInWhitelist(const std::vector<std::string>& allowed_file_names,
29 const std::string& requested_filename,
30 std::string* file_to_open) {
Markus (顧孟勤) 2012/12/13 09:48:52 Do you want to set "file_to_open" to NULL before y
jln (very slow on Chromium) 2012/12/13 19:27:17 Good point. I'll test that it's empty.
31 std::vector<std::string>::const_iterator it;
32 it = std::find(allowed_file_names.begin(), allowed_file_names.end(),
33 requested_filename);
34 if (it < allowed_file_names.end()) { // requested_filename was found?
35 if (file_to_open)
36 *file_to_open = *it;
37 return true;
38 }
39 return false;
40 }
41
42 bool HandleOpenRequest(int fd,
43 const std::vector<std::string>& allowed_file_names,
44 const Pickle& read_pickle, PickleIterator iter) {
45 std::string requested_filename;
46 int flags = 0;
47 if (!read_pickle.ReadString(&iter, &requested_filename) ||
48 !read_pickle.ReadInt(&iter, &flags)) {
49 return -1;
50 }
51
52 Pickle write_pickle;
53 std::vector<int> opened_files;
54 std::string file_to_open;
55 bool file_is_in_whitelist = GetFileNameInWhitelist(allowed_file_names,
56 requested_filename,
57 &file_to_open);
58 if (file_is_in_whitelist && // requested_filename is in the whitelist?
59 flags == O_RDONLY) {
60 int opened_fd = open(file_to_open.c_str(), O_RDONLY);
61 if (opened_fd < 0) {
62 write_pickle.WriteInt(-errno);
63 } else {
64 // Success.
65 opened_files.push_back(opened_fd);
66 write_pickle.WriteInt(0);
67 }
68 } else {
69 write_pickle.WriteInt(-EPERM);
70 }
71
72 CHECK_LE(write_pickle.size(), kMaxMessageLength);
73 ssize_t sent = UnixDomainSocket::SendMsg(fd, write_pickle.data(),
74 write_pickle.size(), opened_files);
75
76 // Close anything we have opened in this process.
77 for (std::vector<int>::iterator it = opened_files.begin();
78 it < opened_files.end(); ++it) {
79 (void) HANDLE_EINTR(close(*it));
80 }
81
82 if (sent <= 0) {
83 LOG(ERROR) << "Could not send IPC reply";
84 return false;
85 }
86 return true;
87 }
88
89 // Handle a request on the IPC channel FD.
90 // A request should have a file descriptor attached on which we will reply and
91 // that we will then close.
92 // A request should start with an int that will be used as the command type.
93 bool HandleRequest(int fd,
94 const std::vector<std::string>& allowed_file_names) {
95
96 std::vector<int> fds;
97 char buf[kMaxMessageLength];
98 errno = 0;
99 const ssize_t msg_len = UnixDomainSocket::RecvMsg(fd, buf,
100 sizeof(buf), &fds);
Markus (顧孟勤) 2012/12/13 09:48:52 What does RecvMsg() do for truncated messages? The
jln (very slow on Chromium) 2012/12/13 19:27:17 It does check msg_flags for MSG_TRUNC and MSG_CTRU
101
102 if (msg_len == 0 || (msg_len == -1 && errno == ECONNRESET)) {
103 // EOF from our parent, or our parent died, we should die.
104 _exit(0);
105 }
106
107 // The parent should send exactly one file descriptor, on which we
108 // will write the reply.
109 if (msg_len < 0 || fds.size() != 1 || fds.at(0) < 0) {
Markus (顧孟勤) 2012/12/13 09:48:52 Instead of saying fds.at(0) all over the place, I
jln (very slow on Chromium) 2012/12/13 19:27:17 Done.
110 PLOG(ERROR) << "Error reading message from the client";
111 return false;
112 }
113
114 Pickle pickle(buf, msg_len);
115 PickleIterator iter(pickle);
116 int command_type;
117 if (pickle.ReadInt(&iter, &command_type)) {
118 bool r = false;
119 // Go through all the possible IPC messages.
120 switch (command_type) {
121 case kCommandOpen:
122 // We reply on the file descriptor sent to us via the IPC channel.
123 r = HandleOpenRequest(fds.at(0), allowed_file_names,
124 pickle, iter);
125 // Close the temporary reply channel.
126 (void) HANDLE_EINTR(close(fds.at(0)));
127 return r;
128 default:
129 NOTREACHED();
130 return false;
131 }
132 }
133
134 LOG(ERROR) << "Error parsing IPC request";
135 return false;
136 }
137
138 } // namespace
139
140 namespace sandbox {
141
142 BrokerProcess::BrokerProcess(const std::vector<std::string>& allowed_file_names,
143 bool fast_check_in_client,
144 bool quiet_failures_for_tests)
145 : initialized_(false),
146 is_child_(false),
147 fast_check_in_client_(fast_check_in_client),
148 quiet_failures_for_tests_(quiet_failures_for_tests),
149 broker_pid_(-1),
150 allowed_file_names_(allowed_file_names),
151 ipc_socketpair_(-1) {
152 }
153
154 BrokerProcess::~BrokerProcess() {
155 if (initialized_ && ipc_socketpair_ != -1) {
156 void (HANDLE_EINTR(close(ipc_socketpair_)));
157 }
158 }
159
160 bool BrokerProcess::Init(void* sandbox_callback) {
161 CHECK(!initialized_);
162 CHECK_EQ(sandbox_callback, (void*) NULL) <<
163 "sandbox_callback is not implemented";
164 int socket_pair[2];
165 if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair)) {
Markus (顧孟勤) 2012/12/13 09:48:52 You need to know about package boundaries don't yo
jln (very slow on Chromium) 2012/12/13 19:27:17 Very good point, thanks! SOCK_DGRAM wouldn't work,
166 LOG(ERROR) << "Failed to create socketpair";
167 return false;
168 }
169
170 int child_pid = fork();
171 if (child_pid == -1) {
172 (void) HANDLE_EINTR(close(socket_pair[0]));
173 (void) HANDLE_EINTR(close(socket_pair[1]));
174 return false;
175 }
176 if (child_pid) {
177 // We are the parent and we have just forked our broker process.
178 (void) HANDLE_EINTR(close(socket_pair[1]));
179 // We should only be able to write to the IPC channel. We'll always send
180 // a new file descriptor to receive the reply on.
181 shutdown(socket_pair[0], SHUT_RD);
Markus (顧孟勤) 2012/12/13 09:48:52 While it is somewhat arbitrary, your choice of ind
jln (very slow on Chromium) 2012/12/13 19:27:17 Done.
182 ipc_socketpair_ = socket_pair[0];
183 initialized_ = true;
Markus (顧孟勤) 2012/12/13 09:48:52 Why do we set initialized_ to true here? We later
jln (very slow on Chromium) 2012/12/13 19:27:17 Done.
184 is_child_ = false;
185 broker_pid_ = child_pid;
186
187 } else {
188 // We are the broker.
189 (void) HANDLE_EINTR(close(socket_pair[0]));
190 // We should only be able to read from this IPC channel. We will send our
191 // replies on a new file descriptor attached to the requests.
192 shutdown(socket_pair[1], SHUT_WR);
193 ipc_socketpair_ = socket_pair[1];
194 initialized_ = true;
195 is_child_ = true;
196 // TODO(jln): activate a sandbox here.
197 for (;;) {
198 HandleRequest(ipc_socketpair_, allowed_file_names_);
199 }
200 }
201
202 initialized_ = true;
203 return true;
204 }
205
206 // This function needs to be async signal safe.
207 int BrokerProcess::Open(const char* pathname, int flags) const {
208 RAW_CHECK(initialized_); // async signal safe CHECK().
209 // There is no point in forwarding a request that we know will be denied.
210 // Of course, the real security check needs to be on the other side of the
211 // IPC.
212 if (fast_check_in_client_) {
213 if(!GetFileNameInWhitelist(allowed_file_names_, pathname, NULL) ||
214 flags != O_RDONLY) {
Markus (顧孟勤) 2012/12/13 09:48:52 Can you add some comments that the handling of fla
jln (very slow on Chromium) 2012/12/13 19:27:17 I'll send a next iteration soon that also supports
215 return -EPERM;
216 }
217 }
218
219 Pickle write_pickle;
220 write_pickle.WriteInt(kCommandOpen);
221 write_pickle.WriteString(pathname);
222 write_pickle.WriteInt(flags);
223 CHECK_LE(write_pickle.size(), kMaxMessageLength);
224
225 int returned_fd = -1;
226 uint8_t reply_buf[kMaxMessageLength];
227 // Send a request (in write_pickle) as well that will include a new
228 // temporary socketpair (created internally by SendRecvMsg()).
229 // Then read the reply on this new socketpair in reply_buf and put an
230 // eventual attached file descriptor in |returned_fd|.
231 // TODO(jln): this API needs some rewriting and documentation.
232 ssize_t msg_len = UnixDomainSocket::SendRecvMsg(ipc_socketpair_,
233 reply_buf,
234 sizeof(reply_buf),
235 &returned_fd,
236 write_pickle);
237 if (msg_len <= 0) {
238 if (!quiet_failures_for_tests_)
239 RAW_LOG(ERROR, "Could not make request to broker process");
240 return -ENOMEM;
241 }
242
243 Pickle read_pickle(reinterpret_cast<char*>(reply_buf), msg_len);
244 PickleIterator iter(read_pickle);
245 int return_value = -1;
246 // Now deserialize the return value and eventually return the file
247 // descriptor.
248 if (read_pickle.ReadInt(&iter, &return_value)) {
249 if (return_value < 0) {
250 CHECK_EQ(returned_fd, -1);
251 return return_value;
252 } else {
253 // We have a real file descriptor to return.
254 CHECK_GE(returned_fd, 0);
255 return returned_fd;
256 }
257 } else {
258 RAW_LOG(ERROR, "Could not read pickle");
259 return -1;
260 }
261 }
262
263 } // namespace sandbox.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698