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 bool SecurityCookieCheckHookTransform::TransformBlockGraph( | |
| 36 const TransformPolicyInterface* policy, | |
| 37 BlockGraph* block_graph, | |
| 38 BlockGraph::Block* header_block) { | |
| 39 BlockGraph::Block* report_gsfailure = nullptr; | |
| 40 BlockGraph::BlockMap& blocks = block_graph->blocks_mutable(); | |
| 41 for (auto& block : blocks) { | |
| 42 std::string name(block.second.name()); | |
| 43 if (name == kReportGsFailure) { | |
| 44 report_gsfailure = &block.second; | |
| 45 break; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 if (report_gsfailure == nullptr) { | |
| 50 LOG(ERROR) << "Could not find " << kReportGsFailure << "."; | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 LOG(INFO) << "Found a " << kReportGsFailure | |
| 55 << " implementation, hooking it now."; | |
| 56 BlockGraph::Section* section_text = block_graph->FindOrAddSection( | |
| 57 pe::kCodeSectionName, pe::kCodeCharacteristics); | |
| 58 | |
| 59 // All of the below is needed to build the instrumentation via the assembler. | |
| 60 BasicBlockSubGraph bbsg; | |
| 61 BasicBlockSubGraph::BlockDescription* block_desc = bbsg.AddBlockDescription( | |
| 62 kSyzygyReportGsFailure, nullptr, BlockGraph::CODE_BLOCK, | |
| 63 section_text->id(), 1, 0); | |
| 64 | |
| 65 BasicCodeBlock* bb = bbsg.AddBasicCodeBlock(kSyzygyReportGsFailure); | |
| 66 block_desc->basic_block_order.push_back(bb); | |
| 67 BasicBlockAssembler assm(bb->instructions().begin(), &bb->instructions()); | |
| 68 assm.mov(Operand(Displacement(0xdeadbeef)), assm::eax); | |
|
chrisha
2017/05/10 15:12:08
Useful to expose this as another class constant, g
0vercl0k
2017/05/10 17:49:52
Done.
| |
| 69 | |
| 70 // Condense into a block. | |
| 71 BlockBuilder block_builder(block_graph); | |
| 72 if (!block_builder.Merge(&bbsg)) { | |
| 73 LOG(ERROR) << "Failed to build " << kSyzygyReportGsFailure << " block."; | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 DCHECK_EQ(1u, block_builder.new_blocks().size()); | |
| 78 DCHECK_EQ(1u, report_gsfailure->referrers().size()); | |
| 79 | |
| 80 if (report_gsfailure->referrers().size() != 1) { | |
|
chrisha
2017/05/10 15:12:08
You could move this check up earlier, before build
0vercl0k
2017/05/10 17:49:52
Done.
| |
| 81 // We bail out if we don't have a single referrer as the only | |
| 82 // expected referrer is supposed to be __security_cookie_check. | |
| 83 // If there is more than one, we would rather bail out than take | |
| 84 // a chance at modifying the behavior of the PE image. | |
| 85 LOG(ERROR) << "Only a single referrer is expected."; | |
|
chrisha
2017/05/10 15:12:08
Better: "Only a single referrer to __report_gsfail
0vercl0k
2017/05/10 17:49:52
Done.
| |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 // Transfer the referrers to the new block, and delete the old one. | |
| 90 BlockGraph::Block* syzygy_report_gsfailure = | |
| 91 block_builder.new_blocks().front(); | |
| 92 report_gsfailure->TransferReferrers( | |
| 93 0, syzygy_report_gsfailure, | |
| 94 BlockGraph::Block::kTransferInternalReferences); | |
| 95 | |
| 96 report_gsfailure->RemoveAllReferences(); | |
| 97 if (!block_graph->RemoveBlock(report_gsfailure)) { | |
| 98 LOG(ERROR) << "Removing " << kReportGsFailure << " failed."; | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 } // namespace transforms | |
| 106 } // namespace instrument | |
| OLD | NEW |