Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "content/common/child_process_sandbox_support_impl_linux.h" | 5 #include "content/common/child_process_sandbox_support_impl_linux.h" |
| 6 | 6 |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| 11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/posix/unix_domain_socket_linux.h" | 12 #include "base/posix/unix_domain_socket_linux.h" |
| 13 #include "base/safe_numerics.h" | |
| 14 #include "base/sys_byteorder.h" | |
| 13 #include "content/common/sandbox_linux.h" | 15 #include "content/common/sandbox_linux.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/linux/WebFontFamily .h" | 16 #include "third_party/WebKit/Source/Platform/chromium/public/linux/WebFontFamily .h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/linux/WebFontRenderSt yle.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/linux/WebFontRenderSt yle.h" |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 | 20 |
| 19 void GetFontFamilyForCharacters(const uint16_t* utf16, | 21 void GetFontFamilyForCharacters(const uint16_t* utf16, |
| 20 size_t num_utf16, | 22 size_t num_utf16, |
| 21 const char* preferred_locale, | 23 const char* preferred_locale, |
| 22 WebKit::WebFontFamily* family) { | 24 WebKit::WebFontFamily* family) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 request.WriteBool(bold); | 94 request.WriteBool(bold); |
| 93 request.WriteBool(italic); | 95 request.WriteBool(italic); |
| 94 request.WriteUInt32(charset); | 96 request.WriteUInt32(charset); |
| 95 uint8_t reply_buf[64]; | 97 uint8_t reply_buf[64]; |
| 96 int fd = -1; | 98 int fd = -1; |
| 97 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf), | 99 UnixDomainSocket::SendRecvMsg(GetSandboxFD(), reply_buf, sizeof(reply_buf), |
| 98 &fd, request); | 100 &fd, request); |
| 99 return fd; | 101 return fd; |
| 100 } | 102 } |
| 101 | 103 |
| 102 bool GetFontTable(int fd, uint32_t table, uint8_t* output, | 104 bool GetFontTable(int fd, uint32_t table_tag, off_t offset, |
| 103 size_t* output_length) { | 105 uint8_t* output, size_t* output_length) { |
| 104 if (table == 0) { | 106 // To prevent overflow, limit offset to 2 GB. |
|
palmer
2013/03/19 21:35:15
If off_t is 64 bits (as it can be), this allows of
bbudge
2013/03/19 23:09:17
Done. Removed the comment.
| |
| 107 if (offset < 0) | |
| 108 return false; | |
| 109 | |
| 110 size_t data_length = 0; // the length of the file data. | |
| 111 off_t data_offset = 0; // the offset of the data in the file. | |
| 112 if (table_tag == 0) { | |
| 113 // Get the entire font file. | |
| 105 struct stat st; | 114 struct stat st; |
| 106 if (fstat(fd, &st) < 0) | 115 if (fstat(fd, &st) < 0) |
| 107 return false; | 116 return false; |
| 108 size_t length = st.st_size; | 117 data_length = base::checked_numeric_cast<size_t>(st.st_size); |
| 109 if (!output) { | 118 } else { |
| 110 *output_length = length; | 119 // Get a font table. Read the header to find its offset in the file. |
| 111 return true; | 120 uint16_t num_tables; |
| 112 } | 121 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables, sizeof(num_tables), |
| 113 if (*output_length < length) | 122 4 /* skip the font type */)); |
| 123 if (n != sizeof(num_tables)) | |
| 114 return false; | 124 return false; |
| 115 *output_length = length; | 125 // Font data is stored in net (big-endian) order. |
| 116 ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0)); | 126 num_tables = base::NetToHost16(num_tables); |
| 117 if (n != static_cast<ssize_t>(length)) | 127 |
| 128 // Read the table directory. | |
| 129 static const size_t kTableEntrySize = 16; | |
| 130 const size_t directory_size = num_tables * kTableEntrySize; | |
| 131 scoped_array<uint8_t> table_entries(new uint8_t[directory_size]); | |
| 132 n = HANDLE_EINTR(pread(fd, table_entries.get(), directory_size, | |
| 133 12 /* skip the SFNT header */)); | |
| 134 if (n != base::checked_numeric_cast<ssize_t>(directory_size)) | |
| 118 return false; | 135 return false; |
| 119 return true; | |
| 120 } | |
| 121 | 136 |
| 122 unsigned num_tables; | 137 for (uint16_t i = 0; i < num_tables; i++) { |
|
palmer
2013/03/19 21:35:15
MINOR NIT: Chromium style is to use pre-increment
bbudge
2013/03/19 23:09:17
Done.
| |
| 123 uint8_t num_tables_buf[2]; | 138 uint8_t* entry = table_entries.get() + i * kTableEntrySize; |
| 124 | 139 uint32_t tag = base::NetToHost32(*reinterpret_cast<uint32_t*>(entry)); |
| 125 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables_buf, sizeof(num_tables_buf), | 140 if (tag == table_tag) { |
| 126 4 /* skip the font type */)); | 141 // Font data is stored in net (big-endian) order. |
| 127 if (n != sizeof(num_tables_buf)) | 142 data_offset = |
| 128 return false; | 143 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8)); |
| 129 | 144 data_length = |
| 130 num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 | | 145 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12)); |
| 131 num_tables_buf[1]; | 146 break; |
| 132 | 147 } |
| 133 // The size in bytes of an entry in the table directory. | |
| 134 static const unsigned kTableEntrySize = 16; | |
| 135 scoped_array<uint8_t> table_entries( | |
| 136 new uint8_t[num_tables * kTableEntrySize]); | |
| 137 n = HANDLE_EINTR(pread(fd, table_entries.get(), num_tables * kTableEntrySize, | |
| 138 12 /* skip the SFNT header */)); | |
| 139 if (n != static_cast<ssize_t>(num_tables * kTableEntrySize)) | |
| 140 return false; | |
| 141 | |
| 142 size_t offset; | |
| 143 size_t length = 0; | |
| 144 for (unsigned i = 0; i < num_tables; i++) { | |
| 145 const uint8_t* entry = table_entries.get() + i * kTableEntrySize; | |
| 146 if (memcmp(entry, &table, sizeof(table)) == 0) { | |
| 147 offset = static_cast<size_t>(entry[8]) << 24 | | |
| 148 static_cast<size_t>(entry[9]) << 16 | | |
| 149 static_cast<size_t>(entry[10]) << 8 | | |
| 150 static_cast<size_t>(entry[11]); | |
| 151 length = static_cast<size_t>(entry[12]) << 24 | | |
| 152 static_cast<size_t>(entry[13]) << 16 | | |
| 153 static_cast<size_t>(entry[14]) << 8 | | |
| 154 static_cast<size_t>(entry[15]); | |
| 155 | |
| 156 break; | |
| 157 } | 148 } |
| 158 } | 149 } |
| 159 | 150 |
| 160 if (!length) | 151 if (!data_length || offset > base::checked_numeric_cast<off_t>(data_length)) |
| 161 return false; | 152 return false; |
| 153 // Make sure it's safe to add the data offset and the caller's logical offset. | |
| 154 // Define the maximum positive offset on 32 bit systems. | |
| 155 static const off_t kMaxPositiveOffset32 = 1 << 30; | |
| 156 if ((offset > kMaxPositiveOffset32 / 2) || | |
| 157 (data_offset > kMaxPositiveOffset32 / 2)) | |
| 158 return false; | |
| 159 data_offset += offset; | |
| 160 data_length -= offset; | |
| 162 | 161 |
| 163 if (!output) { | 162 if (output) { |
| 164 *output_length = length; | 163 // 'output_length' holds the maximum amount of data the caller can accept. |
| 165 return true; | 164 data_length = std::min(data_length, *output_length); |
|
palmer
2013/03/19 21:35:15
This might mean that the caller won't get a comple
bbudge
2013/03/19 23:09:17
We write the number of bytes into their output_len
| |
| 165 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset)); | |
| 166 if (n != base::checked_numeric_cast<ssize_t>(data_length)) | |
| 167 return false; | |
| 166 } | 168 } |
| 167 | 169 *output_length = data_length; |
| 168 if (*output_length < length) | |
| 169 return false; | |
| 170 | |
| 171 *output_length = length; | |
| 172 n = HANDLE_EINTR(pread(fd, output, length, offset)); | |
| 173 if (n != static_cast<ssize_t>(length)) | |
| 174 return false; | |
| 175 | 170 |
| 176 return true; | 171 return true; |
| 177 } | 172 } |
| 178 | 173 |
| 179 } // namespace content | 174 } // namespace content |
| OLD | NEW |