Index: syzygy/instrument/transforms/security_cookie_check_hook_transform.cc |
diff --git a/syzygy/instrument/transforms/security_cookie_check_hook_transform.cc b/syzygy/instrument/transforms/security_cookie_check_hook_transform.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d4669c7e454836bdf404a13f82ac2044942f92fa |
--- /dev/null |
+++ b/syzygy/instrument/transforms/security_cookie_check_hook_transform.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2017 Google Inc. All Rights Reserved. |
+// |
+// Licensed under the Apache License, Version 2.0 (the "License"); |
+// you may not use this file except in compliance with the License. |
+// You may obtain a copy of the License at |
+// |
+// http://www.apache.org/licenses/LICENSE-2.0 |
+// |
+// Unless required by applicable law or agreed to in writing, software |
+// distributed under the License is distributed on an "AS IS" BASIS, |
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+// See the License for the specific language governing permissions and |
+// limitations under the License. |
+// |
+ |
+#include "syzygy/instrument/transforms/security_cookie_check_hook_transform.h" |
+ |
+#include "syzygy/pe/pe_utils.h" |
+ |
+namespace instrument { |
+namespace transforms { |
+ |
+using block_graph::Displacement; |
+using block_graph::Operand; |
+ |
+const char SecurityCookieCheckHookTransform::kTransformName[] = |
+ "SecurityCookieCheckHookTransform"; |
+ |
+bool SecurityCookieCheckHookTransform::TransformBlockGraph( |
+ const TransformPolicyInterface* policy, |
+ BlockGraph* block_graph, |
+ BlockGraph::Block* header_block |
chrisha
2017/05/09 19:18:39
Indent parameters another +2 (4 from start of line
|
+) { |
chrisha
2017/05/09 19:18:39
Move to the previous line.
|
+ BlockGraph::Block *__report_gsfailure = nullptr; |
chrisha
2017/05/09 19:18:40
Move * to left (BlockGraph::Block* __report_gsfail
|
+ const BlockGraph::BlockMap &blocks = block_graph->blocks(); |
chrisha
2017/05/09 19:18:39
Move & to left.
|
+ for (const auto &block : blocks) { |
chrisha
2017/05/09 19:18:39
Move & to left (auto& block)
|
+ std::string name(block.second.name()); |
+ if (name == "__report_gsfailure") { |
chrisha
2017/05/09 19:18:39
Make an anonymous namespaced static global at the
|
+ __report_gsfailure = block_graph->GetBlockById(block.first); |
chrisha
2017/05/09 19:18:39
You've already got the block, no need to go throug
|
+ break; |
+ } |
+ } |
+ |
+ if (__report_gsfailure == nullptr) { |
+ LOG(ERROR) << "Could not find __report_gsfailure."; |
+ return false; |
+ } |
+ |
+ LOG(INFO) << "Found a __report_gsfailure implementation, hooking it now.."; |
chrisha
2017/05/09 19:18:40
Remove extra period.
|
+ BlockGraph::Section* section_text = block_graph->FindOrAddSection( |
+ pe::kCodeSectionName, |
+ pe::kCodeCharacteristics |
chrisha
2017/05/09 19:18:39
Both of these parameters fit on one line, indented
|
+ ); |
chrisha
2017/05/09 19:18:39
Move to end of previous line.
|
+ |
+ // All of the below is needed to build the instrumentation via the assembler |
chrisha
2017/05/09 19:18:39
Missing period on comment.
|
+ BasicBlockSubGraph bbsg; |
+ BasicBlockSubGraph::BlockDescription* block_desc = bbsg.AddBlockDescription( |
+ "__my_report_gsfailure", |
+ nullptr, |
+ BlockGraph::CODE_BLOCK, |
+ section_text->id(), |
+ 1, |
+ 0 |
chrisha
2017/05/09 19:18:39
These likely all fit on one line.
|
+ ); |
chrisha
2017/05/09 19:18:39
Move to end of previous line.
|
+ |
+ BasicCodeBlock* bb = bbsg.AddBasicCodeBlock("__my_report_gsfailure"); |
chrisha
2017/05/09 19:18:39
Prefix with __syzygy_ maybe? Slightly more consist
|
+ block_desc->basic_block_order.push_back(bb); |
+ BasicBlockAssembler assm(bb->instructions().begin(), &bb->instructions()); |
+ assm.mov( |
+ Operand(Displacement(0xdeadbeef)), |
+ assm::eax |
chrisha
2017/05/09 19:18:39
Indent another 2 spaces.
|
+ ); |
chrisha
2017/05/09 19:18:39
Move to previous line.
|
+ |
+ // Condense into a block |
chrisha
2017/05/09 19:18:39
Missing period on comment.
|
+ BlockBuilder block_builder(block_graph); |
+ if (!block_builder.Merge(&bbsg)) { |
+ LOG(ERROR) << "Failed to build __my_report_gsfailure block."; |
chrisha
2017/05/09 19:18:39
Create another string constant with this name and
|
+ return false; |
+ } |
+ |
+ // Exactly one new block should have been created |
chrisha
2017/05/09 19:18:39
This should be a DCHECK (kind of like an assertion
|
+ if (block_builder.new_blocks().size() != 1) { |
+ LOG(ERROR) << "Only one block should have been built."; |
+ return false; |
+ } |
+ |
+ if (__report_gsfailure->references().size() != 1) { |
chrisha
2017/05/09 19:18:39
Ditto with this as a DCHECK assertion.
|
+ VLOG(1) << "Only a single reference is expected."; |
+ } |
+ |
+ // Transfer the referrers to the new block, and delete the old one |
+ BlockGraph::Block* __my_report_gsfailure = block_builder.new_blocks().front(); |
+ __report_gsfailure->TransferReferrers( |
+ 0, |
+ __my_report_gsfailure, |
+ BlockGraph::Block::kTransferInternalReferences |
chrisha
2017/05/09 19:18:39
Indent all of these another 2 spaces, try to fit a
|
+ ); |
chrisha
2017/05/09 19:18:39
Move to end of previous line.
|
+ |
+ __report_gsfailure->RemoveAllReferences(); |
+ if (!block_graph->RemoveBlock(__report_gsfailure)) { |
+ LOG(ERROR) << "Removing __report_gsfailure failed."; |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace transforms |
+} // namespace instrument |