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