Skip to contentSkip to conversation

proc: also check the opener's capability for oom_score_adj and oom_adj

PATCH New Hungyu Lin 1 participant 1 message Dec 5

Subsystems (2)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v2 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.

The capability of the writer also decides whether the write sets oom_score_adj_min. If the privileged process writes 500 to such a file descriptor, oom_score_adj_min becomes 500 and the opener can no longer lower the value back to 0. Writing 500 itself would have left oom_score_adj_min unchanged.

Require the capability from the opener of the file as well as from the writer in both places: check file_ns_capable() in addition to capable() before lowering the value, and the opener's credentials in addition to has_capability_noaudit(), also without auditing, before setting oom_score_adj_min.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
Changes in v2:
- Also require the opener's capability before setting oom_score_adj_min (Oleg).
- The checks that refuse the write keep capable() and file_ns_capable(), so a refused write is still audited.

v1: https://lore.kernel.org/all/[email protected]/

 fs/proc/base.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
fs/proc/base.c +12 -4
index e6d54193b9f5..c94668dcb9db 100644
@@ -1090,16 +1090,23 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 {
 	struct mm_struct *mm = NULL;
 	struct task_struct *task;
+	bool update_min;
 	int err = 0;
 
 	task = get_proc_task(file_inode(file));
 	if (!task)
 		return -ESRCH;
 
+	update_min = !legacy &&
+		     !security_capable(file->f_cred, &init_user_ns, CAP_SYS_RESOURCE,
+				       CAP_OPT_NOAUDIT) &&
+		     has_capability_noaudit(current, CAP_SYS_RESOURCE);
+
 	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 +1119,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;
 		}
@@ -1136,7 +1144,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 	}
 
 	task->signal->oom_score_adj = oom_adj;
-	if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+	if (update_min)
 		task->signal->oom_score_adj_min = (short)oom_adj;
 	trace_oom_score_adj_update(task);
 
@@ -1155,7 +1163,7 @@ static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
 			task_lock(p);
 			if (!p->vfork_done && process_shares_mm(p, mm)) {
 				p->signal->oom_score_adj = oom_adj;
-				if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
+				if (update_min)
 					p->signal->oom_score_adj_min = (short)oom_adj;
 			}
 			task_unlock(p);

base-commit: 9437046953b2fb3bf55b36582450ea0267df82a7

Signature · 1 line
2.43.0