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 if (report_gsfailure->referrers().size() != 1) { |
| 58 // We bail out if we don't have a single referrer as the only |
| 59 // expected referrer is supposed to be __security_cookie_check. |
| 60 // If there is more than one, we would rather bail out than take |
| 61 // a chance at modifying the behavior of the PE image. |
| 62 LOG(ERROR) << "Only a single referrer to " << kReportGsFailure |
| 63 << " is expected."; |
| 64 return false; |
| 65 } |
| 66 |
| 67 LOG(INFO) << "Found a " << kReportGsFailure |
| 68 << " implementation, hooking it now."; |
| 69 BlockGraph::Section* section_text = block_graph->FindOrAddSection( |
| 70 pe::kCodeSectionName, pe::kCodeCharacteristics); |
| 71 |
| 72 // All of the below is needed to build the instrumentation via the assembler. |
| 73 BasicBlockSubGraph bbsg; |
| 74 BasicBlockSubGraph::BlockDescription* block_desc = bbsg.AddBlockDescription( |
| 75 kSyzygyReportGsFailure, nullptr, BlockGraph::CODE_BLOCK, |
| 76 section_text->id(), 1, 0); |
| 77 |
| 78 BasicCodeBlock* bb = bbsg.AddBasicCodeBlock(kSyzygyReportGsFailure); |
| 79 block_desc->basic_block_order.push_back(bb); |
| 80 BasicBlockAssembler assm(bb->instructions().begin(), &bb->instructions()); |
| 81 assm.mov(Operand(Displacement(kInvalidUserAddress)), assm::eax); |
| 82 |
| 83 // Condense into a block. |
| 84 BlockBuilder block_builder(block_graph); |
| 85 if (!block_builder.Merge(&bbsg)) { |
| 86 LOG(ERROR) << "Failed to build " << kSyzygyReportGsFailure << " block."; |
| 87 return false; |
| 88 } |
| 89 |
| 90 DCHECK_EQ(1u, block_builder.new_blocks().size()); |
| 91 |
| 92 // Transfer the referrers to the new block, and delete the old one. |
| 93 BlockGraph::Block* syzygy_report_gsfailure = |
| 94 block_builder.new_blocks().front(); |
| 95 report_gsfailure->TransferReferrers( |
| 96 0, syzygy_report_gsfailure, |
| 97 BlockGraph::Block::kTransferInternalReferences); |
| 98 |
| 99 report_gsfailure->RemoveAllReferences(); |
| 100 if (!block_graph->RemoveBlock(report_gsfailure)) { |
| 101 LOG(ERROR) << "Removing " << kReportGsFailure << " failed."; |
| 102 return false; |
| 103 } |
| 104 |
| 105 return true; |
| 106 } |
| 107 |
| 108 } // namespace transforms |
| 109 } // namespace instrument |
OLD | NEW |