Skip to content

Commit ee1fee9

Browse files
thejhebiederm
authored andcommitted
ptrace: Check PTRACE_O_SUSPEND_SECCOMP permission on PTRACE_SEIZE
Setting PTRACE_O_SUSPEND_SECCOMP is supposed to be a highly privileged operation because it allows the tracee to completely bypass all seccomp filters on kernels with CONFIG_CHECKPOINT_RESTORE=y. It is only supposed to be settable by a process with global CAP_SYS_ADMIN, and only if that process is not subject to any seccomp filters at all. However, while these permission checks were done on the PTRACE_SETOPTIONS path, they were missing on the PTRACE_SEIZE path, which also sets user-specified ptrace flags. Move the permissions checks out into a helper function and let both ptrace_attach() and ptrace_setoptions() call it. Cc: stable@kernel.org Fixes: 13c4a90 ("seccomp: add ptrace options for suspend/resume") Signed-off-by: Jann Horn <jannh@google.com> Link: https://lkml.kernel.org/r/20220319010838.1386861-1-jannh@google.com Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent 6487d1d commit ee1fee9

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

kernel/ptrace.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,26 @@ bool ptrace_may_access(struct task_struct *task, unsigned int mode)
371371
return !err;
372372
}
373373

374+
static int check_ptrace_options(unsigned long data)
375+
{
376+
if (data & ~(unsigned long)PTRACE_O_MASK)
377+
return -EINVAL;
378+
379+
if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
380+
if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
381+
!IS_ENABLED(CONFIG_SECCOMP))
382+
return -EINVAL;
383+
384+
if (!capable(CAP_SYS_ADMIN))
385+
return -EPERM;
386+
387+
if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
388+
current->ptrace & PT_SUSPEND_SECCOMP)
389+
return -EPERM;
390+
}
391+
return 0;
392+
}
393+
374394
static int ptrace_attach(struct task_struct *task, long request,
375395
unsigned long addr,
376396
unsigned long flags)
@@ -382,8 +402,16 @@ static int ptrace_attach(struct task_struct *task, long request,
382402
if (seize) {
383403
if (addr != 0)
384404
goto out;
405+
/*
406+
* This duplicates the check in check_ptrace_options() because
407+
* ptrace_attach() and ptrace_setoptions() have historically
408+
* used different error codes for unknown ptrace options.
409+
*/
385410
if (flags & ~(unsigned long)PTRACE_O_MASK)
386411
goto out;
412+
retval = check_ptrace_options(flags);
413+
if (retval)
414+
return retval;
387415
flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
388416
} else {
389417
flags = PT_PTRACED;
@@ -654,22 +682,11 @@ int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long ds
654682
static int ptrace_setoptions(struct task_struct *child, unsigned long data)
655683
{
656684
unsigned flags;
685+
int ret;
657686

658-
if (data & ~(unsigned long)PTRACE_O_MASK)
659-
return -EINVAL;
660-
661-
if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
662-
if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
663-
!IS_ENABLED(CONFIG_SECCOMP))
664-
return -EINVAL;
665-
666-
if (!capable(CAP_SYS_ADMIN))
667-
return -EPERM;
668-
669-
if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
670-
current->ptrace & PT_SUSPEND_SECCOMP)
671-
return -EPERM;
672-
}
687+
ret = check_ptrace_options(data);
688+
if (ret)
689+
return ret;
673690

674691
/* Avoid intermediate state when all opts are cleared */
675692
flags = child->ptrace;

0 commit comments

Comments
 (0)