Skip to content

Commit 05a0c58

Browse files
ivanivanov884jeffmahoney
authored andcommitted
gdb-attach-fail-reasons-5of5.patch
;; Print reasons for failed attach/spawn incl. SELinux deny_ptrace (BZ 786878). ;;=push+jan http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html Hi, and here is the last bit for new SELinux 'deny_ptrace': https://bugzilla.redhat.com/show_bug.cgi?id=786878 As even PTRACE_TRACEME fails in such case it needs to install hook for even that event. Thanks, Jan gdb/ 2012-03-06 Jan Kratochvil <jan.kratochvil@redhat.com> * common/linux-ptrace.c [HAVE_SELINUX_SELINUX_H]: include selinux/selinux.h. (linux_ptrace_attach_warnings): Call linux_ptrace_create_warnings. (linux_ptrace_create_warnings): New. * common/linux-ptrace.h (linux_ptrace_create_warnings): New declaration. * config.in: Regenerate. * configure: Regenerate. * configure.ac: Check selinux/selinux.h and the selinux library. * inf-ptrace.c (inf_ptrace_me): Check the ptrace result. * linux-nat.c (linux_nat_create_inferior): New variable ex. Wrap to_create_inferior into TRY_CATCH, call linux_ptrace_create_warnings. gdb/gdbserver/ * config.in: Regenerate. * configure: Regenerate. * configure.ac: Check selinux/selinux.h and the selinux library. * linux-low.c (linux_traceme): New function. (linux_create_inferior, linux_tracefork_child): Call it instead of direct ptrace.
1 parent 39ce60c commit 05a0c58

File tree

10 files changed

+182
-2
lines changed

10 files changed

+182
-2
lines changed

gdb/config.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@
261261
/* Define if librpm library is being used. */
262262
#undef HAVE_LIBRPM
263263

264+
/* Define to 1 if you have the `selinux' library (-lselinux). */
265+
#undef HAVE_LIBSELINUX
266+
264267
/* Define to 1 if you have the <libunwind-ia64.h> header file. */
265268
#undef HAVE_LIBUNWIND_IA64_H
266269

@@ -384,6 +387,9 @@
384387
/* Define to 1 if you have the `scm_new_smob' function. */
385388
#undef HAVE_SCM_NEW_SMOB
386389

390+
/* Define to 1 if you have the <selinux/selinux.h> header file. */
391+
#undef HAVE_SELINUX_SELINUX_H
392+
387393
/* Define to 1 if you have the `setlocale' function. */
388394
#undef HAVE_SETLOCALE
389395

gdb/configure

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15790,6 +15790,64 @@ cat >>confdefs.h <<_ACEOF
1579015790
_ACEOF
1579115791

1579215792

15793+
for ac_header in selinux/selinux.h
15794+
do :
15795+
ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
15796+
if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
15797+
cat >>confdefs.h <<_ACEOF
15798+
#define HAVE_SELINUX_SELINUX_H 1
15799+
_ACEOF
15800+
15801+
fi
15802+
15803+
done
15804+
15805+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
15806+
$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
15807+
if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
15808+
$as_echo_n "(cached) " >&6
15809+
else
15810+
ac_check_lib_save_LIBS=$LIBS
15811+
LIBS="-lselinux $LIBS"
15812+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
15813+
/* end confdefs.h. */
15814+
15815+
/* Override any GCC internal prototype to avoid an error.
15816+
Use char because int might match the return type of a GCC
15817+
builtin and then its argument prototype would still apply. */
15818+
#ifdef __cplusplus
15819+
extern "C"
15820+
#endif
15821+
char security_get_boolean_active ();
15822+
int
15823+
main ()
15824+
{
15825+
return security_get_boolean_active ();
15826+
;
15827+
return 0;
15828+
}
15829+
_ACEOF
15830+
if ac_fn_c_try_link "$LINENO"; then :
15831+
ac_cv_lib_selinux_security_get_boolean_active=yes
15832+
else
15833+
ac_cv_lib_selinux_security_get_boolean_active=no
15834+
fi
15835+
rm -f core conftest.err conftest.$ac_objext \
15836+
conftest$ac_exeext conftest.$ac_ext
15837+
LIBS=$ac_check_lib_save_LIBS
15838+
fi
15839+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
15840+
$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
15841+
if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
15842+
cat >>confdefs.h <<_ACEOF
15843+
#define HAVE_LIBSELINUX 1
15844+
_ACEOF
15845+
15846+
LIBS="-lselinux $LIBS"
15847+
15848+
fi
15849+
15850+
1579315851

1579415852
# Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
1579515853
# except that the argument to --with-sysroot is optional.

gdb/configure.ac

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,10 @@ case $host_os in
20202020
esac
20212021
AC_DEFINE_UNQUOTED(GDBINIT,"$gdbinit",[The .gdbinit filename.])
20222022

2023+
dnl Check security_get_boolean_active availability.
2024+
AC_CHECK_HEADERS(selinux/selinux.h)
2025+
AC_CHECK_LIB(selinux, security_get_boolean_active)
2026+
20232027
dnl Handle optional features that can be enabled.
20242028

20252029
# Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,

gdb/gdbserver/config.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
/* Define to 1 if you have the `mcheck' library (-lmcheck). */
127127
#undef HAVE_LIBMCHECK
128128

129+
/* Define to 1 if you have the `selinux' library (-lselinux). */
130+
#undef HAVE_LIBSELINUX
131+
129132
/* Define if the target supports branch tracing. */
130133
#undef HAVE_LINUX_BTRACE
131134

@@ -202,6 +205,9 @@
202205
/* Define to 1 if you have the `pwrite' function. */
203206
#undef HAVE_PWRITE
204207

208+
/* Define to 1 if you have the <selinux/selinux.h> header file. */
209+
#undef HAVE_SELINUX_SELINUX_H
210+
205211
/* Define to 1 if you have the `setns' function. */
206212
#undef HAVE_SETNS
207213

gdb/gdbserver/configure

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8589,6 +8589,64 @@ if $want_ipa ; then
85898589
fi
85908590
fi
85918591
8592+
for ac_header in selinux/selinux.h
8593+
do :
8594+
ac_fn_c_check_header_mongrel "$LINENO" "selinux/selinux.h" "ac_cv_header_selinux_selinux_h" "$ac_includes_default"
8595+
if test "x$ac_cv_header_selinux_selinux_h" = x""yes; then :
8596+
cat >>confdefs.h <<_ACEOF
8597+
#define HAVE_SELINUX_SELINUX_H 1
8598+
_ACEOF
8599+
8600+
fi
8601+
8602+
done
8603+
8604+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_get_boolean_active in -lselinux" >&5
8605+
$as_echo_n "checking for security_get_boolean_active in -lselinux... " >&6; }
8606+
if test "${ac_cv_lib_selinux_security_get_boolean_active+set}" = set; then :
8607+
$as_echo_n "(cached) " >&6
8608+
else
8609+
ac_check_lib_save_LIBS=$LIBS
8610+
LIBS="-lselinux $LIBS"
8611+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
8612+
/* end confdefs.h. */
8613+
8614+
/* Override any GCC internal prototype to avoid an error.
8615+
Use char because int might match the return type of a GCC
8616+
builtin and then its argument prototype would still apply. */
8617+
#ifdef __cplusplus
8618+
extern "C"
8619+
#endif
8620+
char security_get_boolean_active ();
8621+
int
8622+
main ()
8623+
{
8624+
return security_get_boolean_active ();
8625+
;
8626+
return 0;
8627+
}
8628+
_ACEOF
8629+
if ac_fn_c_try_link "$LINENO"; then :
8630+
ac_cv_lib_selinux_security_get_boolean_active=yes
8631+
else
8632+
ac_cv_lib_selinux_security_get_boolean_active=no
8633+
fi
8634+
rm -f core conftest.err conftest.$ac_objext \
8635+
conftest$ac_exeext conftest.$ac_ext
8636+
LIBS=$ac_check_lib_save_LIBS
8637+
fi
8638+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_selinux_security_get_boolean_active" >&5
8639+
$as_echo "$ac_cv_lib_selinux_security_get_boolean_active" >&6; }
8640+
if test "x$ac_cv_lib_selinux_security_get_boolean_active" = x""yes; then :
8641+
cat >>confdefs.h <<_ACEOF
8642+
#define HAVE_LIBSELINUX 1
8643+
_ACEOF
8644+
8645+
LIBS="-lselinux $LIBS"
8646+
8647+
fi
8648+
8649+
85928650
85938651
85948652

gdb/gdbserver/configure.ac

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ if $want_ipa ; then
478478
fi
479479
fi
480480

481+
dnl Check security_get_boolean_active availability.
482+
AC_CHECK_HEADERS(selinux/selinux.h)
483+
AC_CHECK_LIB(selinux, security_get_boolean_active)
484+
481485
AC_SUBST(GDBSERVER_DEPFILES)
482486
AC_SUBST(GDBSERVER_LIBS)
483487
AC_SUBST(srv_xmlbuiltin)

gdb/gdbserver/linux-low.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,16 @@ linux_ptrace_fun ()
971971
{
972972
if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
973973
(PTRACE_TYPE_ARG4) 0) < 0)
974-
trace_start_error_with_name ("ptrace");
974+
{
975+
int save_errno = errno;
976+
977+
std::string msg (linux_ptrace_create_warnings ());
978+
979+
msg += _("Cannot trace created process");
980+
981+
errno = save_errno;
982+
trace_start_error_with_name (msg.c_str ());
983+
}
975984

976985
if (setpgid (0, 0) < 0)
977986
trace_start_error_with_name ("setpgid");

gdb/linux-nat.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,16 @@ linux_nat_target::create_inferior (const char *exec_file,
10981098
/* Make sure we report all signals during startup. */
10991099
pass_signals ({});
11001100

1101-
inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
1101+
try
1102+
{
1103+
inf_ptrace_target::create_inferior (exec_file, allargs, env, from_tty);
1104+
}
1105+
catch (const gdb_exception_error &ex)
1106+
{
1107+
std::string result = linux_ptrace_create_warnings ();
1108+
1109+
throw_error (ex.error, "%s%s", result.c_str (), ex.what ());
1110+
}
11021111
}
11031112

11041113
/* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not

gdb/nat/linux-ptrace.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <sys/procfs.h>
2626
#endif
2727

28+
#ifdef HAVE_SELINUX_SELINUX_H
29+
# include <selinux/selinux.h>
30+
#endif /* HAVE_SELINUX_SELINUX_H */
31+
2832
/* Stores the ptrace options supported by the running kernel.
2933
A value of -1 means we did not check for features yet. A value
3034
of 0 means there are no supported features. */
@@ -50,6 +54,8 @@ linux_ptrace_attach_fail_reason (pid_t pid)
5054
"terminated"),
5155
(int) pid);
5256

57+
result += linux_ptrace_create_warnings ();
58+
5359
return result;
5460
}
5561

@@ -586,6 +592,25 @@ linux_ptrace_init_warnings (void)
586592
linux_ptrace_test_ret_to_nx ();
587593
}
588594

595+
/* Print all possible reasons we could fail to create a traced process. */
596+
597+
std::string
598+
linux_ptrace_create_warnings ()
599+
{
600+
std::string result;
601+
602+
#ifdef HAVE_LIBSELINUX
603+
/* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
604+
forbidden. */
605+
if (security_get_boolean_active ("deny_ptrace") == 1)
606+
string_appendf (result,
607+
_("the SELinux boolean 'deny_ptrace' is enabled, "
608+
"you can disable this process attach protection by: "
609+
"(gdb) shell sudo setsebool deny_ptrace=0\n"));
610+
#endif /* HAVE_LIBSELINUX */
611+
return result;
612+
}
613+
589614
/* Extract extended ptrace event from wait status. */
590615

591616
int

gdb/nat/linux-ptrace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
184184
extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
185185

186186
extern void linux_ptrace_init_warnings (void);
187+
extern std::string linux_ptrace_create_warnings ();
187188
extern void linux_check_ptrace_features (void);
188189
extern void linux_enable_event_reporting (pid_t pid, int attached);
189190
extern void linux_disable_event_reporting (pid_t pid);

0 commit comments

Comments
 (0)