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, off_t offset, |
| 103 size_t* output_length) { | 105 uint8_t* output, size_t* output_length) { |
| 106 if (offset < 0) | |
| 107 return false; | |
| 108 | |
| 109 size_t data_length = 0; | |
| 110 off_t data_offset = 0; | |
| 104 if (table == 0) { | 111 if (table == 0) { |
| 112 // Get the entire font file. | |
| 105 struct stat st; | 113 struct stat st; |
| 106 if (fstat(fd, &st) < 0) | 114 if (fstat(fd, &st) < 0) |
| 107 return false; | 115 return false; |
| 108 size_t length = st.st_size; | 116 data_length = base::checked_numeric_cast<size_t>(st.st_size); |
| 109 if (!output) { | 117 } else { |
| 110 *output_length = length; | 118 // Get a font table. Read the header to find its offset in the file. |
| 111 return true; | 119 uint16_t num_tables; |
| 120 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables, sizeof(num_tables), | |
| 121 4 /* skip the font type */)); | |
| 122 if (n != sizeof(num_tables)) | |
| 123 return false; | |
| 124 // Font data is stored in net (big-endian) order. | |
| 125 num_tables = base::NetToHost16(num_tables); | |
| 126 | |
| 127 // Read the table directory. | |
| 128 // The size in bytes of an entry in the table directory. | |
| 129 static const size_t kDirEntrySize = 16; | |
| 130 scoped_array<uint8_t> table_entries( | |
| 131 new uint8_t[num_tables * kDirEntrySize]); | |
|
palmer
2013/03/15 23:16:47
OPTIONAL NIT: Maybe give this expression a name to
bbudge
2013/03/16 00:07:03
Done.
| |
| 132 n = HANDLE_EINTR(pread(fd, table_entries.get(), num_tables * kDirEntrySize, | |
| 133 12 /* skip the SFNT header */)); | |
| 134 if (n != base::checked_numeric_cast<ssize_t>(num_tables * kDirEntrySize)) | |
| 135 return false; | |
| 136 | |
| 137 for (uint16_t i = 0; i < num_tables; i++) { | |
| 138 uint8_t* entry = table_entries.get() + i * kDirEntrySize; | |
| 139 // The tag is stored in big-endian order at the start of the entry. | |
| 140 if (memcmp(entry, &table, sizeof(table)) == 0) { | |
|
palmer
2013/03/15 23:16:47
But table isn't necessarily big-endian? Seems like
bbudge
2013/03/16 00:07:03
That's true. I don't think this code was endian-co
| |
| 141 // Font data is stored in net (big-endian) order. | |
| 142 data_offset = | |
|
palmer
2013/03/15 23:16:47
So, only the last table entry affects the ultimate
bbudge
2013/03/16 00:07:03
Only the entry with a matching tag. Offsets are ab
| |
| 143 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8)); | |
| 144 data_length = | |
| 145 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12)); | |
| 146 break; | |
| 147 } | |
| 112 } | 148 } |
| 113 if (*output_length < length) | 149 } |
| 114 return false; | 150 |
| 115 *output_length = length; | 151 // 'data_length' is the length of the file data. |
|
palmer
2013/03/15 23:16:47
STYLE: Document these where they are declared, up
bbudge
2013/03/16 00:07:03
Done.
| |
| 116 ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0)); | 152 // 'data_offset' is its offset in the file. |
| 117 if (n != static_cast<ssize_t>(length)) | 153 data_offset += offset; |
|
palmer
2013/03/15 23:16:47
offset + user_controlled data could overflow, of o
bbudge
2013/03/16 00:07:03
Whew, that should be easier. Done.
On 2013/03/15 2
| |
| 118 return false; | 154 off_t max_offset = base::checked_numeric_cast<off_t>(data_length); |
| 155 if (!data_length || data_offset > max_offset) | |
| 156 return false; | |
| 157 data_length -= data_offset; | |
|
palmer
2013/03/15 23:16:47
Potential underflow?
bbudge
2013/03/16 00:07:03
That is checked above, with max_offset.
On 2013/03
| |
| 158 | |
| 159 if (!output) { | |
| 160 *output_length = data_length; | |
| 119 return true; | 161 return true; |
| 120 } | 162 } |
| 121 | 163 |
| 122 unsigned num_tables; | 164 // 'output_length' holds the maximum amount of data the caller can accept. |
| 123 uint8_t num_tables_buf[2]; | 165 data_length = std::min(data_length, *output_length); |
| 124 | 166 *output_length = data_length; |
| 125 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables_buf, sizeof(num_tables_buf), | 167 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset)); |
| 126 4 /* skip the font type */)); | 168 if (n != base::checked_numeric_cast<ssize_t>(data_length)) |
| 127 if (n != sizeof(num_tables_buf)) | |
| 128 return false; | |
| 129 | |
| 130 num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 | | |
| 131 num_tables_buf[1]; | |
| 132 | |
| 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 } | |
| 158 } | |
| 159 | |
| 160 if (!length) | |
| 161 return false; | |
| 162 | |
| 163 if (!output) { | |
| 164 *output_length = length; | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 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; | 169 return false; |
| 175 | 170 |
| 176 return true; | 171 return true; |
| 177 } | 172 } |
| 178 | 173 |
| 179 } // namespace content | 174 } // namespace content |
| OLD | NEW |