From f0417e12dd95dc937dbc7c696d54ba260376a3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Tue, 1 Nov 2011 13:06:27 +0100 Subject: [PATCH] Hardening. --- CMakeLists.txt | 2 +- library/Core.cpp | 13 ++------ library/Process-linux.cpp | 50 +++++++++++++----------------- library/depends/md5/md5wrapper.cpp | 24 ++++++++++++-- plugins/cleaners.cpp | 8 +++-- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb520ff50..942838010 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ set(DF_VERSION_MINOR "31") set(DF_VERSION_PATCH "25") set(DF_VERSION "${DF_VERSION_MAJOR}.${DF_VERSION_MINOR}.${DF_VERSION_PATCH}") -set(DFHACK_RELEASE "7") +set(DFHACK_RELEASE "7b") ## where to install things (after the build is done, classic 'make install' or package structure) # the dfhack libraries will be installed here: diff --git a/library/Core.cpp b/library/Core.cpp index b52ddc6c1..62f556e01 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -410,18 +410,9 @@ bool Core::Init() p = new DFHack::Process(vif); vinfo = p->getDescriptor(); - if(!vinfo) + if(!vinfo || !p->isIdentified()) { - cerr << "Couldn't retrieve version information.\n"; - errorstate = true; - delete p; - p = NULL; - return false; - } - - if (!p->isIdentified()) - { - cerr << "Couldn't identify this version of DF.\n"; + cerr << "Not a known DF version. DFHack will now deactivate.\n"; errorstate = true; delete p; p = NULL; diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index 718d0f804..59adeeeb8 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -48,47 +48,39 @@ Process::Process(VersionInfoFactory * known_versions) const char * exe_link_name = "/proc/self/exe"; const char * cwd_name = "/proc/self/cwd"; const char * cmdline_name = "/proc/self/cmdline"; - char target_name[1024]; int target_result; identified = false; my_descriptor = 0; - // resolve /proc/self/exe link - target_result = readlink(exe_link_name, target_name, sizeof(target_name)-1); - if (target_result == -1) + md5wrapper md5; + // get hash of the running DF process + string hash = md5.getHashFromFile(exe_link_name); + // create linux process, add it to the vector + VersionInfo * vinfo = known_versions->getVersionInfoByMD5(hash); + if(vinfo) { - cerr << "Failed to readlink(/proc/self/exe)\n"; - return; - } - // make sure we have a null terminated string... - target_name[target_result] = 0; - - // is this the regular linux DF? - if (strstr(target_name, "dwarfort.exe") != 0 || strstr(target_name,"Dwarf_Fortress") != 0) - { - md5wrapper md5; - // get hash of the running DF process - string hash = md5.getHashFromFile(target_name); - // create linux process, add it to the vector - VersionInfo * vinfo = known_versions->getVersionInfoByMD5(hash); - if(vinfo) - { - my_descriptor = new VersionInfo(*vinfo); - my_descriptor->setParentProcess(this); - identified = true; - } - else - { - cerr << "Unable to retrieve version information.\n"; - } + my_descriptor = new VersionInfo(*vinfo); + my_descriptor->setParentProcess(this); + identified = true; } else { - cerr << "This isn't DF.\n"; + char * wd = getcwd(NULL, 0); + cerr << "Unable to retrieve version information.\n"; + cerr << "File: " << exe_link_name << endl; + cerr << "MD5: " << hash << endl; + cerr << "working dir: " << wd << endl; + free(wd); } } +Process::~Process() +{ + // destroy our copy of the memory descriptor + delete my_descriptor; +} + string Process::doReadClassName (void * vptr) { //FIXME: BAD!!!!! diff --git a/library/depends/md5/md5wrapper.cpp b/library/depends/md5/md5wrapper.cpp index 241d2bb31..4af7b12d4 100644 --- a/library/depends/md5/md5wrapper.cpp +++ b/library/depends/md5/md5wrapper.cpp @@ -24,7 +24,8 @@ //basic includes #include #include - +#include +#include //my includes #include "md5wrapper.h" #include "md5.h" @@ -118,15 +119,32 @@ std::string md5wrapper::getHashFromFile(std::string filename) //open file if ((file = fopen (filename.c_str(), "rb")) == NULL) { - return "-1"; + return "file unreadable."; } //init md5 md5->MD5Init (&context); //read the filecontent - while ( (len = fread (buffer, 1, 1024, file)) ) + while (1) { + errno = 0; + len = fread (buffer, 1, 1024, file); + if(len != 1024) + { + int err = ferror(file); + //FIXME: check errno here. + if(err) + { + fclose(file); + return strerror(err); + } + if(feof(file)) + { + md5->MD5Update (&context, buffer, len); + break; + } + } md5->MD5Update (&context, buffer, len); } diff --git a/plugins/cleaners.cpp b/plugins/cleaners.cpp index 209ce79b1..94de962ad 100644 --- a/plugins/cleaners.cpp +++ b/plugins/cleaners.cpp @@ -78,10 +78,14 @@ command_result cleanmap (Core * c, bool snow, bool mud) { DFHack::t_spattervein * vein = splatter[i]; // filter snow - if(!snow && vein->mat1 == water_idx && vein->matter_state == DFHack::state_powder) + if(!snow + && vein->mat1 == DFHack::Materials::WATER + && vein->matter_state == DFHack::state_powder) continue; // filter mud - if(!mud && vein->mat1 == mud_idx && vein->matter_state == DFHack::state_solid) + if(!mud + && vein->mat1 == DFHack::Materials::MUD + && vein->matter_state == DFHack::state_solid) continue; Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein); cleaned = true;