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

b2f73e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
b2f73e
From: Fedora GDB patches <invalid@email.com>
b2f73e
Date: Fri, 27 Oct 2017 21:07:50 +0200
b2f73e
Subject: gdb-attach-fail-reasons-5of5.patch
b2f73e
b2f73e
;; Print reasons for failed attach/spawn incl. SELinux deny_ptrace (BZ 786878).
b2f73e
;;=push+jan
b2f73e
b2f73e
http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html
b2f73e
b2f73e
Hi,
b2f73e
b2f73e
and here is the last bit for new SELinux 'deny_ptrace':
b2f73e
	https://bugzilla.redhat.com/show_bug.cgi?id=786878
b2f73e
b2f73e
As even PTRACE_TRACEME fails in such case it needs to install hook for even
b2f73e
that event.
b2f73e
b2f73e
Thanks,
b2f73e
Jan
b2f73e
b2f73e
gdb/
b2f73e
2012-03-06  Jan Kratochvil  <jan.kratochvil@redhat.com>
b2f73e
b2f73e
	* common/linux-ptrace.c [HAVE_SELINUX_SELINUX_H]: include
b2f73e
	selinux/selinux.h.
b2f73e
	(linux_ptrace_attach_warnings): Call linux_ptrace_create_warnings.
b2f73e
	(linux_ptrace_create_warnings): New.
b2f73e
	* common/linux-ptrace.h (linux_ptrace_create_warnings): New declaration.
b2f73e
	* config.in: Regenerate.
b2f73e
	* configure: Regenerate.
b2f73e
	* configure.ac: Check selinux/selinux.h and the selinux library.
b2f73e
	* inf-ptrace.c (inf_ptrace_me): Check the ptrace result.
b2f73e
	* linux-nat.c (linux_nat_create_inferior): New variable ex.  Wrap
b2f73e
	to_create_inferior into TRY_CATCH, call linux_ptrace_create_warnings.
b2f73e
b2f73e
gdb/gdbserver/
b2f73e
	* config.in: Regenerate.
b2f73e
	* configure: Regenerate.
b2f73e
	* configure.ac: Check selinux/selinux.h and the selinux library.
b2f73e
	* linux-low.c (linux_traceme): New function.
b2f73e
	(linux_create_inferior, linux_tracefork_child): Call it instead of
b2f73e
	direct ptrace.
b2f73e
b2f73e
diff --git a/gdb/config.in b/gdb/config.in
b2f73e
--- a/gdb/config.in
b2f73e
+++ b/gdb/config.in
b2f73e
@@ -251,6 +251,9 @@
b2f73e
 /* Define if librpm library is being used. */
b2f73e
 #undef HAVE_LIBRPM
b2f73e
 
b2f73e
+/* Define to 1 if you have the `selinux' library (-lselinux). */
b2f73e
+#undef HAVE_LIBSELINUX
b2f73e
+
b2f73e
 /* Define to 1 if you have the <libunwind-ia64.h> header file. */
b2f73e
 #undef HAVE_LIBUNWIND_IA64_H
b2f73e
 
b2f73e
@@ -386,6 +389,9 @@
b2f73e
 /* Define to 1 if you have the `scm_new_smob' function. */
b2f73e
 #undef HAVE_SCM_NEW_SMOB
b2f73e
 
b2f73e
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
b2f73e
+#undef HAVE_SELINUX_SELINUX_H
b2f73e
+
b2f73e
 /* Define to 1 if you have the `setlocale' function. */
b2f73e
 #undef HAVE_SETLOCALE
b2f73e
 
b2f73e
diff --git a/gdb/configure b/gdb/configure
b2f73e
--- a/gdb/configure
b2f73e
+++ b/gdb/configure
b2f73e
@@ -16434,6 +16434,64 @@ cat >>confdefs.h <<_ACEOF
b2f73e
 _ACEOF
b2f73e
 
b2f73e
 
b2f73e
+for ac_header in selinux/selinux.h
b2f73e
+do :
b2f73e
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
b2f73e
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
b2f73e
+  cat >>confdefs.h <<_ACEOF
b2f73e
+#define HAVE_SELINUX_SELINUX_H 1
b2f73e
+_ACEOF
b2f73e
+
b2f73e
+fi
b2f73e
+
b2f73e
+done
b2f73e
+
b2f73e
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
b2f73e
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
b2f73e
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
b2f73e
+  $as_echo_n "(cached) " >&6
b2f73e
+else
b2f73e
+  ac_check_lib_save_LIBS=$LIBS
b2f73e
+LIBS="-lselinux  $LIBS"
b2f73e
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
b2f73e
+/* end confdefs.h.  */
b2f73e
+
b2f73e
+/* Override any GCC internal prototype to avoid an error.
b2f73e
+   Use char because int might match the return type of a GCC
b2f73e
+   builtin and then its argument prototype would still apply.  */
b2f73e
+#ifdef __cplusplus
b2f73e
+extern "C"
b2f73e
+#endif
b2f73e
+char security_get_boolean_active ();
b2f73e
+int
b2f73e
+main ()
b2f73e
+{
b2f73e
+return security_get_boolean_active ();
b2f73e
+  ;
b2f73e
+  return 0;
b2f73e
+}
b2f73e
+_ACEOF
b2f73e
+if ac_fn_c_try_link "$LINENO"; then :
b2f73e
+  ac_cv_lib_selinux_security_get_boolean_active=yes
b2f73e
+else
b2f73e
+  ac_cv_lib_selinux_security_get_boolean_active=no
b2f73e
+fi
b2f73e
+rm -f core conftest.err conftest.$ac_objext \
b2f73e
+    conftest$ac_exeext conftest.$ac_ext
b2f73e
+LIBS=$ac_check_lib_save_LIBS
b2f73e
+fi
b2f73e
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
b2f73e
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
b2f73e
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
b2f73e
+  cat >>confdefs.h <<_ACEOF
b2f73e
+#define HAVE_LIBSELINUX 1
b2f73e
+_ACEOF
b2f73e
+
b2f73e
+  LIBS="-lselinux $LIBS"
b2f73e
+
b2f73e
+fi
b2f73e
+
b2f73e
+
b2f73e
 
b2f73e
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
b2f73e
 # except that the argument to --with-sysroot is optional.
b2f73e
diff --git a/gdb/configure.ac b/gdb/configure.ac
b2f73e
--- a/gdb/configure.ac
b2f73e
+++ b/gdb/configure.ac
b2f73e
@@ -1964,6 +1964,10 @@ case $host_os in
b2f73e
 esac
b2f73e
 AC_DEFINE_UNQUOTED(GDBINIT,"$gdbinit",[The .gdbinit filename.])
b2f73e
 
b2f73e
+dnl Check security_get_boolean_active availability.
b2f73e
+AC_CHECK_HEADERS(selinux/selinux.h)
b2f73e
+AC_CHECK_LIB(selinux, security_get_boolean_active)
b2f73e
+
b2f73e
 dnl Handle optional features that can be enabled.
b2f73e
 
b2f73e
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
b2f73e
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
b2f73e
--- a/gdb/gdbserver/config.in
b2f73e
+++ b/gdb/gdbserver/config.in
b2f73e
@@ -125,6 +125,9 @@
b2f73e
 /* Define to 1 if you have the `dl' library (-ldl). */
b2f73e
 #undef HAVE_LIBDL
b2f73e
 
b2f73e
+/* Define to 1 if you have the `selinux' library (-lselinux). */
b2f73e
+#undef HAVE_LIBSELINUX
b2f73e
+
b2f73e
 /* Define if the target supports branch tracing. */
b2f73e
 #undef HAVE_LINUX_BTRACE
b2f73e
 
b2f73e
@@ -210,6 +213,9 @@
b2f73e
 /* Define to 1 if you have the `pwrite' function. */
b2f73e
 #undef HAVE_PWRITE
b2f73e
 
b2f73e
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
b2f73e
+#undef HAVE_SELINUX_SELINUX_H
b2f73e
+
b2f73e
 /* Define to 1 if you have the `setns' function. */
b2f73e
 #undef HAVE_SETNS
b2f73e
 
b2f73e
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
b2f73e
--- a/gdb/gdbserver/configure
b2f73e
+++ b/gdb/gdbserver/configure
b2f73e
@@ -9398,6 +9398,64 @@ if $want_ipa ; then
b2f73e
    fi
b2f73e
 fi
b2f73e
 
b2f73e
+for ac_header in selinux/selinux.h
b2f73e
+do :
b2f73e
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
b2f73e
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
b2f73e
+  cat >>confdefs.h <<_ACEOF
b2f73e
+#define HAVE_SELINUX_SELINUX_H 1
b2f73e
+_ACEOF
b2f73e
+
b2f73e
+fi
b2f73e
+
b2f73e
+done
b2f73e
+
b2f73e
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
b2f73e
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
b2f73e
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
b2f73e
+  $as_echo_n "(cached) " >&6
b2f73e
+else
b2f73e
+  ac_check_lib_save_LIBS=$LIBS
b2f73e
+LIBS="-lselinux  $LIBS"
b2f73e
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
b2f73e
+/* end confdefs.h.  */
b2f73e
+
b2f73e
+/* Override any GCC internal prototype to avoid an error.
b2f73e
+   Use char because int might match the return type of a GCC
b2f73e
+   builtin and then its argument prototype would still apply.  */
b2f73e
+#ifdef __cplusplus
b2f73e
+extern "C"
b2f73e
+#endif
b2f73e
+char security_get_boolean_active ();
b2f73e
+int
b2f73e
+main ()
b2f73e
+{
b2f73e
+return security_get_boolean_active ();
b2f73e
+  ;
b2f73e
+  return 0;
b2f73e
+}
b2f73e
+_ACEOF
b2f73e
+if ac_fn_c_try_link "$LINENO"; then :
b2f73e
+  ac_cv_lib_selinux_security_get_boolean_active=yes
b2f73e
+else
b2f73e
+  ac_cv_lib_selinux_security_get_boolean_active=no
b2f73e
+fi
b2f73e
+rm -f core conftest.err conftest.$ac_objext \
b2f73e
+    conftest$ac_exeext conftest.$ac_ext
b2f73e
+LIBS=$ac_check_lib_save_LIBS
b2f73e
+fi
b2f73e
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
b2f73e
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
b2f73e
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
b2f73e
+  cat >>confdefs.h <<_ACEOF
b2f73e
+#define HAVE_LIBSELINUX 1
b2f73e
+_ACEOF
b2f73e
+
b2f73e
+  LIBS="-lselinux $LIBS"
b2f73e
+
b2f73e
+fi
b2f73e
+
b2f73e
+
b2f73e
 
b2f73e
 
b2f73e
 
b2f73e
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
b2f73e
--- a/gdb/gdbserver/configure.ac
b2f73e
+++ b/gdb/gdbserver/configure.ac
b2f73e
@@ -465,6 +465,10 @@ if $want_ipa ; then
b2f73e
    fi
b2f73e
 fi
b2f73e
 
b2f73e
+dnl Check security_get_boolean_active availability.
b2f73e
+AC_CHECK_HEADERS(selinux/selinux.h)
b2f73e
+AC_CHECK_LIB(selinux, security_get_boolean_active)
b2f73e
+
b2f73e
 AC_SUBST(GDBSERVER_DEPFILES)
b2f73e
 AC_SUBST(GDBSERVER_LIBS)
b2f73e
 AC_SUBST(srv_xmlbuiltin)
b2f73e
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
b2f73e
--- a/gdb/gdbserver/linux-low.c
b2f73e
+++ b/gdb/gdbserver/linux-low.c
b2f73e
@@ -968,7 +968,16 @@ linux_ptrace_fun ()
b2f73e
 {
b2f73e
   if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
b2f73e
 	      (PTRACE_TYPE_ARG4) 0) < 0)
b2f73e
-    trace_start_error_with_name ("ptrace");
b2f73e
+    {
b2f73e
+      int save_errno = errno;
b2f73e
+
b2f73e
+      std::string msg (linux_ptrace_create_warnings ());
b2f73e
+
b2f73e
+      msg += _("Cannot trace created process");
b2f73e
+
b2f73e
+      errno = save_errno;
b2f73e
+      trace_start_error_with_name (msg.c_str ());
b2f73e
+    }
b2f73e
 
b2f73e
   if (setpgid (0, 0) < 0)
b2f73e
     trace_start_error_with_name ("setpgid");
b2f73e
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
b2f73e
--- a/gdb/linux-nat.c
b2f73e
+++ b/gdb/linux-nat.c
b2f73e
@@ -1092,7 +1092,16 @@ linux_nat_target::create_inferior (const char *exec_file,
b2f73e
   /* Make sure we report all signals during startup.  */
b2f73e
   pass_signals ({});
b2f73e
 
b2f73e
-  inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
b2f73e
+  try
b2f73e
+    {
b2f73e
+      inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
b2f73e
+    }
b2f73e
+  catch (const gdb_exception_error &ex)
b2f73e
+    {
b2f73e
+      std::string result =  linux_ptrace_create_warnings ();
b2f73e
+
b2f73e
+      throw_error (ex.error, "%s%s", result.c_str (), ex.message->c_str ());
b2f73e
+    }
b2f73e
 }
b2f73e
 
b2f73e
 /* Callback for linux_proc_attach_tgid_threads.  Attach to PTID if not
b2f73e
diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c
b2f73e
--- a/gdb/nat/linux-ptrace.c
b2f73e
+++ b/gdb/nat/linux-ptrace.c
b2f73e
@@ -25,6 +25,10 @@
b2f73e
 #include <sys/procfs.h>
b2f73e
 #endif
b2f73e
 
b2f73e
+#ifdef HAVE_SELINUX_SELINUX_H
b2f73e
+# include <selinux/selinux.h>
b2f73e
+#endif /* HAVE_SELINUX_SELINUX_H */
b2f73e
+
b2f73e
 /* Stores the ptrace options supported by the running kernel.
b2f73e
    A value of -1 means we did not check for features yet.  A value
b2f73e
    of 0 means there are no supported features.  */
b2f73e
@@ -50,6 +54,8 @@ linux_ptrace_attach_fail_reason (pid_t pid)
b2f73e
 		      "terminated"),
b2f73e
 		    (int) pid);
b2f73e
 
b2f73e
+  result += linux_ptrace_create_warnings ();
b2f73e
+
b2f73e
   return result;
b2f73e
 }
b2f73e
 
b2f73e
@@ -586,6 +592,25 @@ linux_ptrace_init_warnings (void)
b2f73e
   linux_ptrace_test_ret_to_nx ();
b2f73e
 }
b2f73e
 
b2f73e
+/* Print all possible reasons we could fail to create a traced process.  */
b2f73e
+
b2f73e
+std::string
b2f73e
+linux_ptrace_create_warnings ()
b2f73e
+{
b2f73e
+  std::string result;
b2f73e
+
b2f73e
+#ifdef HAVE_LIBSELINUX
b2f73e
+  /* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
b2f73e
+     forbidden.  */
b2f73e
+  if (security_get_boolean_active ("deny_ptrace") == 1)
b2f73e
+    string_appendf (result,
b2f73e
+		    _("the SELinux boolean 'deny_ptrace' is enabled, "
b2f73e
+		      "you can disable this process attach protection by: "
b2f73e
+		      "(gdb) shell sudo setsebool deny_ptrace=0\n"));
b2f73e
+#endif /* HAVE_LIBSELINUX */
b2f73e
+  return result;
b2f73e
+}
b2f73e
+
b2f73e
 /* Extract extended ptrace event from wait status.  */
b2f73e
 
b2f73e
 int
b2f73e
diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h
b2f73e
--- a/gdb/nat/linux-ptrace.h
b2f73e
+++ b/gdb/nat/linux-ptrace.h
b2f73e
@@ -184,6 +184,7 @@ extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
b2f73e
 extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
b2f73e
 
b2f73e
 extern void linux_ptrace_init_warnings (void);
b2f73e
+extern std::string linux_ptrace_create_warnings ();
b2f73e
 extern void linux_check_ptrace_features (void);
b2f73e
 extern void linux_enable_event_reporting (pid_t pid, int attached);
b2f73e
 extern void linux_disable_event_reporting (pid_t pid);