Skip to contentSkip to conversation

psi: allow unprivileged users with CAP_SYS_RESOURCE to write psi files

PATCH Superseded Josh Hunt 5 participants 7 messages Apr 1, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Josh Hunt

author
#0 lore raw

PATCH 1/1 psi: allow unprivileged users with CAP_SYS_RESOURCE to write psi files

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

---
 kernel/sched/psi.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/sched/psi.c +6 -3
index b1b00e9bd7ed..98ff7baf1ba8 100644
@@ -1270,6 +1270,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 	if (!nbytes)
 		return -EINVAL;
 
+	if (!capable(CAP_SYS_RESOURCE))
+		return -EPERM;
+
 	buf_size = min(nbytes, sizeof(buf));
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
@@ -1353,9 +1356,9 @@ static int __init psi_proc_init(void)
 {
 	if (psi_enable) {
 		proc_mkdir("pressure", NULL);
-		proc_create("pressure/io", 0, NULL, &psi_io_proc_ops);
-		proc_create("pressure/memory", 0, NULL, &psi_memory_proc_ops);
-		proc_create("pressure/cpu", 0, NULL, &psi_cpu_proc_ops);
+		proc_create("pressure/io", 0666, NULL, &psi_io_proc_ops);
+		proc_create("pressure/memory", 0666, NULL, &psi_memory_proc_ops);
+		proc_create("pressure/cpu", 0666, NULL, &psi_cpu_proc_ops);
 	}
 	return 0;
 }
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

The test for CAP_SYS_RESOURCE really needs to be in open rather than in write.

Otherwise a suid root executable could have stdout redirected into these files.

Eric

Quoted 32 lines
---
 kernel/sched/psi.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/sched/psi.c +6 -3
index b1b00e9bd7ed..98ff7baf1ba8 100644
@@ -1270,6 +1270,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 	if (!nbytes)
 		return -EINVAL;
 
+	if (!capable(CAP_SYS_RESOURCE))
+		return -EPERM;
+
 	buf_size = min(nbytes, sizeof(buf));
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
@@ -1353,9 +1356,9 @@ static int __init psi_proc_init(void)
 {
 	if (psi_enable) {
 		proc_mkdir("pressure", NULL);
-		proc_create("pressure/io", 0, NULL, &psi_io_proc_ops);
-		proc_create("pressure/memory", 0, NULL, &psi_memory_proc_ops);
-		proc_create("pressure/cpu", 0, NULL, &psi_cpu_proc_ops);
+		proc_create("pressure/io", 0666, NULL, &psi_io_proc_ops);
+		proc_create("pressure/memory", 0666, NULL, &psi_memory_proc_ops);
+		proc_create("pressure/cpu", 0666, NULL, &psi_cpu_proc_ops);
 	}
 	return 0;
 }
#2 lore raw
Quoted 11 lines · Eric W. Biederman
Quoted 3 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

The test for CAP_SYS_RESOURCE really needs to be in open rather than in write.

Otherwise a suid root executable could have stdout redirected into these files.

Right. Or check against f_cred. (See uses of kallsyms_show_value())
https://www.kernel.org/doc/html/latest/security/credentials.html#open-file-credentials

-Kees

Quoted 36 lines

Eric

Quoted 32 lines
---
 kernel/sched/psi.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/sched/psi.c +6 -3
index b1b00e9bd7ed..98ff7baf1ba8 100644
@@ -1270,6 +1270,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 	if (!nbytes)
 		return -EINVAL;
 
+	if (!capable(CAP_SYS_RESOURCE))
+		return -EPERM;
+
 	buf_size = min(nbytes, sizeof(buf));
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
@@ -1353,9 +1356,9 @@ static int __init psi_proc_init(void)
 {
 	if (psi_enable) {
 		proc_mkdir("pressure", NULL);
-		proc_create("pressure/io", 0, NULL, &psi_io_proc_ops);
-		proc_create("pressure/memory", 0, NULL, &psi_memory_proc_ops);
-		proc_create("pressure/cpu", 0, NULL, &psi_cpu_proc_ops);
+		proc_create("pressure/io", 0666, NULL, &psi_io_proc_ops);
+		proc_create("pressure/memory", 0666, NULL, &psi_memory_proc_ops);
+		proc_create("pressure/cpu", 0666, NULL, &psi_cpu_proc_ops);
 	}
 	return 0;
 }
Signature · 1 line
Kees Cook
#3 lore raw
Quoted 7 lines · Kees Cook
Quoted 6 lines · Eric W. Biederman
Quoted 3 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

Quoted 8 lines
Quoted 5 lines

The test for CAP_SYS_RESOURCE really needs to be in open rather than in write.

Otherwise a suid root executable could have stdout redirected into these files.

Right. Or check against f_cred. (See uses of kallsyms_show_value())
https://www.kernel.org/doc/html/latest/security/credentials.html#open-file-credentials

We really want to limit checking against f_cred to those cases where we break userspace by checking in open. AKA the cases where we made the mistake of putting the permission check in the wrong place and now can't fix it.

Since this change is change the permissions that open uses already I don't see any reason we can't perform a proper check in open.

Eric

author
#4 lore raw
Quoted 23 lines · Eric W. Biederman
Quoted 7 lines · Kees Cook
Quoted 6 lines · Eric W. Biederman
Quoted 3 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

Quoted 8 lines
Quoted 5 lines

The test for CAP_SYS_RESOURCE really needs to be in open rather than in write.

Otherwise a suid root executable could have stdout redirected into these files.

Right. Or check against f_cred. (See uses of kallsyms_show_value())
https://urldefense.com/v3/__https://www.kernel.org/doc/html/latest/security/credentials.html*open-file-credentials__;Iw!!GjvTz_vk!B_aeVyHMG20VNUGx001EFKpeYlahLQHye7oS8sokXuZOhVDTtF_deDl71a_KYA$

We really want to limit checking against f_cred to those cases where we break userspace by checking in open. AKA the cases where we made the mistake of putting the permission check in the wrong place and now can't fix it.

Quoted 5 lines

Since this change is change the permissions that open uses already I don't see any reason we can't perform a proper check in open.

Eric

Thank you for the feedback. I will spin a v2 doing the check in open.

Josh

maintainer
#5 lore raw
In reply to Josh Hunt
Quoted 5 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

I suppose that's ok, but lets also ask Johannes.

Quoted 34 lines
---
 kernel/sched/psi.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/sched/psi.c +6 -3
index b1b00e9bd7ed..98ff7baf1ba8 100644
@@ -1270,6 +1270,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
 	if (!nbytes)
 		return -EINVAL;
 
+	if (!capable(CAP_SYS_RESOURCE))
+		return -EPERM;
+
 	buf_size = min(nbytes, sizeof(buf));
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;
@@ -1353,9 +1356,9 @@ static int __init psi_proc_init(void)
 {
 	if (psi_enable) {
 		proc_mkdir("pressure", NULL);
-		proc_create("pressure/io", 0, NULL, &psi_io_proc_ops);
-		proc_create("pressure/memory", 0, NULL, &psi_memory_proc_ops);
-		proc_create("pressure/cpu", 0, NULL, &psi_cpu_proc_ops);
+		proc_create("pressure/io", 0666, NULL, &psi_io_proc_ops);
+		proc_create("pressure/memory", 0666, NULL, &psi_memory_proc_ops);
+		proc_create("pressure/cpu", 0666, NULL, &psi_cpu_proc_ops);
 	}
 	return 0;
 }
Signature · 1 line
2.17.1
maintainer Acked-by
#6 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Josh Hunt

Currently only root can write files under /proc/pressure. Relax this to allow tasks running as unprivileged users with CAP_SYS_RESOURCE to be able to write to these files.

I suppose that's ok, but lets also ask Johannes.

The write creates a kthread that runs as SCHED_FIFO. For userspace threads this is reserved for CAP_SYS_NICE tasks, but it's a kernel thread and not arbitrary code, so I suppose CAP_SYS_RESOURCE is fine.