aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/ptrace.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2012-03-23 18:02:42 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-23 19:58:40 -0400
commitaa9147c98f27550bd39416eca5a5844e54bced26 (patch)
treed73386b9f58034ea3332c5359c04cfc13c085a7f /kernel/ptrace.c
parent86b6c1f301faf085de5a3f9ce16b8de6e69c729b (diff)
ptrace: make PTRACE_SEIZE set ptrace options specified in 'data' parameter
This can be used to close a few corner cases in strace where we get unwanted racy behavior after attach, but before we have a chance to set options (the notorious post-execve SIGTRAP comes to mind), and removes the need to track "did we set opts for this task" state in strace internals. While we are at it: Make it possible to extend SEIZE in the future with more functionality by passing non-zero 'addr' parameter. To that end, error out if 'addr' is non-zero. PTRACE_ATTACH did not (and still does not) have such check, and users (strace) do pass garbage there... let's avoid repeating this mistake with SEIZE. Set all task->ptrace bits in one operation - before this change, we were adding PT_SEIZED and PT_PTRACE_CAP with task->ptrace |= BIT ops. This was probably ok (not a bug), but let's be on a safer side. Changes since v2: use (unsigned long) casts instead of (long) ones, move PTRACE_SEIZE_DEVEL-related code to separate lines of code. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Acked-by: Tejun Heo <tj@kernel.org> Cc: Pedro Alves <palves@redhat.com> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Cc: Jan Kratochvil <jan.kratochvil@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel/ptrace.c')
-rw-r--r--kernel/ptrace.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 9acd07a6f5bb..4661c5bc07e5 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -231,6 +231,7 @@ bool ptrace_may_access(struct task_struct *task, unsigned int mode)
231} 231}
232 232
233static int ptrace_attach(struct task_struct *task, long request, 233static int ptrace_attach(struct task_struct *task, long request,
234 unsigned long addr,
234 unsigned long flags) 235 unsigned long flags)
235{ 236{
236 bool seize = (request == PTRACE_SEIZE); 237 bool seize = (request == PTRACE_SEIZE);
@@ -238,19 +239,29 @@ static int ptrace_attach(struct task_struct *task, long request,
238 239
239 /* 240 /*
240 * SEIZE will enable new ptrace behaviors which will be implemented 241 * SEIZE will enable new ptrace behaviors which will be implemented
241 * gradually. SEIZE_DEVEL is used to prevent applications 242 * gradually. SEIZE_DEVEL bit is used to prevent applications
242 * expecting full SEIZE behaviors trapping on kernel commits which 243 * expecting full SEIZE behaviors trapping on kernel commits which
243 * are still in the process of implementing them. 244 * are still in the process of implementing them.
244 * 245 *
245 * Only test programs for new ptrace behaviors being implemented 246 * Only test programs for new ptrace behaviors being implemented
246 * should set SEIZE_DEVEL. If unset, SEIZE will fail with -EIO. 247 * should set SEIZE_DEVEL. If unset, SEIZE will fail with -EIO.
247 * 248 *
248 * Once SEIZE behaviors are completely implemented, this flag and 249 * Once SEIZE behaviors are completely implemented, this flag
249 * the following test will be removed. 250 * will be removed.
250 */ 251 */
251 retval = -EIO; 252 retval = -EIO;
252 if (seize && !(flags & PTRACE_SEIZE_DEVEL)) 253 if (seize) {
253 goto out; 254 if (addr != 0)
255 goto out;
256 if (!(flags & PTRACE_SEIZE_DEVEL))
257 goto out;
258 flags &= ~(unsigned long)PTRACE_SEIZE_DEVEL;
259 if (flags & ~(unsigned long)PTRACE_O_MASK)
260 goto out;
261 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
262 } else {
263 flags = PT_PTRACED;
264 }
254 265
255 audit_ptrace(task); 266 audit_ptrace(task);
256 267
@@ -282,11 +293,11 @@ static int ptrace_attach(struct task_struct *task, long request,
282 if (task->ptrace) 293 if (task->ptrace)
283 goto unlock_tasklist; 294 goto unlock_tasklist;
284 295
285 task->ptrace = PT_PTRACED;
286 if (seize) 296 if (seize)
287 task->ptrace |= PT_SEIZED; 297 flags |= PT_SEIZED;
288 if (ns_capable(task_user_ns(task), CAP_SYS_PTRACE)) 298 if (ns_capable(task_user_ns(task), CAP_SYS_PTRACE))
289 task->ptrace |= PT_PTRACE_CAP; 299 flags |= PT_PTRACE_CAP;
300 task->ptrace = flags;
290 301
291 __ptrace_link(task, current); 302 __ptrace_link(task, current);
292 303
@@ -879,7 +890,7 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
879 } 890 }
880 891
881 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 892 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
882 ret = ptrace_attach(child, request, data); 893 ret = ptrace_attach(child, request, addr, data);
883 /* 894 /*
884 * Some architectures need to do book-keeping after 895 * Some architectures need to do book-keeping after
885 * a ptrace attach. 896 * a ptrace attach.
@@ -1022,7 +1033,7 @@ asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
1022 } 1033 }
1023 1034
1024 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) { 1035 if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1025 ret = ptrace_attach(child, request, data); 1036 ret = ptrace_attach(child, request, addr, data);
1026 /* 1037 /*
1027 * Some architectures need to do book-keeping after 1038 * Some architectures need to do book-keeping after
1028 * a ptrace attach. 1039 * a ptrace attach.