OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/renderer_host/render_sandbox_host_linux.h" | 5 #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 #include <sys/uio.h> | 9 #include <sys/uio.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
11 #include <sys/poll.h> | 11 #include <sys/poll.h> |
12 #include <time.h> | 12 #include <time.h> |
13 | 13 |
14 #include <vector> | |
15 | |
16 #include "base/command_line.h" | |
17 #include "base/eintr_wrapper.h" | 14 #include "base/eintr_wrapper.h" |
18 #include "base/linux_util.h" | 15 #include "base/platform_file.h" |
| 16 #include "base/process_util.h" |
| 17 #include "base/logging.h" |
| 18 #include "base/message_loop.h" |
19 #include "base/pickle.h" | 19 #include "base/pickle.h" |
20 #include "base/process_util.h" | |
21 #include "base/scoped_ptr.h" | |
22 #include "base/string_util.h" | 20 #include "base/string_util.h" |
23 #include "base/unix_domain_socket_posix.h" | 21 #include "base/unix_domain_socket_posix.h" |
24 #include "chrome/common/sandbox_methods_linux.h" | 22 #include "chrome/common/sandbox_methods_linux.h" |
25 #include "webkit/api/public/gtk/WebFontInfo.h" | 23 #include "webkit/api/public/gtk/WebFontInfo.h" |
26 | 24 |
27 #include "SkFontHost_fontconfig_direct.h" | 25 #include "SkFontHost_fontconfig_direct.h" |
28 #include "SkFontHost_fontconfig_ipc.h" | 26 #include "SkFontHost_fontconfig_ipc.h" |
29 | 27 |
30 using WebKit::WebCString; | 28 using WebKit::WebCString; |
31 using WebKit::WebFontInfo; | 29 using WebKit::WebFontInfo; |
32 using WebKit::WebUChar; | 30 using WebKit::WebUChar; |
33 | 31 |
34 // http://code.google.com/p/chromium/wiki/LinuxSandboxIPC | 32 // http://code.google.com/p/chromium/wiki/LinuxSandboxIPC |
35 | 33 |
36 // BEWARE: code in this file run across *processes* (not just threads). | 34 // BEWARE: code in this file run across *processes* (not just threads). |
37 | 35 |
38 // This code runs in a child process | 36 // This code runs in a child process |
39 class SandboxIPCProcess { | 37 class SandboxIPCProcess { |
40 public: | 38 public: |
41 // lifeline_fd: this is the read end of a pipe which the browser process | 39 // lifeline_fd: this is the read end of a pipe which the browser process |
42 // holds the other end of. If the browser process dies, its descriptors are | 40 // holds the other end of. If the browser process dies, its descriptors are |
43 // closed and we will noticed an EOF on the pipe. That's our signal to exit. | 41 // closed and we will noticed an EOF on the pipe. That's our signal to exit. |
44 // browser_socket: the browser's end of the sandbox IPC socketpair. From the | 42 // browser_socket: the browser's end of the sandbox IPC socketpair. From the |
45 // point of view of the renderer, it's talking to the browser but this | 43 // point of view of the renderer, it's talking to the browser but this |
46 // object actually services the requests. | 44 // object actually services the requests. |
47 // sandbox_cmd: the path of the sandbox executable | 45 SandboxIPCProcess(int lifeline_fd, int browser_socket) |
48 SandboxIPCProcess(int lifeline_fd, int browser_socket, | |
49 std::string sandbox_cmd) | |
50 : lifeline_fd_(lifeline_fd), | 46 : lifeline_fd_(lifeline_fd), |
51 browser_socket_(browser_socket), | 47 browser_socket_(browser_socket), |
52 font_config_(new FontConfigDirect()) { | 48 font_config_(new FontConfigDirect()) { |
53 base::InjectiveMultimap multimap; | 49 base::InjectiveMultimap multimap; |
54 multimap.push_back(base::InjectionArc(0, lifeline_fd, false)); | 50 multimap.push_back(base::InjectionArc(0, lifeline_fd, false)); |
55 multimap.push_back(base::InjectionArc(0, browser_socket, false)); | 51 multimap.push_back(base::InjectionArc(0, browser_socket, false)); |
56 | 52 |
57 base::CloseSuperfluousFds(multimap); | 53 base::CloseSuperfluousFds(multimap); |
58 | |
59 if (!sandbox_cmd.empty()) { | |
60 sandbox_cmd_.push_back(sandbox_cmd); | |
61 sandbox_cmd_.push_back(base::kFindInodeSwitch); | |
62 } | |
63 } | 54 } |
64 | 55 |
65 void Run() { | 56 void Run() { |
66 struct pollfd pfds[2]; | 57 struct pollfd pfds[2]; |
67 pfds[0].fd = lifeline_fd_; | 58 pfds[0].fd = lifeline_fd_; |
68 pfds[0].events = POLLIN; | 59 pfds[0].events = POLLIN; |
69 pfds[1].fd = browser_socket_; | 60 pfds[1].fd = browser_socket_; |
70 pfds[1].events = POLLIN; | 61 pfds[1].events = POLLIN; |
71 | 62 |
72 bool failed_polls = 0; | 63 bool failed_polls = 0; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 goto error; | 107 goto error; |
117 | 108 |
118 if (kind == FontConfigIPC::METHOD_MATCH) { | 109 if (kind == FontConfigIPC::METHOD_MATCH) { |
119 HandleFontMatchRequest(fd, pickle, iter, fds); | 110 HandleFontMatchRequest(fd, pickle, iter, fds); |
120 } else if (kind == FontConfigIPC::METHOD_OPEN) { | 111 } else if (kind == FontConfigIPC::METHOD_OPEN) { |
121 HandleFontOpenRequest(fd, pickle, iter, fds); | 112 HandleFontOpenRequest(fd, pickle, iter, fds); |
122 } else if (kind == LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS) { | 113 } else if (kind == LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS) { |
123 HandleGetFontFamilyForChars(fd, pickle, iter, fds); | 114 HandleGetFontFamilyForChars(fd, pickle, iter, fds); |
124 } else if (kind == LinuxSandbox::METHOD_LOCALTIME) { | 115 } else if (kind == LinuxSandbox::METHOD_LOCALTIME) { |
125 HandleLocaltime(fd, pickle, iter, fds); | 116 HandleLocaltime(fd, pickle, iter, fds); |
126 } else if (kind == LinuxSandbox::METHOD_GET_CHILD_WITH_INODE) { | |
127 HandleGetChildWithInode(fd, pickle, iter, fds); | |
128 } | 117 } |
129 | 118 |
130 error: | 119 error: |
131 for (std::vector<int>::const_iterator | 120 for (std::vector<int>::const_iterator |
132 i = fds.begin(); i != fds.end(); ++i) { | 121 i = fds.begin(); i != fds.end(); ++i) { |
133 close(*i); | 122 close(*i); |
134 } | 123 } |
135 } | 124 } |
136 | 125 |
137 void HandleFontMatchRequest(int fd, const Pickle& pickle, void* iter, | 126 void HandleFontMatchRequest(int fd, Pickle& pickle, void* iter, |
138 std::vector<int>& fds) { | 127 std::vector<int>& fds) { |
139 bool fileid_valid; | 128 bool fileid_valid; |
140 uint32_t fileid; | 129 uint32_t fileid; |
141 bool is_bold, is_italic; | 130 bool is_bold, is_italic; |
142 std::string family; | 131 std::string family; |
143 | 132 |
144 if (!pickle.ReadBool(&iter, &fileid_valid)) | 133 if (!pickle.ReadBool(&iter, &fileid_valid)) |
145 return; | 134 return; |
146 if (fileid_valid) { | 135 if (fileid_valid) { |
147 if (!pickle.ReadUInt32(&iter, &fileid)) | 136 if (!pickle.ReadUInt32(&iter, &fileid)) |
(...skipping 18 matching lines...) Expand all Loading... |
166 } else { | 155 } else { |
167 reply.WriteBool(true); | 156 reply.WriteBool(true); |
168 reply.WriteUInt32(result_fileid); | 157 reply.WriteUInt32(result_fileid); |
169 reply.WriteString(result_family); | 158 reply.WriteString(result_family); |
170 reply.WriteBool(is_bold); | 159 reply.WriteBool(is_bold); |
171 reply.WriteBool(is_italic); | 160 reply.WriteBool(is_italic); |
172 } | 161 } |
173 SendRendererReply(fds, reply, -1); | 162 SendRendererReply(fds, reply, -1); |
174 } | 163 } |
175 | 164 |
176 void HandleFontOpenRequest(int fd, const Pickle& pickle, void* iter, | 165 void HandleFontOpenRequest(int fd, Pickle& pickle, void* iter, |
177 std::vector<int>& fds) { | 166 std::vector<int>& fds) { |
178 uint32_t fileid; | 167 uint32_t fileid; |
179 if (!pickle.ReadUInt32(&iter, &fileid)) | 168 if (!pickle.ReadUInt32(&iter, &fileid)) |
180 return; | 169 return; |
181 const int result_fd = font_config_->Open(fileid); | 170 const int result_fd = font_config_->Open(fileid); |
182 | 171 |
183 Pickle reply; | 172 Pickle reply; |
184 if (result_fd == -1) { | 173 if (result_fd == -1) { |
185 reply.WriteBool(false); | 174 reply.WriteBool(false); |
186 } else { | 175 } else { |
187 reply.WriteBool(true); | 176 reply.WriteBool(true); |
188 } | 177 } |
189 | 178 |
190 SendRendererReply(fds, reply, result_fd); | 179 SendRendererReply(fds, reply, result_fd); |
191 | 180 |
192 if (result_fd >= 0) | 181 if (result_fd >= 0) |
193 close(result_fd); | 182 close(result_fd); |
194 } | 183 } |
195 | 184 |
196 void HandleGetFontFamilyForChars(int fd, const Pickle& pickle, void* iter, | 185 void HandleGetFontFamilyForChars(int fd, Pickle& pickle, void* iter, |
197 std::vector<int>& fds) { | 186 std::vector<int>& fds) { |
198 // The other side of this call is | 187 // The other side of this call is |
199 // chrome/renderer/renderer_sandbox_support_linux.cc | 188 // chrome/renderer/renderer_sandbox_support_linux.cc |
200 | 189 |
201 int num_chars; | 190 int num_chars; |
202 if (!pickle.ReadInt(&iter, &num_chars)) | 191 if (!pickle.ReadInt(&iter, &num_chars)) |
203 return; | 192 return; |
204 | 193 |
205 // We don't want a corrupt renderer asking too much of us, it might | 194 // We don't want a corrupt renderer asking too much of us, it might |
206 // overflow later in the code. | 195 // overflow later in the code. |
(...skipping 19 matching lines...) Expand all Loading... |
226 | 215 |
227 Pickle reply; | 216 Pickle reply; |
228 if (family.data()) { | 217 if (family.data()) { |
229 reply.WriteString(family.data()); | 218 reply.WriteString(family.data()); |
230 } else { | 219 } else { |
231 reply.WriteString(""); | 220 reply.WriteString(""); |
232 } | 221 } |
233 SendRendererReply(fds, reply, -1); | 222 SendRendererReply(fds, reply, -1); |
234 } | 223 } |
235 | 224 |
236 void HandleLocaltime(int fd, const Pickle& pickle, void* iter, | 225 void HandleLocaltime(int fd, Pickle& pickle, void* iter, |
237 std::vector<int>& fds) { | 226 std::vector<int>& fds) { |
238 // The other side of this call is in zygote_main_linux.cc | 227 // The other side of this call is in zygote_main_linux.cc |
239 | 228 |
240 std::string time_string; | 229 std::string time_string; |
241 if (!pickle.ReadString(&iter, &time_string) || | 230 if (!pickle.ReadString(&iter, &time_string) || |
242 time_string.size() != sizeof(time_t)) { | 231 time_string.size() != sizeof(time_t)) { |
243 return; | 232 return; |
244 } | 233 } |
245 | 234 |
246 time_t time; | 235 time_t time; |
247 memcpy(&time, time_string.data(), sizeof(time)); | 236 memcpy(&time, time_string.data(), sizeof(time)); |
248 // We use localtime here because we need the tm_zone field to be filled | 237 // We use localtime here because we need the tm_zone field to be filled |
249 // out. Since we are a single-threaded process, this is safe. | 238 // out. Since we are a single-threaded process, this is safe. |
250 const struct tm* expanded_time = localtime(&time); | 239 const struct tm* expanded_time = localtime(&time); |
251 | 240 |
252 const std::string result_string( | 241 const std::string result_string( |
253 reinterpret_cast<const char*>(expanded_time), sizeof(struct tm)); | 242 reinterpret_cast<const char*>(expanded_time), sizeof(struct tm)); |
254 | 243 |
255 Pickle reply; | 244 Pickle reply; |
256 reply.WriteString(result_string); | 245 reply.WriteString(result_string); |
257 reply.WriteString(expanded_time->tm_zone); | 246 reply.WriteString(expanded_time->tm_zone); |
258 SendRendererReply(fds, reply, -1); | 247 SendRendererReply(fds, reply, -1); |
259 } | 248 } |
260 | 249 |
261 void HandleGetChildWithInode(int fd, const Pickle& pickle, void* iter, | |
262 std::vector<int>& fds) { | |
263 // The other side of this call is in zygote_main_linux.cc | |
264 if (sandbox_cmd_.empty()) { | |
265 LOG(ERROR) << "Not in the sandbox, this should not be called"; | |
266 return; | |
267 } | |
268 | |
269 uint64_t inode; | |
270 if (!pickle.ReadUInt64(&iter, &inode)) | |
271 return; | |
272 | |
273 base::ProcessId pid = 0; | |
274 std::string inode_output; | |
275 | |
276 std::vector<std::string> sandbox_cmd = sandbox_cmd_; | |
277 sandbox_cmd.push_back(IntToString(inode)); | |
278 CommandLine get_inode_cmd(sandbox_cmd); | |
279 if (base::GetAppOutput(get_inode_cmd, &inode_output)) | |
280 StringToInt(inode_output, &pid); | |
281 | |
282 if (!pid) { | |
283 LOG(ERROR) << "Could not get pid"; | |
284 return; | |
285 } | |
286 | |
287 Pickle reply; | |
288 reply.WriteInt(pid); | |
289 SendRendererReply(fds, reply, -1); | |
290 } | |
291 | |
292 void SendRendererReply(const std::vector<int>& fds, const Pickle& reply, | 250 void SendRendererReply(const std::vector<int>& fds, const Pickle& reply, |
293 int reply_fd) { | 251 int reply_fd) { |
294 struct msghdr msg; | 252 struct msghdr msg; |
295 memset(&msg, 0, sizeof(msg)); | 253 memset(&msg, 0, sizeof(msg)); |
296 struct iovec iov = {const_cast<void*>(reply.data()), reply.size()}; | 254 struct iovec iov = {const_cast<void*>(reply.data()), reply.size()}; |
297 msg.msg_iov = &iov; | 255 msg.msg_iov = &iov; |
298 msg.msg_iovlen = 1; | 256 msg.msg_iovlen = 1; |
299 | 257 |
300 char control_buffer[CMSG_SPACE(sizeof(int))]; | 258 char control_buffer[CMSG_SPACE(sizeof(int))]; |
301 | 259 |
302 if (reply_fd != -1) { | 260 if (reply_fd != -1) { |
303 struct cmsghdr *cmsg; | 261 struct cmsghdr *cmsg; |
304 | 262 |
305 msg.msg_control = control_buffer; | 263 msg.msg_control = control_buffer; |
306 msg.msg_controllen = sizeof(control_buffer); | 264 msg.msg_controllen = sizeof(control_buffer); |
307 cmsg = CMSG_FIRSTHDR(&msg); | 265 cmsg = CMSG_FIRSTHDR(&msg); |
308 cmsg->cmsg_level = SOL_SOCKET; | 266 cmsg->cmsg_level = SOL_SOCKET; |
309 cmsg->cmsg_type = SCM_RIGHTS; | 267 cmsg->cmsg_type = SCM_RIGHTS; |
310 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | 268 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
311 memcpy(CMSG_DATA(cmsg), &reply_fd, sizeof(reply_fd)); | 269 memcpy(CMSG_DATA(cmsg), &reply_fd, sizeof(int)); |
312 msg.msg_controllen = cmsg->cmsg_len; | 270 msg.msg_controllen = cmsg->cmsg_len; |
313 } | 271 } |
314 | 272 |
315 HANDLE_EINTR(sendmsg(fds[0], &msg, MSG_DONTWAIT)); | 273 HANDLE_EINTR(sendmsg(fds[0], &msg, MSG_DONTWAIT)); |
316 } | 274 } |
317 | 275 |
318 // --------------------------------------------------------------------------- | 276 // --------------------------------------------------------------------------- |
319 | 277 |
320 const int lifeline_fd_; | 278 const int lifeline_fd_; |
321 const int browser_socket_; | 279 const int browser_socket_; |
322 FontConfigDirect* const font_config_; | 280 FontConfigDirect* const font_config_; |
323 std::vector<std::string> sandbox_cmd_; | |
324 }; | 281 }; |
325 | 282 |
326 // ----------------------------------------------------------------------------- | 283 // ----------------------------------------------------------------------------- |
327 | 284 |
328 // Runs on the main thread at startup. | 285 // Runs on the main thread at startup. |
329 RenderSandboxHostLinux::RenderSandboxHostLinux() | 286 RenderSandboxHostLinux::RenderSandboxHostLinux() { |
330 : init_(false) { | |
331 } | |
332 | |
333 void RenderSandboxHostLinux::Init(const std::string& sandbox_path) { | |
334 DCHECK(!init_); | |
335 init_ = true; | |
336 | |
337 int fds[2]; | 287 int fds[2]; |
338 // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the renderer from | 288 // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the renderer from |
339 // sending datagrams to other sockets on the system. The sandbox may prevent | 289 // sending datagrams to other sockets on the system. The sandbox may prevent |
340 // the renderer from calling socket() to create new sockets, but it'll still | 290 // the renderer from calling socket() to create new sockets, but it'll still |
341 // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send | 291 // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send |
342 // a datagram to any (abstract) socket on the same system. With | 292 // a datagram to any (abstract) socket on the same system. With |
343 // SOCK_SEQPACKET, this is prevented. | 293 // SOCK_SEQPACKET, this is prevented. |
344 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | 294 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); |
345 | 295 |
346 renderer_socket_ = fds[0]; | 296 renderer_socket_ = fds[0]; |
347 const int browser_socket = fds[1]; | 297 const int browser_socket = fds[1]; |
348 | 298 |
349 int pipefds[2]; | 299 int pipefds[2]; |
350 CHECK(0 == pipe(pipefds)); | 300 CHECK(0 == pipe(pipefds)); |
351 const int child_lifeline_fd = pipefds[0]; | 301 const int child_lifeline_fd = pipefds[0]; |
352 childs_lifeline_fd_ = pipefds[1]; | 302 childs_lifeline_fd_ = pipefds[1]; |
353 | 303 |
354 pid_ = fork(); | 304 pid_ = fork(); |
355 if (pid_ == 0) { | 305 if (pid_ == 0) { |
356 SandboxIPCProcess handler(child_lifeline_fd, browser_socket, sandbox_path); | 306 SandboxIPCProcess handler(child_lifeline_fd, browser_socket); |
357 handler.Run(); | 307 handler.Run(); |
358 _exit(0); | 308 _exit(0); |
359 } | 309 } |
360 } | 310 } |
361 | 311 |
362 RenderSandboxHostLinux::~RenderSandboxHostLinux() { | 312 RenderSandboxHostLinux::~RenderSandboxHostLinux() { |
363 if (init_) { | 313 HANDLE_EINTR(close(renderer_socket_)); |
364 HANDLE_EINTR(close(renderer_socket_)); | 314 HANDLE_EINTR(close(childs_lifeline_fd_)); |
365 HANDLE_EINTR(close(childs_lifeline_fd_)); | |
366 } | |
367 } | 315 } |
OLD | NEW |