Blame SOURCES/gdb-attach-fail-reasons-5of5.patch

861f93
http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html
861f93
Subject: [patch 3/3] attach-fail-reasons: SELinux deny_ptrace
861f93
861f93
Hi,
861f93
861f93
and here is the last bit for new SELinux 'deny_ptrace':
861f93
	https://bugzilla.redhat.com/show_bug.cgi?id=786878
861f93
861f93
As even PTRACE_TRACEME fails in such case it needs to install hook for even
861f93
that event.
861f93
861f93
861f93
Thanks,
861f93
Jan
861f93
861f93
861f93
gdb/
861f93
2012-03-06  Jan Kratochvil  <jan.kratochvil@redhat.com>
861f93
861f93
	* common/linux-ptrace.c [HAVE_SELINUX_SELINUX_H]: include
861f93
	selinux/selinux.h.
861f93
	(linux_ptrace_attach_warnings): Call linux_ptrace_create_warnings.
861f93
	(linux_ptrace_create_warnings): New.
861f93
	* common/linux-ptrace.h (linux_ptrace_create_warnings): New declaration.
861f93
	* config.in: Regenerate.
861f93
	* configure: Regenerate.
861f93
	* configure.ac: Check selinux/selinux.h and the selinux library.
861f93
	* inf-ptrace.c (inf_ptrace_me): Check the ptrace result.
861f93
	* linux-nat.c (linux_nat_create_inferior): New variable ex.  Wrap
861f93
	to_create_inferior into TRY_CATCH, call linux_ptrace_create_warnings.
861f93
861f93
gdb/gdbserver/
861f93
	* config.in: Regenerate.
861f93
	* configure: Regenerate.
861f93
	* configure.ac: Check selinux/selinux.h and the selinux library.
861f93
	* linux-low.c (linux_traceme): New function.
861f93
	(linux_create_inferior, linux_tracefork_child): Call it instead of
861f93
	direct ptrace.
861f93
861f93
Index: gdb-7.5.50.20130215/gdb/common/linux-ptrace.c
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/common/linux-ptrace.c	2013-01-08 20:38:51.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/common/linux-ptrace.c	2013-02-15 22:38:05.782456279 +0100
861f93
@@ -29,6 +29,10 @@
861f93
 #include "gdb_assert.h"
861f93
 #include "gdb_wait.h"
861f93
 
861f93
+#ifdef HAVE_SELINUX_SELINUX_H
861f93
+# include <selinux/selinux.h>
861f93
+#endif /* HAVE_SELINUX_SELINUX_H */
861f93
+
861f93
 /* Find all possible reasons we could fail to attach PID and append these
861f93
    newline terminated reason strings to initialized BUFFER.  '\0' termination
861f93
    of BUFFER must be done by the caller.  */
861f93
@@ -48,6 +52,8 @@ linux_ptrace_attach_warnings (pid_t pid,
861f93
     buffer_xml_printf (buffer, _("warning: process %d is a zombie "
861f93
 				 "- the process has already terminated\n"),
861f93
 		       (int) pid);
861f93
+
861f93
+  linux_ptrace_create_warnings (buffer);
861f93
 }
861f93
 
861f93
 #if defined __i386__ || defined __x86_64__
861f93
@@ -243,3 +249,19 @@ linux_ptrace_init_warnings (void)
861f93
 
861f93
   linux_ptrace_test_ret_to_nx ();
861f93
 }
861f93
+
861f93
+/* Print all possible reasons we could fail to create a traced process.  */
861f93
+
861f93
+void
861f93
+linux_ptrace_create_warnings (struct buffer *buffer)
861f93
+{
861f93
+#ifdef HAVE_LIBSELINUX
861f93
+  /* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
861f93
+     forbidden.  */
861f93
+  if (security_get_boolean_active ("deny_ptrace") == 1)
861f93
+    buffer_xml_printf (buffer,
861f93
+		       _("the SELinux boolean 'deny_ptrace' is enabled, "
861f93
+			 "you can disable this process attach protection by: "
861f93
+			 "(gdb) shell sudo setsebool deny_ptrace=0"));
861f93
+#endif /* HAVE_LIBSELINUX */
861f93
+}
861f93
Index: gdb-7.5.50.20130215/gdb/common/linux-ptrace.h
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/common/linux-ptrace.h	2013-01-01 07:32:54.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/common/linux-ptrace.h	2013-02-15 22:38:05.782456279 +0100
861f93
@@ -69,5 +69,6 @@ struct buffer;
861f93
 
861f93
 extern void linux_ptrace_attach_warnings (pid_t pid, struct buffer *buffer);
861f93
 extern void linux_ptrace_init_warnings (void);
861f93
+extern void linux_ptrace_create_warnings (struct buffer *buffer);
861f93
 
861f93
 #endif /* COMMON_LINUX_PTRACE_H */
861f93
Index: gdb-7.5.50.20130215/gdb/configure.ac
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/configure.ac	2013-02-15 22:37:57.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/configure.ac	2013-02-15 22:38:05.783456281 +0100
861f93
@@ -2068,6 +2068,10 @@ then
861f93
 	      [Define if you support the personality syscall.])
861f93
 fi
861f93
 
861f93
+dnl Check security_get_boolean_active availability.
861f93
+AC_CHECK_HEADERS(selinux/selinux.h)
861f93
+AC_CHECK_LIB(selinux, security_get_boolean_active)
861f93
+
861f93
 dnl Handle optional features that can be enabled.
861f93
 
861f93
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
861f93
Index: gdb-7.5.50.20130215/gdb/gdbserver/configure.ac
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/gdbserver/configure.ac	2013-01-01 07:33:00.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/gdbserver/configure.ac	2013-02-15 22:38:05.783456281 +0100
861f93
@@ -451,6 +451,10 @@ if $want_ipa ; then
861f93
    fi
861f93
 fi
861f93
 
861f93
+dnl Check security_get_boolean_active availability.
861f93
+AC_CHECK_HEADERS(selinux/selinux.h)
861f93
+AC_CHECK_LIB(selinux, security_get_boolean_active)
861f93
+
861f93
 AC_SUBST(GDBSERVER_DEPFILES)
861f93
 AC_SUBST(GDBSERVER_LIBS)
861f93
 AC_SUBST(USE_THREAD_DB)
861f93
Index: gdb-7.5.50.20130215/gdb/gdbserver/linux-low.c
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/gdbserver/linux-low.c	2013-02-04 18:47:00.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/gdbserver/linux-low.c	2013-02-15 22:39:54.256591069 +0100
861f93
@@ -602,6 +602,29 @@ add_lwp (ptid_t ptid)
861f93
   return lwp;
861f93
 }
861f93
 
861f93
+/* Execute PTRACE_TRACEME with error checking.  */
861f93
+
861f93
+static void
861f93
+linux_traceme (const char *program)
861f93
+{
861f93
+  int save_errno;
861f93
+  struct buffer buffer;
861f93
+
861f93
+  errno = 0;
861f93
+  if (ptrace (PTRACE_TRACEME, 0,
861f93
+	      (PTRACE_ARG3_TYPE) 0, (PTRACE_ARG4_TYPE) 0) == 0)
861f93
+    return;
861f93
+
861f93
+  save_errno = errno;
861f93
+  buffer_init (&buffer);
861f93
+  linux_ptrace_create_warnings (&buffer);
861f93
+  buffer_grow_str0 (&buffer, "");
861f93
+  fprintf (stderr, _("%sCannot trace created process %s: %s.\n"),
861f93
+	   buffer_finish (&buffer), program, strerror (save_errno));
861f93
+  fflush (stderr);
861f93
+  _exit (0177);
861f93
+}
861f93
+
861f93
 /* Start an inferior process and returns its pid.
861f93
    ALLARGS is a vector of program-name and args. */
861f93
 
861f93
@@ -642,7 +665,7 @@ linux_create_inferior (char *program, ch
861f93
 
861f93
   if (pid == 0)
861f93
     {
861f93
-      ptrace (PTRACE_TRACEME, 0, (PTRACE_ARG3_TYPE) 0, (PTRACE_ARG4_TYPE) 0);
861f93
+      linux_traceme (program);
861f93
 
861f93
 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does.  */
861f93
       signal (__SIGRTMIN + 1, SIG_DFL);
861f93
@@ -4587,7 +4610,7 @@ linux_tracefork_grandchild (void *arg)
861f93
 static int
861f93
 linux_tracefork_child (void *arg)
861f93
 {
861f93
-  ptrace (PTRACE_TRACEME, 0, (PTRACE_ARG3_TYPE) 0, (PTRACE_ARG4_TYPE) 0);
861f93
+  linux_traceme ("PTRACE_O_TRACEFORK test");
861f93
   kill (getpid (), SIGSTOP);
861f93
 
861f93
 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
861f93
Index: gdb-7.5.50.20130215/gdb/inf-ptrace.c
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/inf-ptrace.c	2013-01-01 07:32:45.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/inf-ptrace.c	2013-02-15 22:38:05.786456289 +0100
861f93
@@ -104,7 +104,15 @@ static void
861f93
 inf_ptrace_me (void)
861f93
 {
861f93
   /* "Trace me, Dr. Memory!"  */
861f93
+  errno = 0;
861f93
   ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
861f93
+  if (errno != 0)
861f93
+    {
861f93
+      fprintf_unfiltered (gdb_stderr, _("Cannot create process: %s\n"),
861f93
+			  safe_strerror (errno));
861f93
+      gdb_flush (gdb_stderr);
861f93
+      _exit (0177);
861f93
+    }
861f93
 }
861f93
 
861f93
 /* Start a new inferior Unix child process.  EXEC_FILE is the file to
861f93
Index: gdb-7.5.50.20130215/gdb/linux-nat.c
861f93
===================================================================
861f93
--- gdb-7.5.50.20130215.orig/gdb/linux-nat.c	2013-02-15 22:34:44.000000000 +0100
861f93
+++ gdb-7.5.50.20130215/gdb/linux-nat.c	2013-02-15 22:38:05.787456291 +0100
861f93
@@ -1557,6 +1557,7 @@ linux_nat_create_inferior (struct target
861f93
 #ifdef HAVE_PERSONALITY
861f93
   int personality_orig = 0, personality_set = 0;
861f93
 #endif /* HAVE_PERSONALITY */
861f93
+  volatile struct gdb_exception ex;
861f93
 
861f93
   /* The fork_child mechanism is synchronous and calls target_wait, so
861f93
      we have to mask the async mode.  */
861f93
@@ -1581,7 +1582,10 @@ linux_nat_create_inferior (struct target
861f93
   /* Make sure we report all signals during startup.  */
861f93
   linux_nat_pass_signals (0, NULL);
861f93
 
861f93
-  linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
861f93
+  TRY_CATCH (ex, RETURN_MASK_ERROR)
861f93
+    {
861f93
+      linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
861f93
+    }
861f93
 
861f93
 #ifdef HAVE_PERSONALITY
861f93
   if (personality_set)
861f93
@@ -1593,6 +1597,24 @@ linux_nat_create_inferior (struct target
861f93
 		 safe_strerror (errno));
861f93
     }
861f93
 #endif /* HAVE_PERSONALITY */
861f93
+
861f93
+  if (ex.reason < 0)
861f93
+    {
861f93
+      struct buffer buffer;
861f93
+      char *message, *buffer_s;
861f93
+
861f93
+      message = xstrdup (ex.message);
861f93
+      make_cleanup (xfree, message);
861f93
+
861f93
+      buffer_init (&buffer);
861f93
+      linux_ptrace_create_warnings (&buffer);
861f93
+
861f93
+      buffer_grow_str0 (&buffer, "");
861f93
+      buffer_s = buffer_finish (&buffer);
861f93
+      make_cleanup (xfree, buffer_s);
861f93
+
861f93
+      throw_error (ex.error, "%s%s", buffer_s, message);
861f93
+    }
861f93
 }
861f93
 
861f93
 static void