]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
refactor this into common code
authorRudolf Polzer <divverent@alientrap.org>
Mon, 4 Jun 2012 10:15:24 +0000 (12:15 +0200)
committerRudolf Polzer <divverent@alientrap.org>
Mon, 4 Jun 2012 10:15:24 +0000 (12:15 +0200)
sys.h
sys_linux.c
sys_sdl.c
sys_shared.c
sys_win.c

diff --git a/sys.h b/sys.h
index e5247d351afabe274f3e1a933b5915b89bc5b199..c4c8c55958dc160d3d9855384cb1a97b90ab9964 100644 (file)
--- a/sys.h
+++ b/sys.h
@@ -115,5 +115,8 @@ void Sys_InitProcessNice (void);
 void Sys_MakeProcessNice (void);
 void Sys_MakeProcessMean (void);
 
+// call this from main(); if it returns false, exit using return
+qboolean anticheat_init(char **envp);
+
 #endif
 
index 70fe819d19d957f7bfce18234a1b316a07e0906e..bf7cd29b1e5ab6aedb1fb9d5de255d8af5d5ce89 100644 (file)
@@ -148,118 +148,6 @@ void Sys_InitConsole (void)
 {
 }
 
-#ifdef ANTICHEAT
-# ifndef WIN32
-#  include <sys/ptrace.h>
-#  include <sys/wait.h>
-#  include <errno.h>
-# endif
-#endif
-static qboolean anticheat_init(char **envp)
-{
-#ifdef ANTICHEAT
-#define FAIL return false
-
-       // anti LD_PRELOAD
-       // note that we're using envp here, so one doesn't simply hook into getenv()
-       static char *unsecure_envvars =
-               // UNSECURE_ENVVARS from glibc
-               "GCONV_PATH\0" // libraries are loaded from here
-               //"GETCONF_DIR\0" // harmless, can only fake getconf() output
-               //"HOSTALIASES\0" // harmless, just messes with DNS
-               "LD_AUDIT\0" // loads libraries
-               // "LD_DEBUG\0" // harmless, just shows data
-               // "LD_DEBUG_OUTPUT\0" // harmless, just shows data
-               "LD_DYNAMIC_WEAK\0" // changes name resolution
-               "LD_LIBRARY_PATH\0" // loads libraries
-               "LD_ORIGIN_PATH\0" // may load libraries
-               "LD_PRELOAD\0" // loads libraries
-               // "LD_PROFILE\0" // harmless, just creates profile
-               // "LD_SHOW_AUXV\0" // harmless, just shows data
-               // "LD_USE_LOAD_BIAS\0" // harmless
-               // "LOCALDOMAIN\0" // harmless, just messes with DNS
-               // "LOCPATH\0" // harmless, just messes with locales
-               // "MALLOC_TRACE\0" // harmless, just shows data
-               // "NIS_PATH\0" // harmless, just messes with DNS and user ID data
-               // "NLSPATH\0" // harmless, just messes with locales
-               // "RESOLV_HOST_CONF\0" // harmless, can cause private data to be printed from suid, but we're not suid
-               // "RES_OPTIONS\0" // harmless, just messes with DNS
-               // "TMPDIR\0" // harmless, we don't use temp files anyway
-               // "TZDIR\0" // harmless, just enables time travel
-               // EXTRA_UNSECURE_ENVVARS from glibc
-               "LD_AOUT_LIBRARY_PATH\0" // loads libraries
-               "LD_AOUT_PRELOAD\0" // loads libraries
-               ;
-
-       while(*envp)
-       {
-               char *p = unsecure_envvars;
-               while(*p)
-               {
-                       char *q = envp[0];
-                       while(*p && *p == *q)
-                       {
-                               ++p;
-                               ++q;
-                       }
-                       if(*p == 0 && *q == '=')
-                               FAIL; // match!
-                       // next!
-                       while(*p)
-                               ++p;
-                       ++p;
-               }
-               ++envp;
-       }
-
-       // anti ptrace; also, make a forked process copy to detach from debuggers
-# ifndef WIN32
-       {
-               pid_t pid = fork();
-               if(pid < 0)
-                       FAIL;
-               if(pid == 0)
-               {
-                       // nothing to do here
-               }
-               else
-               {
-                       // parent
-                       int status;
-                       if(ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0)
-                       {
-                               kill(pid, SIGKILL);
-                               FAIL;
-                       }
-                       for(;;)
-                       {
-                               if(waitpid(pid, &status, 0) == (pid_t) -1)
-                               {
-                                       if(errno == ECHILD) // process no longer exists
-                                               FAIL;
-                               }
-                               if(WIFEXITED(status))
-                               {
-                                       exit(WEXITSTATUS(status));
-                               }
-                               if(WIFSTOPPED(status))
-                               {
-                                       printf("ptrace: continue... (signal: %d)\n", (int) WSTOPSIG(status));
-                                       if(ptrace(PTRACE_CONT, pid, (void *) WSTOPSIG(status), NULL) < 0)
-                                       {
-                                               perror("okay.png");
-                                       }
-                               }
-                       }
-                       // never gonna get to here
-               }
-       }
-# endif
-#endif
-
-       return true;
-}
-
 int main (int argc, char **argv, char **envp)
 {
        if(!anticheat_init(envp))
index c00cf4082140fffef21efe76d33ec9ca5f5c62f9..4863ac76e8caca2392fed93d5280c0650fa2e4c1 100644 (file)
--- a/sys_sdl.c
+++ b/sys_sdl.c
@@ -176,8 +176,11 @@ void Sys_InitConsole (void)
 {
 }
 
-int main (int argc, char *argv[])
+int main (int argc, char *argv[], char *envp[])
 {
+       if(!anticheat_init(envp))
+               return 42;
+
        signal(SIGFPE, SIG_IGN);
 
        com_argc = argc;
index 10f5a60e5d7126a145188698246ddeb23479840c..a2a3852b1750db29c7323689985d53c567decdce 100644 (file)
@@ -640,3 +640,119 @@ void Sys_MakeProcessMean (void)
 {
 }
 #endif
+
+#ifdef ANTICHEAT
+# ifndef WIN32
+#  include <sys/ptrace.h>
+#  include <sys/wait.h>
+#  include <errno.h>
+# endif
+#endif
+qboolean anticheat_init(char **envp)
+{
+#ifdef ANTICHEAT
+# define FAIL return false
+
+       // anti LD_PRELOAD
+       // note that we're using envp here, so one doesn't simply hook into getenv()
+       static char *unsecure_envvars =
+               // UNSECURE_ENVVARS from glibc
+               "GCONV_PATH\0" // libraries are loaded from here
+               //"GETCONF_DIR\0" // harmless, can only fake getconf() output
+               //"HOSTALIASES\0" // harmless, just messes with DNS
+               "LD_AUDIT\0" // loads libraries
+               // "LD_DEBUG\0" // harmless, just shows data
+               // "LD_DEBUG_OUTPUT\0" // harmless, just shows data
+               "LD_DYNAMIC_WEAK\0" // changes name resolution
+               "LD_LIBRARY_PATH\0" // loads libraries
+               "LD_ORIGIN_PATH\0" // may load libraries
+               "LD_PRELOAD\0" // loads libraries
+               // "LD_PROFILE\0" // harmless, just creates profile
+               // "LD_SHOW_AUXV\0" // harmless, just shows data
+               // "LD_USE_LOAD_BIAS\0" // harmless
+               // "LOCALDOMAIN\0" // harmless, just messes with DNS
+               // "LOCPATH\0" // harmless, just messes with locales
+               // "MALLOC_TRACE\0" // harmless, just shows data
+               // "NIS_PATH\0" // harmless, just messes with DNS and user ID data
+               // "NLSPATH\0" // harmless, just messes with locales
+               // "RESOLV_HOST_CONF\0" // harmless, can cause private data to be printed from suid, but we're not suid
+               // "RES_OPTIONS\0" // harmless, just messes with DNS
+               // "TMPDIR\0" // harmless, we don't use temp files anyway
+               // "TZDIR\0" // harmless, just enables time travel
+               // EXTRA_UNSECURE_ENVVARS from glibc
+               "LD_AOUT_LIBRARY_PATH\0" // loads libraries
+               "LD_AOUT_PRELOAD\0" // loads libraries
+               ;
+
+       if(envp)
+       {
+               while(*envp)
+               {
+                       char *p = unsecure_envvars;
+                       while(*p)
+                       {
+                               char *q = envp[0];
+                               while(*p && *p == *q)
+                               {
+                                       ++p;
+                                       ++q;
+                               }
+                               if(*p == 0 && *q == '=')
+                                       FAIL; // match!
+                               // next!
+                               while(*p)
+                                       ++p;
+                               ++p;
+                       }
+                       ++envp;
+               }
+       }
+
+       // anti ptrace; also, make a forked process copy to detach from debuggers
+# ifndef WIN32
+       {
+               pid_t pid = fork();
+               if(pid < 0)
+                       FAIL;
+               if(pid == 0)
+               {
+                       // nothing to do here
+               }
+               else
+               {
+                       // parent
+                       int status;
+                       if(ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0)
+                       {
+                               kill(pid, SIGKILL);
+                               FAIL;
+                       }
+                       for(;;)
+                       {
+                               if(waitpid(pid, &status, 0) == (pid_t) -1)
+                               {
+                                       if(errno == ECHILD) // process no longer exists
+                                               FAIL;
+                               }
+                               if(WIFEXITED(status))
+                               {
+                                       exit(WEXITSTATUS(status));
+                               }
+                               if(WIFSTOPPED(status))
+                               {
+                                       printf("ptrace: continue... (signal: %d)\n", (int) WSTOPSIG(status));
+                                       if(ptrace(PTRACE_CONT, pid, (void *) WSTOPSIG(status), NULL) < 0)
+                                       {
+                                               perror("okay.png");
+                                       }
+                               }
+                       }
+                       // never gonna get to here
+               }
+       }
+# endif
+#endif
+
+       return true;
+}
+
index de1f47187c2e0f766414bebf630c3d62cdcae9ed..695a06428deae3c3837eb6cecb6efb805a89780f 100644 (file)
--- a/sys_win.c
+++ b/sys_win.c
@@ -314,6 +314,9 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin
 
        global_hInstance = hInstance;
 
+       if(!anticheat_init(NULL))
+               return 42;
+
        lpBuffer.dwLength = sizeof(MEMORYSTATUS);
        GlobalMemoryStatus (&lpBuffer);