proc: also check the opener's capability for oom_score_adj and oom_adj
Subsystems (2)
PATCH 1/1 proc: also check the opener's capability for oom_score_adj and oom_adj
__set_oom_adj() checks CAP_SYS_RESOURCE against the task that calls write(). A task without the capability can open its own /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj and have a privileged process write to that file descriptor, for example a set-user-ID program that inherits it as standard output. The write then lowers the value below oom_score_adj_min, down to OOM_SCORE_ADJ_MIN, which the opener is not allowed to do.
Require the capability from the opener of the file as well as from the
writer: check file_ns_capable() in addition to capable().
--- fs/proc/base.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/proc/base.c +4 -2
@@ -1099,7 +1099,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)mutex_lock (&oom_adj_mutex);if (legacy) {if (oom_adj < task->signal->oom_score_adj && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }
@@ -1112,7 +1113,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) task_pid_nr(task)); }else {if ((short )oom_adj < task->signal->oom_score_adj_min && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }
base-commit: 9437046953b2fb3bf55b36582450ea0267df82a7
Signature · 1 line
2.43.0
Quoted 11 lines · Hungyu Lin
__set_oom_adj()checksCAP_SYS_RESOURCEagainst the task that callswrite(). A task without the capability can open its own /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj and have a privileged process write to that file descriptor, for example a set-user-ID program that inherits it as standard output. The write then lowers the value belowoom_score_adj_min, down toOOM_SCORE_ADJ_MIN, which the opener is not allowed to do.Require the capability from the opener of the file as well as from the
writer: checkfile_ns_capable()
Looks reasonable but
Quoted 1 line
in addition to
capable().
do we really need to keep the old capable(CAP_SYS_RESOURCE) check?
If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
Just in case, I am not saying that the old check should be removed, I am trying to understand.
Oleg.
Quoted 37 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Hungyu Lin <[email protected]>--- fs/proc/base.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/proc/base.c +4
-2@@ -1099,7 +1099,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)mutex_lock (&oom_adj_mutex);if (legacy) {if (oom_adj < task->signal->oom_score_adj && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }@@ -1112,7 +1113,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) task_pid_nr(task)); }else {if ((short )oom_adj < task->signal->oom_score_adj_min && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }base-commit:
9437046953b2fb3bf55b36582450ea0267df82a7Signature · 1 line
2.43.0
I just noticed that __set_oom_adj() has more CAP_SYS_RESOURCE checks below.
Shouldn't we unify them or even factor out?
Oleg.
Quoted 65 lines · Oleg Nesterov
Quoted 11 lines · Hungyu Lin
__set_oom_adj()checksCAP_SYS_RESOURCEagainst the task that callswrite(). A task without the capability can open its own /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj and have a privileged process write to that file descriptor, for example a set-user-ID program that inherits it as standard output. The write then lowers the value belowoom_score_adj_min, down toOOM_SCORE_ADJ_MIN, which the opener is not allowed to do.Require the capability from the opener of the file as well as from the
writer: checkfile_ns_capable()Looks reasonable but
Quoted 1 line
in addition to
capable().do we really need to keep the old
capable(CAP_SYS_RESOURCE)check?If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
Just in case, I am not saying that the old check should be removed, I am trying to understand.
Oleg.
Quoted 37 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Hungyu Lin <[email protected]>--- fs/proc/base.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)fs/proc/base.c +4
-2@@ -1099,7 +1099,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)mutex_lock (&oom_adj_mutex);if (legacy) {if (oom_adj < task->signal->oom_score_adj && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }@@ -1112,7 +1113,8 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) task_pid_nr(task)); }else {if ((short )oom_adj < task->signal->oom_score_adj_min && - !capable (CAP_SYS_RESOURCE )) { + (!file_ns_capable (file, &init_user_ns,CAP_SYS_RESOURCE ) || + !capable (CAP_SYS_RESOURCE ))) { err = -EACCES ; goto err_unlock; }base-commit:
9437046953b2fb3bf55b36582450ea0267df82a7--
2.43.0
Quoted 4 lines · Oleg Nesterov
do we really need to keep the old
capable(CAP_SYS_RESOURCE)check?If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
A write that needs CAP_SYS_RESOURCE still needs it from the writer, even if the opener was privileged. For example, a task can open its own oom_score_adj with CAP_SYS_RESOURCE and still have the fd after dropping that capability and calling execve(). If we only check the opener, it could then lower its value down to OOM_SCORE_ADJ_MIN, and any children it creates afterward would inherit that.
I kept capable() so that a fix for stable doesn't relax anything. Or do you think the opener check alone is enough?
Thanks,
Hungyu
Quoted 7 lines · Hungyu Lin
Quoted 4 lines · Oleg Nesterov
do we really need to keep the old
capable(CAP_SYS_RESOURCE)check?If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
Quoted 6 lines
A write that needs
CAP_SYS_RESOURCEstill needs it from the writer, even if the opener was privileged. For example, a task can open its ownoom_score_adjwithCAP_SYS_RESOURCEand still have the fd after dropping that capability and callingexecve(). If we only check the opener, it could then lower its value down toOOM_SCORE_ADJ_MIN, and any children it creates afterward would inherit that.
And my question was: do we really care about this case?
Quoted 1 line
I kept
capable()so that a fix for stable doesn't relax anything.
OK,
Quoted 2 lines
Or do
you think the opener check alone is enough?
Well, let me quote the part of my email you have removed:
Just in case, I am not saying that the old check should be removed, I am trying to understand.
So no, I am not sure the opener check alone is enough.
Oleg.
Quoted 3 lines · Oleg Nesterov
I just noticed that
__set_oom_adj()has moreCAP_SYS_RESOURCEchecks below.Shouldn't we unify them or even factor out?
The checks below only decide whether a successful write also sets oom_score_adj_min. The checks above already prevent a write through an fd opened without CAP_SYS_RESOURCE from lowering it, so I left them as they are.
I can factor out the common check in the two rejection paths in a separate patch on top of this one if you like.
Thanks,
Hungyu
Quoted 6 lines · Hungyu Lin
Quoted 3 lines · Oleg Nesterov
I just noticed that
__set_oom_adj()has moreCAP_SYS_RESOURCEchecks below.Shouldn't we unify them or even factor out?
Quoted 7 lines
The checks below only decide whether a successful write also sets
oom_score_adj_min. The checks above already prevent a write through an fd opened withoutCAP_SYS_RESOURCEfrom lowering it, so I left them as they are.I can factor out the common check in the two rejection paths in a separate patch on top of this one if you like.
No, up to you, I don't really understand this code anyway,
Oleg.
Quoted 10 lines · Hungyu Lin
__set_oom_adj()checksCAP_SYS_RESOURCEagainst the task that callswrite(). A task without the capability can open its own /proc/<pid>/oom_score_adj or /proc/<pid>/oom_adj and have a privileged process write to that file descriptor, for example a set-user-ID program that inherits it as standard output. The write then lowers the value belowoom_score_adj_min, down toOOM_SCORE_ADJ_MIN, which the opener is not allowed to do.Require the capability from the opener of the file as well as from the
writer: checkfile_ns_capable()in addition tocapable().
LGTM. A few additional comments below.
Quoted 4 lines · Oleg Nesterov
do we really need to keep the old
capable(CAP_SYS_RESOURCE)check?If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
I agree with keeping the writer-side check: a permission check should not be relaxed without a detailed justification. Checking both the opener and the writer is an existing pattern in the kernel (uid_map/gid_map), and it is also what Documentation/security/credentials.rst, "Open File Credentials", asks for. So I'd suggest keeping it for now; if there is a use case for removing it, that can be discussed in a separate patch.
Quoted 1 line
program that inherits it as standard output.
For later readers who wonder how this can happen in practice, sudo's -S -p is a ready-made example (the prompt is written to stderr, before authentication).
And there are also a few existing instances of the same pattern:
- /proc/<pid>/timerslack_ns: the reach is even wider than oom_score_adj; any process can be the target. The fix is the same: add file_ns_capable(file, __task_cred(p)->user_ns, CAP_SYS_NICE).
- /proc/<pid>/make-it-fail (proc_fault_inject_write, fs/proc/base.c:1409): the same writer-only capable(CAP_SYS_RESOURCE); there should be no security impact, but it could be fixed along the way for consistency.
--
Chen Linxuan
Damn ;) let me apologise first, I'am afraid my comments just added the unnecessary noise to this thread.
But don't worry, I am going to add even more noise, please see below.
Quoted 2 lines · Chen Linxuan
LGTM. A few additional comments below.
Same here, please see below.
Quoted 8 lines · Oleg Nesterov
Quoted 4 lines
do we really need to keep the old
capable(CAP_SYS_RESOURCE)check?If a privileged opener of /proc/<pid>/oom_score_adj passes fd to to unprivileged writer, we probably don't care?
I agree with keeping the writer-side check: a permission check should not be relaxed without a detailed justification.
OK, agreed. In case it wasn't clear let me repeat: I didn't really try to argue, I just tried to understand this patch.
Oh. Now let me also repeat that I don't really understand this code, so you can safely ignore me.
-------------------------------------------------------------------------------
That said...
So. To simplify, lets forget about "legacy", ->vfork_done, and CAP_OPT_NOAUDIT. And about this patch for the moment. In essence we have
if (oom_adj < oom_score_adj_min && !CAP_SYS_RESOURCE) // CHECK 1 return -EACCES; oom_score_adj = oom_adj; // unconditional if CHECK 1 passes if (CAP_SYS_RESOURCE) // CHECK 2 oom_score_adj_min = oom_adj;
This already looks messy to me, but perhaps this is because I don't really understand this logic.
Now lets recall that this patch adds file_ns_capable() into CHECK 1, but not into CHECK 2.
Now. Lets suppose a task T has oom_score_adj = oom_score_adj_min = 0. This task does "setuid-app-which-writes-500-to-stdout > /proc/<getpid()>/oom_score_adj".
CHECK 1 is skipped. CHECK 2 passes (app is setuid), oom_score_adj_min is raised to 500. Now, T can't lower oom_score_adj back to 0.
Compare that with the case when T writes "500" directly: oom_score_adj becomes 500, but oom_score_adj_min is still 0. And in this case T still can lower its oom_score_adj to 0.
That is why I tried to suggest to unify / factor out CAP_SYS_RESOURCE checks (again, let's ignore the possible noise from audit). I mean, suppose we add
bool xxx = CAP_SYS_RESOURCE && file_ns_capable(CAP_SYS_RESOURCE);
at the start and change __set_oom_adj() to use it consistently. In this case CHECK 1 and 2 will agree at least in the scenario above: oom_score_adj_min will be 0.
Oleg.