| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "content/public/common/common_sandbox_support_linux.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <sys/stat.h> |
| 9 |
| 10 #include <limits> |
| 11 #include <memory> |
| 12 |
| 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/sys_byteorder.h" |
| 16 #include "base/trace_event/trace_event.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 bool GetFontTable(int fd, |
| 21 uint32_t table_tag, |
| 22 off_t offset, |
| 23 uint8_t* output, |
| 24 size_t* output_length) { |
| 25 if (offset < 0) |
| 26 return false; |
| 27 |
| 28 size_t data_length = 0; // the length of the file data. |
| 29 off_t data_offset = 0; // the offset of the data in the file. |
| 30 if (table_tag == 0) { |
| 31 // Get the entire font file. |
| 32 struct stat st; |
| 33 if (fstat(fd, &st) < 0) |
| 34 return false; |
| 35 data_length = base::checked_cast<size_t>(st.st_size); |
| 36 } else { |
| 37 // Get a font table. Read the header to find its offset in the file. |
| 38 uint16_t num_tables; |
| 39 ssize_t n = HANDLE_EINTR( |
| 40 pread(fd, &num_tables, sizeof(num_tables), 4 /* skip the font type */)); |
| 41 if (n != sizeof(num_tables)) |
| 42 return false; |
| 43 // Font data is stored in net (big-endian) order. |
| 44 num_tables = base::NetToHost16(num_tables); |
| 45 |
| 46 // Read the table directory. |
| 47 static const size_t kTableEntrySize = 16; |
| 48 const size_t directory_size = num_tables * kTableEntrySize; |
| 49 std::unique_ptr<uint8_t[]> table_entries(new uint8_t[directory_size]); |
| 50 n = HANDLE_EINTR(pread(fd, table_entries.get(), directory_size, |
| 51 12 /* skip the SFNT header */)); |
| 52 if (n != base::checked_cast<ssize_t>(directory_size)) |
| 53 return false; |
| 54 |
| 55 for (uint16_t i = 0; i < num_tables; ++i) { |
| 56 uint8_t* entry = table_entries.get() + i * kTableEntrySize; |
| 57 uint32_t tag = *reinterpret_cast<uint32_t*>(entry); |
| 58 if (tag == table_tag) { |
| 59 // Font data is stored in net (big-endian) order. |
| 60 data_offset = |
| 61 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 8)); |
| 62 data_length = |
| 63 base::NetToHost32(*reinterpret_cast<uint32_t*>(entry + 12)); |
| 64 break; |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 if (!data_length) |
| 70 return false; |
| 71 // Clamp |offset| inside the allowable range. This allows the read to succeed |
| 72 // but return 0 bytes. |
| 73 offset = std::min(offset, base::checked_cast<off_t>(data_length)); |
| 74 // Make sure it's safe to add the data offset and the caller's logical offset. |
| 75 // Define the maximum positive offset on 32 bit systems. |
| 76 static const off_t kMaxPositiveOffset32 = 0x7FFFFFFF; // 2 GB - 1. |
| 77 if ((offset > kMaxPositiveOffset32 / 2) || |
| 78 (data_offset > kMaxPositiveOffset32 / 2)) |
| 79 return false; |
| 80 data_offset += offset; |
| 81 data_length -= offset; |
| 82 |
| 83 if (output) { |
| 84 // 'output_length' holds the maximum amount of data the caller can accept. |
| 85 data_length = std::min(data_length, *output_length); |
| 86 ssize_t n = HANDLE_EINTR(pread(fd, output, data_length, data_offset)); |
| 87 if (n != base::checked_cast<ssize_t>(data_length)) |
| 88 return false; |
| 89 } |
| 90 *output_length = data_length; |
| 91 |
| 92 return true; |
| 93 } |
| 94 |
| 95 } // namespace content |
| OLD | NEW |