Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Arthur Sonzogni | b948e67 | 2024-07-31 08:29:04 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
Etienne Pierre-doray | efbbccf | 2018-07-24 15:42:33 | [diff] [blame] | 10 | #include "components/zucchini/reloc_win32.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
| 13 | #include <tuple> |
| 14 | #include <utility> |
| 15 | |
Samuel Huang | ff0bc47 | 2017-10-26 06:55:15 | [diff] [blame] | 16 | #include "base/logging.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 17 | #include "base/numerics/safe_conversions.h" |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 18 | #include "components/zucchini/algorithm.h" |
| 19 | #include "components/zucchini/io_utils.h" |
| 20 | #include "components/zucchini/type_win_pe.h" |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 21 | |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 22 | namespace zucchini { |
| 23 | |
| 24 | /******** RelocUnitWin32 ********/ |
| 25 | |
| 26 | RelocUnitWin32::RelocUnitWin32() = default; |
| 27 | RelocUnitWin32::RelocUnitWin32(uint8_t type_in, |
| 28 | offset_t location_in, |
| 29 | rva_t target_rva_in) |
| 30 | : type(type_in), location(location_in), target_rva(target_rva_in) {} |
| 31 | |
| 32 | bool operator==(const RelocUnitWin32& a, const RelocUnitWin32& b) { |
| 33 | return std::tie(a.type, a.location, a.target_rva) == |
| 34 | std::tie(b.type, b.location, b.target_rva); |
| 35 | } |
| 36 | |
| 37 | /******** RelocRvaReaderWin32 ********/ |
| 38 | |
| 39 | // static |
| 40 | bool RelocRvaReaderWin32::FindRelocBlocks( |
| 41 | ConstBufferView image, |
| 42 | BufferRegion reloc_region, |
| 43 | std::vector<offset_t>* reloc_block_offsets) { |
| 44 | CHECK_LT(reloc_region.size, kOffsetBound); |
| 45 | ConstBufferView reloc_data = image[reloc_region]; |
| 46 | reloc_block_offsets->clear(); |
| 47 | while (reloc_data.size() >= sizeof(pe::RelocHeader)) { |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 48 | reloc_block_offsets->push_back( |
| 49 | base::checked_cast<offset_t>(reloc_data.begin() - image.begin())); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 50 | auto size = reloc_data.read<pe::RelocHeader>(0).size; |
| 51 | // |size| must be aligned to 4-bytes. |
| 52 | if (size < sizeof(pe::RelocHeader) || size % 4 || size > reloc_data.size()) |
| 53 | return false; |
| 54 | reloc_data.remove_prefix(size); |
| 55 | } |
| 56 | return reloc_data.empty(); // Fail if trailing data exist. |
| 57 | } |
| 58 | |
| 59 | RelocRvaReaderWin32::RelocRvaReaderWin32( |
| 60 | ConstBufferView image, |
| 61 | BufferRegion reloc_region, |
| 62 | const std::vector<offset_t>& reloc_block_offsets, |
| 63 | offset_t lo, |
| 64 | offset_t hi) |
| 65 | : image_(image) { |
| 66 | CHECK_LE(lo, hi); |
| 67 | lo = base::checked_cast<offset_t>(reloc_region.InclusiveClamp(lo)); |
| 68 | hi = base::checked_cast<offset_t>(reloc_region.InclusiveClamp(hi)); |
| 69 | end_it_ = image_.begin() + hi; |
| 70 | |
| 71 | // By default, get GetNext() to produce empty output. |
| 72 | cur_reloc_units_ = BufferSource(end_it_, 0); |
| 73 | if (reloc_block_offsets.empty()) |
| 74 | return; |
| 75 | |
| 76 | // Find the block that contains |lo|. |
| 77 | auto block_it = std::upper_bound(reloc_block_offsets.begin(), |
| 78 | reloc_block_offsets.end(), lo); |
| 79 | DCHECK(block_it != reloc_block_offsets.begin()); |
| 80 | --block_it; |
| 81 | |
| 82 | // Initialize |cur_reloc_units_| and |rva_hi_bits_|. |
| 83 | if (!LoadRelocBlock(image_.begin() + *block_it)) |
| 84 | return; // Nothing left. |
| 85 | |
| 86 | // Skip |cur_reloc_units_| to |lo|, truncating up. |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 87 | offset_t cur_reloc_units_offset = |
| 88 | base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin()); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 89 | if (lo > cur_reloc_units_offset) { |
| 90 | offset_t delta = |
Samuel Huang | 2c50b5af | 2018-06-13 17:47:39 | [diff] [blame] | 91 | AlignCeil<offset_t>(lo - cur_reloc_units_offset, kRelocUnitSize); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame] | 92 | // Okay if this empties |cur_reloc_units_|. |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 93 | cur_reloc_units_.Skip(delta); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | RelocRvaReaderWin32::RelocRvaReaderWin32(RelocRvaReaderWin32&&) = default; |
| 98 | |
| 99 | RelocRvaReaderWin32::~RelocRvaReaderWin32() = default; |
| 100 | |
| 101 | // Unrolls a nested loop: outer = reloc blocks and inner = reloc entries. |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 102 | std::optional<RelocUnitWin32> RelocRvaReaderWin32::GetNext() { |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 103 | // "Outer loop" to find non-empty reloc block. |
| 104 | while (cur_reloc_units_.Remaining() < kRelocUnitSize) { |
| 105 | if (!LoadRelocBlock(cur_reloc_units_.end())) |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 106 | return std::nullopt; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 107 | } |
| 108 | if (end_it_ - cur_reloc_units_.begin() < kRelocUnitSize) |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 109 | return std::nullopt; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 110 | // "Inner loop" to extract single reloc unit. |
Etienne Pierre-doray | f6c70a2 | 2018-09-11 13:40:46 | [diff] [blame] | 111 | offset_t location = |
| 112 | base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin()); |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 113 | uint16_t entry = cur_reloc_units_.read<uint16_t>(0); |
| 114 | uint8_t type = static_cast<uint8_t>(entry >> 12); |
| 115 | rva_t rva = rva_hi_bits_ + (entry & 0xFFF); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame] | 116 | if (!cur_reloc_units_.Skip(kRelocUnitSize)) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 117 | return std::nullopt; |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame] | 118 | } |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 119 | return RelocUnitWin32{type, location, rva}; |
| 120 | } |
| 121 | |
| 122 | bool RelocRvaReaderWin32::LoadRelocBlock( |
| 123 | ConstBufferView::const_iterator block_begin) { |
| 124 | ConstBufferView header_buf(block_begin, sizeof(pe::RelocHeader)); |
| 125 | if (header_buf.end() >= end_it_ || |
| 126 | end_it_ - header_buf.end() < kRelocUnitSize) { |
| 127 | return false; |
| 128 | } |
| 129 | const auto& header = header_buf.read<pe::RelocHeader>(0); |
| 130 | rva_hi_bits_ = header.rva_hi; |
| 131 | uint32_t block_size = header.size; |
Calder Kitagawa | 9754448 | 2018-04-05 12:56:50 | [diff] [blame] | 132 | if (block_size < sizeof(pe::RelocHeader)) |
| 133 | return false; |
| 134 | if ((block_size - sizeof(pe::RelocHeader)) % kRelocUnitSize != 0) |
| 135 | return false; |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 136 | cur_reloc_units_ = BufferSource(block_begin, block_size); |
Samuel Huang | c019fe9 | 2023-05-31 04:14:21 | [diff] [blame] | 137 | cur_reloc_units_.Skip(sizeof(pe::RelocHeader)); // Always succeeds. |
huangs | 062026a1 | 2017-10-05 16:26:05 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | /******** RelocReaderWin32 ********/ |
|
|