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

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