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

26bbde
http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html
26bbde
Subject: [patch 3/3] attach-fail-reasons: SELinux deny_ptrace
26bbde
26bbde
Hi,
26bbde
26bbde
and here is the last bit for new SELinux 'deny_ptrace':
26bbde
	https://bugzilla.redhat.com/show_bug.cgi?id=786878
26bbde
26bbde
As even PTRACE_TRACEME fails in such case it needs to install hook for even
26bbde
that event.
26bbde
26bbde
26bbde
Thanks,
26bbde
Jan
26bbde
26bbde
26bbde
gdb/
26bbde
2012-03-06  Jan Kratochvil  <jan.kratochvil@redhat.com>
26bbde
26bbde
	* common/linux-ptrace.c [HAVE_SELINUX_SELINUX_H]: include
26bbde
	selinux/selinux.h.
26bbde
	(linux_ptrace_attach_warnings): Call linux_ptrace_create_warnings.
26bbde
	(linux_ptrace_create_warnings): New.
26bbde
	* common/linux-ptrace.h (linux_ptrace_create_warnings): New declaration.
26bbde
	* config.in: Regenerate.
26bbde
	* configure: Regenerate.
26bbde
	* configure.ac: Check selinux/selinux.h and the selinux library.
26bbde
	* inf-ptrace.c (inf_ptrace_me): Check the ptrace result.
26bbde
	* linux-nat.c (linux_nat_create_inferior): New variable ex.  Wrap
26bbde
	to_create_inferior into TRY_CATCH, call linux_ptrace_create_warnings.
26bbde
26bbde
gdb/gdbserver/
26bbde
	* config.in: Regenerate.
26bbde
	* configure: Regenerate.
26bbde
	* configure.ac: Check selinux/selinux.h and the selinux library.
26bbde
	* linux-low.c (linux_traceme): New function.
26bbde
	(linux_create_inferior, linux_tracefork_child): Call it instead of
26bbde
	direct ptrace.
26bbde
26bbde
Index: gdb-7.9.90.20150709/gdb/nat/linux-ptrace.c
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/nat/linux-ptrace.c	2015-07-09 18:24:13.705140384 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/nat/linux-ptrace.c	2015-07-09 18:24:24.911235973 +0200
26bbde
@@ -23,6 +23,10 @@
26bbde
 #include "buffer.h"
26bbde
 #include "gdb_wait.h"
26bbde
 
26bbde
+#ifdef HAVE_SELINUX_SELINUX_H
26bbde
+# include <selinux/selinux.h>
26bbde
+#endif /* HAVE_SELINUX_SELINUX_H */
26bbde
+
26bbde
 /* Stores the ptrace options supported by the running kernel.
26bbde
    A value of -1 means we did not check for features yet.  A value
26bbde
    of 0 means there are no supported features.  */
26bbde
@@ -47,6 +51,8 @@ linux_ptrace_attach_fail_reason (pid_t p
26bbde
     buffer_xml_printf (buffer, _("process %d is a zombie "
26bbde
 				 "- the process has already terminated"),
26bbde
 		       (int) pid);
26bbde
+
26bbde
+  linux_ptrace_create_warnings (buffer);
26bbde
 }
26bbde
 
26bbde
 /* See linux-ptrace.h.  */
26bbde
@@ -581,6 +587,22 @@ linux_ptrace_init_warnings (void)
26bbde
   linux_ptrace_test_ret_to_nx ();
26bbde
 }
26bbde
 
26bbde
+/* Print all possible reasons we could fail to create a traced process.  */
26bbde
+
26bbde
+void
26bbde
+linux_ptrace_create_warnings (struct buffer *buffer)
26bbde
+{
26bbde
+#ifdef HAVE_LIBSELINUX
26bbde
+  /* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
26bbde
+     forbidden.  */
26bbde
+  if (security_get_boolean_active ("deny_ptrace") == 1)
26bbde
+    buffer_xml_printf (buffer,
26bbde
+		       _("the SELinux boolean 'deny_ptrace' is enabled, "
26bbde
+			 "you can disable this process attach protection by: "
26bbde
+			 "(gdb) shell sudo setsebool deny_ptrace=0"));
26bbde
+#endif /* HAVE_LIBSELINUX */
26bbde
+}
26bbde
+
26bbde
 /* Extract extended ptrace event from wait status.  */
26bbde
 
26bbde
 int
26bbde
Index: gdb-7.9.90.20150709/gdb/nat/linux-ptrace.h
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/nat/linux-ptrace.h	2015-07-09 18:24:13.706140392 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/nat/linux-ptrace.h	2015-07-09 18:24:24.912235981 +0200
26bbde
@@ -157,6 +157,7 @@ extern void linux_ptrace_attach_fail_rea
26bbde
 extern char *linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
26bbde
 
26bbde
 extern void linux_ptrace_init_warnings (void);
26bbde
+extern void linux_ptrace_create_warnings (struct buffer *buffer);
26bbde
 extern void linux_check_ptrace_features (void);
26bbde
 extern void linux_enable_event_reporting (pid_t pid, int attached);
26bbde
 extern void linux_disable_event_reporting (pid_t pid);
26bbde
Index: gdb-7.9.90.20150709/gdb/configure.ac
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/configure.ac	2015-07-09 18:24:13.707140401 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/configure.ac	2015-07-09 18:24:24.913235990 +0200
26bbde
@@ -2150,6 +2150,10 @@ case $host_os in
26bbde
 esac
26bbde
 AC_DEFINE_UNQUOTED(GDBINIT,"$gdbinit",[The .gdbinit filename.])
26bbde
 
26bbde
+dnl Check security_get_boolean_active availability.
26bbde
+AC_CHECK_HEADERS(selinux/selinux.h)
26bbde
+AC_CHECK_LIB(selinux, security_get_boolean_active)
26bbde
+
26bbde
 dnl Handle optional features that can be enabled.
26bbde
 
26bbde
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
26bbde
Index: gdb-7.9.90.20150709/gdb/gdbserver/configure.ac
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/gdbserver/configure.ac	2015-07-09 18:24:13.707140401 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/gdbserver/configure.ac	2015-07-09 18:24:24.913235990 +0200
26bbde
@@ -499,6 +499,10 @@ if $want_ipa ; then
26bbde
    fi
26bbde
 fi
26bbde
 
26bbde
+dnl Check security_get_boolean_active availability.
26bbde
+AC_CHECK_HEADERS(selinux/selinux.h)
26bbde
+AC_CHECK_LIB(selinux, security_get_boolean_active)
26bbde
+
26bbde
 AC_SUBST(GDBSERVER_DEPFILES)
26bbde
 AC_SUBST(GDBSERVER_LIBS)
26bbde
 AC_SUBST(srv_xmlbuiltin)
26bbde
Index: gdb-7.9.90.20150709/gdb/gdbserver/linux-low.c
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/gdbserver/linux-low.c	2015-07-09 18:24:13.709140418 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/gdbserver/linux-low.c	2015-07-09 18:24:24.914235998 +0200
26bbde
@@ -755,6 +755,29 @@ add_lwp (ptid_t ptid)
26bbde
   return lwp;
26bbde
 }
26bbde
 
26bbde
+/* Execute PTRACE_TRACEME with error checking.  */
26bbde
+
26bbde
+static void
26bbde
+linux_traceme (const char *program)
26bbde
+{
26bbde
+  int save_errno;
26bbde
+  struct buffer buffer;
26bbde
+
26bbde
+  errno = 0;
26bbde
+  if (ptrace (PTRACE_TRACEME, 0,
26bbde
+	      (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0) == 0)
26bbde
+    return;
26bbde
+
26bbde
+  save_errno = errno;
26bbde
+  buffer_init (&buffer);
26bbde
+  linux_ptrace_create_warnings (&buffer);
26bbde
+  buffer_grow_str0 (&buffer, "");
26bbde
+  fprintf (stderr, _("%sCannot trace created process %s: %s.\n"),
26bbde
+	   buffer_finish (&buffer), program, strerror (save_errno));
26bbde
+  fflush (stderr);
26bbde
+  _exit (0177);
26bbde
+}
26bbde
+
26bbde
 /* Start an inferior process and returns its pid.
26bbde
    ALLARGS is a vector of program-name and args. */
26bbde
 
26bbde
@@ -778,7 +801,7 @@ linux_create_inferior (char *program, ch
26bbde
   if (pid == 0)
26bbde
     {
26bbde
       close_most_fds ();
26bbde
-      ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
26bbde
+      linux_traceme (program);
26bbde
 
26bbde
 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does.  */
26bbde
       signal (__SIGRTMIN + 1, SIG_DFL);
26bbde
Index: gdb-7.9.90.20150709/gdb/inf-ptrace.c
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/inf-ptrace.c	2015-07-09 18:24:13.710140426 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/inf-ptrace.c	2015-07-09 18:24:24.915236007 +0200
26bbde
@@ -79,7 +79,15 @@ static void
26bbde
 inf_ptrace_me (void)
26bbde
 {
26bbde
   /* "Trace me, Dr. Memory!"  */
26bbde
+  errno = 0;
26bbde
   ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
26bbde
+  if (errno != 0)
26bbde
+    {
26bbde
+      fprintf_unfiltered (gdb_stderr, _("Cannot create process: %s\n"),
26bbde
+			  safe_strerror (errno));
26bbde
+      gdb_flush (gdb_stderr);
26bbde
+      _exit (0177);
26bbde
+    }
26bbde
 }
26bbde
 
26bbde
 /* Start a new inferior Unix child process.  EXEC_FILE is the file to
26bbde
Index: gdb-7.9.90.20150709/gdb/linux-nat.c
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/linux-nat.c	2015-07-09 18:24:13.712140443 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/linux-nat.c	2015-07-09 18:24:24.916236015 +0200
26bbde
@@ -1184,6 +1184,7 @@ linux_nat_create_inferior (struct target
26bbde
 {
26bbde
   struct cleanup *restore_personality
26bbde
     = maybe_disable_address_space_randomization (disable_randomization);
26bbde
+  volatile struct gdb_exception ex;
26bbde
 
26bbde
   /* The fork_child mechanism is synchronous and calls target_wait, so
26bbde
      we have to mask the async mode.  */
26bbde
@@ -1191,7 +1192,28 @@ linux_nat_create_inferior (struct target
26bbde
   /* Make sure we report all signals during startup.  */
26bbde
   linux_nat_pass_signals (ops, 0, NULL);
26bbde
 
26bbde
-  linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
26bbde
+  TRY
26bbde
+    {
26bbde
+      linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
26bbde
+    }
26bbde
+  CATCH (ex, RETURN_MASK_ERROR)
26bbde
+    {
26bbde
+      struct buffer buffer;
26bbde
+      char *message, *buffer_s;
26bbde
+
26bbde
+      message = xstrdup (ex.message);
26bbde
+      make_cleanup (xfree, message);
26bbde
+
26bbde
+      buffer_init (&buffer);
26bbde
+      linux_ptrace_create_warnings (&buffer);
26bbde
+
26bbde
+      buffer_grow_str0 (&buffer, "");
26bbde
+      buffer_s = buffer_finish (&buffer);
26bbde
+      make_cleanup (xfree, buffer_s);
26bbde
+
26bbde
+      throw_error (ex.error, "%s%s", buffer_s, message);
26bbde
+    }
26bbde
+  END_CATCH
26bbde
 
26bbde
   do_cleanups (restore_personality);
26bbde
 }
26bbde
Index: gdb-7.9.90.20150709/gdb/config.in
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/config.in	2015-07-09 18:24:13.713140452 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/config.in	2015-07-09 18:24:24.916236015 +0200
26bbde
@@ -264,6 +264,9 @@
26bbde
 /* Define if librpm library is being used. */
26bbde
 #undef HAVE_LIBRPM
26bbde
 
26bbde
+/* Define to 1 if you have the `selinux' library (-lselinux). */
26bbde
+#undef HAVE_LIBSELINUX
26bbde
+
26bbde
 /* Define to 1 if you have the <libunwind-ia64.h> header file. */
26bbde
 #undef HAVE_LIBUNWIND_IA64_H
26bbde
 
26bbde
@@ -396,6 +399,9 @@
26bbde
 /* Define to 1 if you have the `scm_new_smob' function. */
26bbde
 #undef HAVE_SCM_NEW_SMOB
26bbde
 
26bbde
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
26bbde
+#undef HAVE_SELINUX_SELINUX_H
26bbde
+
26bbde
 /* Define to 1 if you have the `setlocale' function. */
26bbde
 #undef HAVE_SETLOCALE
26bbde
 
26bbde
Index: gdb-7.9.90.20150709/gdb/configure
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/configure	2015-07-09 18:24:13.718140495 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/configure	2015-07-09 18:24:24.920236049 +0200
26bbde
@@ -14228,6 +14228,64 @@ cat >>confdefs.h <<_ACEOF
26bbde
 _ACEOF
26bbde
 
26bbde
 
26bbde
+for ac_header in selinux/selinux.h
26bbde
+do :
26bbde
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
26bbde
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
26bbde
+  cat >>confdefs.h <<_ACEOF
26bbde
+#define HAVE_SELINUX_SELINUX_H 1
26bbde
+_ACEOF
26bbde
+
26bbde
+fi
26bbde
+
26bbde
+done
26bbde
+
26bbde
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
26bbde
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
26bbde
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
26bbde
+  $as_echo_n "(cached) " >&6
26bbde
+else
26bbde
+  ac_check_lib_save_LIBS=$LIBS
26bbde
+LIBS="-lselinux  $LIBS"
26bbde
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
26bbde
+/* end confdefs.h.  */
26bbde
+
26bbde
+/* Override any GCC internal prototype to avoid an error.
26bbde
+   Use char because int might match the return type of a GCC
26bbde
+   builtin and then its argument prototype would still apply.  */
26bbde
+#ifdef __cplusplus
26bbde
+extern "C"
26bbde
+#endif
26bbde
+char security_get_boolean_active ();
26bbde
+int
26bbde
+main ()
26bbde
+{
26bbde
+return security_get_boolean_active ();
26bbde
+  ;
26bbde
+  return 0;
26bbde
+}
26bbde
+_ACEOF
26bbde
+if ac_fn_c_try_link "$LINENO"; then :
26bbde
+  ac_cv_lib_selinux_security_get_boolean_active=yes
26bbde
+else
26bbde
+  ac_cv_lib_selinux_security_get_boolean_active=no
26bbde
+fi
26bbde
+rm -f core conftest.err conftest.$ac_objext \
26bbde
+    conftest$ac_exeext conftest.$ac_ext
26bbde
+LIBS=$ac_check_lib_save_LIBS
26bbde
+fi
26bbde
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
26bbde
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
26bbde
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
26bbde
+  cat >>confdefs.h <<_ACEOF
26bbde
+#define HAVE_LIBSELINUX 1
26bbde
+_ACEOF
26bbde
+
26bbde
+  LIBS="-lselinux $LIBS"
26bbde
+
26bbde
+fi
26bbde
+
26bbde
+
26bbde
 
26bbde
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
26bbde
 # except that the argument to --with-sysroot is optional.
26bbde
Index: gdb-7.9.90.20150709/gdb/gdbserver/config.in
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/gdbserver/config.in	2015-07-09 18:24:13.719140503 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/gdbserver/config.in	2015-07-09 18:24:24.920236049 +0200
26bbde
@@ -113,6 +113,9 @@
26bbde
 /* Define to 1 if you have the `mcheck' library (-lmcheck). */
26bbde
 #undef HAVE_LIBMCHECK
26bbde
 
26bbde
+/* Define to 1 if you have the `selinux' library (-lselinux). */
26bbde
+#undef HAVE_LIBSELINUX
26bbde
+
26bbde
 /* Define if the target supports branch tracing. */
26bbde
 #undef HAVE_LINUX_BTRACE
26bbde
 
26bbde
@@ -189,6 +192,9 @@
26bbde
 /* Define to 1 if you have the `setns' function. */
26bbde
 #undef HAVE_SETNS
26bbde
 
26bbde
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
26bbde
+#undef HAVE_SELINUX_SELINUX_H
26bbde
+
26bbde
 /* Define to 1 if you have the <sgtty.h> header file. */
26bbde
 #undef HAVE_SGTTY_H
26bbde
 
26bbde
Index: gdb-7.9.90.20150709/gdb/gdbserver/configure
26bbde
===================================================================
26bbde
--- gdb-7.9.90.20150709.orig/gdb/gdbserver/configure	2015-07-09 18:24:13.721140520 +0200
26bbde
+++ gdb-7.9.90.20150709/gdb/gdbserver/configure	2015-07-09 18:24:24.922236066 +0200
26bbde
@@ -7022,6 +7022,64 @@ if $want_ipa ; then
26bbde
    fi
26bbde
 fi
26bbde
 
26bbde
+for ac_header in selinux/selinux.h
26bbde
+do :
26bbde
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
26bbde
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
26bbde
+  cat >>confdefs.h <<_ACEOF
26bbde
+#define HAVE_SELINUX_SELINUX_H 1
26bbde
+_ACEOF
26bbde
+
26bbde
+fi
26bbde
+
26bbde
+done
26bbde
+
26bbde
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
26bbde
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
26bbde
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
26bbde
+  $as_echo_n "(cached) " >&6
26bbde
+else
26bbde
+  ac_check_lib_save_LIBS=$LIBS
26bbde
+LIBS="-lselinux  $LIBS"
26bbde
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
26bbde
+/* end confdefs.h.  */
26bbde
+
26bbde
+/* Override any GCC internal prototype to avoid an error.
26bbde
+   Use char because int might match the return type of a GCC
26bbde
+   builtin and then its argument prototype would still apply.  */
26bbde
+#ifdef __cplusplus
26bbde
+extern "C"
26bbde
+#endif
26bbde
+char security_get_boolean_active ();
26bbde
+int
26bbde
+main ()
26bbde
+{
26bbde
+return security_get_boolean_active ();
26bbde
+  ;
26bbde
+  return 0;
26bbde
+}
26bbde
+_ACEOF
26bbde
+if ac_fn_c_try_link "$LINENO"; then :
26bbde
+  ac_cv_lib_selinux_security_get_boolean_active=yes
26bbde
+else
26bbde
+  ac_cv_lib_selinux_security_get_boolean_active=no
26bbde
+fi
26bbde
+rm -f core conftest.err conftest.$ac_objext \
26bbde
+    conftest$ac_exeext conftest.$ac_ext
26bbde
+LIBS=$ac_check_lib_save_LIBS
26bbde
+fi
26bbde
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
26bbde
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
26bbde
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
26bbde
+  cat >>confdefs.h <<_ACEOF
26bbde
+#define HAVE_LIBSELINUX 1
26bbde
+_ACEOF
26bbde
+
26bbde
+  LIBS="-lselinux $LIBS"
26bbde
+
26bbde
+fi
26bbde
+
26bbde
+
26bbde
 
26bbde
 
26bbde