Message ID | 87wo7svy96.fsf_-_@x220.int.ebiederm.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | pidfd: Stop taking cred_guard_mutex | expand |
On Tue, Mar 10, 2020 at 01:52:05PM -0500, Eric W. Biederman wrote: > > During exec some file descriptors are closed and the files struct is > unshared. But all of that can happen at other times and it has the > same protections during exec as at ordinary times. So stop taking the > cred_guard_mutex as it is useless. > > Furthermore he cred_guard_mutex is a bad idea because it is deadlock > prone, as it is held in serveral while waiting possibly indefinitely > for userspace to do something. > > Cc: Sargun Dhillon <sargun@sargun.me> > Cc: Christian Brauner <christian.brauner@ubuntu.com> > Cc: Arnd Bergmann <arnd@arndb.de> > Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall") > Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> > --- > kernel/pid.c | 6 ------ > 1 file changed, 6 deletions(-) > > Christian if you don't have any objections I will take this one through > my tree. Sure. Acked-by: Christian Brauner <christian.brauner@ubuntu.com> > > I tried to figure out why this code path takes the cred_guard_mutex and > the archive on lore.kernel.org was not helpful in finding that part of > the conversation. Let me think a little harder and hopefully get back to you with a sensible explanation.
On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > During exec some file descriptors are closed and the files struct is > unshared. But all of that can happen at other times and it has the > same protections during exec as at ordinary times. So stop taking the > cred_guard_mutex as it is useless. > > Furthermore he cred_guard_mutex is a bad idea because it is deadlock > prone, as it is held in serveral while waiting possibly indefinitely > for userspace to do something. Please don't. Just use the new exec_update_mutex like everywhere else. > Cc: Sargun Dhillon <sargun@sargun.me> > Cc: Christian Brauner <christian.brauner@ubuntu.com> > Cc: Arnd Bergmann <arnd@arndb.de> > Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall") > Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> > --- > kernel/pid.c | 6 ------ > 1 file changed, 6 deletions(-) > > Christian if you don't have any objections I will take this one through > my tree. > > I tried to figure out why this code path takes the cred_guard_mutex and > the archive on lore.kernel.org was not helpful in finding that part of > the conversation. That was my suggestion. > diff --git a/kernel/pid.c b/kernel/pid.c > index 60820e72634c..53646d5616d2 100644 > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -577,17 +577,11 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) > struct file *file; > int ret; > > - ret = mutex_lock_killable(&task->signal->cred_guard_mutex); > - if (ret) > - return ERR_PTR(ret); > - > if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) > file = fget_task(task, fd); > else > file = ERR_PTR(-EPERM); > > - mutex_unlock(&task->signal->cred_guard_mutex); > - > return file ?: ERR_PTR(-EBADF); > } If you make this change, then if this races with execution of a setuid program that afterwards e.g. opens a unix domain socket, an attacker will be able to steal that socket and inject messages into communication with things like DBus. procfs currently has the same race, and that still needs to be fixed, but at least procfs doesn't let you open things like sockets because they don't have a working ->open handler, and it enforces the normal permission check for opening files.
Jann Horn <jannh@google.com> writes: > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >> During exec some file descriptors are closed and the files struct is >> unshared. But all of that can happen at other times and it has the >> same protections during exec as at ordinary times. So stop taking the >> cred_guard_mutex as it is useless. >> >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock >> prone, as it is held in serveral while waiting possibly indefinitely >> for userspace to do something. > > Please don't. Just use the new exec_update_mutex like everywhere else. > >> Cc: Sargun Dhillon <sargun@sargun.me> >> Cc: Christian Brauner <christian.brauner@ubuntu.com> >> Cc: Arnd Bergmann <arnd@arndb.de> >> Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall") >> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> >> --- >> kernel/pid.c | 6 ------ >> 1 file changed, 6 deletions(-) >> >> Christian if you don't have any objections I will take this one through >> my tree. >> >> I tried to figure out why this code path takes the cred_guard_mutex and >> the archive on lore.kernel.org was not helpful in finding that part of >> the conversation. > > That was my suggestion. > >> diff --git a/kernel/pid.c b/kernel/pid.c >> index 60820e72634c..53646d5616d2 100644 >> --- a/kernel/pid.c >> +++ b/kernel/pid.c >> @@ -577,17 +577,11 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) >> struct file *file; >> int ret; >> >> - ret = mutex_lock_killable(&task->signal->cred_guard_mutex); >> - if (ret) >> - return ERR_PTR(ret); >> - >> if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) >> file = fget_task(task, fd); >> else >> file = ERR_PTR(-EPERM); >> >> - mutex_unlock(&task->signal->cred_guard_mutex); >> - >> return file ?: ERR_PTR(-EBADF); >> } > > If you make this change, then if this races with execution of a setuid > program that afterwards e.g. opens a unix domain socket, an attacker > will be able to steal that socket and inject messages into > communication with things like DBus. procfs currently has the same > race, and that still needs to be fixed, but at least procfs doesn't > let you open things like sockets because they don't have a working > ->open handler, and it enforces the normal permission check for > opening files. It isn't only exec that can change credentials. Do we need a lock for changing credentials? Wouldn't it be sufficient to simply test ptrace_may_access after we get a copy of the file? If we need a lock around credential change let's design and build that. Having a mismatch between what a lock is designed to do, and what people use it for can only result in other bugs as people get confused. Eric
On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > Jann Horn <jannh@google.com> writes: > > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > >> During exec some file descriptors are closed and the files struct is > >> unshared. But all of that can happen at other times and it has the > >> same protections during exec as at ordinary times. So stop taking the > >> cred_guard_mutex as it is useless. > >> > >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock > >> prone, as it is held in serveral while waiting possibly indefinitely > >> for userspace to do something. > > > > Please don't. Just use the new exec_update_mutex like everywhere else. > > > >> Cc: Sargun Dhillon <sargun@sargun.me> > >> Cc: Christian Brauner <christian.brauner@ubuntu.com> > >> Cc: Arnd Bergmann <arnd@arndb.de> > >> Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall") > >> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> > >> --- > >> kernel/pid.c | 6 ------ > >> 1 file changed, 6 deletions(-) > >> > >> Christian if you don't have any objections I will take this one through > >> my tree. > >> > >> I tried to figure out why this code path takes the cred_guard_mutex and > >> the archive on lore.kernel.org was not helpful in finding that part of > >> the conversation. > > > > That was my suggestion. > > > >> diff --git a/kernel/pid.c b/kernel/pid.c > >> index 60820e72634c..53646d5616d2 100644 > >> --- a/kernel/pid.c > >> +++ b/kernel/pid.c > >> @@ -577,17 +577,11 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) > >> struct file *file; > >> int ret; > >> > >> - ret = mutex_lock_killable(&task->signal->cred_guard_mutex); > >> - if (ret) > >> - return ERR_PTR(ret); > >> - > >> if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) > >> file = fget_task(task, fd); > >> else > >> file = ERR_PTR(-EPERM); > >> > >> - mutex_unlock(&task->signal->cred_guard_mutex); > >> - > >> return file ?: ERR_PTR(-EBADF); > >> } > > > > If you make this change, then if this races with execution of a setuid > > program that afterwards e.g. opens a unix domain socket, an attacker > > will be able to steal that socket and inject messages into > > communication with things like DBus. procfs currently has the same > > race, and that still needs to be fixed, but at least procfs doesn't > > let you open things like sockets because they don't have a working > > ->open handler, and it enforces the normal permission check for > > opening files. > > It isn't only exec that can change credentials. Do we need a lock for > changing credentials? Hmm, I guess so? Normally, a task that's changing credentials becomes nondumpable at the same time (and there are explicit memory barriers in commit_creds() and __ptrace_may_access() to enforce the ordering for this); so you normally don't see tasks becoming ptrace-accessible via anything other than execve(). But I guess if someone opens a root-only file, closes it, drops privileges, and then explicitly does prctl(PR_SET_DUMPABLE, 1), we should probably protect that, too. > Wouldn't it be sufficient to simply test ptrace_may_access after > we get a copy of the file? There are also setuid helpers that can, after having done privileged stuff, drop privileges and call execve(); after that, ptrace_may_access() succeeds again. In particular, polkit has a helper that does this. > If we need a lock around credential change let's design and build that. > Having a mismatch between what a lock is designed to do, and what > people use it for can only result in other bugs as people get confused. Hmm... what benefits do we get from making it a separate lock? I guess it would allow us to make it a per-task lock instead of a signal_struct-wide one? That might be helpful...
On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: > On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > > Jann Horn <jannh@google.com> writes: > > > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > > >> During exec some file descriptors are closed and the files struct is > > >> unshared. But all of that can happen at other times and it has the > > >> same protections during exec as at ordinary times. So stop taking the > > >> cred_guard_mutex as it is useless. > > >> > > >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock > > >> prone, as it is held in serveral while waiting possibly indefinitely > > >> for userspace to do something. [...] > > > If you make this change, then if this races with execution of a setuid > > > program that afterwards e.g. opens a unix domain socket, an attacker > > > will be able to steal that socket and inject messages into > > > communication with things like DBus. procfs currently has the same > > > race, and that still needs to be fixed, but at least procfs doesn't > > > let you open things like sockets because they don't have a working > > > ->open handler, and it enforces the normal permission check for > > > opening files. > > > > It isn't only exec that can change credentials. Do we need a lock for > > changing credentials? [...] > > If we need a lock around credential change let's design and build that. > > Having a mismatch between what a lock is designed to do, and what > > people use it for can only result in other bugs as people get confused. > > Hmm... what benefits do we get from making it a separate lock? I guess > it would allow us to make it a per-task lock instead of a > signal_struct-wide one? That might be helpful... But actually, isn't the core purpose of the cred_guard_mutex to guard against concurrent credential changes anyway? That's what almost everyone uses it for, and it's in the name...
On 3/10/20 9:10 PM, Jann Horn wrote: > On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: >> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >>> Jann Horn <jannh@google.com> writes: >>>> On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >>>>> During exec some file descriptors are closed and the files struct is >>>>> unshared. But all of that can happen at other times and it has the >>>>> same protections during exec as at ordinary times. So stop taking the >>>>> cred_guard_mutex as it is useless. >>>>> >>>>> Furthermore he cred_guard_mutex is a bad idea because it is deadlock >>>>> prone, as it is held in serveral while waiting possibly indefinitely >>>>> for userspace to do something. > [...] >>>> If you make this change, then if this races with execution of a setuid >>>> program that afterwards e.g. opens a unix domain socket, an attacker >>>> will be able to steal that socket and inject messages into >>>> communication with things like DBus. procfs currently has the same >>>> race, and that still needs to be fixed, but at least procfs doesn't >>>> let you open things like sockets because they don't have a working >>>> ->open handler, and it enforces the normal permission check for >>>> opening files. >>> >>> It isn't only exec that can change credentials. Do we need a lock for >>> changing credentials? > [...] >>> If we need a lock around credential change let's design and build that. >>> Having a mismatch between what a lock is designed to do, and what >>> people use it for can only result in other bugs as people get confused. >> >> Hmm... what benefits do we get from making it a separate lock? I guess >> it would allow us to make it a per-task lock instead of a >> signal_struct-wide one? That might be helpful... > > But actually, isn't the core purpose of the cred_guard_mutex to guard > against concurrent credential changes anyway? That's what almost > everyone uses it for, and it's in the name... > The main reason d'etre of exec_update_mutex is to get a consitent view of task->mm and task credentials. The reason why you want the cred_guard_mutex, is that some action is changing the resulting credentials that the execve is about to install, and that is the data flow in the opposite direction. Bernd.
Jann Horn <jannh@google.com> writes: > On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: >> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >> > Jann Horn <jannh@google.com> writes: >> > > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >> > >> During exec some file descriptors are closed and the files struct is >> > >> unshared. But all of that can happen at other times and it has the >> > >> same protections during exec as at ordinary times. So stop taking the >> > >> cred_guard_mutex as it is useless. >> > >> >> > >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock >> > >> prone, as it is held in serveral while waiting possibly indefinitely >> > >> for userspace to do something. > [...] >> > > If you make this change, then if this races with execution of a setuid >> > > program that afterwards e.g. opens a unix domain socket, an attacker >> > > will be able to steal that socket and inject messages into >> > > communication with things like DBus. procfs currently has the same >> > > race, and that still needs to be fixed, but at least procfs doesn't >> > > let you open things like sockets because they don't have a working >> > > ->open handler, and it enforces the normal permission check for >> > > opening files. >> > >> > It isn't only exec that can change credentials. Do we need a lock for >> > changing credentials? > [...] >> > If we need a lock around credential change let's design and build that. >> > Having a mismatch between what a lock is designed to do, and what >> > people use it for can only result in other bugs as people get confused. >> >> Hmm... what benefits do we get from making it a separate lock? I guess >> it would allow us to make it a per-task lock instead of a >> signal_struct-wide one? That might be helpful... > > But actually, isn't the core purpose of the cred_guard_mutex to guard > against concurrent credential changes anyway? That's what almost > everyone uses it for, and it's in the name... Having been through all of the users nope. Maybe someone tried to repurpose for that. I haven't traced through when it went the it was renamed from cred_exec_mutex to cred_guard_mutex. The original purpose was to make make exec and ptrace deadlock. But it was seen as being there to allow safely calculating the new credentials before the point of now return. Because if a process is ptraced or not affects the new credential calculations. Unfortunately offering that guarantee fundamentally leads to deadlock. So ptrace_attach and seccomp use the cred_guard_mutex to guarantee a deadlock. The common use is to take cred_guard_mutex to guard the window when credentials and process details are out of sync in exec. But there is at least do_io_accounting that seems to have the same justification for holding __pidfd_fget. With effort I suspect we can replace exec_change_mutex with task_lock. When we are guaranteed to be single threaded placing exec_change_mutex in signal_struct doesn't really help us (except maybe in some races?). The deep problem is no one really understands cred_guard_mutex so it is a mess. Code with poorly defined semantics is always wrong somewhere for someone. Which is part of why I am attacking this and having the conversations to make certain I understand what is going on. I see your point about commit_creds making a process undumpable. So in practice it really is only exec that changes creds in a way that ptrace_may_access will allow the process to be inspected. So I guess for now the practical non-regressing course is to change everything to my exec_change_mutex, removing the deadlock. Then we figure out how to cleanly deal with the races inspecting a process with changing credentials has. Eric
On Tue, Mar 10, 2020 at 03:57:35PM -0500, Eric W. Biederman wrote: > Jann Horn <jannh@google.com> writes: > > > On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: > >> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > >> > Jann Horn <jannh@google.com> writes: > >> > > On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > >> > >> During exec some file descriptors are closed and the files struct is > >> > >> unshared. But all of that can happen at other times and it has the > >> > >> same protections during exec as at ordinary times. So stop taking the > >> > >> cred_guard_mutex as it is useless. > >> > >> > >> > >> Furthermore he cred_guard_mutex is a bad idea because it is deadlock > >> > >> prone, as it is held in serveral while waiting possibly indefinitely > >> > >> for userspace to do something. > > [...] > >> > > If you make this change, then if this races with execution of a setuid > >> > > program that afterwards e.g. opens a unix domain socket, an attacker > >> > > will be able to steal that socket and inject messages into > >> > > communication with things like DBus. procfs currently has the same > >> > > race, and that still needs to be fixed, but at least procfs doesn't > >> > > let you open things like sockets because they don't have a working > >> > > ->open handler, and it enforces the normal permission check for > >> > > opening files. > >> > > >> > It isn't only exec that can change credentials. Do we need a lock for > >> > changing credentials? > > [...] > >> > If we need a lock around credential change let's design and build that. > >> > Having a mismatch between what a lock is designed to do, and what > >> > people use it for can only result in other bugs as people get confused. > >> > >> Hmm... what benefits do we get from making it a separate lock? I guess > >> it would allow us to make it a per-task lock instead of a > >> signal_struct-wide one? That might be helpful... > > > > But actually, isn't the core purpose of the cred_guard_mutex to guard > > against concurrent credential changes anyway? That's what almost > > everyone uses it for, and it's in the name... > > Having been through all of the users nope. > > Maybe someone tried to repurpose for that. I haven't traced through > when it went the it was renamed from cred_exec_mutex to > cred_guard_mutex. > > The original purpose was to make make exec and ptrace deadlock. But it > was seen as being there to allow safely calculating the new credentials > before the point of now return. Because if a process is ptraced or not > affects the new credential calculations. Unfortunately offering that > guarantee fundamentally leads to deadlock. > > So ptrace_attach and seccomp use the cred_guard_mutex to guarantee > a deadlock. > > The common use is to take cred_guard_mutex to guard the window when > credentials and process details are out of sync in exec. But there > is at least do_io_accounting that seems to have the same justification > for holding __pidfd_fget. > > With effort I suspect we can replace exec_change_mutex with task_lock. > When we are guaranteed to be single threaded placing exec_change_mutex > in signal_struct doesn't really help us (except maybe in some races?). > > The deep problem is no one really understands cred_guard_mutex so it is > a mess. Code with poorly defined semantics is always wrong somewhere This is a good point. When discussing patches sensitive to credential changes cred_guard_mutex was always introduced as having the purpose to guard against concurrent credential changes. And I'm pretty sure that that's how most people have been using it for quite a long time. I mean, it's at least the case for seccomp and proc and probably quite a few more. So the problem seems to me that it has clear _intended_ semantics that runs into issues in all sorts of cases. So if cred_guard_mutex is not that then we seem to need to provide something that serves it's intended purpose.
On 3/10/20 9:22 PM, Bernd Edlinger wrote: > On 3/10/20 9:10 PM, Jann Horn wrote: >> On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: >>> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >>>> Jann Horn <jannh@google.com> writes: >>>>> On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: >>>>>> During exec some file descriptors are closed and the files struct is >>>>>> unshared. But all of that can happen at other times and it has the >>>>>> same protections during exec as at ordinary times. So stop taking the >>>>>> cred_guard_mutex as it is useless. >>>>>> >>>>>> Furthermore he cred_guard_mutex is a bad idea because it is deadlock >>>>>> prone, as it is held in serveral while waiting possibly indefinitely >>>>>> for userspace to do something. >> [...] >>>>> If you make this change, then if this races with execution of a setuid >>>>> program that afterwards e.g. opens a unix domain socket, an attacker >>>>> will be able to steal that socket and inject messages into >>>>> communication with things like DBus. procfs currently has the same >>>>> race, and that still needs to be fixed, but at least procfs doesn't >>>>> let you open things like sockets because they don't have a working >>>>> ->open handler, and it enforces the normal permission check for >>>>> opening files. >>>> >>>> It isn't only exec that can change credentials. Do we need a lock for >>>> changing credentials? >> [...] >>>> If we need a lock around credential change let's design and build that. >>>> Having a mismatch between what a lock is designed to do, and what >>>> people use it for can only result in other bugs as people get confused. >>> >>> Hmm... what benefits do we get from making it a separate lock? I guess >>> it would allow us to make it a per-task lock instead of a >>> signal_struct-wide one? That might be helpful... >> >> But actually, isn't the core purpose of the cred_guard_mutex to guard >> against concurrent credential changes anyway? That's what almost >> everyone uses it for, and it's in the name... >> > > The main reason d'etre of exec_update_mutex is to get a consitent > view of task->mm and task credentials. > > The reason why you want the cred_guard_mutex, is that some action > is changing the resulting credentials that the execve is about > to install, and that is the data flow in the opposite direction. > So in other words, you need the exec_update_mutex when you access another thread's credentials and possibly the mmap at the same time. You need the cred_guard_mutex when you *change* the credentials of another thread. (Where you cannot be sure that the other thread just started to execve something) You need no mutex at all when you are just accessing or even changing the credentials of the current thread. (If another thread is doing execve, your task will be killed, and wether or not the credentials were changed does not matter any more) > > Bernd. >
On Wed, Mar 11, 2020 at 7:12 AM Bernd Edlinger <bernd.edlinger@hotmail.de> wrote: > On 3/10/20 9:22 PM, Bernd Edlinger wrote: > > On 3/10/20 9:10 PM, Jann Horn wrote: > >> On Tue, Mar 10, 2020 at 9:00 PM Jann Horn <jannh@google.com> wrote: > >>> On Tue, Mar 10, 2020 at 8:29 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > >>>> Jann Horn <jannh@google.com> writes: > >>>>> On Tue, Mar 10, 2020 at 7:54 PM Eric W. Biederman <ebiederm@xmission.com> wrote: > >>>>>> During exec some file descriptors are closed and the files struct is > >>>>>> unshared. But all of that can happen at other times and it has the > >>>>>> same protections during exec as at ordinary times. So stop taking the > >>>>>> cred_guard_mutex as it is useless. > >>>>>> > >>>>>> Furthermore he cred_guard_mutex is a bad idea because it is deadlock > >>>>>> prone, as it is held in serveral while waiting possibly indefinitely > >>>>>> for userspace to do something. > >> [...] > >>>>> If you make this change, then if this races with execution of a setuid > >>>>> program that afterwards e.g. opens a unix domain socket, an attacker > >>>>> will be able to steal that socket and inject messages into > >>>>> communication with things like DBus. procfs currently has the same > >>>>> race, and that still needs to be fixed, but at least procfs doesn't > >>>>> let you open things like sockets because they don't have a working > >>>>> ->open handler, and it enforces the normal permission check for > >>>>> opening files. > >>>> > >>>> It isn't only exec that can change credentials. Do we need a lock for > >>>> changing credentials? > >> [...] > >>>> If we need a lock around credential change let's design and build that. > >>>> Having a mismatch between what a lock is designed to do, and what > >>>> people use it for can only result in other bugs as people get confused. > >>> > >>> Hmm... what benefits do we get from making it a separate lock? I guess > >>> it would allow us to make it a per-task lock instead of a > >>> signal_struct-wide one? That might be helpful... > >> > >> But actually, isn't the core purpose of the cred_guard_mutex to guard > >> against concurrent credential changes anyway? That's what almost > >> everyone uses it for, and it's in the name... > >> > > > > The main reason d'etre of exec_update_mutex is to get a consitent > > view of task->mm and task credentials. > > > The reason why you want the cred_guard_mutex, is that some action > > is changing the resulting credentials that the execve is about > > to install, and that is the data flow in the opposite direction. > > > > So in other words, you need the exec_update_mutex when you > access another thread's credentials and possibly the mmap at the > same time. Or the file descriptor table, or register state, ... > You need no mutex at all when you are just accessing or > even changing the credentials of the current thread. (If another > thread is doing execve, your task will be killed, and wether > or not the credentials were changed does not matter any more) Only if the only access checks you care about are those related to mm access.
On Tue, Mar 10, 2020 at 03:57:35PM -0500, Eric W. Biederman wrote: > So ptrace_attach and seccomp use the cred_guard_mutex to guarantee > a deadlock. Well, that's the result, but seccomp uses it because it wants to be certain that credentials and no_new_privs are changed together "atomically".
diff --git a/kernel/pid.c b/kernel/pid.c index 60820e72634c..53646d5616d2 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -577,17 +577,11 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) struct file *file; int ret; - ret = mutex_lock_killable(&task->signal->cred_guard_mutex); - if (ret) - return ERR_PTR(ret); - if (ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS)) file = fget_task(task, fd); else file = ERR_PTR(-EPERM); - mutex_unlock(&task->signal->cred_guard_mutex); - return file ?: ERR_PTR(-EBADF); }
During exec some file descriptors are closed and the files struct is unshared. But all of that can happen at other times and it has the same protections during exec as at ordinary times. So stop taking the cred_guard_mutex as it is useless. Furthermore he cred_guard_mutex is a bad idea because it is deadlock prone, as it is held in serveral while waiting possibly indefinitely for userspace to do something. Cc: Sargun Dhillon <sargun@sargun.me> Cc: Christian Brauner <christian.brauner@ubuntu.com> Cc: Arnd Bergmann <arnd@arndb.de> Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall") Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> --- kernel/pid.c | 6 ------ 1 file changed, 6 deletions(-) Christian if you don't have any objections I will take this one through my tree. I tried to figure out why this code path takes the cred_guard_mutex and the archive on lore.kernel.org was not helpful in finding that part of the conversation.