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