diff --git a/SConscript b/SConscript index dc4a159919bcbd89675c50c2db00769a8a1a58ad..22050cb8d38f541921d5033e6b3aa33f8407132a 100644 --- a/SConscript +++ b/SConscript @@ -16,6 +16,7 @@ Import('env') myenv=env myenv.Replace(SECURITY_TRANSFORMS_HOME=os.environ['SECURITY_TRANSFORMS_HOME']) myenv.Replace(ZIPR_HOME=os.environ['ZIPR_HOME']) +myenv.Replace(IRDB_SDK=os.environ['IRDB_SDK']) myenv.Replace(ZIPR_SDK=os.environ['ZIPR_SDK']) myenv.Replace(ZIPR_INSTALL=os.environ['ZIPR_INSTALL']) myenv.Replace(do_cgc=ARGUMENTS.get("do_cgc",0)) @@ -30,8 +31,9 @@ files= ''' cpppath=''' . $ZIPR_HOME/third_party/ELFIO/elfio-2.2 - $SECURITY_TRANSFORMS_HOME/include/ - $SECURITY_TRANSFORMS_HOME/libIRDB/include/ + $IRDB_SDK/include/ + $SECURITY_TRANSFORMS_HOME/include + $SECURITY_TRANSFORMS_HOME/libIRDB/include $SECURITY_TRANSFORMS_HOME/libtransform/include $ZIPR_HOME/include/ $ZIPR_SDK/include/ diff --git a/unpin.cpp b/unpin.cpp index 4a9264db9b4fb857d835b6a3c573a929c572b9af..58949e6c960e1f179f963162bdbf9f4f2d4d1e80 100644 --- a/unpin.cpp +++ b/unpin.cpp @@ -32,14 +32,14 @@ #include <zipr_sdk.h> #include <string> #include <algorithm> -#include "utils.hpp" +// #include "utils.hpp" #include "Rewrite_Utility.hpp" #include "unpin.h" #include <memory> #include <inttypes.h> -using namespace libIRDB; +using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; using namespace ELFIO; @@ -61,10 +61,10 @@ static std::string findAndReplace(const std::string& in_str, const std::string& static bool has_cfi_reloc(Instruction_t* insn) { - for(auto reloc : insn->GetRelocations()) + for(auto reloc : insn->getRelocations()) { /* check for a nonce relocation */ - if ( reloc -> GetType().find("cfi_nonce") != string::npos ) + if ( reloc -> getType().find("cfi_nonce") != string::npos ) { return true; } @@ -85,13 +85,13 @@ ZiprOptionsNamespace_t *Unpin_t::RegisterOptions(ZiprOptionsNamespace_t *global) auto unpin_ns = new ZiprOptionsNamespace_t("unpin"); global->AddOption(&m_verbose); - m_should_cfi_pin.SetDescription("Pin CFI instructions."); + m_should_cfi_pin.setDescription("Pin CFI instructions."); unpin_ns->AddOption(&m_should_cfi_pin); - m_on.SetDescription("Turn unpin plugin on/off."); + m_on.setDescription("Turn unpin plugin on/off."); unpin_ns->AddOption(&m_on); - m_max_unpins.SetDescription("Set how many unpins are allowed, useful for debugging."); + m_max_unpins.setDescription("Set how many unpins are allowed, useful for debugging."); unpin_ns->AddOption(&m_max_unpins); return unpin_ns; @@ -113,21 +113,21 @@ void Unpin_t::DoUnpinForFixedCalls() auto insn_unpins=0; auto missed_unpins=0; - for(auto from_insn : zo->GetFileIR()->GetInstructions()) + for(auto from_insn : zo->getFileIR()->getInstructions()) { - for(auto reloc : from_insn->GetRelocations()) + for(auto reloc : from_insn->getRelocations()) { // this probably won't work on shared objects. // complicated with the push64-reloc plugin also rewriting these things? - if(reloc->GetType()==string("32-bit") || reloc->GetType()==string("push64")) + if(reloc->getType()==string("32-bit") || reloc->getType()==string("push64")) { // skip if there's no WRT, that means it's unpinned for something besides a fixed call. - if(reloc->GetWRT()==NULL) + if(reloc->getWRT()==NULL) continue; // getWRT returns an BaseObj, but this reloc type expects an instruction // safe cast and check. - auto wrt_insn=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + auto wrt_insn=dynamic_cast<Instruction_t*>(reloc->getWRT()); assert(wrt_insn); unpins++; @@ -150,13 +150,13 @@ void Unpin_t::DoUnpinForScoops() auto missed_unpins=0; auto scoop_unpins=0; - for(auto scoop : zo->GetFileIR()->GetDataScoops()) + for(auto scoop : zo->getFileIR()->getDataScoops()) { - for(auto reloc : scoop->GetRelocations()) + for(auto reloc : scoop->getRelocations()) { - if(reloc->GetType()==string("data_to_insn_ptr")) + if(reloc->getType()==string("data_to_insn_ptr")) { - auto insn=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + auto insn=dynamic_cast<Instruction_t*>(reloc->getWRT()); // getWRT returns an BaseObj, but this reloc type expects an instruction // safe cast and check. assert(insn); @@ -188,14 +188,14 @@ Zipr_SDK::ZiprPreference Unpin_t::RetargetCallback( auto& ms=*zo->GetMemorySpace(); auto insn = callback_entry->Instruction(); auto& locMap=*(zo->GetLocationMap()); - for(auto reloc : insn->GetRelocations()) + for(auto reloc : insn->getRelocations()) { - if (reloc->GetType()==string("callback_to_scoop")) + if (reloc->getType()==string("callback_to_scoop")) { - auto wrt = dynamic_cast<DataScoop_t*>(reloc->GetWRT()); - auto addend = reloc->GetAddend(); + auto wrt = dynamic_cast<DataScoop_t*>(reloc->getWRT()); + auto addend = reloc->getAddend(); - target_address = wrt->GetStart()->GetVirtualOffset() + addend; + target_address = wrt->getStart()->getVirtualOffset() + addend; if (m_verbose) { cout << "Unpin::callback_to_scoop: target_addr " @@ -216,29 +216,29 @@ void Unpin_t::DoUpdate() // scan for instructions that were placed, and now need an update. void Unpin_t::DoUpdateForInstructions() { - for(auto from_insn : zo->GetFileIR()->GetInstructions()) + for(auto from_insn : zo->getFileIR()->getInstructions()) { - for(auto reloc : from_insn->GetRelocations()) + for(auto reloc : from_insn->getRelocations()) { // this probably won't work on shared objects. // complicated with the push64-reloc plugin also rewriting these things? - if(reloc->GetType()==string("32-bit") || reloc->GetType()==string("push64")) + if(reloc->getType()==string("32-bit") || reloc->getType()==string("push64")) HandleRetAddrReloc(from_insn,reloc); // instruction has a pcrel memory operand. - else if(reloc->GetType()==string("pcrel")) // && reloc->GetWRT()!=NULL) + else if(reloc->getType()==string("pcrel")) // && reloc->getWRT()!=NULL) HandlePcrelReloc(from_insn,reloc); // instruction has a absolute memory operand that needs it's displacement updated. - else if(reloc->GetType()==string("absoluteptr_to_scoop")) + else if(reloc->getType()==string("absoluteptr_to_scoop")) HandleAbsptrReloc(from_insn,reloc); // instruction has an immediate that needs an update. - else if(reloc->GetType()==string("immedptr_to_scoop")) + else if(reloc->getType()==string("immedptr_to_scoop")) HandleImmedptrReloc(from_insn,reloc); // deal with a callback, think this isn't used anymore - else if(reloc->GetType()==string("callback_to_scoop")) + else if(reloc->getType()==string("callback_to_scoop")) HandleCallbackReloc(from_insn,reloc); } } @@ -246,27 +246,28 @@ void Unpin_t::DoUpdateForInstructions() void Unpin_t::DoUpdateForScoops() { - auto byte_width=zo->GetFileIR()->GetArchitectureBitWidth()/8; - for(auto scoop : zo->GetFileIR()->GetDataScoops()) + auto byte_width=zo->getFileIR()->getArchitectureBitWidth()/8; + for(auto scoop : zo->getFileIR()->getDataScoops()) { - assert(scoop->GetEnd()->GetVirtualOffset() - scoop->GetStart()->GetVirtualOffset()+1 == scoop->GetSize()); - assert(scoop->GetContents().size() == scoop->GetSize()); - auto scoop_contents=scoop->GetContents(); + if(scoop->isExecuteable()) continue; + assert(scoop->getEnd()->getVirtualOffset() - scoop->getStart()->getVirtualOffset()+1 == scoop->getSize()); + assert(scoop->getContents().size() == scoop->getSize()); + auto scoop_contents=scoop->getContents(); - for(auto reloc : scoop->GetRelocations()) + for(auto reloc : scoop->getRelocations()) { - if(reloc->GetType()==string("data_to_insn_ptr")) + if(reloc->getType()==string("data_to_insn_ptr")) { - virtual_offset_t reloff=reloc->GetOffset(); - Instruction_t* insn=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + VirtualOffset_t reloff=reloc->getOffset(); + Instruction_t* insn=dynamic_cast<Instruction_t*>(reloc->getWRT()); // getWRT returns an BaseObj, but this reloc type expects an instruction // safe cast and check. assert(insn); Zipr_SDK::InstructionLocationMap_t &locMap=*(zo->GetLocationMap()); - libIRDB::virtual_offset_t newLoc=locMap[insn]; + IRDB_SDK::VirtualOffset_t newLoc=locMap[insn]; - cout<<"Unpin::Unpinned data_to_insn_ptr insn ("<<hex<<insn->GetBaseID()<<":" - <<insn->getDisassembly()<<") with offset="<<hex<<reloc->GetOffset() + cout<<"Unpin::Unpinned data_to_insn_ptr insn ("<<hex<<insn->getBaseID()<<":" + <<insn->getDisassembly()<<") with offset="<<hex<<reloc->getOffset() <<". Insn moved to "<<hex<<newLoc<<endl; bool found=should_cfi_pin(insn); @@ -279,7 +280,7 @@ void Unpin_t::DoUpdateForScoops() else { // determine how big the ptr is. - int ptrsize=zo->GetFileIR()->GetArchitectureBitWidth()/8; + int ptrsize=zo->getFileIR()->getArchitectureBitWidth()/8; char addr[ptrsize]; // convert it to bytes. @@ -300,41 +301,41 @@ void Unpin_t::DoUpdateForScoops() } } - else if(reloc->GetType()==string("dataptr_to_scoop")) + else if(reloc->getType()==string("dataptr_to_scoop")) { - DataScoop_t *wrt=dynamic_cast<DataScoop_t*>(reloc->GetWRT()); + DataScoop_t *wrt=dynamic_cast<DataScoop_t*>(reloc->getWRT()); assert(wrt); - virtual_offset_t val_to_patch=0; + VirtualOffset_t val_to_patch=0; const char* data=scoop_contents.c_str(); if(byte_width==4) - val_to_patch=*(int*)&data[reloc->GetOffset()]; + val_to_patch=*(int*)&data[reloc->getOffset()]; else if(byte_width==8) - val_to_patch=*(long long*)&data[reloc->GetOffset()]; + val_to_patch=*(long long*)&data[reloc->getOffset()]; else assert(0); // wrt scoop should be placed. - assert(wrt->GetStart()->GetVirtualOffset() !=0 ); - virtual_offset_t new_val_to_patch=val_to_patch + wrt->GetStart()->GetVirtualOffset(); + assert(wrt->getStart()->getVirtualOffset() !=0 ); + VirtualOffset_t new_val_to_patch=val_to_patch + wrt->getStart()->getVirtualOffset(); if(byte_width==4) { unsigned int intnewval=(unsigned int)new_val_to_patch; // 64->32 narrowing OK. - scoop_contents.replace(reloc->GetOffset(), byte_width, (char*)&intnewval, byte_width); + scoop_contents.replace(reloc->getOffset(), byte_width, (char*)&intnewval, byte_width); } else if(byte_width==8) { - scoop_contents.replace(reloc->GetOffset(), byte_width, (char*)&new_val_to_patch, byte_width); + scoop_contents.replace(reloc->getOffset(), byte_width, (char*)&new_val_to_patch, byte_width); } else assert(0); - cout<<"Patched "<<scoop->GetName()<<"+"<<hex<<reloc->GetOffset()<<" to value "<<hex<<new_val_to_patch<<endl; + cout<<"Patched "<<scoop->getName()<<"+"<<hex<<reloc->getOffset()<<" to value "<<hex<<new_val_to_patch<<endl; } } - scoop->SetContents(scoop_contents); + scoop->setContents(scoop_contents); } } @@ -343,7 +344,7 @@ extern "C" Zipr_SDK::ZiprPluginInterface_t* GetPluginInterface( Zipr_SDK::Zipr_t* zipr_object) { - const auto mt=zipr_object->GetFileIR()->GetArchitecture()->getMachineType(); + const auto mt=zipr_object->getFileIR()->getArchitecture()->getMachineType(); return mt==admtX86_64 ? (Unpin_t*)new UnpinX86_t (zipr_object) : diff --git a/unpin.h b/unpin.h index 224b3ffdb0e7ea75bbd8fdcad24ccbaec4b89cc7..367ad7a013d59e9d180e71087fc8fe162c86a8c9 100644 --- a/unpin.h +++ b/unpin.h @@ -31,7 +31,7 @@ #ifndef unpin_h #define unpin_h -#include <libIRDB-core.hpp> +#include <irdb-core> #include <zipr_sdk.h> class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t @@ -48,7 +48,7 @@ class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t missed_unpins(0), ms(*zo->GetMemorySpace()), locMap(*(zo->GetLocationMap())), - firp(*(zo->GetFileIR())) + firp(*(zo->getFileIR())) { } @@ -74,13 +74,13 @@ class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t Zipr_SDK::RangeAddress_t &target_address); protected: // designed for arch-specific override. - virtual void HandleRetAddrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc)=0; - virtual void HandlePcrelReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc)=0; - virtual void HandleAbsptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc)=0; - virtual void HandleImmedptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc)=0; - virtual void HandleCallbackReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc)=0; + virtual void HandleRetAddrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; + virtual void HandlePcrelReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; + virtual void HandleAbsptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; + virtual void HandleImmedptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; + virtual void HandleCallbackReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc)=0; - bool should_cfi_pin(Instruction_t* insn); + bool should_cfi_pin(IRDB_SDK::Instruction_t* insn); // workhorses void DoUnpin(); @@ -102,7 +102,7 @@ class Unpin_t : public Zipr_SDK::ZiprPluginInterface_t int missed_unpins=0; Zipr_SDK::MemorySpace_t& ms; Zipr_SDK::InstructionLocationMap_t& locMap; - libIRDB::FileIR_t& firp; + IRDB_SDK::FileIR_t& firp; }; @@ -117,11 +117,11 @@ class UnpinX86_t : public Unpin_t } protected: // designed for arch-specific override. - void HandleRetAddrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandlePcrelReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleAbsptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleImmedptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleCallbackReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; + void HandleRetAddrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandlePcrelReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleAbsptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleImmedptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleCallbackReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; }; @@ -136,11 +136,11 @@ class UnpinAarch64_t : public Unpin_t } protected: // designed for arch-specific override. - void HandleRetAddrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandlePcrelReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleAbsptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleImmedptrReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; - void HandleCallbackReloc(libIRDB::Instruction_t* from_insn,libIRDB::Relocation_t* reloc) override; + void HandleRetAddrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandlePcrelReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleAbsptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleImmedptrReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; + void HandleCallbackReloc(IRDB_SDK::Instruction_t* from_insn,IRDB_SDK::Relocation_t* reloc) override; }; diff --git a/unpin_aarch64.cpp b/unpin_aarch64.cpp index e3f101d2149a3dd926bde3e072dd92d7b62d558c..12e0ea6363f4f955d45d5087b22c09adcc323688 100644 --- a/unpin_aarch64.cpp +++ b/unpin_aarch64.cpp @@ -39,7 +39,7 @@ #include <inttypes.h> -using namespace libIRDB; +using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; using namespace ELFIO; @@ -54,32 +54,32 @@ void UnpinAarch64_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* reloc) { // decode the instruction and find the pcrel operand - const auto disasm=DecodedInstruction_t(from_insn); - const auto operands=disasm.getOperands(); - const auto the_arg_it=find_if(ALLOF(operands),[](const DecodedOperand_t& op){ return op.isPcrel(); }); - const auto bo_wrt=reloc->GetWRT(); - const auto scoop_wrt=dynamic_cast<DataScoop_t*>(reloc->GetWRT()); - const auto insn_wrt=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + const auto disasm=DecodedInstruction_t::factory(from_insn); + const auto operands=disasm->getOperands(); + const auto the_arg_it=find_if(ALLOF(operands),[](const shared_ptr<DecodedOperand_t>& op){ return op->isPcrel(); }); + const auto bo_wrt=reloc->getWRT(); + const auto scoop_wrt=dynamic_cast<DataScoop_t*>(reloc->getWRT()); + const auto insn_wrt=dynamic_cast<Instruction_t*>(reloc->getWRT()); assert(the_arg_it!=operands.end()); const auto the_arg=*the_arg_it; - const auto mt=firp.GetArchitecture()->getMachineType(); + const auto mt=firp.getArchitecture()->getMachineType(); // get the new insn addr - const auto from_insn_location=(virtual_offset_t)locMap[from_insn]; + const auto from_insn_location=(VirtualOffset_t)locMap[from_insn]; // get WRT info - libIRDB::virtual_offset_t to_addr=0xdeadbeef; // noteable value that shouldn't be used. + IRDB_SDK::VirtualOffset_t to_addr=0xdeadbeef; // noteable value that shouldn't be used. string convert_string; if(scoop_wrt) { - to_addr=scoop_wrt->GetStart()->GetVirtualOffset(); - convert_string=string("scoop ")+scoop_wrt->GetName(); + to_addr=scoop_wrt->getStart()->getVirtualOffset(); + convert_string=string("scoop ")+scoop_wrt->getName(); } else if(insn_wrt) { to_addr=locMap[insn_wrt]; - convert_string=string("insn ")+to_string(insn_wrt->GetBaseID())+ + convert_string=string("insn ")+to_string(insn_wrt->getBaseID())+ ":"+insn_wrt->getDisassembly(); } else @@ -92,23 +92,23 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re assert(bo_wrt==nullptr); // not yet imp'd WRT offsetting. assert(to_addr==0); // not yet imp'd WRT offsetting. - const auto mnemonic = disasm.getMnemonic(); + const auto mnemonic = disasm->getMnemonic(); const auto is_adr_type = mnemonic=="adr"; const auto is_adrp_type = mnemonic=="adrp"; const auto is_ldr_type = mnemonic=="ldr"; - const auto is_ldr_int_type = is_ldr_type && disasm.getOperand(0).isGeneralPurposeRegister(); - const auto is_ldr_fp_type = is_ldr_type && disasm.getOperand(0).isFpuRegister(); + const auto is_ldr_int_type = is_ldr_type && disasm->getOperand(0)->isGeneralPurposeRegister(); + const auto is_ldr_fp_type = is_ldr_type && disasm->getOperand(0)->isFpuRegister(); const auto is_ldrsw_type = mnemonic=="ldrsw"; const auto mask1 =(1<< 1)-1; const auto mask2 =(1<< 2)-1; const auto mask5 =(1<< 5)-1; const auto mask12=(1<<12)-1; const auto mask19=(1<<19)-1; - const auto orig_insn_addr=from_insn->GetAddress()->GetVirtualOffset(); // original location + const auto orig_insn_addr=from_insn->getAddress()->getVirtualOffset(); // original location const auto insn_bytes_len=4; // arm is always 4. uint8_t insn_bytes[insn_bytes_len]; // compiler disallows init on some platforms. // but memcpy should init it sufficiently. - memcpy(insn_bytes, from_insn->GetDataBits().c_str(), insn_bytes_len); + memcpy(insn_bytes, from_insn->getDataBits().c_str(), insn_bytes_len); const auto full_insn=*(uint32_t*)insn_bytes; const auto op_byte=insn_bytes[3]; @@ -131,7 +131,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re const auto orig_insn_pageno = orig_insn_addr>>shift_dist; const auto new_insn_pageno = from_insn_location>>shift_dist; const auto new_imm21_ext = imm21_ext + (int64_t)orig_insn_pageno - - (int64_t)new_insn_pageno + (int64_t)reloc->GetAddend()+(int64_t)to_addr; + (int64_t)new_insn_pageno + (int64_t)reloc->getAddend()+(int64_t)to_addr; // make sure no overflow. if(((new_imm21_ext << 43) >> 43) == new_imm21_ext) @@ -146,7 +146,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re { cout << "Relocating a adr(p) pcrel relocation with orig_pageno=" << hex << (orig_insn_pageno << 12) << " offset=(page-pc+" << imm21_ext << ")" << endl; - cout << "Based on: " << disasm.getDisassembly() << hex << " originally at " << orig_insn_addr + cout << "Based on: " << disasm->getDisassembly() << hex << " originally at " << orig_insn_addr << " now located at : 0x" << hex << from_insn_location << " with offset=(page-pc + " << new_imm21_ext << ")" << endl; } @@ -165,10 +165,10 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re * L2: b ft */ const auto tramp_size=3*4; // 3 insns, 4 bytes each - const auto address_to_generate=imm21_ext+orig_insn_addr+(int64_t)reloc->GetAddend()+(int64_t)to_addr; + const auto address_to_generate=imm21_ext+orig_insn_addr+(int64_t)reloc->getAddend()+(int64_t)to_addr; const auto destreg=full_insn&mask5; - const auto tramp_range=ms.GetFreeRange(tramp_size); - const auto tramp_start=tramp_range.GetStart(); + const auto tramp_range=ms.getFreeRange(tramp_size); + const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. ms.SplitFreeRange({tramp_start,tramp_start+12}); @@ -218,7 +218,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re zo->ApplyPatch(L2,FT); // should be few enough of these to always print - cout<< "Had to trampoline " << disasm.getDisassembly() << "@"<<FA<<" to " + cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " << hex << L0 << "-" << L0+tramp_size << endl; } } @@ -228,8 +228,8 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // ldr s/d/q reg : opc2 0111 0 0 imm19 Rt5, opc2 indicate size (00,01,10 -> s/d/q) const auto imm19 = ((int64_t)full_insn >> 5 ) & mask19; const auto imm19_ext= (imm19 << 45) >> 45; - const auto referenced_addr=(imm19_ext<<2)+from_insn->GetAddress()->GetVirtualOffset()+4; - const auto new_imm19_ext =((int64_t)referenced_addr-(int64_t)from_insn_location-4+(int64_t)reloc->GetAddend()+(int64_t)to_addr)>>2; + const auto referenced_addr=(imm19_ext<<2)+from_insn->getAddress()->getVirtualOffset()+4; + const auto new_imm19_ext =((int64_t)referenced_addr-(int64_t)from_insn_location-4+(int64_t)reloc->getAddend()+(int64_t)to_addr)>>2; if( ((new_imm19_ext << 45) >> 45) == new_imm19_ext) { const auto clean_new_insn = full_insn & ~(mask19 << 5); @@ -240,14 +240,14 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re { cout << "Relocating a ldr pcrel relocation with orig_addr=" << hex << (referenced_addr) << " offset=(pc+" << imm19_ext << ")" << endl; - cout << "Based on: " << disasm.getDisassembly() + cout << "Based on: " << disasm->getDisassembly() << " now located at : 0x" << hex << from_insn_location << " with offset=(pc + " << new_imm19_ext << ")" << endl; } } else { - const auto address_to_generate=(imm19_ext<<2)+orig_insn_addr+(int64_t)reloc->GetAddend()+(int64_t)to_addr; + const auto address_to_generate=(imm19_ext<<2)+orig_insn_addr+(int64_t)reloc->getAddend()+(int64_t)to_addr; const auto destreg=full_insn&mask5; const auto FA=from_insn_location; const auto FT=from_insn_location+4; @@ -268,8 +268,8 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re * L2: b ft */ const auto tramp_size=3*4; // 3 insns, 4 bytes each - const auto tramp_range=ms.GetFreeRange(tramp_size); - const auto tramp_start=tramp_range.GetStart(); + const auto tramp_range=ms.getFreeRange(tramp_size); + const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. ms.SplitFreeRange({tramp_start,tramp_start+12}); @@ -319,7 +319,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re zo->ApplyPatch(L2,FT); // should be few enough of these to always print - cout<< "Had to trampoline " << disasm.getDisassembly() << "@"<<FA<<" to " + cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " << hex << L0 << "-" << L0+tramp_size-1 << endl; } @@ -342,8 +342,8 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // allocate and reserve space for the code. const auto tramp_size=5*4; // 3 insns, 4 bytes each - const auto tramp_range=ms.GetFreeRange(tramp_size); - const auto tramp_start=tramp_range.GetStart(); + const auto tramp_range=ms.getFreeRange(tramp_size); + const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. ms.SplitFreeRange({tramp_start,tramp_start+12}); @@ -428,7 +428,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re zo->ApplyPatch(L4,FT); // should be few enough of these to always print - cout<< "Had to trampoline " << disasm.getDisassembly() << "@"<<FA<<" to " + cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " << hex << L0 << "-" << L0+tramp_size-1 << endl; } @@ -442,8 +442,8 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re // ldrsw x reg : 1001 1000 imm19 Rt const auto imm19 = ((int64_t)full_insn >> 5 ) & mask19; const auto imm19_ext= (imm19 << 45) >> 45; - const auto referenced_addr=(imm19_ext<<2)+from_insn->GetAddress()->GetVirtualOffset()+4; - const auto new_imm19_ext =((int64_t)referenced_addr-(int64_t)from_insn_location-4+(int64_t)reloc->GetAddend()+(int64_t)to_addr)>>2; + const auto referenced_addr=(imm19_ext<<2)+from_insn->getAddress()->getVirtualOffset()+4; + const auto new_imm19_ext =((int64_t)referenced_addr-(int64_t)from_insn_location-4+(int64_t)reloc->getAddend()+(int64_t)to_addr)>>2; if( ((new_imm19_ext << 45) >> 45) == new_imm19_ext) { const auto clean_new_insn = full_insn & ~(mask19 << 5); @@ -454,7 +454,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re { cout << "Relocating a ldrsw pcrel relocation with orig_addr=" << hex << (referenced_addr) << " offset=(pc+" << imm19_ext << ")" << endl; - cout << "Based on: " << disasm.getDisassembly() + cout << "Based on: " << disasm->getDisassembly() << " now located at : 0x" << hex << from_insn_location << " with offset=(pc + " << new_imm19_ext << ")" << endl; } @@ -472,10 +472,10 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re * L2: b ft */ const auto tramp_size=3*4; // 3 insns, 4 bytes each - const auto address_to_generate=(imm19_ext<<2)+orig_insn_addr+(int64_t)reloc->GetAddend()+(int64_t)to_addr; + const auto address_to_generate=(imm19_ext<<2)+orig_insn_addr+(int64_t)reloc->getAddend()+(int64_t)to_addr; const auto destreg=full_insn&mask5; - const auto tramp_range=ms.GetFreeRange(tramp_size); - const auto tramp_start=tramp_range.GetStart(); + const auto tramp_range=ms.getFreeRange(tramp_size); + const auto tramp_start=tramp_range.getStart(); // don't be too fancy, just reserve 12 bytes. ms.SplitFreeRange({tramp_start,tramp_start+12}); @@ -525,7 +525,7 @@ void UnpinAarch64_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* re zo->ApplyPatch(L2,FT); // should be few enough of these to always print - cout<< "Had to trampoline " << disasm.getDisassembly() << "@"<<FA<<" to " + cout<< "Had to trampoline " << disasm->getDisassembly() << "@"<<FA<<" to " << hex << L0 << "-" << L0+tramp_size-1 << endl; } } diff --git a/unpin_x86.cpp b/unpin_x86.cpp index 61577cac20a0618adfe846bfaa5d77c5e9a62c5b..b3185187f39982c68a7bbc2879ade23f63857ace 100644 --- a/unpin_x86.cpp +++ b/unpin_x86.cpp @@ -39,7 +39,7 @@ #include <inttypes.h> -using namespace libIRDB; +using namespace IRDB_SDK; using namespace std; using namespace Zipr_SDK; using namespace ELFIO; @@ -49,12 +49,12 @@ using namespace ELFIO; void UnpinX86_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* reloc) { // skip if there's no WRT, that means it's unpinned for something besides a fixed call. - if(reloc->GetWRT()==NULL) + if(reloc->getWRT()==NULL) return; // getWRT returns an BaseObj, but this reloc type expects an instruction // safe cast and check. - auto wrt_insn=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + auto wrt_insn=dynamic_cast<Instruction_t*>(reloc->getWRT()); assert(wrt_insn); if(should_cfi_pin(wrt_insn)) return; @@ -63,14 +63,14 @@ void UnpinX86_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* relo auto from_insn_location=locMap[from_insn]; // 32-bit code and main executables just push a full 32-bit addr. - if(zo->GetELFIO()->get_type()==ET_EXEC) + if(zo->getELFIO()->get_type()==ET_EXEC) { // not handled in push64_relocs which is disabled for shared objects. // expecting a 32-bit push, length=5 - assert(from_insn->GetDataBits()[0]==0x68); - assert(from_insn->GetDataBits().size()==5); + assert(from_insn->getDataBits()[0]==0x68); + assert(from_insn->getDataBits().size()==5); // down and upcast to ensure we fit in 31-bits. - assert(wrt_insn_location == (libIRDB::virtual_offset_t)(int)wrt_insn_location); + assert(wrt_insn_location == (IRDB_SDK::VirtualOffset_t)(int)wrt_insn_location); assert(sizeof(int)==4); // paranoid. unsigned char newpush[5]; @@ -78,10 +78,10 @@ void UnpinX86_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* relo *(int*)&newpush[1]=(int)wrt_insn_location; cout<<"Unpin::Updating push32/push64-exe insn:" - <<dec<<from_insn->GetBaseID()<<":"<<from_insn->getDisassembly()<<"@"<<hex<<from_insn_location<<" to point at " - <<dec<<wrt_insn ->GetBaseID()<<":"<<wrt_insn ->getDisassembly()<<"@"<<hex<<wrt_insn_location <<endl; + <<dec<<from_insn->getBaseID()<<":"<<from_insn->getDisassembly()<<"@"<<hex<<from_insn_location<<" to point at " + <<dec<<wrt_insn ->getBaseID()<<":"<<wrt_insn ->getDisassembly()<<"@"<<hex<<wrt_insn_location <<endl; - for(auto i=0U;i<from_insn->GetDataBits().size();i++) + for(auto i=0U;i<from_insn->getDataBits().size();i++) { unsigned char newbyte=newpush[i]; ms[from_insn_location+i]=newbyte; @@ -97,32 +97,32 @@ void UnpinX86_t::HandleRetAddrReloc(Instruction_t* from_insn, Relocation_t* relo void UnpinX86_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* reloc) { // decode the instruction and find the pcrel operand - const auto disasm=DecodedInstruction_t(from_insn); - const auto operands=disasm.getOperands(); - const auto the_arg_it=find_if(ALLOF(operands),[](const DecodedOperand_t& op){ return op.isPcrel(); }); - const auto bo_wrt=reloc->GetWRT(); - const auto scoop_wrt=dynamic_cast<DataScoop_t*>(reloc->GetWRT()); - const auto insn_wrt=dynamic_cast<Instruction_t*>(reloc->GetWRT()); + const auto disasm=DecodedInstruction_t::factory(from_insn); + const auto operands=disasm->getOperands(); + const auto the_arg_it=find_if(ALLOF(operands),[](const shared_ptr<DecodedOperand_t>& op){ return op->isPcrel(); }); + const auto bo_wrt=reloc->getWRT(); + const auto scoop_wrt=dynamic_cast<DataScoop_t*>(reloc->getWRT()); + const auto insn_wrt=dynamic_cast<Instruction_t*>(reloc->getWRT()); assert(the_arg_it!=operands.end()); const auto the_arg=*the_arg_it; - const auto mt=firp.GetArchitecture()->getMachineType(); + const auto mt=firp.getArchitecture()->getMachineType(); // get the new insn addr - const auto from_insn_location=(virtual_offset_t)locMap[from_insn]; + const auto from_insn_location=(VirtualOffset_t)locMap[from_insn]; // get WRT info - libIRDB::virtual_offset_t to_addr=0xdeadbeef; // noteable value that shouldn't be used. + IRDB_SDK::VirtualOffset_t to_addr=0xdeadbeef; // noteable value that shouldn't be used. string convert_string; if(scoop_wrt) { - to_addr=scoop_wrt->GetStart()->GetVirtualOffset(); - convert_string=string("scoop ")+scoop_wrt->GetName(); + to_addr=scoop_wrt->getStart()->getVirtualOffset(); + convert_string=string("scoop ")+scoop_wrt->getName(); } else if(insn_wrt) { to_addr=locMap[insn_wrt]; - convert_string=string("insn ")+to_string(insn_wrt->GetBaseID())+ + convert_string=string("insn ")+to_string(insn_wrt->getBaseID())+ ":"+insn_wrt->getDisassembly(); } else @@ -132,89 +132,89 @@ void UnpinX86_t::HandlePcrelReloc(Instruction_t* from_insn, Relocation_t* reloc) convert_string=string("no-object"); } - const auto rel_addr1=the_arg.getMemoryDisplacement()+from_insn->GetDataBits().size(); - const auto disp_offset=(int)disasm.getMemoryDisplacementOffset(the_arg,from_insn); - const auto disp_size=(int)the_arg.getMemoryDisplacementEncodingSize(); + const auto rel_addr1=the_arg->getMemoryDisplacement()+from_insn->getDataBits().size(); + const auto disp_offset=(int)disasm->getMemoryDisplacementOffset(the_arg.get(),from_insn); + const auto disp_size=(int)the_arg->getMemoryDisplacementEncodingSize(); assert(disp_size==4); - assert(0<disp_offset && disp_offset<=from_insn->GetDataBits().size() - disp_size); + assert(0<disp_offset && disp_offset<=from_insn->getDataBits().size() - disp_size); - const auto new_disp=(int)(rel_addr1 + to_addr - from_insn->GetDataBits().size()-from_insn_location); - const auto newbits=from_insn->GetDataBits().replace(disp_offset, disp_size, (char*)&new_disp, disp_size); - from_insn->SetDataBits(newbits); + const auto new_disp=(int)(rel_addr1 + to_addr - from_insn->getDataBits().size()-from_insn_location); + const auto newbits=from_insn->getDataBits().replace(disp_offset, disp_size, (char*)&new_disp, disp_size); + from_insn->setDataBits(newbits); ms.PlopBytes(from_insn_location, newbits.c_str(), newbits.size()); - const auto disasm2=DecodedInstruction_t(from_insn); + const auto disasm2=DecodedInstruction_t::factory(from_insn); cout<<"unpin:pcrel:new_disp="<<hex<<new_disp<<endl; cout<<"unpin:pcrel:new_insn_addr="<<hex<<from_insn_location<<endl; - cout<<"unpin:pcrel:Converting "<<hex<<from_insn->GetBaseID()<<":"<<disasm.getDisassembly() - <<" to "<<disasm2.getDisassembly() <<" wrt "<< convert_string <<endl; + cout<<"unpin:pcrel:Converting "<<hex<<from_insn->getBaseID()<<":"<<disasm->getDisassembly() + <<" to "<<disasm2->getDisassembly() <<" wrt "<< convert_string <<endl; } void UnpinX86_t::HandleAbsptrReloc(Instruction_t* from_insn, Relocation_t* reloc) { - const auto disasm=DecodedInstruction_t(from_insn); - const auto operands=disasm.getOperands(); + const auto disasm=DecodedInstruction_t::factory(from_insn); + const auto operands=disasm->getOperands(); // push/pop from memory might have a memory operand with no string to represent the implicit stack operand. - const auto the_arg_it=find_if(ALLOF(operands),[](const DecodedOperand_t& op){ return op.isMemory() && op.getString()!=""; }); - DataScoop_t* wrt=dynamic_cast<DataScoop_t*>(reloc->GetWRT()); + const auto the_arg_it=find_if(ALLOF(operands),[](const shared_ptr<DecodedOperand_t>& op){ return op->isMemory() && op->getString()!=""; }); + DataScoop_t* wrt=dynamic_cast<DataScoop_t*>(reloc->getWRT()); assert(wrt); assert(the_arg_it!=operands.end()); const auto &the_arg=*the_arg_it; - virtual_offset_t rel_addr1=the_arg.getMemoryDisplacement(); + VirtualOffset_t rel_addr1=the_arg->getMemoryDisplacement(); - int disp_offset=disasm.getMemoryDisplacementOffset(the_arg,from_insn); - int disp_size=the_arg.getMemoryDisplacementEncodingSize(); + int disp_offset=disasm->getMemoryDisplacementOffset(the_arg.get(),from_insn); + int disp_size=the_arg->getMemoryDisplacementEncodingSize(); assert(disp_size==4); - assert(0<disp_offset && disp_offset<=from_insn->GetDataBits().size() - disp_size); - assert(reloc->GetWRT()); + assert(0<disp_offset && disp_offset<=from_insn->getDataBits().size() - disp_size); + assert(reloc->getWRT()); - unsigned int new_disp=the_arg.getMemoryDisplacement() + wrt->GetStart()->GetVirtualOffset(); - from_insn->SetDataBits(from_insn->GetDataBits().replace(disp_offset, disp_size, (char*)&new_disp, disp_size)); + unsigned int new_disp=the_arg->getMemoryDisplacement() + wrt->getStart()->getVirtualOffset(); + from_insn->setDataBits(from_insn->getDataBits().replace(disp_offset, disp_size, (char*)&new_disp, disp_size)); // update the instruction in the memory space. - libIRDB::virtual_offset_t from_insn_location=locMap[from_insn]; - for(unsigned int i=0;i<from_insn->GetDataBits().size();i++) + IRDB_SDK::VirtualOffset_t from_insn_location=locMap[from_insn]; + for(unsigned int i=0;i<from_insn->getDataBits().size();i++) { - unsigned char newbyte=from_insn->GetDataBits()[i]; + unsigned char newbyte=from_insn->getDataBits()[i]; ms[from_insn_location+i]=newbyte; //cout<<"Updating push["<<i<<"] from "<<hex<<oldbyte<<" to "<<newbyte<<endl; } - const auto disasm2=DecodedInstruction_t(from_insn); - cout<<"unpin:absptr_to_scoop:Converting "<<hex<<from_insn->GetBaseID()<<":"<<disasm.getDisassembly() - <<" to "<<disasm2.getDisassembly() <<" for scoop: "<<wrt->GetName()<<endl; + const auto disasm2=DecodedInstruction_t::factory(from_insn); + cout<<"unpin:absptr_to_scoop:Converting "<<hex<<from_insn->getBaseID()<<":"<<disasm->getDisassembly() + <<" to "<<disasm2->getDisassembly() <<" for scoop: "<<wrt->getName()<<endl; } void UnpinX86_t::HandleImmedptrReloc(Instruction_t* from_insn, Relocation_t* reloc) { - DataScoop_t* wrt=dynamic_cast<DataScoop_t*>(reloc->GetWRT()); + DataScoop_t* wrt=dynamic_cast<DataScoop_t*>(reloc->getWRT()); assert(wrt); - const auto disasm=DecodedInstruction_t(from_insn); - virtual_offset_t rel_addr2=disasm.getImmediate(); - virtual_offset_t new_addr = rel_addr2 + wrt->GetStart()->GetVirtualOffset(); + const auto disasm=DecodedInstruction_t::factory(from_insn); + VirtualOffset_t rel_addr2=disasm->getImmediate(); + VirtualOffset_t new_addr = rel_addr2 + wrt->getStart()->getVirtualOffset(); - from_insn->SetDataBits(from_insn->GetDataBits().replace(from_insn->GetDataBits().size()-4, 4, (char*)&new_addr, 4)); + from_insn->setDataBits(from_insn->getDataBits().replace(from_insn->getDataBits().size()-4, 4, (char*)&new_addr, 4)); - libIRDB::virtual_offset_t from_insn_location=locMap[from_insn]; - for(unsigned int i=0;i<from_insn->GetDataBits().size();i++) + IRDB_SDK::VirtualOffset_t from_insn_location=locMap[from_insn]; + for(unsigned int i=0;i<from_insn->getDataBits().size();i++) { - unsigned char newbyte=from_insn->GetDataBits()[i]; + unsigned char newbyte=from_insn->getDataBits()[i]; ms[from_insn_location+i]=newbyte; //cout<<"Updating push["<<i<<"] from "<<hex<<oldbyte<<" to "<<newbyte<<endl; } - const auto disasm2=DecodedInstruction_t(from_insn); - cout<<"unpin:immedptr_to_scoop:Converting "<<hex<<from_insn->GetBaseID()<<":"<<disasm.getDisassembly() - <<" to "<<disasm2.getDisassembly() <<" for scoop: "<<wrt->GetName()<<endl; + const auto disasm2=DecodedInstruction_t::factory(from_insn); + cout<<"unpin:immedptr_to_scoop:Converting "<<hex<<from_insn->getBaseID()<<":"<<disasm->getDisassembly() + <<" to "<<disasm2->getDisassembly() <<" for scoop: "<<wrt->getName()<<endl; } void UnpinX86_t::HandleCallbackReloc(Instruction_t* from_insn, Relocation_t* reloc) { - DataScoop_t *wrt = dynamic_cast<DataScoop_t*>(reloc->GetWRT()); - int addend = reloc->GetAddend(); + DataScoop_t *wrt = dynamic_cast<DataScoop_t*>(reloc->getWRT()); + int addend = reloc->getAddend(); char bytes[]={(char)0x48, (char)0x8d, (char)0x64, @@ -225,7 +225,7 @@ void UnpinX86_t::HandleCallbackReloc(Instruction_t* from_insn, Relocation_t* rel if (m_verbose) cout << "The call insn is " - << from_insn->GetDataBits().length() << " bytes long." << endl; + << from_insn->getDataBits().length() << " bytes long." << endl; call_addr = locMap[from_insn]; @@ -238,14 +238,14 @@ void UnpinX86_t::HandleCallbackReloc(Instruction_t* from_insn, Relocation_t* rel * Put down the bogus pop. */ at = call_addr + 1; - at = call_addr + from_insn->GetDataBits().length(); + at = call_addr + from_insn->getDataBits().length(); ms.PlopBytes(at, bytes, sizeof(bytes)); /* * Turn off the following flags so that this * is left alone when it is being plopped. */ - from_insn->SetTarget(NULL); - from_insn->SetCallback(""); + from_insn->setTarget(NULL); + from_insn->setCallback(""); }