From e90de61263ae014423b5b7b066290873b63ccd23 Mon Sep 17 00:00:00 2001 From: Peter Veenstra Date: Mon, 24 Jun 2019 20:09:59 +0000 Subject: [PATCH] Create fopen_wrapper so we can filter out specific directories, which DOS games should have no access to Imported-from: https://svn.code.sf.net/p/dosbox/code-0/dosbox/trunk@4238 --- include/cross.h | 1 + src/misc/cross.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/cross.h b/include/cross.h index 1a81ebe0..90b65315 100644 --- a/include/cross.h +++ b/include/cross.h @@ -105,4 +105,5 @@ bool read_directory_first(dir_information* dirp, char* entry_name, bool& is_dire bool read_directory_next(dir_information* dirp, char* entry_name, bool& is_directory); void close_directory(dir_information* dirp); +FILE *fopen_wrap(const char *path, const char *mode); #endif diff --git a/src/misc/cross.cpp b/src/misc/cross.cpp index 8fbefbfc..49c20aa8 100644 --- a/src/misc/cross.cpp +++ b/src/misc/cross.cpp @@ -21,6 +21,7 @@ #include "cross.h" #include "support.h" #include +#include #include #ifdef WIN32 @@ -244,3 +245,60 @@ void close_directory(dir_information* dirp) { } #endif + +FILE *fopen_wrap(const char *path, const char *mode) { +#if defined(WIN32) || defined(OS2) + ; +#elif defined (MACOSX) + ; +#else +#if defined (HAVE_REALPATH) + char work[CROSS_LEN] = {0}; + strncpy(work,path,CROSS_LEN-1); + char* last = strrchr(work,'/'); + + if (last) { + if (last != work) { + *last = 0; + //If this compare fails, then we are dealing with files in / + //Which is outside the scope, but test anyway. + //However as realpath only works for exising files. The testing is + //in that case not done against new files. + } + char* check = realpath(work,NULL); + if (check) { + if ( ( strlen(check) == 5 && strcmp(check,"/proc") == 0) || strncmp(check,"/proc/",6) == 0) { +// LOG_MSG("lst hit %s blocking!",path); + free(check); + return NULL; + } + free(check); + } + } + +#if 0 +//Lightweight version, but then existing files can still be read, which is not ideal + if (strpbrk(mode,"aw+") != NULL) { + LOG_MSG("pbrk ok"); + char* check = realpath(path,NULL); + //Will be null if file doesn't exist.... ENOENT + //TODO What about unlink /proc/self/mem and then create it ? + //Should be safe for what we want.. + if (check) { + if (strncmp(check,"/proc/",6) == 0) { + free(check); + return NULL; + } + free(check); + } + } +*/ +#endif //0 + +#endif //HAVE_REALPATH +#endif + + return fopen(path,mode); +} + +