Sfoglia il codice sorgente

* Cleaned up trace detection code.
* Fixed hang on running binary on some FreeBSD systems.


svn: 2352

Bryan Drewery 21 anni fa
parent
commit
fc14156867
2 ha cambiato i file con 31 aggiunte e 48 eliminazioni
  1. 2 0
      doc/UPDATES
  2. 29 48
      src/shell.c

+ 2 - 0
doc/UPDATES

@@ -26,6 +26,8 @@ Lines prefixed with '-' were disabled before release and are not finished, or ar
 * Help for 'botcmd' was off/outdated
 * Fixed cmd_[un]stick not using "ban" by default for mask type. (#145)
 * Fixed default binary name always becoming "wraith" instead of original binary name.
+* Cleaned up trace detection code.
+* Fixed hang on running binary on some FreeBSD systems.
 
 1.2.6
 * (REVERTED FROM 1.2.3) Disabled all memory allocations after a segfault (Fixes CPU spinning)

+ 29 - 48
src/shell.c

@@ -345,14 +345,12 @@ void check_promisc()
 #endif /* SIOCGIFCONF */
 }
 
-#ifdef __linux__
 bool traced = 0;
 
 static void got_sigtrap(int z)
 {
   traced = 0;
 }
-#endif
 
 void check_trace(int start)
 {
@@ -368,13 +366,20 @@ void check_trace(int start)
   int x, i;
   pid_t parent = getpid();
 
-#ifdef __linux__
   /* we send ourselves a SIGTRAP, if we recieve, we're not being traced, otherwise we are. */
   signal(SIGTRAP, got_sigtrap);
   traced = 1;
   raise(SIGTRAP);
   /* no longer need this__asm__("INT3"); //SIGTRAP */
   signal(SIGTRAP, SIG_DFL);
+
+  if (!traced) {
+    signal(SIGINT, got_sigtrap);
+    traced = 1;
+    raise(SIGINT);
+    signal(SIGINT, SIG_DFL);
+  }
+
   if (traced) {
     if (start) {
       kill(parent, SIGKILL);
@@ -382,52 +387,28 @@ void check_trace(int start)
     } else
       detected(DETECT_TRACE, "I'm being traced!");
   } else {
-    x = fork();
-    if (x == -1)
-      return;
-    else if (x == 0) {
-      i = ptrace(PTRACE_ATTACH, parent, 0, 0);
-      if (i == (-1) && errno == EPERM) {
-        if (start) {
-          kill(parent, SIGKILL);
-          exit(1);
-        } else
-          detected(DETECT_TRACE, "I'm being traced!");
-      } else {
-        waitpid(parent, &i, 0);
-        kill(parent, SIGCHLD);
-        ptrace(PTRACE_DETACH, parent, 0, 0);
-        kill(parent, SIGCHLD);
-      }
-      exit(0);
-    } else
-      wait(&i);
-  }
-#endif /* __linux__ */
-#ifdef BSD
-  x = fork();
-  if (x == -1)
-    return;
-  else if (x == 0) {
-    i = ptrace(PT_ATTACH, parent, 0, 0);
-    if (i == (-1) && errno == EBUSY) {
-        if (start) {
-          kill(parent, SIGKILL);
-          exit(1);
-        } else
-          detected(DETECT_TRACE, "I'm being traced");
-    } else {
-      wait(&i);
-      i = ptrace(PT_CONTINUE, parent, (caddr_t) 1, 0);
-      kill(parent, SIGCHLD);
-      wait(&i);
-      i = ptrace(PT_DETACH, parent, (caddr_t) 1, 0);
-      wait(&i);
+  /* now, let's attempt to ptrace ourself */
+    switch ((x = fork())) {
+      case -1:
+        return;
+      case 0:		//child
+        i = ptrace(PT_ATTACH, parent, 0, 0);
+        if (i == -1) {
+          if (start) {
+            kill(parent, SIGKILL);
+            exit(1);
+          } else
+            detected(DETECT_TRACE, "I'm being traced!");
+        } else {
+          waitpid(parent, NULL, 0);
+          ptrace(PT_DETACH, parent, (char *) 1, 0);
+          kill(parent, SIGCHLD);
+        }
+        exit(0);
+      default:		//parent
+        waitpid(x, NULL, 0);
     }
-    exit(0);
-  } else
-    waitpid(x, NULL, 0);
-#endif /* BSD */
+  }
 }
 #endif /* !CYGWIN_HACKS */