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