Skip to content

Commit f6581f5

Browse files
thejhebiederm
authored andcommitted
ptrace: restore smp_rmb() in __ptrace_may_access()
Restore the read memory barrier in __ptrace_may_access() that was deleted a couple years ago. Also add comments on this barrier and the one it pairs with to explain why they're there (as far as I understand). Fixes: bfedb58 ("mm: Add a user_ns owner to mm_struct and fix ptrace permission checks") Cc: [email protected] Acked-by: Kees Cook <[email protected]> Acked-by: Oleg Nesterov <[email protected]> Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent f6e2aa9 commit f6581f5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

kernel/cred.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,15 @@ int commit_creds(struct cred *new)
450450
if (task->mm)
451451
set_dumpable(task->mm, suid_dumpable);
452452
task->pdeath_signal = 0;
453+
/*
454+
* If a task drops privileges and becomes nondumpable,
455+
* the dumpability change must become visible before
456+
* the credential change; otherwise, a __ptrace_may_access()
457+
* racing with this change may be able to attach to a task it
458+
* shouldn't be able to attach to (as if the task had dropped
459+
* privileges without becoming nondumpable).
460+
* Pairs with a read barrier in __ptrace_may_access().
461+
*/
453462
smp_wmb();
454463
}
455464

kernel/ptrace.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
323323
return -EPERM;
324324
ok:
325325
rcu_read_unlock();
326+
/*
327+
* If a task drops privileges and becomes nondumpable (through a syscall
328+
* like setresuid()) while we are trying to access it, we must ensure
329+
* that the dumpability is read after the credentials; otherwise,
330+
* we may be able to attach to a task that we shouldn't be able to
331+
* attach to (as if the task had dropped privileges without becoming
332+
* nondumpable).
333+
* Pairs with a write barrier in commit_creds().
334+
*/
335+
smp_rmb();
326336
mm = task->mm;
327337
if (mm &&
328338
((get_dumpable(mm) != SUID_DUMP_USER) &&

0 commit comments

Comments
 (0)