exit: Retain nsproxy for exit_task_work() work entries
Michal Koutný
maintainerPATCH 1/1 exit: Retain nsproxy for exit_task_work() work entries
The reported issue is an attempted write in a cgroup file, by a zombie who has/had acct(2) enabled. Such a write needs cgroup_ns for access checking. Ordinary acct_process() would not be affected by this as it is called well before exit_task_namespaces(). However, the reported NULL dereference is a different acct data writer:
Call Trace: <TASK> kernfs_fop_write_iter+0x3b6/0x510 fs/kernfs/file.c:296 __kernel_write+0x5d1/0xaf0 fs/read_write.c:535 do_acct_process+0x112a/0x17b0 kernel/acct.c:518 acct_pin_kill+0x27/0x130 kernel/acct.c:173 pin_kill+0x2a6/0x940 fs/fs_pin.c:44 mnt_pin_kill+0xc1/0x170 fs/fs_pin.c:81 cleanup_mnt+0x4bc/0x510 fs/namespace.c:1130 task_work_run+0x146/0x1c0 kernel/task_work.c:164 exit_task_work include/linux/task_work.h:32 [inline] do_exit+0x705/0x24f0 kernel/exit.c:832 do_group_exit+0x168/0x2d0 kernel/exit.c:929 get_signal+0x16b0/0x2090 kernel/signal.c:2820 arch_do_signal_or_restart+0x9c/0x730 arch/x86/kernel/signal.c:868 handle_signal_work kernel/entry/common.c:148 [inline] exit_to_user_mode_loop kernel/entry/common.c:172 [inline] exit_to_user_mode_prepare+0x191/0x220 kernel/entry/common.c:207 __syscall_exit_to_user_mode_work kernel/entry/common.c:289 [inline] syscall_exit_to_user_mode+0x2e/0x70 kernel/entry/common.c:300 do_syscall_64+0x53/0xd0 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x44/0xae
i.e. called as one of task_work_run() entries.
The historical commit 8aac62706ada ("move exit_task_namespaces() outside of exit_notify()") argues that exit_task_namespaces() must come before exit_task_work() because ipc_ns cleanup calls fput/task_work_add.
There is accompanying commit e7b2c4069252 ("fput: task_work_add() can fail if the caller has passed exit_task_work()") in the original series that makes fput() robust in situations when task_work_add() cannot be used anymore.
So in order to ensure that task_work_run() entries of the exiting task have the nsproxy still available, swap the order of exit_task_namespaces() and exit_task_work().
This change may appear like a partial revert of 8aac62706ada but this particular ordering change shouldn't matter with the fix from e7b2c4069252 and the other reason for 8aac62706ada (keeping exit_notify simpler) still holds.
--- kernel/exit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
I wasn't able to reproduce the syzbot's crash manually so the effectiveness of the fix is only based on the reasoning.
kernel/exit.c +1 -1
@@ -828,8 +828,8 @@ void __noreturn do_exit(long code)exit_fs (tsk);if (group_dead)disassociate_ctty (1 ); -exit_task_namespaces (tsk);exit_task_work (tsk); +exit_task_namespaces (tsk);exit_thread (tsk);/*
Signature · 1 line
2.33.1
Adding the security list and a couple of other people because I feel like I just opened pandora's box, and I don't know how bad this issue is.
TL;DR the cgroup file system is checking permissions at write time.
Quoted 40 lines · Michal Koutný
The reported issue is an attempted write in a cgroup file, by a zombie who has/had acct(2) enabled. Such a write needs
cgroup_nsfor access checking. Ordinaryacct_process()would not be affected by this as it is called well beforeexit_task_namespaces(). However, the reported NULL dereference is a different acct data writer:Call Trace: <TASK> kernfs_fop_write_iter+0x3b6/0x510 fs/kernfs/file.c:296 __kernel_write+0x5d1/0xaf0 fs/read_write.c:535 do_acct_process+0x112a/0x17b0 kernel/acct.c:518 acct_pin_kill+0x27/0x130 kernel/acct.c:173 pin_kill+0x2a6/0x940 fs/fs_pin.c:44 mnt_pin_kill+0xc1/0x170 fs/fs_pin.c:81 cleanup_mnt+0x4bc/0x510 fs/namespace.c:1130 task_work_run+0x146/0x1c0 kernel/task_work.c:164 exit_task_work include/linux/task_work.h:32 [inline] do_exit+0x705/0x24f0 kernel/exit.c:832 do_group_exit+0x168/0x2d0 kernel/exit.c:929 get_signal+0x16b0/0x2090 kernel/signal.c:2820 arch_do_signal_or_restart+0x9c/0x730 arch/x86/kernel/signal.c:868 handle_signal_work kernel/entry/common.c:148 [inline] exit_to_user_mode_loop kernel/entry/common.c:172 [inline] exit_to_user_mode_prepare+0x191/0x220 kernel/entry/common.c:207 __syscall_exit_to_user_mode_work kernel/entry/common.c:289 [inline] syscall_exit_to_user_mode+0x2e/0x70 kernel/entry/common.c:300 do_syscall_64+0x53/0xd0 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x44/0xaei.e. called as one of
task_work_run()entries.The historical commit
8aac62706ada("moveexit_task_namespaces()outside ofexit_notify()") argues thatexit_task_namespaces()must come beforeexit_task_work()becauseipc_nscleanup calls fput/task_work_add.There is accompanying commit
e7b2c4069252("fput:task_work_add()can fail if the caller has passedexit_task_work()") in the original series that makesfput()robust in situations whentask_work_add()cannot be used anymore.
Quoted 8 lines
So in order to ensure that
task_work_run()entries of the exiting task have the nsproxy still available, swap the order ofexit_task_namespaces()andexit_task_work().This change may appear like a partial revert of
8aac62706adabut this particular ordering change shouldn't matter with the fix frome7b2c4069252and the other reason for8aac62706ada(keepingexit_notifysimpler) still holds.
I think I follow your reasoning and I think it will even fix the issue but no. That is completely very much not the correct fix.
That permission check in cgroup_file_write is just plain wrong.
Permission checks on files need to happen at open time, not at write time. It is all too easy to confuse something that writes to stdout to write to a file of your choosing to make permission checking at write time a good idea.
It looks like the this issue was introduced in commit 5136f6365ce3 ("cgroup: implement "nsdelegate" mount option").
I wish I knew where I was when that patch was posted for review so I could suggest a better implementation.
That said I can't quite tell if the test should be moved into cgroup_file_open or if there is a permission entry that would work.
Oh bleep!
The likely named cgroup_procs_write_permission isn't a permission method at all. It is called from cgroup_procs_write which I believe is called from cgroup_file_write.
I may be wrong but at first glance this looks like the cgroup code is going to need significant surgery to get the permission checks happening at open time where they belong.
Please don't apply this patch.
exit_task_work running after exit_task_namespaces is the messenger that just told us about something ugly.
Eric
Quoted 26 lines
Reported-by [email protected]Cc Oleg Nesterov <[email protected]>Fixes 5136f6365ce3 ("cgroup: implement "nsdelegate" mount option")Signed-off-by Michal Koutný <[email protected]>--- kernel/exit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)I wasn't able to reproduce the syzbot's crash manually so the effectiveness of the fix is only based on the reasoning.
kernel/exit.c +1
-1@@ -828,8 +828,8 @@ void __noreturn do_exit(long code)exit_fs (tsk);if (group_dead)disassociate_ctty (1 ); -exit_task_namespaces (tsk);exit_task_work (tsk); +exit_task_namespaces (tsk);exit_thread (tsk);/*
Tejun Heo
maintainerHello, Eric.
Quoted 4 lines · Eric W. Biederman
Permission checks on files need to happen at open time, not at write time. It is all too easy to confuse something that writes to stdout to write to a file of your choosing to make permission checking at write time a good idea.
Whether a given operation is allowed or not is dependent on the content of the write. I don't see how it can be moved to open time. I'm not a fan of the fs based interface but they're not real files and it has been like this from the beginning.
Quoted 2 lines
That said I can't quite tell if the test should be moved into
cgroup_file_openor if there is a permission entry that would work.
It can't.
Quoted 3 lines
I may be wrong but at first glance this looks like the cgroup code is going to need significant surgery to get the permission checks happening at open time where they belong.
There's no way to change that without changing the interface drastically.
Quoted 4 lines
Please don't apply this patch.
exit_task_workrunning afterexit_task_namespacesis the messenger that just told us about something ugly.
I don't see good alternatives here. You got any?
Thanks.
Signature · 1 line
tejun
Quoted 5 lines · Tejun Heo
Quoted 3 lines
That said I can't quite tell if the test should be moved into
cgroup_file_openor if there is a permission entry that would work.It can't.
Two options:
(a) anybody doing "current process" permission checks at IO time
should then also check that the credentials are still the same as when the file was opened.
(b) alternatively, go ahead and do the permission check at IO time,
but do it using "file->f_cred" (ie the open-time permission), not the current process ones.
In the above, (a) and (b) are basically the same: it uses f_cred for permission checking. The only difference is that in (a) you may be using some function that _technically_ uses the implicit "current credentials" (there are many of them), and you just separately make sure that those current credentials are identical to what they were at open time.
Obviously (b) is hugely preferred, but sometimes the code organization (ie "file or f_cred just isn't passed down deep enough") means that
(a) might be the only realistic option.
IOW, it's not *wrong* to do permission checking at IO time, but it absolutely needs to be done using the open-time credentials.
Linus
Tejun Heo
maintainerHello,
Quoted 11 lines · Linus Torvalds
(b) alternatively, go ahead and do the permission check at IO time,
but do it using "file->f_cred" (ie the open-time permission), not the current process ones.In the above, (a) and (b) are basically the same: it uses
f_credfor permission checking. The only difference is that in (a) you may be using some function that _technically_ uses the implicit "current credentials" (there are many of them), and you just separately make sure that those current credentials are identical to what they were at open time.
Quoted 6 lines
Obviously (b) is hugely preferred, but sometimes the code organization (ie "file or
f_credjust isn't passed down deep enough") means that
(a) might be the only realistic option.IOW, it's not *wrong* to do permission checking at IO time, but it absolutely needs to be done using the open-time credentials.
Yeah, (b) sounds good to me. Will look into it.
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerHello,
Quoted 3 lines · Tejun Heo
Quoted 3 lines
(b) alternatively, go ahead and do the permission check at IO time,
but do it using "file->f_cred" (ie the open-time permission), not the current process ones.
So, I have sth like the following. It builds and euid-open test case behaves as expected (can't evade delegation restrictions if the fd was opened with lesser euid). The namespace part is a bit more involved as f_cred doesn't capture them on open. I made cgroup file open path capture it and use that for all permission checks. Please let me know if anything looks weird. Otherwise, I'm gonna add selftests and prep the patchset.
Thanks.
kernel/cgroup/cgroup-v1.c +4 -3
@@ -504,10 +504,11 @@ static ssize_t __cgroup1_procs_write(str goto out_unlock;/* -* Even if we're attaching all tasks in the thread group, we only -* need to check permissions on one of them. +* Even if we're attaching all tasks in the thread group, we only need +* to check permissions on one of them. Check permissions using the +* credentials from file open to protect against inherited fd attacks. */ - cred =current_cred (); + cred = of->file->f_cred; tcred =get_task_cred (task); if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && !uid_eq(cred->euid, tcred->uid) &&
kernel/cgroup/cgroup.c +59 -22
194 lines not on this page · raw message
kernel/cgroup/cgroup-internal.h +14 -0
@@ -65,6 +65,20 @@ static inline struct cgroup_fs_context * return container_of(kfc, struct cgroup_fs_context, kfc); } +struct cgroup_file_ctx { +struct cgroup_namespace *ns; + +union { +struct { +struct css_task_iter *it; + } procs; + +struct { +void *trigger; + } psi; + }; +}; +/* * A cgroup can be associated with multiple css_sets as different tasks may * belong to different cgroups on different hierarchies. In the other
Michal Koutný
maintainerkernel/cgroup/cgroup.c static ssize_t __cgroup_procs_write(stru · line 4950
Quoted 6 lines · Tejun Heo
kernel/cgroup/cgroup.c +4
-1+ saved_cred =override_creds (of->file->f_cred); ret =cgroup_attach_permissions (src_cgrp, dst_cgrp, - of->file->f_path.dentry->d_sb, threadgroup); + of->file->f_path.dentry->d_sb, + threadgroup, ctx->ns); +revert_creds (saved_cred);
I wonder now whether such a wrap shouldn't also be around cgroup_kill() too (+ replacement of send_sig() with group_send_sig_info() [1])?
This shouldn't break the use case of passing cgroup kill fd to a less privileged task for (auto)destruction purposes but on the other hand it would prevent subverting the fd to a more privileged confused task to kill otherwise disallowed processes.
Thanks,
Michal
Quoted 11 lines · Michal Koutný
kernel/cgroup/cgroup.c static ssize_t __cgroup_procs_write(stru · line 4950
Quoted 6 lines · Tejun Heo
kernel/cgroup/cgroup.c +4
-1+ saved_cred =override_creds (of->file->f_cred); ret =cgroup_attach_permissions (src_cgrp, dst_cgrp, - of->file->f_path.dentry->d_sb, threadgroup); + of->file->f_path.dentry->d_sb, + threadgroup, ctx->ns); +revert_creds (saved_cred);I wonder now whether such a wrap shouldn't also be around
cgroup_kill()too (+ replacement ofsend_sig()withgroup_send_sig_info()[1])?
send_sig() isn't used that was changed in response to a review. I'm confused.
Quoted 5 lines
This shouldn't break the use case of passing cgroup kill fd to a less privileged task for (auto)destruction purposes but on the other hand it would prevent subverting the fd to a more privileged confused task to kill otherwise disallowed processes.
Kill and freeze only do time permission checking at open. Why would you introduce another write time check?
Michal Koutný
maintainerQuoted 2 lines · Christian Brauner
send_sig()isn't used that was changed in response to a review. I'm confused.
Sorry for ambiguity, I meant this instance [1].
Quoted 2 lines
Kill and freeze only do time permission checking at open. Why would you introduce another write time check?
Let's have a cgroup G with tasks t1,...,tn (run by user u) and some monitoring tasks m1,...,mk belonging to a different user v != u.
Currently u can kill also the tasks of v -- I'm not sure if that's intentional. My argument would apply if it wasn't -- it'd be suscebtible to similar abuse, i.e. passing the opened fd to a more privileged process to kill also v's tasks. (But if the intention is to be able to kill anyone in the cgroup, then it likely doesn't matter.)
Michal
Quoted 5 lines · Michal Koutný
Quoted 2 lines · Christian Brauner
send_sig()isn't used that was changed in response to a review. I'm confused.Sorry for ambiguity, I meant this instance [1].
Sure, seems good.
Quoted 9 lines
Quoted 3 lines
Kill and freeze only do time permission checking at open. Why would you introduce another write time check?
Let's have a cgroup G with tasks t1,...,tn (run by user u) and some monitoring tasks m1,...,mk belonging to a different user v != u.
Currently u can kill also the tasks of v -- I'm not sure if that's intentional. My argument would apply if it wasn't -- it'd be suscebtible
That was discussed and is intentional and is supposed to mirror the behavior of cgroup.freeze. Delegated killing was supposed to work and was one use-case.
Michal Koutný
maintainerQuoted 3 lines · Christian Brauner
That was discussed and is intentional and is supposed to mirror the behavior of cgroup.freeze. Delegated killing was supposed to work and was one use-case.
Thanks for the clarification.
The cgroup_kill() doesn't need the override_creds() treating then (clearing my previous wondering).
Michal
Michal Koutný
maintainerQuoted 1 line · Eric W. Biederman
TL;DR the cgroup file system is checking permissions at write time.
Thank you for bringing that up (handled in a separate thread now).
Quoted 2 lines
I think I follow your reasoning and I think it will even fix the issue but no.
FTR, part of Tejun's series [1] ensures that cgroup_ns is accessed directly without nsproxy and a reference to it is kept while the file is opened. I.e. that'd properly fix this particular crash reported by syzbot.
Quoted 4 lines
Please don't apply this patch.
exit_task_workrunning afterexit_task_namespacesis the messenger that just told us about something ugly.
In (my) theory some other task_work callbacks could (transitively) rely on the current->nsproxy which could still be cleared by exit_task_namespaces().
Is there another reason why to have exit_task_namespaces() before exit_task_work()?
Thanks,
Michal
Quoted 15 lines · Michal Koutný
Quoted 1 line · Eric W. Biederman
TL;DR the cgroup file system is checking permissions at write time.
Thank you for bringing that up (handled in a separate thread now).
Quoted 2 lines
I think I follow your reasoning and I think it will even fix the issue but no.
FTR, part of Tejun's series [1] ensures that
cgroup_nsis accessed directly without nsproxy and a reference to it is kept while the file is opened. I.e. that'd properly fix this particular crash reported by syzbot.Quoted 2 lines
Please don't apply this patch.
Quoted 8 lines
Quoted 2 lines
exit_task_workrunning afterexit_task_namespacesis the messenger that just told us about something ugly.In (my) theory some other
task_workcallbacks could (transitively) rely on thecurrent->nsproxywhich could still be cleared byexit_task_namespaces().
Is there another reason why to haveexit_task_namespaces()beforeexit_task_work()?
We already have the principle that things are going to be cleaned up before exit_task_work is called and exit_files depends upon that.
So I think the burden is to find a good reason why exit_task_work should move not to defend it.
If we don't want things cleaned up before exit_task_work it should come at the start of do_exit and exit_files and others need to stop depending upon it. Which seems like challenging change to make.
Eric