keiths / rpms / gdb

Forked from rpms/gdb 3 days ago
Clone
a8767b3
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
f524ac5
From: Fedora GDB patches <invalid@email.com>
f524ac5
Date: Fri, 27 Oct 2017 21:07:50 +0200
fd7e5d7
Subject: gdb-attach-fail-reasons-5of5.patch
f524ac5
f637971
;; Print reasons for failed attach/spawn incl. SELinux deny_ptrace (BZ 786878).
f637971
;;=push+jan
f524ac5
f524ac5
http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html
f524ac5
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
Thanks,
Jan Kratochvil c64e732
Jan
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
f637971
diff --git a/gdb/config.in b/gdb/config.in
f637971
--- a/gdb/config.in
f637971
+++ b/gdb/config.in
ed8730b
@@ -253,6 +253,9 @@
Jan Kratochvil 872aab0
 /* Define if librpm library is being used. */
Jan Kratochvil 872aab0
 #undef HAVE_LIBRPM
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+/* Define to 1 if you have the `selinux' library (-lselinux). */
Jan Kratochvil 872aab0
+#undef HAVE_LIBSELINUX
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
 /* Define to 1 if you have the <libunwind-ia64.h> header file. */
Jan Kratochvil 872aab0
 #undef HAVE_LIBUNWIND_IA64_H
Jan Kratochvil 872aab0
 
ed8730b
@@ -388,6 +391,9 @@
Jan Kratochvil eb6cb2d
 /* Define to 1 if you have the `scm_new_smob' function. */
Jan Kratochvil eb6cb2d
 #undef HAVE_SCM_NEW_SMOB
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
Jan Kratochvil 872aab0
+#undef HAVE_SELINUX_SELINUX_H
Jan Kratochvil 872aab0
+
Jan Kratochvil 43b0bce
 /* Define to 1 if you have the `setlocale' function. */
Jan Kratochvil 43b0bce
 #undef HAVE_SETLOCALE
Jan Kratochvil 872aab0
 
f637971
diff --git a/gdb/configure b/gdb/configure
f637971
--- a/gdb/configure
f637971
+++ b/gdb/configure
ed8730b
@@ -16861,6 +16861,64 @@ cat >>confdefs.h <<_ACEOF
Jan Kratochvil 872aab0
 _ACEOF
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+for ac_header in selinux/selinux.h
Jan Kratochvil 872aab0
+do :
Jan Kratochvil 872aab0
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
Jan Kratochvil 872aab0
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
Jan Kratochvil 872aab0
+  cat >>confdefs.h <<_ACEOF
Jan Kratochvil 872aab0
+#define HAVE_SELINUX_SELINUX_H 1
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+done
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
Jan Kratochvil 872aab0
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
Jan Kratochvil 872aab0
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
Jan Kratochvil 872aab0
+  $as_echo_n "(cached) " >&6
Jan Kratochvil 872aab0
+else
Jan Kratochvil 872aab0
+  ac_check_lib_save_LIBS=$LIBS
Jan Kratochvil 872aab0
+LIBS="-lselinux  $LIBS"
Jan Kratochvil 872aab0
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
Jan Kratochvil 872aab0
+/* end confdefs.h.  */
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+/* Override any GCC internal prototype to avoid an error.
Jan Kratochvil 872aab0
+   Use char because int might match the return type of a GCC
Jan Kratochvil 872aab0
+   builtin and then its argument prototype would still apply.  */
Jan Kratochvil 872aab0
+#ifdef __cplusplus
Jan Kratochvil 872aab0
+extern "C"
Jan Kratochvil 872aab0
+#endif
Jan Kratochvil 872aab0
+char security_get_boolean_active ();
Jan Kratochvil 872aab0
+int
Jan Kratochvil 872aab0
+main ()
Jan Kratochvil 872aab0
+{
Jan Kratochvil 872aab0
+return security_get_boolean_active ();
Jan Kratochvil 872aab0
+  ;
Jan Kratochvil 872aab0
+  return 0;
Jan Kratochvil 872aab0
+}
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+if ac_fn_c_try_link "$LINENO"; then :
Jan Kratochvil 872aab0
+  ac_cv_lib_selinux_security_get_boolean_active=yes
Jan Kratochvil 872aab0
+else
Jan Kratochvil 872aab0
+  ac_cv_lib_selinux_security_get_boolean_active=no
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+rm -f core conftest.err conftest.$ac_objext \
Jan Kratochvil 872aab0
+    conftest$ac_exeext conftest.$ac_ext
Jan Kratochvil 872aab0
+LIBS=$ac_check_lib_save_LIBS
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
Jan Kratochvil 872aab0
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
Jan Kratochvil 872aab0
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
Jan Kratochvil 872aab0
+  cat >>confdefs.h <<_ACEOF
Jan Kratochvil 872aab0
+#define HAVE_LIBSELINUX 1
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+  LIBS="-lselinux $LIBS"
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
Jan Kratochvil 872aab0
 # except that the argument to --with-sysroot is optional.
f637971
diff --git a/gdb/configure.ac b/gdb/configure.ac
f637971
--- a/gdb/configure.ac
f637971
+++ b/gdb/configure.ac
ed8730b
@@ -1900,6 +1900,10 @@ case $host_os in
f637971
 esac
f637971
 AC_DEFINE_UNQUOTED(GDBINIT,"$gdbinit",[The .gdbinit filename.])
f637971
 
f637971
+dnl Check security_get_boolean_active availability.
f637971
+AC_CHECK_HEADERS(selinux/selinux.h)
f637971
+AC_CHECK_LIB(selinux, security_get_boolean_active)
f637971
+
f637971
 dnl Handle optional features that can be enabled.
f637971
 
f637971
 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
ed8730b
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
ed8730b
--- a/gdb/linux-nat.c
ed8730b
+++ b/gdb/linux-nat.c
ed8730b
@@ -1103,7 +1103,16 @@ linux_nat_target::create_inferior (const char *exec_file,
ed8730b
   /* Make sure we report all signals during startup.  */
ed8730b
   pass_signals ({});
ed8730b
 
ed8730b
-  inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
ed8730b
+  try
ed8730b
+    {
ed8730b
+      inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
ed8730b
+    }
ed8730b
+  catch (const gdb_exception_error &ex)
ed8730b
+    {
ed8730b
+      std::string result =  linux_ptrace_create_warnings ();
ed8730b
+
ed8730b
+      throw_error (ex.error, "%s%s", result.c_str (), ex.message->c_str ());
ed8730b
+    }
ed8730b
 }
ed8730b
 
ed8730b
 /* Callback for linux_proc_attach_tgid_threads.  Attach to PTID if not
ed8730b
diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c
ed8730b
--- a/gdb/nat/linux-ptrace.c
ed8730b
+++ b/gdb/nat/linux-ptrace.c
ed8730b
@@ -25,6 +25,10 @@
ed8730b
 #include <sys/procfs.h>
ed8730b
 #endif
ed8730b
 
ed8730b
+#ifdef HAVE_SELINUX_SELINUX_H
ed8730b
+# include <selinux/selinux.h>
ed8730b
+#endif /* HAVE_SELINUX_SELINUX_H */
ed8730b
+
ed8730b
 /* Stores the ptrace options supported by the running kernel.
ed8730b
    A value of -1 means we did not check for features yet.  A value
ed8730b
    of 0 means there are no supported features.  */
ed8730b
@@ -50,6 +54,8 @@ linux_ptrace_attach_fail_reason (pid_t pid)
ed8730b
 		      "terminated"),
ed8730b
 		    (int) pid);
ed8730b
 
ed8730b
+  result += linux_ptrace_create_warnings ();
ed8730b
+
ed8730b
   return result;
ed8730b
 }
ed8730b
 
ed8730b
@@ -586,6 +592,25 @@ linux_ptrace_init_warnings (void)
ed8730b
   linux_ptrace_test_ret_to_nx ();
ed8730b
 }
ed8730b
 
ed8730b
+/* Print all possible reasons we could fail to create a traced process.  */
ed8730b
+
ed8730b
+std::string
ed8730b
+linux_ptrace_create_warnings ()
ed8730b
+{
ed8730b
+  std::string result;
ed8730b
+
ed8730b
+#ifdef HAVE_LIBSELINUX
ed8730b
+  /* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
ed8730b
+     forbidden.  */
ed8730b
+  if (security_get_boolean_active ("deny_ptrace") == 1)
ed8730b
+    string_appendf (result,
ed8730b
+		    _("the SELinux boolean 'deny_ptrace' is enabled, "
ed8730b
+		      "you can disable this process attach protection by: "
ed8730b
+		      "(gdb) shell sudo setsebool deny_ptrace=0\n"));
ed8730b
+#endif /* HAVE_LIBSELINUX */
ed8730b
+  return result;
ed8730b
+}
ed8730b
+
ed8730b
 /* Extract extended ptrace event from wait status.  */
ed8730b
 
ed8730b
 int
ed8730b
diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h
ed8730b
--- a/gdb/nat/linux-ptrace.h
ed8730b
+++ b/gdb/nat/linux-ptrace.h
ed8730b
@@ -184,6 +184,7 @@ extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
ed8730b
 extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
ed8730b
 
ed8730b
 extern void linux_ptrace_init_warnings (void);
ed8730b
+extern std::string linux_ptrace_create_warnings ();
ed8730b
 extern void linux_check_ptrace_features (void);
ed8730b
 extern void linux_enable_event_reporting (pid_t pid, int attached);
ed8730b
 extern void linux_disable_event_reporting (pid_t pid);
ed8730b
diff --git a/gdbserver/config.in b/gdbserver/config.in
ed8730b
--- a/gdbserver/config.in
ed8730b
+++ b/gdbserver/config.in
ed8730b
@@ -143,6 +143,9 @@
ed8730b
 /* Define if you have the ipt library. */
ed8730b
 #undef HAVE_LIBIPT
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+/* Define to 1 if you have the `selinux' library (-lselinux). */
Jan Kratochvil 872aab0
+#undef HAVE_LIBSELINUX
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
 /* Define if the target supports branch tracing. */
Jan Kratochvil 872aab0
 #undef HAVE_LINUX_BTRACE
Jan Kratochvil 872aab0
 
ed8730b
@@ -249,6 +252,9 @@
ed8730b
 /* Define to 1 if you have the `sbrk' function. */
ed8730b
 #undef HAVE_SBRK
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+/* Define to 1 if you have the <selinux/selinux.h> header file. */
Jan Kratochvil 872aab0
+#undef HAVE_SELINUX_SELINUX_H
Jan Kratochvil 872aab0
+
Jan Kratochvil c296a6a
 /* Define to 1 if you have the `setns' function. */
Jan Kratochvil c296a6a
 #undef HAVE_SETNS
Jan Kratochvil 872aab0
 
ed8730b
diff --git a/gdbserver/configure b/gdbserver/configure
ed8730b
--- a/gdbserver/configure
ed8730b
+++ b/gdbserver/configure
ed8730b
@@ -10683,6 +10683,64 @@ if $want_ipa ; then
Jan Kratochvil 872aab0
    fi
Jan Kratochvil 872aab0
 fi
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
+for ac_header in selinux/selinux.h
Jan Kratochvil 872aab0
+do :
Jan Kratochvil 872aab0
+  ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
Jan Kratochvil 872aab0
+if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
Jan Kratochvil 872aab0
+  cat >>confdefs.h <<_ACEOF
Jan Kratochvil 872aab0
+#define HAVE_SELINUX_SELINUX_H 1
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+done
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
Jan Kratochvil 872aab0
+$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
Jan Kratochvil 872aab0
+if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
Jan Kratochvil 872aab0
+  $as_echo_n "(cached) " >&6
Jan Kratochvil 872aab0
+else
Jan Kratochvil 872aab0
+  ac_check_lib_save_LIBS=$LIBS
Jan Kratochvil 872aab0
+LIBS="-lselinux  $LIBS"
Jan Kratochvil 872aab0
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
Jan Kratochvil 872aab0
+/* end confdefs.h.  */
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+/* Override any GCC internal prototype to avoid an error.
Jan Kratochvil 872aab0
+   Use char because int might match the return type of a GCC
Jan Kratochvil 872aab0
+   builtin and then its argument prototype would still apply.  */
Jan Kratochvil 872aab0
+#ifdef __cplusplus
Jan Kratochvil 872aab0
+extern "C"
Jan Kratochvil 872aab0
+#endif
Jan Kratochvil 872aab0
+char security_get_boolean_active ();
Jan Kratochvil 872aab0
+int
Jan Kratochvil 872aab0
+main ()
Jan Kratochvil 872aab0
+{
Jan Kratochvil 872aab0
+return security_get_boolean_active ();
Jan Kratochvil 872aab0
+  ;
Jan Kratochvil 872aab0
+  return 0;
Jan Kratochvil 872aab0
+}
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+if ac_fn_c_try_link "$LINENO"; then :
Jan Kratochvil 872aab0
+  ac_cv_lib_selinux_security_get_boolean_active=yes
Jan Kratochvil 872aab0
+else
Jan Kratochvil 872aab0
+  ac_cv_lib_selinux_security_get_boolean_active=no
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+rm -f core conftest.err conftest.$ac_objext \
Jan Kratochvil 872aab0
+    conftest$ac_exeext conftest.$ac_ext
Jan Kratochvil 872aab0
+LIBS=$ac_check_lib_save_LIBS
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
Jan Kratochvil 872aab0
+$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
Jan Kratochvil 872aab0
+if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
Jan Kratochvil 872aab0
+  cat >>confdefs.h <<_ACEOF
Jan Kratochvil 872aab0
+#define HAVE_LIBSELINUX 1
Jan Kratochvil 872aab0
+_ACEOF
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+  LIBS="-lselinux $LIBS"
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+fi
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
+
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
 
Jan Kratochvil 872aab0
 
ed8730b
diff --git a/gdbserver/configure.ac b/gdbserver/configure.ac
ed8730b
--- a/gdbserver/configure.ac
ed8730b
+++ b/gdbserver/configure.ac
ed8730b
@@ -401,6 +401,10 @@ if $want_ipa ; then
f637971
    fi
f637971
 fi
f637971
 
f637971
+dnl Check security_get_boolean_active availability.
f637971
+AC_CHECK_HEADERS(selinux/selinux.h)
f637971
+AC_CHECK_LIB(selinux, security_get_boolean_active)
f637971
+
f637971
 AC_SUBST(GDBSERVER_DEPFILES)
f637971
 AC_SUBST(GDBSERVER_LIBS)
f637971
 AC_SUBST(srv_xmlbuiltin)
ed8730b
diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
ed8730b
--- a/gdbserver/linux-low.cc
ed8730b
+++ b/gdbserver/linux-low.cc
ed8730b
@@ -932,7 +932,16 @@ linux_ptrace_fun ()
f637971
 {
f637971
   if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
f637971
 	      (PTRACE_TYPE_ARG4) 0) < 0)
f637971
-    trace_start_error_with_name ("ptrace");
f637971
+    {
f637971
+      int save_errno = errno;
f637971
+
2bcd68d
+      std::string msg (linux_ptrace_create_warnings ());
f637971
+
2bcd68d
+      msg += _("Cannot trace created process");
f637971
+
f637971
+      errno = save_errno;
f637971
+      trace_start_error_with_name (msg.c_str ());
f637971
+    }
f637971
 
f637971
   if (setpgid (0, 0) < 0)
f637971
     trace_start_error_with_name ("setpgid");