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

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