Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(472)

Side by Side Diff: content/common/child_process_sandbox_support_impl_linux.cc

Issue 12433021: Modify content::GetFontTable so clients can control what is read. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clamp offsets larger than the data, don't fail. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 if (offset < 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.
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;
112 } 120 ssize_t n = HANDLE_EINTR(pread(fd, &num_tables, sizeof(num_tables),
113 if (*output_length < length) 121 4 /* skip the font type */));
122 if (n != sizeof(num_tables))
114 return false; 123 return false;
115 *output_length = length; 124 // Font data is stored in net (big-endian) order.
116 ssize_t n = HANDLE_EINTR(pread(fd, output, length, 0)); 125 num_tables = base::NetToHost16(num_tables);
117 if (n != static_cast<ssize_t>(length)) 126
127 // Read the table directory.
128 static const size_t kTableEntrySize = 16;
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))
118 return false; 134 return false;
119 return true;
120 }
121 135
122 unsigned num_tables; 136 for (uint16_t i = 0; i < num_tables; ++i) {
123 uint8_t num_tables_buf[2]; 137 uint8_t* entry = table_entries.get() + i * kTableEntrySize;
124 138 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), 139 if (tag == table_tag) {
126 4 /* skip the font type */)); 140 // Font data is stored in net (big-endian) order.
127 if (n != sizeof(num_tables_buf)) 141 data_offset =
128 return false; 142 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8));
129 143 data_length =
130 num_tables = static_cast<unsigned>(num_tables_buf[0]) << 8 | 144 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12));
131 num_tables_buf[1]; 145 break;
132 146 }
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 } 147 }
158 } 148 }
159 149
160 if (!length) 150 if (!data_length)
161 return false; 151 return false;
152 // Clamp |offset| inside the allowable range. This allows the read to succeed
153 // but return 0 bytes.
154 offset = std::min(offset, base::checked_numeric_cast<off_t>(data_length));
bbudge 2013/03/20 19:50:04 Without this change, it's difficult to match font
155 // Make sure it's safe to add the data offset and the caller's logical offset.
156 // Define the maximum positive offset on 32 bit systems.
157 static const off_t kMaxPositiveOffset32 = 0x7FFFFFFF; // 2 GB - 1.
158 if ((offset > kMaxPositiveOffset32 / 2) ||
159 (data_offset > kMaxPositiveOffset32 / 2))
160 return false;
161 data_offset += offset;
162 data_length -= offset;
162 163
163 if (!output) { 164 if (output) {
164 *output_length = length; 165 // 'output_length' holds the maximum amount of data the caller can accept.
165 return true; 166 data_length = std::min(data_length, *output_length);
167 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset));
168 if (n != base::checked_numeric_cast<ssize_t>(data_length))
169 return false;
166 } 170 }
167 171 *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 172
176 return true; 173 return true;
177 } 174 }
178 175
179 } // namespace content 176 } // namespace content
OLDNEW
« no previous file with comments | « chrome/renderer/pepper/ppb_pdf_impl.cc ('k') | content/public/common/child_process_sandbox_support_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698