OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 "chrome/browser/renderer_host/render_sandbox_host_linux.h" | |
6 | |
7 #include <fcntl.h> | |
8 #include <fontconfig/fontconfig.h> | |
9 #include <stdint.h> | |
10 #include <unistd.h> | |
11 #include <sys/uio.h> | |
12 #include <sys/stat.h> | |
13 #include <sys/socket.h> | |
14 #include <sys/poll.h> | |
15 #include <time.h> | |
16 | |
17 #include <vector> | |
18 | |
19 #include "base/command_line.h" | |
20 #include "base/eintr_wrapper.h" | |
21 #include "base/linux_util.h" | |
22 #include "base/pickle.h" | |
23 #include "base/process_util.h" | |
24 #include "base/scoped_ptr.h" | |
25 #include "base/shared_memory.h" | |
26 #include "base/singleton.h" | |
27 #include "base/string_number_conversions.h" | |
28 #include "base/string_util.h" | |
29 #include "chrome/common/font_config_ipc_linux.h" | |
30 #include "chrome/common/sandbox_methods_linux.h" | |
31 #include "chrome/common/unix_domain_socket_posix.h" | |
32 #include "skia/ext/SkFontHost_fontconfig_direct.h" | |
33 #include "third_party/npapi/bindings/npapi_extensions.h" | |
34 #include "third_party/WebKit/Source/WebKit/chromium/public/gtk/WebFontInfo.h" | |
35 | |
36 using WebKit::WebCString; | |
37 using WebKit::WebFontInfo; | |
38 using WebKit::WebUChar; | |
39 | |
40 // http://code.google.com/p/chromium/wiki/LinuxSandboxIPC | |
41 | |
42 // BEWARE: code in this file run across *processes* (not just threads). | |
43 | |
44 // This code runs in a child process | |
45 class SandboxIPCProcess { | |
46 public: | |
47 // lifeline_fd: this is the read end of a pipe which the browser process | |
48 // holds the other end of. If the browser process dies, its descriptors are | |
49 // closed and we will noticed an EOF on the pipe. That's our signal to exit. | |
50 // browser_socket: the browser's end of the sandbox IPC socketpair. From the | |
51 // point of view of the renderer, it's talking to the browser but this | |
52 // object actually services the requests. | |
53 // sandbox_cmd: the path of the sandbox executable | |
54 SandboxIPCProcess(int lifeline_fd, int browser_socket, | |
55 std::string sandbox_cmd) | |
56 : lifeline_fd_(lifeline_fd), | |
57 browser_socket_(browser_socket), | |
58 font_config_(new FontConfigDirect()) { | |
59 base::InjectiveMultimap multimap; | |
60 multimap.push_back(base::InjectionArc(0, lifeline_fd, false)); | |
61 multimap.push_back(base::InjectionArc(0, browser_socket, false)); | |
62 | |
63 base::CloseSuperfluousFds(multimap); | |
64 | |
65 if (!sandbox_cmd.empty()) { | |
66 sandbox_cmd_.push_back(sandbox_cmd); | |
67 sandbox_cmd_.push_back(base::kFindInodeSwitch); | |
68 } | |
69 } | |
70 | |
71 void Run() { | |
72 struct pollfd pfds[2]; | |
73 pfds[0].fd = lifeline_fd_; | |
74 pfds[0].events = POLLIN; | |
75 pfds[1].fd = browser_socket_; | |
76 pfds[1].events = POLLIN; | |
77 | |
78 int failed_polls = 0; | |
79 for (;;) { | |
80 const int r = HANDLE_EINTR(poll(pfds, 2, -1)); | |
81 if (r < 1) { | |
82 LOG(WARNING) << "poll errno:" << errno; | |
83 if (failed_polls++ == 3) { | |
84 LOG(FATAL) << "poll failing. Sandbox host aborting."; | |
85 return; | |
86 } | |
87 continue; | |
88 } | |
89 | |
90 failed_polls = 0; | |
91 | |
92 if (pfds[0].revents) { | |
93 // our parent died so we should too. | |
94 _exit(0); | |
95 } | |
96 | |
97 if (pfds[1].revents) { | |
98 HandleRequestFromRenderer(browser_socket_); | |
99 } | |
100 } | |
101 } | |
102 | |
103 private: | |
104 // --------------------------------------------------------------------------- | |
105 // Requests from the renderer... | |
106 | |
107 void HandleRequestFromRenderer(int fd) { | |
108 std::vector<int> fds; | |
109 | |
110 // A FontConfigIPC::METHOD_MATCH message could be kMaxFontFamilyLength | |
111 // bytes long (this is the largest message type). | |
112 // 128 bytes padding are necessary so recvmsg() does not return MSG_TRUNC | |
113 // error for a maximum length message. | |
114 char buf[FontConfigInterface::kMaxFontFamilyLength + 128]; | |
115 | |
116 const ssize_t len = UnixDomainSocket::RecvMsg(fd, buf, sizeof(buf), &fds); | |
117 if (len == -1) { | |
118 // TODO: should send an error reply, or the sender might block forever. | |
119 NOTREACHED() | |
120 << "Sandbox host message is larger than kMaxFontFamilyLength"; | |
121 return; | |
122 } | |
123 if (fds.size() == 0) | |
124 return; | |
125 | |
126 Pickle pickle(buf, len); | |
127 void* iter = NULL; | |
128 | |
129 int kind; | |
130 if (!pickle.ReadInt(&iter, &kind)) | |
131 goto error; | |
132 | |
133 if (kind == FontConfigIPC::METHOD_MATCH) { | |
134 HandleFontMatchRequest(fd, pickle, iter, fds); | |
135 } else if (kind == FontConfigIPC::METHOD_OPEN) { | |
136 HandleFontOpenRequest(fd, pickle, iter, fds); | |
137 } else if (kind == LinuxSandbox::METHOD_GET_FONT_FAMILY_FOR_CHARS) { | |
138 HandleGetFontFamilyForChars(fd, pickle, iter, fds); | |
139 } else if (kind == LinuxSandbox::METHOD_LOCALTIME) { | |
140 HandleLocaltime(fd, pickle, iter, fds); | |
141 } else if (kind == LinuxSandbox::METHOD_GET_CHILD_WITH_INODE) { | |
142 HandleGetChildWithInode(fd, pickle, iter, fds); | |
143 } else if (kind == LinuxSandbox::METHOD_GET_STYLE_FOR_STRIKE) { | |
144 HandleGetStyleForStrike(fd, pickle, iter, fds); | |
145 } else if (kind == LinuxSandbox::METHOD_MAKE_SHARED_MEMORY_SEGMENT) { | |
146 HandleMakeSharedMemorySegment(fd, pickle, iter, fds); | |
147 } else if (kind == LinuxSandbox::METHOD_MATCH_WITH_FALLBACK) { | |
148 HandleMatchWithFallback(fd, pickle, iter, fds); | |
149 } | |
150 | |
151 error: | |
152 for (std::vector<int>::const_iterator | |
153 i = fds.begin(); i != fds.end(); ++i) { | |
154 close(*i); | |
155 } | |
156 } | |
157 | |
158 void HandleFontMatchRequest(int fd, const Pickle& pickle, void* iter, | |
159 std::vector<int>& fds) { | |
160 bool filefaceid_valid; | |
161 uint32_t filefaceid; | |
162 | |
163 if (!pickle.ReadBool(&iter, &filefaceid_valid)) | |
164 return; | |
165 if (filefaceid_valid) { | |
166 if (!pickle.ReadUInt32(&iter, &filefaceid)) | |
167 return; | |
168 } | |
169 bool is_bold, is_italic; | |
170 if (!pickle.ReadBool(&iter, &is_bold) || | |
171 !pickle.ReadBool(&iter, &is_italic)) { | |
172 return; | |
173 } | |
174 | |
175 uint32_t characters_bytes; | |
176 if (!pickle.ReadUInt32(&iter, &characters_bytes)) | |
177 return; | |
178 const char* characters = NULL; | |
179 if (characters_bytes > 0) { | |
180 const uint32_t kMaxCharactersBytes = 1 << 10; | |
181 if (characters_bytes % 2 != 0 || // We expect UTF-16. | |
182 characters_bytes > kMaxCharactersBytes || | |
183 !pickle.ReadBytes(&iter, &characters, characters_bytes)) | |
184 return; | |
185 } | |
186 | |
187 std::string family; | |
188 if (!pickle.ReadString(&iter, &family)) | |
189 return; | |
190 | |
191 std::string result_family; | |
192 unsigned result_filefaceid; | |
193 const bool r = font_config_->Match( | |
194 &result_family, &result_filefaceid, filefaceid_valid, filefaceid, | |
195 family, characters, characters_bytes, &is_bold, &is_italic); | |
196 | |
197 Pickle reply; | |
198 if (!r) { | |
199 reply.WriteBool(false); | |
200 } else { | |
201 reply.WriteBool(true); | |
202 reply.WriteUInt32(result_filefaceid); | |
203 reply.WriteString(result_family); | |
204 reply.WriteBool(is_bold); | |
205 reply.WriteBool(is_italic); | |
206 } | |
207 SendRendererReply(fds, reply, -1); | |
208 } | |
209 | |
210 void HandleFontOpenRequest(int fd, const Pickle& pickle, void* iter, | |
211 std::vector<int>& fds) { | |
212 uint32_t filefaceid; | |
213 if (!pickle.ReadUInt32(&iter, &filefaceid)) | |
214 return; | |
215 const int result_fd = font_config_->Open(filefaceid); | |
216 | |
217 Pickle reply; | |
218 if (result_fd == -1) { | |
219 reply.WriteBool(false); | |
220 } else { | |
221 reply.WriteBool(true); | |
222 } | |
223 | |
224 SendRendererReply(fds, reply, result_fd); | |
225 | |
226 if (result_fd >= 0) | |
227 close(result_fd); | |
228 } | |
229 | |
230 void HandleGetFontFamilyForChars(int fd, const Pickle& pickle, void* iter, | |
231 std::vector<int>& fds) { | |
232 // The other side of this call is | |
233 // chrome/renderer/renderer_sandbox_support_linux.cc | |
234 | |
235 int num_chars; | |
236 if (!pickle.ReadInt(&iter, &num_chars)) | |
237 return; | |
238 | |
239 // We don't want a corrupt renderer asking too much of us, it might | |
240 // overflow later in the code. | |
241 static const int kMaxChars = 4096; | |
242 if (num_chars < 1 || num_chars > kMaxChars) { | |
243 LOG(WARNING) << "HandleGetFontFamilyForChars: too many chars: " | |
244 << num_chars; | |
245 return; | |
246 } | |
247 | |
248 scoped_array<WebUChar> chars(new WebUChar[num_chars]); | |
249 | |
250 for (int i = 0; i < num_chars; ++i) { | |
251 uint32_t c; | |
252 if (!pickle.ReadUInt32(&iter, &c)) { | |
253 return; | |
254 } | |
255 | |
256 chars[i] = c; | |
257 } | |
258 | |
259 WebCString family = WebFontInfo::familyForChars(chars.get(), num_chars); | |
260 | |
261 Pickle reply; | |
262 if (family.data()) { | |
263 reply.WriteString(family.data()); | |
264 } else { | |
265 reply.WriteString(""); | |
266 } | |
267 SendRendererReply(fds, reply, -1); | |
268 } | |
269 | |
270 void HandleGetStyleForStrike(int fd, const Pickle& pickle, void* iter, | |
271 std::vector<int>& fds) { | |
272 std::string family; | |
273 int sizeAndStyle; | |
274 | |
275 if (!pickle.ReadString(&iter, &family) || | |
276 !pickle.ReadInt(&iter, &sizeAndStyle)) { | |
277 return; | |
278 } | |
279 | |
280 WebKit::WebFontRenderStyle style; | |
281 WebFontInfo::renderStyleForStrike(family.c_str(), sizeAndStyle, &style); | |
282 | |
283 Pickle reply; | |
284 reply.WriteInt(style.useBitmaps); | |
285 reply.WriteInt(style.useAutoHint); | |
286 reply.WriteInt(style.useHinting); | |
287 reply.WriteInt(style.hintStyle); | |
288 reply.WriteInt(style.useAntiAlias); | |
289 reply.WriteInt(style.useSubpixel); | |
290 | |
291 SendRendererReply(fds, reply, -1); | |
292 } | |
293 | |
294 void HandleLocaltime(int fd, const Pickle& pickle, void* iter, | |
295 std::vector<int>& fds) { | |
296 // The other side of this call is in zygote_main_linux.cc | |
297 | |
298 std::string time_string; | |
299 if (!pickle.ReadString(&iter, &time_string) || | |
300 time_string.size() != sizeof(time_t)) { | |
301 return; | |
302 } | |
303 | |
304 time_t time; | |
305 memcpy(&time, time_string.data(), sizeof(time)); | |
306 // We use localtime here because we need the tm_zone field to be filled | |
307 // out. Since we are a single-threaded process, this is safe. | |
308 const struct tm* expanded_time = localtime(&time); | |
309 | |
310 std::string result_string; | |
311 const char* time_zone_string = ""; | |
312 if (expanded_time != NULL) { | |
313 result_string = std::string(reinterpret_cast<const char*>(expanded_time), | |
314 sizeof(struct tm)); | |
315 time_zone_string = expanded_time->tm_zone; | |
316 } | |
317 | |
318 Pickle reply; | |
319 reply.WriteString(result_string); | |
320 reply.WriteString(time_zone_string); | |
321 SendRendererReply(fds, reply, -1); | |
322 } | |
323 | |
324 void HandleGetChildWithInode(int fd, const Pickle& pickle, void* iter, | |
325 std::vector<int>& fds) { | |
326 // The other side of this call is in zygote_main_linux.cc | |
327 if (sandbox_cmd_.empty()) { | |
328 LOG(ERROR) << "Not in the sandbox, this should not be called"; | |
329 return; | |
330 } | |
331 | |
332 uint64_t inode; | |
333 if (!pickle.ReadUInt64(&iter, &inode)) | |
334 return; | |
335 | |
336 base::ProcessId pid = 0; | |
337 std::string inode_output; | |
338 | |
339 std::vector<std::string> sandbox_cmd = sandbox_cmd_; | |
340 sandbox_cmd.push_back(base::Int64ToString(inode)); | |
341 CommandLine get_inode_cmd(sandbox_cmd); | |
342 if (base::GetAppOutput(get_inode_cmd, &inode_output)) | |
343 base::StringToInt(inode_output, &pid); | |
344 | |
345 if (!pid) { | |
346 // Even though the pid is invalid, we still need to reply to the zygote | |
347 // and not just return here. | |
348 LOG(ERROR) << "Could not get pid"; | |
349 } | |
350 | |
351 Pickle reply; | |
352 reply.WriteInt(pid); | |
353 SendRendererReply(fds, reply, -1); | |
354 } | |
355 | |
356 void HandleMakeSharedMemorySegment(int fd, const Pickle& pickle, void* iter, | |
357 std::vector<int>& fds) { | |
358 uint32_t shm_size; | |
359 if (!pickle.ReadUInt32(&iter, &shm_size)) | |
360 return; | |
361 int shm_fd = -1; | |
362 base::SharedMemory shm; | |
363 if (shm.CreateAnonymous(shm_size)) | |
364 shm_fd = shm.handle().fd; | |
365 Pickle reply; | |
366 SendRendererReply(fds, reply, shm_fd); | |
367 } | |
368 | |
369 void HandleMatchWithFallback(int fd, const Pickle& pickle, void* iter, | |
370 std::vector<int>& fds) { | |
371 // Unlike the other calls, for which we are an indirection in front of | |
372 // WebKit or Skia, this call is always made via this sandbox helper | |
373 // process. Therefore the fontconfig code goes in here directly. | |
374 | |
375 std::string face; | |
376 bool is_bold, is_italic; | |
377 uint32 charset; | |
378 | |
379 if (!pickle.ReadString(&iter, &face) || | |
380 face.empty() || | |
381 !pickle.ReadBool(&iter, &is_bold) || | |
382 !pickle.ReadBool(&iter, &is_italic) || | |
383 !pickle.ReadUInt32(&iter, &charset)) { | |
384 return; | |
385 } | |
386 | |
387 FcLangSet* langset = FcLangSetCreate(); | |
388 MSCharSetToFontconfig(langset, charset); | |
389 | |
390 FcPattern* pattern = FcPatternCreate(); | |
391 // TODO(agl): FC_FAMILy needs to change | |
392 FcPatternAddString(pattern, FC_FAMILY, (FcChar8*) face.c_str()); | |
393 if (is_bold) | |
394 FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD); | |
395 if (is_italic) | |
396 FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC); | |
397 FcPatternAddLangSet(pattern, FC_LANG, langset); | |
398 FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); | |
399 FcConfigSubstitute(NULL, pattern, FcMatchPattern); | |
400 FcDefaultSubstitute(pattern); | |
401 | |
402 FcResult result; | |
403 FcFontSet* font_set = FcFontSort(0, pattern, 0, 0, &result); | |
404 int font_fd = -1; | |
405 int good_enough_index = -1; | |
406 bool good_enough_index_set = false; | |
407 | |
408 if (font_set) { | |
409 for (int i = 0; i < font_set->nfont; ++i) { | |
410 FcPattern* current = font_set->fonts[i]; | |
411 | |
412 // Older versions of fontconfig have a bug where they cannot select | |
413 // only scalable fonts so we have to manually filter the results. | |
414 FcBool is_scalable; | |
415 if (FcPatternGetBool(current, FC_SCALABLE, 0, | |
416 &is_scalable) != FcResultMatch || | |
417 !is_scalable) { | |
418 continue; | |
419 } | |
420 | |
421 FcChar8* c_filename; | |
422 if (FcPatternGetString(current, FC_FILE, 0, &c_filename) != | |
423 FcResultMatch) { | |
424 continue; | |
425 } | |
426 | |
427 // We only want to return sfnt (TrueType) based fonts. We don't have a | |
428 // very good way of detecting this so we'll filter based on the | |
429 // filename. | |
430 bool is_sfnt = false; | |
431 static const char kSFNTExtensions[][5] = { | |
432 ".ttf", ".otc", ".TTF", ".ttc", "" | |
433 }; | |
434 const size_t filename_len = strlen(reinterpret_cast<char*>(c_filename)); | |
435 for (unsigned j = 0; ; j++) { | |
436 if (kSFNTExtensions[j][0] == 0) { | |
437 // None of the extensions matched. | |
438 break; | |
439 } | |
440 const size_t ext_len = strlen(kSFNTExtensions[j]); | |
441 if (filename_len > ext_len && | |
442 memcmp(c_filename + filename_len - ext_len, | |
443 kSFNTExtensions[j], ext_len) == 0) { | |
444 is_sfnt = true; | |
445 break; | |
446 } | |
447 } | |
448 | |
449 if (!is_sfnt) | |
450 continue; | |
451 | |
452 // This font is good enough to pass muster, but we might be able to do | |
453 // better with subsequent ones. | |
454 if (!good_enough_index_set) { | |
455 good_enough_index = i; | |
456 good_enough_index_set = true; | |
457 } | |
458 | |
459 FcValue matrix; | |
460 bool have_matrix = FcPatternGet(current, FC_MATRIX, 0, &matrix) == 0; | |
461 | |
462 if (is_italic && have_matrix) { | |
463 // we asked for an italic font, but fontconfig is giving us a | |
464 // non-italic font with a transformation matrix. | |
465 continue; | |
466 } | |
467 | |
468 FcValue embolden; | |
469 const bool have_embolden = | |
470 FcPatternGet(current, FC_EMBOLDEN, 0, &embolden) == 0; | |
471 | |
472 if (is_bold && have_embolden) { | |
473 // we asked for a bold font, but fontconfig gave us a non-bold font | |
474 // and asked us to apply fake bolding. | |
475 continue; | |
476 } | |
477 | |
478 font_fd = open(reinterpret_cast<char*>(c_filename), O_RDONLY); | |
479 if (font_fd >= 0) | |
480 break; | |
481 } | |
482 } | |
483 | |
484 if (font_fd == -1 && good_enough_index_set) { | |
485 // We didn't find a font that we liked, so we fallback to something | |
486 // acceptable. | |
487 FcPattern* current = font_set->fonts[good_enough_index]; | |
488 FcChar8* c_filename; | |
489 FcPatternGetString(current, FC_FILE, 0, &c_filename); | |
490 font_fd = open(reinterpret_cast<char*>(c_filename), O_RDONLY); | |
491 } | |
492 | |
493 if (font_set) | |
494 FcFontSetDestroy(font_set); | |
495 FcPatternDestroy(pattern); | |
496 | |
497 Pickle reply; | |
498 SendRendererReply(fds, reply, font_fd); | |
499 | |
500 if (font_fd >= 0) { | |
501 if (HANDLE_EINTR(close(font_fd)) < 0) | |
502 PLOG(ERROR) << "close"; | |
503 } | |
504 } | |
505 | |
506 // MSCharSetToFontconfig translates a Microsoft charset identifier to a | |
507 // fontconfig language set by appending to |langset|. | |
508 static void MSCharSetToFontconfig(FcLangSet* langset, unsigned fdwCharSet) { | |
509 // We have need to translate raw fdwCharSet values into terms that | |
510 // fontconfig can understand. (See the description of fdwCharSet in the MSDN | |
511 // documentation for CreateFont: | |
512 // http://msdn.microsoft.com/en-us/library/dd183499(VS.85).aspx ) | |
513 // | |
514 // Although the argument is /called/ 'charset', the actual values conflate | |
515 // character sets (which are sets of Unicode code points) and character | |
516 // encodings (which are algorithms for turning a series of bits into a | |
517 // series of code points.) Sometimes the values will name a language, | |
518 // sometimes they'll name an encoding. In the latter case I'm assuming that | |
519 // they mean the set of code points in the domain of that encoding. | |
520 // | |
521 // fontconfig deals with ISO 639-1 language codes: | |
522 // http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | |
523 // | |
524 // So, for each of the documented fdwCharSet values I've had to take a | |
525 // guess at the set of ISO 639-1 languages intended. | |
526 | |
527 switch (fdwCharSet) { | |
528 case NPCharsetAnsi: | |
529 // These values I don't really know what to do with, so I'm going to map | |
530 // them to English also. | |
531 case NPCharsetDefault: | |
532 case NPCharsetMac: | |
533 case NPCharsetOEM: | |
534 case NPCharsetSymbol: | |
535 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("en")); | |
536 break; | |
537 case NPCharsetBaltic: | |
538 // The three baltic languages. | |
539 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("et")); | |
540 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lv")); | |
541 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("lt")); | |
542 break; | |
543 // TODO(jungshik): Would we be better off mapping Big5 to zh-tw | |
544 // and GB2312 to zh-cn? Fontconfig has 4 separate orthography | |
545 // files (zh-{cn,tw,hk,mo}. | |
546 case NPCharsetChineseBIG5: | |
547 case NPCharsetGB2312: | |
548 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("zh")); | |
549 break; | |
550 case NPCharsetEastEurope: | |
551 // A scattering of eastern European languages. | |
552 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("pl")); | |
553 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("cs")); | |
554 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("sk")); | |
555 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hu")); | |
556 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("hr")); | |
557 break; | |
558 case NPCharsetGreek: | |
559 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("el")); | |
560 break; | |
561 case NPCharsetHangul: | |
562 case NPCharsetJohab: | |
563 // Korean | |
564 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ko")); | |
565 break; | |
566 case NPCharsetRussian: | |
567 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ru")); | |
568 break; | |
569 case NPCharsetShiftJIS: | |
570 // Japanese | |
571 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ja")); | |
572 break; | |
573 case NPCharsetTurkish: | |
574 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("tr")); | |
575 break; | |
576 case NPCharsetVietnamese: | |
577 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("vi")); | |
578 break; | |
579 case NPCharsetArabic: | |
580 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("ar")); | |
581 break; | |
582 case NPCharsetHebrew: | |
583 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("he")); | |
584 break; | |
585 case NPCharsetThai: | |
586 FcLangSetAdd(langset, reinterpret_cast<const FcChar8*>("th")); | |
587 break; | |
588 // default: | |
589 // Don't add any languages in that case that we don't recognise the | |
590 // constant. | |
591 } | |
592 } | |
593 | |
594 void SendRendererReply(const std::vector<int>& fds, const Pickle& reply, | |
595 int reply_fd) { | |
596 struct msghdr msg; | |
597 memset(&msg, 0, sizeof(msg)); | |
598 struct iovec iov = {const_cast<void*>(reply.data()), reply.size()}; | |
599 msg.msg_iov = &iov; | |
600 msg.msg_iovlen = 1; | |
601 | |
602 char control_buffer[CMSG_SPACE(sizeof(int))]; | |
603 | |
604 if (reply_fd != -1) { | |
605 struct stat st; | |
606 if (fstat(reply_fd, &st) == 0 && S_ISDIR(st.st_mode)) { | |
607 LOG(FATAL) << "Tried to send a directory descriptor over sandbox IPC"; | |
608 // We must never send directory descriptors to a sandboxed process | |
609 // because they can use openat with ".." elements in the path in order | |
610 // to escape the sandbox and reach the real filesystem. | |
611 } | |
612 | |
613 struct cmsghdr *cmsg; | |
614 msg.msg_control = control_buffer; | |
615 msg.msg_controllen = sizeof(control_buffer); | |
616 cmsg = CMSG_FIRSTHDR(&msg); | |
617 cmsg->cmsg_level = SOL_SOCKET; | |
618 cmsg->cmsg_type = SCM_RIGHTS; | |
619 cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
620 memcpy(CMSG_DATA(cmsg), &reply_fd, sizeof(reply_fd)); | |
621 msg.msg_controllen = cmsg->cmsg_len; | |
622 } | |
623 | |
624 if (HANDLE_EINTR(sendmsg(fds[0], &msg, MSG_DONTWAIT)) < 0) | |
625 PLOG(ERROR) << "sendmsg"; | |
626 } | |
627 | |
628 // --------------------------------------------------------------------------- | |
629 | |
630 const int lifeline_fd_; | |
631 const int browser_socket_; | |
632 FontConfigDirect* const font_config_; | |
633 std::vector<std::string> sandbox_cmd_; | |
634 }; | |
635 | |
636 // ----------------------------------------------------------------------------- | |
637 | |
638 // Runs on the main thread at startup. | |
639 RenderSandboxHostLinux::RenderSandboxHostLinux() | |
640 : initialized_(false), | |
641 renderer_socket_(0), | |
642 childs_lifeline_fd_(0), | |
643 pid_(0) { | |
644 } | |
645 | |
646 // static | |
647 RenderSandboxHostLinux* RenderSandboxHostLinux::GetInstance() { | |
648 return Singleton<RenderSandboxHostLinux>::get(); | |
649 } | |
650 | |
651 void RenderSandboxHostLinux::Init(const std::string& sandbox_path) { | |
652 DCHECK(!initialized_); | |
653 initialized_ = true; | |
654 | |
655 int fds[2]; | |
656 // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the renderer from | |
657 // sending datagrams to other sockets on the system. The sandbox may prevent | |
658 // the renderer from calling socket() to create new sockets, but it'll still | |
659 // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send | |
660 // a datagram to any (abstract) socket on the same system. With | |
661 // SOCK_SEQPACKET, this is prevented. | |
662 CHECK(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) == 0); | |
663 | |
664 renderer_socket_ = fds[0]; | |
665 const int browser_socket = fds[1]; | |
666 | |
667 int pipefds[2]; | |
668 CHECK(0 == pipe(pipefds)); | |
669 const int child_lifeline_fd = pipefds[0]; | |
670 childs_lifeline_fd_ = pipefds[1]; | |
671 | |
672 pid_ = fork(); | |
673 if (pid_ == 0) { | |
674 SandboxIPCProcess handler(child_lifeline_fd, browser_socket, sandbox_path); | |
675 handler.Run(); | |
676 _exit(0); | |
677 } | |
678 } | |
679 | |
680 RenderSandboxHostLinux::~RenderSandboxHostLinux() { | |
681 if (initialized_) { | |
682 if (HANDLE_EINTR(close(renderer_socket_)) < 0) | |
683 PLOG(ERROR) << "close"; | |
684 if (HANDLE_EINTR(close(childs_lifeline_fd_)) < 0) | |
685 PLOG(ERROR) << "close"; | |
686 } | |
687 } | |
OLD | NEW |