Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // | |
| 15 | |
| 16 #include "syzygy/instrument/transforms/security_cookie_check_hook_transform.h" | |
| 17 | |
| 18 #include "syzygy/pe/pe_utils.h" | |
| 19 | |
| 20 namespace instrument { | |
| 21 namespace transforms { | |
| 22 | |
| 23 using block_graph::Displacement; | |
| 24 using block_graph::Operand; | |
| 25 | |
| 26 const char SecurityCookieCheckHookTransform::kTransformName[] = | |
| 27 "SecurityCookieCheckHookTransform"; | |
| 28 | |
| 29 const char SecurityCookieCheckHookTransform::kReportGsFailure[] = | |
| 30 "__report_gsfailure"; | |
| 31 | |
| 32 const char SecurityCookieCheckHookTransform::kSyzygyReportGsFailure[] = | |
| 33 "__syzygy_report_gsfailure"; | |
| 34 | |
| 35 const uint32_t SecurityCookieCheckHookTransform::kInvalidUserAddress = | |
| 36 0xdeadbeef; | |
| 37 | |
| 38 bool SecurityCookieCheckHookTransform::TransformBlockGraph( | |
| 39 const TransformPolicyInterface* policy, | |
| 40 BlockGraph* block_graph, | |
| 41 BlockGraph::Block* header_block) { | |
| 42 BlockGraph::Block* report_gsfailure = nullptr; | |
| 43 BlockGraph::BlockMap& blocks = block_graph->blocks_mutable(); | |
| 44 for (auto& block : blocks) { | |
| 45 std::string name(block.second.name()); | |
| 46 if (name == kReportGsFailure) { | |
| 47 report_gsfailure = &block.second; | |
| 48 break; | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 if (report_gsfailure == nullptr) { | |
| 53 LOG(ERROR) << "Could not find " << kReportGsFailure << "."; | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 DCHECK_EQ(1u, report_gsfailure->referrers().size()); | |
| 58 | |
| 59 if (report_gsfailure->referrers().size() != 1) { | |
|
chrisha
2017/05/10 20:18:42
(Sorry, didn't catch this before.)
No need for bo
0vercl0k
2017/05/10 20:51:46
No worries - I was a bit confused about it too :).
| |
| 60 // We bail out if we don't have a single referrer as the only | |
| 61 // expected referrer is supposed to be __security_cookie_check. | |
| 62 // If there is more than one, we would rather bail out than take | |
| 63 // a chance at modifying the behavior of the PE image. | |
| 64 LOG(ERROR) << "Only a single referrer to " << kReportGsFailure | |
| 65 << " is expected."; | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 LOG(INFO) << "Found a " << kReportGsFailure | |
| 70 << " implementation, hooking it now."; | |
| 71 BlockGraph::Section* section_text = block_graph->FindOrAddSection( | |
| 72 pe::kCodeSectionName, pe::kCodeCharacteristics); | |
| 73 | |
| 74 // All of the below is needed to build the instrumentation via the assembler. | |
| 75 BasicBlockSubGraph bbsg; | |
| 76 BasicBlockSubGraph::BlockDescription* block_desc = bbsg.AddBlockDescription( | |
| 77 kSyzygyReportGsFailure, nullptr, BlockGraph::CODE_BLOCK, | |
| 78 section_text->id(), 1, 0); | |
| 79 | |
| 80 BasicCodeBlock* bb = bbsg.AddBasicCodeBlock(kSyzygyReportGsFailure); | |
| 81 block_desc->basic_block_order.push_back(bb); | |
| 82 BasicBlockAssembler assm(bb->instructions().begin(), &bb->instructions()); | |
| 83 assm.mov(Operand(Displacement(kInvalidUserAddress)), assm::eax); | |
| 84 | |
| 85 // Condense into a block. | |
| 86 BlockBuilder block_builder(block_graph); | |
| 87 if (!block_builder.Merge(&bbsg)) { | |
| 88 LOG(ERROR) << "Failed to build " << kSyzygyReportGsFailure << " block."; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 DCHECK_EQ(1u, block_builder.new_blocks().size()); | |
| 93 | |
| 94 // Transfer the referrers to the new block, and delete the old one. | |
| 95 BlockGraph::Block* syzygy_report_gsfailure = | |
| 96 block_builder.new_blocks().front(); | |
| 97 report_gsfailure->TransferReferrers( | |
| 98 0, syzygy_report_gsfailure, | |
| 99 BlockGraph::Block::kTransferInternalReferences); | |
| 100 | |
| 101 report_gsfailure->RemoveAllReferences(); | |
| 102 if (!block_graph->RemoveBlock(report_gsfailure)) { | |
| 103 LOG(ERROR) << "Removing " << kReportGsFailure << " failed."; | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 } // namespace transforms | |
| 111 } // namespace instrument | |
| OLD | NEW |