From 5eeedc4d4fa48fc24a817331556ac13f9f3a660b Mon Sep 17 00:00:00 2001 From: Jason Hiser <jdhiser@gmail.com> Date: Thu, 20 Dec 2018 10:48:00 -0500 Subject: [PATCH] unified filenames --- src/SConscript | 5 +- src/{archbase.cpp => arch_base.cpp} | 4 +- src/patcher_arm64.cpp | 71 ++++++++++ src/patcher_base.cpp | 73 +++++++++++ src/patcher_x86.cpp | 193 ++++++++++++++++++++++++++++ src/pinner_arm64.cpp | 2 +- src/pinner_base.cpp | 4 +- src/pinner_x86.cpp | 4 +- src/zipr.cpp | 4 +- 9 files changed, 351 insertions(+), 9 deletions(-) rename src/{archbase.cpp => arch_base.cpp} (93%) create mode 100644 src/patcher_arm64.cpp create mode 100644 src/patcher_base.cpp create mode 100644 src/patcher_x86.cpp diff --git a/src/SConscript b/src/SConscript index b3b1641..6642023 100644 --- a/src/SConscript +++ b/src/SConscript @@ -23,10 +23,13 @@ files= ''' zipr_dollop_man.cpp elfwrite.cpp ehwrite.cpp - archbase.cpp + arch_base.cpp pinner_arm64.cpp pinner_base.cpp pinner_x86.cpp + patcher_arm64.cpp + patcher_base.cpp + patcher_x86.cpp ''' # ELFIO needs to be first so we get the zipr version instead of the sectrans version. the zipr version is modified to include get_offset. diff --git a/src/archbase.cpp b/src/arch_base.cpp similarity index 93% rename from src/archbase.cpp rename to src/arch_base.cpp index 7af208b..719f7ba 100644 --- a/src/archbase.cpp +++ b/src/arch_base.cpp @@ -4,8 +4,8 @@ namespace zipr { -#include <arch/archX86.hpp> -#include <arch/archARM64.hpp> +#include <arch/arch_x86.hpp> +#include <arch/arch_arm64.hpp> } #include <memory> #include <Rewrite_Utility.hpp> diff --git a/src/patcher_arm64.cpp b/src/patcher_arm64.cpp new file mode 100644 index 0000000..9d00993 --- /dev/null +++ b/src/patcher_arm64.cpp @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. + * + * This software is furnished under a license and/or other restrictive + * terms and may be used and copied only in accordance with such terms + * and the inclusion of the above copyright notice. This software or + * any other copies thereof may not be provided or otherwise made + * available to any other person without the express written consent + * of an authorized representative of Zephyr Software LCC. Title to, + * ownership of, and all rights in the software is retained by + * Zephyr Software LCC. + * + * Zephyr Software LLC. Proprietary Information + * + * Unless otherwise specified, the information contained in this + * directory, following this legend, and/or referenced herein is + * Zephyr Software LLC. (Zephyr) Proprietary Information. + * + * CONTACT + * + * For technical assistance, contact Zephyr Software LCC. at: + * + * + * Zephyr Software, LLC + * 2040 Tremont Rd + * Charlottesville, VA 22911 + * + * E-mail: jwd@zephyr-software.com + **************************************************************************/ + +#include <zipr_all.h> +namespace zipr +{ +#include "patcher/patcher_arm64.hpp" +} +#include <libIRDB-core.hpp> +#include <Rewrite_Utility.hpp> +#include <iostream> +#include <stdlib.h> +#include <string.h> +#include <map> +#include <assert.h> +#include <sys/mman.h> +#include <ctype.h> +#include <iostream> // std::cout +#include <string> // std::string, std::to_string +#include <fstream> + +#include "elfio/elfio.hpp" +#include "elfio/elfio_dump.hpp" +#include "targ-config.h" +//#include <bea_deprecated.hpp> + +#define ALLOF(a) begin(a),end(a) + +using namespace libIRDB; +using namespace std; +using namespace zipr; +using namespace ELFIO; +using namespace IRDBUtility; + +ZiprPatcherARM64_t::ZiprPatcherARM64_t(Zipr_SDK::Zipr_t* p_parent) +{ assert(0); } +void ZiprPatcherARM64_t::ApplyNopToPatch(RangeAddress_t addr) +{ assert(0); } +void ZiprPatcherARM64_t::ApplyPatch(RangeAddress_t from_addr, RangeAddress_t to_addr) +{ assert(0); } +void ZiprPatcherARM64_t::PatchJump(RangeAddress_t at_addr, RangeAddress_t to_addr) +{ assert(0); } + + diff --git a/src/patcher_base.cpp b/src/patcher_base.cpp new file mode 100644 index 0000000..8e290ad --- /dev/null +++ b/src/patcher_base.cpp @@ -0,0 +1,73 @@ +/*************************************************************************** + * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. + * + * This software is furnished under a license and/or other restrictive + * terms and may be used and copied only in accordance with such terms + * and the inclusion of the above copyright notice. This software or + * any other copies thereof may not be provided or otherwise made + * available to any other person without the express written consent + * of an authorized representative of Zephyr Software LCC. Title to, + * ownership of, and all rights in the software is retained by + * Zephyr Software LCC. + * + * Zephyr Software LLC. Proprietary Information + * + * Unless otherwise specified, the information contained in this + * directory, following this legend, and/or referenced herein is + * Zephyr Software LLC. (Zephyr) Proprietary Information. + * + * CONTACT + * + * For technical assistance, contact Zephyr Software LCC. at: + * + * + * Zephyr Software, LLC + * 2040 Tremont Rd + * Charlottesville, VA 22911 + * + * E-mail: jwd@zephyr-software.com + **************************************************************************/ + +#include <zipr_all.h> +{ +#include "patcher/patcher_arm64.hpp" +#include "patcher/patcher_x86.hpp" +} + +#include <libIRDB-core.hpp> +#include <Rewrite_Utility.hpp> +#include <iostream> +#include <stdlib.h> +#include <string.h> +#include <map> +#include <assert.h> +#include <sys/mman.h> +#include <ctype.h> +#include <iostream> // std::cout +#include <string> // std::string, std::to_string +#include <fstream> + +#include "elfio/elfio.hpp" +#include "elfio/elfio_dump.hpp" +#include "targ-config.h" +//#include <bea_deprecated.hpp> + +#define ALLOF(a) begin(a),end(a) + +using namespace libIRDB; +using namespace std; +using namespace zipr; +using namespace ELFIO; +using namespace IRDBUtility; + +unique_ptr<ZiprPatcherBase_t> ZiprPatcherBase_t::factory(Zipr_SDK::Zipr_t* p_parent) +{ + auto l_firp=p_parent->GetFileIR(); + auto ret= l_firp->GetArchitecture()->getMachineType() == admtX86_64 ? (ZiprPatcherBase_t*)new ZiprPatcherX86_t (p_parent) : + l_firp->GetArchitecture()->getMachineType() == admtI386 ? (ZiprPatcherBase_t*)new ZiprPatcherX86_t (p_parent) : + l_firp->GetArchitecture()->getMachineType() == admtAarch64 ? (ZiprPatcherBase_t*)new ZiprPatcherARM64_t(p_parent) : + throw domain_error("Cannot init architecture"); + + return unique_ptr<ZiprPatcherBase_t>(ret); +} + diff --git a/src/patcher_x86.cpp b/src/patcher_x86.cpp new file mode 100644 index 0000000..1fa777f --- /dev/null +++ b/src/patcher_x86.cpp @@ -0,0 +1,193 @@ +/*************************************************************************** + * Copyright (c) 2014 Zephyr Software LLC. All rights reserved. + * + * This software is furnished under a license and/or other restrictive + * terms and may be used and copied only in accordance with such terms + * and the inclusion of the above copyright notice. This software or + * any other copies thereof may not be provided or otherwise made + * available to any other person without the express written consent + * of an authorized representative of Zephyr Software LCC. Title to, + * ownership of, and all rights in the software is retained by + * Zephyr Software LCC. + * + * Zephyr Software LLC. Proprietary Information + * + * Unless otherwise specified, the information contained in this + * directory, following this legend, and/or referenced herein is + * Zephyr Software LLC. (Zephyr) Proprietary Information. + * + * CONTACT + * + * For technical assistance, contact Zephyr Software LCC. at: + * + * + * Zephyr Software, LLC + * 2040 Tremont Rd + * Charlottesville, VA 22911 + * + * E-mail: jwd@zephyr-software.com + **************************************************************************/ + +#include <zipr_all.h> +namespace zipr +{ +#include "patcher/patcher_x86.hpp" +} +#include <libIRDB-core.hpp> +#include <Rewrite_Utility.hpp> +#include <iostream> +#include <stdlib.h> +#include <string.h> +#include <map> +#include <assert.h> +#include <sys/mman.h> +#include <ctype.h> +#include <iostream> // std::cout +#include <string> // std::string, std::to_string +#include <fstream> + +#include "elfio/elfio.hpp" +#include "elfio/elfio_dump.hpp" +#include "targ-config.h" + +#define ALLOF(a) begin(a),end(a) + +using namespace libIRDB; +using namespace std; +using namespace zipr; +using namespace ELFIO; +using namespace IRDBUtility; + +ZiprPatcherX86_t::ZiprPatcherX86_t(Zipr_SDK::Zipr_t* p_parent) : + m_parent(dynamic_cast<zipr::ZiprImpl_t*>(p_parent)), // upcast to ZiprImpl + m_firp(p_parent->GetFileIR()), + memory_space(*p_parent->GetMemorySpace()) +{ +} + +void ZiprPatcherX86_t::RewritePCRelOffset(RangeAddress_t from_addr,RangeAddress_t to_addr, int insn_length, int offset_pos) +{ + int new_offset=((unsigned int)to_addr)-((unsigned int)from_addr)-((unsigned int)insn_length); + + memory_space[from_addr+offset_pos+0]=(new_offset>>0)&0xff; + memory_space[from_addr+offset_pos+1]=(new_offset>>8)&0xff; + memory_space[from_addr+offset_pos+2]=(new_offset>>16)&0xff; + memory_space[from_addr+offset_pos+3]=(new_offset>>24)&0xff; +} + +void ZiprPatcherX86_t::ApplyNopToPatch(RangeAddress_t addr) +{ + /* + * TODO: Add assertion that this is really a patch. + */ + + /* + * 0F 1F 44 00 00H + */ + memory_space[addr] = (unsigned char)0x0F; + memory_space[addr+1] = (unsigned char)0x1F; + memory_space[addr+2] = (unsigned char)0x44; + memory_space[addr+3] = (unsigned char)0x00; + memory_space[addr+4] = (unsigned char)0x00; +} + +void ZiprPatcherX86_t::ApplyPatch(RangeAddress_t from_addr, RangeAddress_t to_addr) +{ + unsigned char insn_first_byte=memory_space[from_addr]; + unsigned char insn_second_byte=memory_space[from_addr+1]; + + switch(insn_first_byte) + { + case (unsigned char)0xF: // two byte escape + { + assert( insn_second_byte==(unsigned char)0x80 || // should be a JCC + insn_second_byte==(unsigned char)0x81 || + insn_second_byte==(unsigned char)0x82 || + insn_second_byte==(unsigned char)0x83 || + insn_second_byte==(unsigned char)0x84 || + insn_second_byte==(unsigned char)0x85 || + insn_second_byte==(unsigned char)0x86 || + insn_second_byte==(unsigned char)0x87 || + insn_second_byte==(unsigned char)0x88 || + insn_second_byte==(unsigned char)0x89 || + insn_second_byte==(unsigned char)0x8a || + insn_second_byte==(unsigned char)0x8b || + insn_second_byte==(unsigned char)0x8c || + insn_second_byte==(unsigned char)0x8d || + insn_second_byte==(unsigned char)0x8e || + insn_second_byte==(unsigned char)0x8f ); + + RewritePCRelOffset(from_addr,to_addr,6,2); + break; + } + + case (unsigned char)0xe8: // call + case (unsigned char)0xe9: // jmp + { + RewritePCRelOffset(from_addr,to_addr,5,1); + break; + } + + case (unsigned char)0xf0: // lock + case (unsigned char)0xf2: // rep/repe + case (unsigned char)0xf3: // repne + case (unsigned char)0x2e: // cs override + case (unsigned char)0x36: // ss override + case (unsigned char)0x3e: // ds override + case (unsigned char)0x26: // es override + case (unsigned char)0x64: // fs override + case (unsigned char)0x65: // gs override + case (unsigned char)0x66: // operand size override + case (unsigned char)0x67: // address size override + { + cout << "found patch for instruction with prefix. prefix is: "<<hex<<insn_first_byte<<". Recursing at "<<from_addr+1<<dec<<endl; + // recurse at addr+1 if we find a prefix byte has been plopped. + return this->ApplyPatch(from_addr+1, to_addr); + } + default: + { + if(m_firp->GetArchitectureBitWidth()==64) /* 64-bit x86 machine assumed */ + { + /* check for REX prefix */ + if((unsigned char)0x40 <= insn_first_byte && insn_first_byte <= (unsigned char)0x4f) + { + cout << "found patch for instruction with prefix. prefix is: "<<hex<<insn_first_byte<<". Recursing at "<<from_addr+1<<dec<<endl; + // recurse at addr+1 if we find a prefix byte has been plopped. + return this->ApplyPatch(from_addr+1, to_addr); + } + } + std::cerr << "insn_first_byte: 0x" << hex << (int)insn_first_byte << dec << std::endl; + assert(0); + } + } +} + +void ZiprPatcherX86_t::PatchJump(RangeAddress_t at_addr, RangeAddress_t to_addr) +{ + uintptr_t off=to_addr-at_addr-2; + + assert(!memory_space.IsByteFree(at_addr)); + + switch(memory_space[at_addr]) + { + case (char)0xe9: /* 5byte jump */ + { + RewritePCRelOffset(at_addr,to_addr,5,1); + break; + } + case (char)0xeb: /* 2byte jump */ + { + assert(off==(uintptr_t)(char)off); + + assert(!memory_space.IsByteFree(at_addr+1)); + memory_space[at_addr+1]=(char)off; + break; + } + default: + { + assert(false); + } + } +} + + diff --git a/src/pinner_arm64.cpp b/src/pinner_arm64.cpp index 765669b..63e28b2 100644 --- a/src/pinner_arm64.cpp +++ b/src/pinner_arm64.cpp @@ -2,7 +2,7 @@ namespace zipr { -#include <pinner/pinnerARM64.hpp> +#include <pinner/pinner_arm64.hpp> } #include <memory> #include <Rewrite_Utility.hpp> diff --git a/src/pinner_base.cpp b/src/pinner_base.cpp index 7707fea..a4c1b1c 100644 --- a/src/pinner_base.cpp +++ b/src/pinner_base.cpp @@ -2,8 +2,8 @@ namespace zipr { -#include <pinner/pinnerX86.hpp> -#include <pinner/pinnerARM64.hpp> +#include <pinner/pinner_x86.hpp> +#include <pinner/pinner_arm64.hpp> } #include <memory> #include <Rewrite_Utility.hpp> diff --git a/src/pinner_x86.cpp b/src/pinner_x86.cpp index e9f907b..8c11488 100644 --- a/src/pinner_x86.cpp +++ b/src/pinner_x86.cpp @@ -12,8 +12,8 @@ namespace zipr { -#include <pinner/pinnerX86.hpp> -#include <pinner/pinnerARM64.hpp> +#include <pinner/pinne_x86.hpp> +#include <pinner/pinner_arm64.hpp> } #include <memory> #include <Rewrite_Utility.hpp> diff --git a/src/zipr.cpp b/src/zipr.cpp index f88b4fe..2fbcd7b 100644 --- a/src/zipr.cpp +++ b/src/zipr.cpp @@ -55,6 +55,8 @@ using namespace zipr; using namespace ELFIO; using namespace IRDBUtility; + +#if 0 class ZiprPatcherBase_t { public: @@ -224,7 +226,7 @@ unique_ptr<ZiprPatcherBase_t> ZiprPatcherBase_t::factory(Zipr_SDK::Zipr_t* p_par return unique_ptr<ZiprPatcherBase_t>(ret); } - +#endif inline uintptr_t page_round_up(uintptr_t x) { -- GitLab