Re: [PATCH] exec: Fix a deadlock in ptrace

From: Christian Brauner
Date: Mon Mar 02 2020 - 02:49:18 EST


On Mon, Mar 02, 2020 at 08:47:53AM +0100, Christian Brauner wrote:
> On Sun, Mar 01, 2020 at 09:00:22PM +0100, Jann Horn wrote:
> > On Sun, Mar 1, 2020 at 7:52 PM Christian Brauner
> > <christian.brauner@xxxxxxxxxx> wrote:
> > > On Sun, Mar 01, 2020 at 07:21:03PM +0100, Jann Horn wrote:
> > > > On Sun, Mar 1, 2020 at 12:27 PM Bernd Edlinger
> > > > <bernd.edlinger@xxxxxxxxxx> wrote:
> > > > > The proposed solution is to have a second mutex that is
> > > > > used in mm_access, so it is allowed to continue while the
> > > > > dying threads are not yet terminated.
> > > >
> > > > Just for context: When I proposed something similar back in 2016,
> > > > https://lore.kernel.org/linux-fsdevel/20161102181806.GB1112@xxxxxxxxxx/
> > > > was the resulting discussion thread. At least back then, I looked
> > > > through the various existing users of cred_guard_mutex, and the only
> > > > places that couldn't be converted to the new second mutex were
> > > > PTRACE_ATTACH and SECCOMP_FILTER_FLAG_TSYNC.
> > > >
> > > >
> > > > The ideal solution would IMO be something like this: Decide what the
> > > > new task's credentials should be *before* reaching de_thread(),
> > > > install them into a second cred* on the task (together with the new
> > > > dumpability), drop the cred_guard_mutex, and let ptrace_may_access()
> > > > check against both. After that, some further restructuring might even
> > >
> > > Hm, so essentially a private ptrace_access_cred member in task_struct?
> >
> > And a second dumpability field, because that changes together with the
> > creds during execve. (Btw, currently the dumpability is in the
> > mm_struct, but that's kinda wrong. The mm_struct is removed from a
> > task on exit while access checks can still be performed against it, and
> > currently ptrace_may_access() just lets the access go through in that
> > case, which weakens the protection offered by PR_SET_DUMPABLE when
> > used for security purposes. I think it ought to be moved over into the
> > task_struct.)
> >
> > > That would presumably also involve altering various LSM hooks to look at
> > > ptrace_access_cred.
> >
> > When I tried to implement this in the past, I changed the LSM hook to
> > take the target task's cred* as an argument, and then called the LSM
> > hook twice from ptrace_may_access(). IIRC having the target task's
> > creds as an argument works for almost all the LSMs, with the exception
> > of Yama, which doesn't really care about the target task's creds, so
> > you have to pass in both the task_struct* and the cred*.
>
> It seems we should try PoCing this.

Independent of the fix for Bernd's issue that is.