Skip to content

Commit aa9147c

Browse files
Denys Vlasenkotorvalds
authored andcommitted
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>
1 parent 86b6c1f commit aa9147c

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

kernel/ptrace.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,37 @@ bool ptrace_may_access(struct task_struct *task, unsigned int mode)
231231
}
232232

233233
static int ptrace_attach(struct task_struct *task, long request,
234+
unsigned long addr,
234235
unsigned long flags)
235236
{
236237
bool seize = (request == PTRACE_SEIZE);
237238
int retval;
238239

239240
/*
240241
* 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
242243
* expecting full SEIZE behaviors trapping on kernel commits which
243244
* are still in the process of implementing them.
244245
*
245246
* Only test programs for new ptrace behaviors being implemented
246247
* should set SEIZE_DEVEL. If unset, SEIZE will fail with -EIO.
247248
*
248-
* Once SEIZE behaviors are completely implemented, this flag and
249-
* the following test will be removed.
249+
* Once SEIZE behaviors are completely implemented, this flag
250+
* will be removed.
250251
*/
251252
retval = -EIO;
252-
if (seize && !(flags & PTRACE_SEIZE_DEVEL))
253-
goto out;
253+
if (seize) {
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+
}
254265

255266
audit_ptrace(task);
256267

@@ -282,11 +293,11 @@ static int ptrace_attach(struct task_struct *task, long request,
282293
if (task->ptrace)
283294
goto unlock_tasklist;
284295

285-
task->ptrace = PT_PTRACED;
286296
if (seize)
287-
task->ptrace |= PT_SEIZED;
297+
flags |= PT_SEIZED;
288298
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;
290301

291302
__ptrace_link(task, current);
292303

@@ -879,7 +890,7 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
879890
}
880891

881892
if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
882-
ret = ptrace_attach(child, request, data);
893+
ret = ptrace_attach(child, request, addr, data);
883894
/*
884895
* Some architectures need to do book-keeping after
885896
* a ptrace attach.
@@ -1022,7 +1033,7 @@ asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
10221033
}
10231034

10241035
if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
1025-
ret = ptrace_attach(child, request, data);
1036+
ret = ptrace_attach(child, request, addr, data);
10261037
/*
10271038
* Some architectures need to do book-keeping after
10281039
* a ptrace attach.

0 commit comments

Comments
 (0)