1 http://sourceware.org/ml/gdb-patches/2012-03/msg00171.html
2 Subject: [patch 3/3] attach-fail-reasons: SELinux deny_ptrace
6 and here is the last bit for new SELinux 'deny_ptrace':
7 https://bugzilla.redhat.com/show_bug.cgi?id=786878
9 As even PTRACE_TRACEME fails in such case it needs to install hook for even
18 2012-03-06 Jan Kratochvil <jan.kratochvil@redhat.com>
20 * common/linux-ptrace.c [HAVE_SELINUX_SELINUX_H]: include
22 (linux_ptrace_attach_warnings): Call linux_ptrace_create_warnings.
23 (linux_ptrace_create_warnings): New.
24 * common/linux-ptrace.h (linux_ptrace_create_warnings): New declaration.
25 * config.in: Regenerate.
26 * configure: Regenerate.
27 * configure.ac: Check selinux/selinux.h and the selinux library.
28 * inf-ptrace.c (inf_ptrace_me): Check the ptrace result.
29 * linux-nat.c (linux_nat_create_inferior): New variable ex. Wrap
30 to_create_inferior into TRY_CATCH, call linux_ptrace_create_warnings.
33 * config.in: Regenerate.
34 * configure: Regenerate.
35 * configure.ac: Check selinux/selinux.h and the selinux library.
36 * linux-low.c (linux_traceme): New function.
37 (linux_create_inferior, linux_tracefork_child): Call it instead of
40 --- a/gdb/common/linux-ptrace.c
41 +++ b/gdb/common/linux-ptrace.c
43 #include "linux-ptrace.h"
44 #include "linux-procfs.h"
46 +#ifdef HAVE_SELINUX_SELINUX_H
47 +# include <selinux/selinux.h>
48 +#endif /* HAVE_SELINUX_SELINUX_H */
50 /* Print all possible reasons we could fail to attach PID. */
53 @@ -41,4 +45,21 @@ linux_ptrace_attach_warnings (pid_t pid)
54 if (linux_proc_pid_is_zombie (pid))
55 warning (_("process %d is a zombie - the process has already terminated"),
58 + linux_ptrace_create_warnings ();
61 +/* Print all possible reasons we could fail to create a traced process. */
64 +linux_ptrace_create_warnings (void)
66 +#ifdef HAVE_LIBSELINUX
67 + /* -1 is returned for errors, 0 if it has no effect, 1 if PTRACE_ATTACH is
69 + if (security_get_boolean_active ("deny_ptrace") == 1)
70 + warning (_("the SELinux boolean 'deny_ptrace' is enabled, "
71 + "you can disable this process attach protection by: "
72 + "(gdb) shell sudo setsebool deny_ptrace=0"));
73 +#endif /* HAVE_LIBSELINUX */
75 --- a/gdb/common/linux-ptrace.h
76 +++ b/gdb/common/linux-ptrace.h
80 extern void linux_ptrace_attach_warnings (pid_t pid);
81 +extern void linux_ptrace_create_warnings (void);
83 #endif /* COMMON_LINUX_PTRACE_H */
84 --- a/gdb/configure.ac
85 +++ b/gdb/configure.ac
86 @@ -1748,6 +1748,10 @@ then
87 [Define if you support the personality syscall.])
90 +dnl Check security_get_boolean_active availability.
91 +AC_CHECK_HEADERS(selinux/selinux.h)
92 +AC_CHECK_LIB(selinux, security_get_boolean_active)
94 dnl Handle optional features that can be enabled.
96 # Support for --with-sysroot is a copy of GDB_AC_WITH_DIR,
97 --- a/gdb/gdbserver/configure.ac
98 +++ b/gdb/gdbserver/configure.ac
99 @@ -411,6 +411,10 @@ if $want_ipa ; then
103 +dnl Check security_get_boolean_active availability.
104 +AC_CHECK_HEADERS(selinux/selinux.h)
105 +AC_CHECK_LIB(selinux, security_get_boolean_active)
107 AC_SUBST(GDBSERVER_DEPFILES)
108 AC_SUBST(GDBSERVER_LIBS)
109 AC_SUBST(USE_THREAD_DB)
110 --- a/gdb/gdbserver/linux-low.c
111 +++ b/gdb/gdbserver/linux-low.c
112 @@ -550,6 +550,25 @@ add_lwp (ptid_t ptid)
116 +/* Execute PTRACE_TRACEME with error checking. */
119 +linux_traceme (const char *program)
124 + if (ptrace (PTRACE_TRACEME, 0, NULL, NULL) == 0)
127 + save_errno = errno;
128 + linux_ptrace_create_warnings ();
129 + fprintf (stderr, _("Cannot trace created process %s: %s.\n"), program,
130 + strerror (save_errno));
135 /* Start an inferior process and returns its pid.
136 ALLARGS is a vector of program-name and args. */
138 @@ -590,7 +609,7 @@ linux_create_inferior (char *program, char **allargs)
142 - ptrace (PTRACE_TRACEME, 0, 0, 0);
143 + linux_traceme (program);
145 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
146 signal (__SIGRTMIN + 1, SIG_DFL);
147 @@ -4386,7 +4405,7 @@ linux_tracefork_grandchild (void *arg)
149 linux_tracefork_child (void *arg)
151 - ptrace (PTRACE_TRACEME, 0, 0, 0);
152 + linux_traceme ("PTRACE_O_TRACEFORK test");
153 kill (getpid (), SIGSTOP);
155 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
156 --- a/gdb/inf-ptrace.c
157 +++ b/gdb/inf-ptrace.c
158 @@ -105,7 +105,15 @@ static void
161 /* "Trace me, Dr. Memory!" */
163 ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
166 + fprintf_unfiltered (gdb_stderr, _("Cannot create process: %s\n"),
167 + safe_strerror (errno));
168 + gdb_flush (gdb_stderr);
173 /* Start a new inferior Unix child process. EXEC_FILE is the file to
174 --- a/gdb/linux-nat.c
175 +++ b/gdb/linux-nat.c
176 @@ -1570,6 +1570,7 @@ linux_nat_create_inferior (struct target_ops *ops,
177 #ifdef HAVE_PERSONALITY
178 int personality_orig = 0, personality_set = 0;
179 #endif /* HAVE_PERSONALITY */
180 + volatile struct gdb_exception ex;
182 /* The fork_child mechanism is synchronous and calls target_wait, so
183 we have to mask the async mode. */
184 @@ -1594,7 +1595,10 @@ linux_nat_create_inferior (struct target_ops *ops,
185 /* Make sure we report all signals during startup. */
186 linux_nat_pass_signals (0, NULL);
188 - linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
189 + TRY_CATCH (ex, RETURN_MASK_ERROR)
191 + linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty);
194 #ifdef HAVE_PERSONALITY
196 @@ -1606,6 +1610,12 @@ linux_nat_create_inferior (struct target_ops *ops,
197 safe_strerror (errno));
199 #endif /* HAVE_PERSONALITY */
203 + linux_ptrace_create_warnings ();
204 + throw_exception (ex);