diff --git a/libEXEIO/include/exeio.h b/libEXEIO/include/exeio.h index 2da0c87070182cdae946700a239c9ca44b6aca49..483a3c19a06e37eabd6953c63aab411d37d55435 100644 --- a/libEXEIO/include/exeio.h +++ b/libEXEIO/include/exeio.h @@ -23,6 +23,8 @@ namespace EXEIO public: virtual bool isLoadable() const =0; virtual bool isExecutable() const =0; + virtual bool isWriteable() const =0; + virtual bool isReadable() const =0; virtual bool isBSS() const =0; virtual const char* get_data() const =0; virtual std::string get_name() const =0; diff --git a/libEXEIO/include/exeio_elf.h b/libEXEIO/include/exeio_elf.h index 9dc698b220681cce9c2e174fe1d5ed41828a4cb8..4013d27d4165461688117463ca83b24fe7e78ad3 100644 --- a/libEXEIO/include/exeio_elf.h +++ b/libEXEIO/include/exeio_elf.h @@ -23,6 +23,8 @@ namespace EXEIO bool isLoadable() const { return (s->get_flags() & SHF_ALLOC) == SHF_ALLOC; } bool isExecutable() const { return (s->get_flags() & SHF_EXECINSTR) == SHF_EXECINSTR; } + bool isWriteable() const { return (s->get_flags() & SHF_WRITE) == SHF_WRITE; } + bool isReadable() const { return isLoadable(); } bool isBSS() const { return (s->get_type() == SHT_NOBITS); } const char* get_data() const { return s->get_data(); } std::string get_name() const { return s->get_name(); } diff --git a/libEXEIO/include/exeio_pe.h b/libEXEIO/include/exeio_pe.h index d67967df0c04c82d041db30e6545b03099159789..c77995c623ce2a3753fa3bd0152b51dc5e5aed7c 100644 --- a/libEXEIO/include/exeio_pe.h +++ b/libEXEIO/include/exeio_pe.h @@ -23,6 +23,8 @@ namespace EXEIO bool isLoadable() const { return s->readable(); } bool isExecutable() const { return s->executable(); } + bool isWriteable() const { return s->writeable(); } + bool isReadable() const { return s->readable(); } bool isBSS() const { return s->empty(); } const char* get_data() const { return s->get_raw_data().c_str(); } std::string get_name() const { return s->get_name(); } diff --git a/libIRDB/test/fill_in_cfg.cpp b/libIRDB/test/fill_in_cfg.cpp index acb29fbafe6abb76f7aac28ab688d5cd95e30017..091442052ab4669d531a4e3cd82b5de9f061987f 100644 --- a/libIRDB/test/fill_in_cfg.cpp +++ b/libIRDB/test/fill_in_cfg.cpp @@ -22,6 +22,7 @@ #include <libIRDB-core.hpp> #include <iostream> +#include <fstream> #include <stdlib.h> #include <string.h> #include <map> @@ -239,12 +240,6 @@ void add_new_instructions(FileIR_t *firp) -#if 0 - ::Elf64_Off sec_hdr_off, sec_off; - ::Elf_Half secnum, strndx, secndx; - ::Elf_Word secsize; - -#endif int secnum = elfiop.sections.size(); int secndx=0; @@ -253,14 +248,11 @@ void add_new_instructions(FileIR_t *firp) /* look through each section and find the missing target*/ for (secndx=1; secndx<secnum; secndx++) { -// int flags = elfiop.sections[secndx]->get_flags(); - /* not a loaded section */ - if( !elfiop.sections[secndx]->isLoadable()) // (flags & SHF_ALLOC) != SHF_ALLOC) + if( !elfiop.sections[secndx]->isLoadable()) continue; /* loaded, and contains instruction, record the bounds */ - // if( (flags & SHF_EXECINSTR) != SHF_EXECINSTR) if( !elfiop.sections[secndx]->isExecutable()) continue; @@ -432,6 +424,68 @@ void fill_in_cfg(FileIR_t *firp) } +void fill_in_scoops(FileIR_t *firp) +{ + int secnum = elfiop.sections.size(); + int secndx=0; + + /* look through each section */ + for (secndx=1; secndx<secnum; secndx++) + { + /* not a loaded section, try next section */ + if(elfiop.sections[secndx]->isLoadable()) + { + cout<<"Skipping scoop for section (not loadable) "<<elfiop.sections[secndx]->get_name()<<endl; + continue; + } + + if(elfiop.sections[secndx]->isWriteable() && elfiop.sections[secndx]->isExecutable()) + { + ofstream fout("warning.txt"); + fout<<"Found that section "<<elfiop.sections[secndx]->get_name()<<" is both writeable and executable. Program is inherently unsafe!"<<endl; + } + + /* executable sections handled by other bits. */ + if(elfiop.sections[secndx]->isExecutable()) + { + cout<<"Skipping scoop for section (executable) "<<elfiop.sections[secndx]->get_name()<<endl; + continue; + } + + /* name */ + string name=elfiop.sections[secndx]->get_name(); + + /* start address */ + AddressID_t *startaddr=new AddressID_t(); + assert(startaddr); + startaddr->SetVirtualOffset( elfiop.sections[secndx]->get_address()); + startaddr->SetFileID(firp->GetFile()->GetBaseID()); + firp->GetAddresses().insert(startaddr); + + /* end */ + AddressID_t *endaddr=new AddressID_t(); + assert(endaddr); + endaddr->SetVirtualOffset( elfiop.sections[secndx]->get_address() + elfiop.sections[secndx]->get_size()); + endaddr->SetFileID(firp->GetFile()->GetBaseID()); + firp->GetAddresses().insert(endaddr); + + Type_t *chunk_type=NULL; /* FIXME -- need to figure out the type system for schoops, but NULL should remain valid */ + + /* permissions */ + int permissions= + ( elfiop.sections[secndx]->isReadable() << 2 ) | + ( elfiop.sections[secndx]->isWriteable() << 1 ) | + ( elfiop.sections[secndx]->isExecutable() << 0 ) ; + + DataScoop_t *newscoop=new DataScoop_t(BaseObj_t::NOT_IN_DATABASE, name, startaddr, endaddr, NULL, permissions); + assert(newscoop); + firp->GetDataScoops().insert(newscoop); + + cout<<"Allocated new scoop for section "<<name<<endl; + + } + +} main(int argc, char* argv[]) { @@ -480,6 +534,7 @@ main(int argc, char* argv[]) EXEIO::dump::section_headers(cout,elfiop); fill_in_cfg(firp); + fill_in_scoops(firp); // write the DB back and commit our changes firp->WriteToDB();