--- Makefile.orig	2014-04-10 22:26:45.000000000 -0400
+++ Makefile	2014-04-10 22:35:02.000000000 -0400
@@ -419,7 +419,7 @@
 BUILDDIR = /w/gpl/tor-0.2.4.21-as
 CC = gcc
 CCDEPMODE = depmode=gcc3
-CFLAGS = -g -O2 -D_FORTIFY_SOURCE=2 -fstack-protector-all -Wstack-protector -fwrapv --param ssp-buffer-size=1 -fPIE -Wall -fno-strict-aliasing
+CFLAGS = -g -O1 -D_FORTIFY_SOURCE=2 -fstack-protector-all -Wstack-protector -fwrapv --param ssp-buffer-size=1 -fPIE -Wall -fno-strict-aliasing -fsanitize=address -fno-omit-frame-pointer
 CONFDIR = /usr/local/etc/tor
 CPP = gcc -E
 CPPFLAGS = -I/usr/local/include -I${top_srcdir}/src/common   
--- src/common/util.c.orig	2014-02-25 15:04:22.000000000 -0500
+++ src/common/util.c	2014-04-10 22:51:52.000000000 -0400
@@ -72,6 +72,9 @@
 #ifdef HAVE_SYS_FCNTL_H
 #include <sys/fcntl.h>
 #endif
+#ifdef HAVE_PRCTL
+#include <sys/prctl.h>
+#endif
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
@@ -3135,6 +3138,8 @@
 finish_daemon(const char *desired_cwd)
 {
   int nullfd;
+  int outfd;
+  int errfd;
   char c = '.';
   if (finish_daemon_called)
     return;
@@ -3150,23 +3155,45 @@
     exit(1);
   }
 
-  nullfd = tor_open_cloexec("/dev/null", O_RDWR, 0);
-  if (nullfd < 0) {
+  nullfd = tor_open_cloexec("/dev/null", O_RDONLY, 0);
+  if (nullfd < 3) {
     log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
     exit(1);
   }
+  outfd = tor_open_cloexec("out.tor", O_WRONLY | O_CREAT | O_APPEND, 0);
+  if (outfd < 3) {
+    log_err(LD_GENERAL,"out.tor can't be opened. Exiting.");
+    exit(1);
+  }
+  errfd = tor_open_cloexec("err.tor", O_WRONLY | O_CREAT | O_APPEND, 0);
+  if (errfd < 3) {
+    log_err(LD_GENERAL,"err.tor can't be opened. Exiting.");
+    exit(1);
+  }
   /* close fds linking to invoking terminal, but
    * close usual incoming fds, but redirect them somewhere
    * useful so the fds don't get reallocated elsewhere.
    */
   if (dup2(nullfd,0) < 0 ||
-      dup2(nullfd,1) < 0 ||
-      dup2(nullfd,2) < 0) {
+      dup2(outfd,1) < 0 ||
+      dup2(errfd,2) < 0) {
     log_err(LD_GENERAL,"dup2 failed. Exiting.");
     exit(1);
   }
-  if (nullfd > 2)
-    close(nullfd);
+  close(errfd);
+  close(outfd);
+  close(nullfd);
+#ifdef HAVE_PRCTL
+  if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) != 0) {
+    log_err(LD_GENERAL,"prctl failed. Exiting.");
+    exit(1);
+  }
+#endif
+#if defined(__has_feature)
+#if __has_feature(address_sanitizer)
+  __sanitizer_sandbox_on_notify(NULL);
+#endif
+#endif
   /* signal success */
   if (write(daemon_filedes[1], &c, sizeof(char)) != sizeof(char)) {
     log_err(LD_GENERAL,"write failed. Exiting.");
