Skip to contentSkip to conversation

cgroup: limit cgroup psi file writes to processes with CAP_SYS_RESOURCE

PATCH In review Suren Baghdasaryan 2 participants 6 messages Mar 1, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cgroup: limit cgroup psi file writes to processes with CAP_SYS_RESOURCE

Currently /proc/pressure/* files can be written only by processes with CAP_SYS_RESOURCE capability to prevent any unauthorized user from creating psi triggers. However no such limitation is required for per-cgroup pressure files. Fix this inconsistency by requiring the same capability for writing per-cgroup psi files.

Fixes 6db12ee0456d ("psi: allow unprivileged users with CAP_SYS_RESOURCE to write psi files")
---
 kernel/cgroup/cgroup.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
kernel/cgroup/cgroup.c +10 -0
index 935e8121b21e..b600a6baaeca 100644
@@ -3867,6 +3867,12 @@ static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of,
 	return psi_trigger_poll(&ctx->psi.trigger, of->file, pt);
 }
 
+static int cgroup_pressure_open(struct kernfs_open_file *of)
+{
+	return (of->file->f_mode & FMODE_WRITE && !capable(CAP_SYS_RESOURCE)) ?
+		-EPERM : 0;
+}
+
 static void cgroup_pressure_release(struct kernfs_open_file *of)
 {
 	struct cgroup_file_ctx *ctx = of->priv;
@@ -5266,6 +5272,7 @@ static struct cftype cgroup_psi_files[] = {
 	{
 		.name = "io.pressure",
 		.file_offset = offsetof(struct cgroup, psi_files[PSI_IO]),
+		.open = cgroup_pressure_open,
 		.seq_show = cgroup_io_pressure_show,
 		.write = cgroup_io_pressure_write,
 		.poll = cgroup_pressure_poll,
@@ -5274,6 +5281,7 @@ static struct cftype cgroup_psi_files[] = {
 	{
 		.name = "memory.pressure",
 		.file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]),
+		.open = cgroup_pressure_open,
 		.seq_show = cgroup_memory_pressure_show,
 		.write = cgroup_memory_pressure_write,
 		.poll = cgroup_pressure_poll,
@@ -5282,6 +5290,7 @@ static struct cftype cgroup_psi_files[] = {
 	{
 		.name = "cpu.pressure",
 		.file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]),
+		.open = cgroup_pressure_open,
 		.seq_show = cgroup_cpu_pressure_show,
 		.write = cgroup_cpu_pressure_write,
 		.poll = cgroup_pressure_poll,
@@ -5291,6 +5300,7 @@ static struct cftype cgroup_psi_files[] = {
 	{
 		.name = "irq.pressure",
 		.file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]),
+		.open = cgroup_pressure_open,
 		.seq_show = cgroup_irq_pressure_show,
 		.write = cgroup_irq_pressure_write,
 		.poll = cgroup_pressure_poll,
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
#1 lore raw
Quoted 7 lines · Suren Baghdasaryan

Currently /proc/pressure/* files can be written only by processes with CAP_SYS_RESOURCE capability to prevent any unauthorized user from creating psi triggers. However no such limitation is required for per-cgroup pressure files. Fix this inconsistency by requiring the same capability for writing per-cgroup psi files.

Fixes 6db12ee0456d ("psi: allow unprivileged users with CAP_SYS_RESOURCE to write psi files")

Is this really a regression from this commit? 6db12ee0456d is changing permissions of those files to be world writeable with the CAP_SYS_RESOURCE requirement. Permissions of cgroup files is not changed and the default mode is 644 (with root as an owner) so only privileged processes are allowed without any delegation.

I think you should instead construct this slightly differently. The ultimate goal is to allow a reasonable delegation after all, no?

So keep your current patch and extend it by removing the min timeout constrain and justify the change by the necessity of the granularity tuning as reported by Sudarshan Rajagopala. If this causes any regression then a revert would also return the min timeout constrain back and we will have to think about a different approach.

The consistency with the global case is a valid point only partially because different cgroups might have different owners which is not usually the case for the global psi interface, right?

Makes sense?

Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 8 lines · On Wed, Mar 1, 2023 at 1:47 AM Michal Hocko <[email protected]> wrote:
Quoted 6 lines · Suren Baghdasaryan

Currently /proc/pressure/* files can be written only by processes with CAP_SYS_RESOURCE capability to prevent any unauthorized user from creating psi triggers. However no such limitation is required for per-cgroup pressure files. Fix this inconsistency by requiring the same capability for writing per-cgroup psi files.

Quoted 7 lines
Quoted 1 line
Fixes 6db12ee0456d ("psi: allow unprivileged users with CAP_SYS_RESOURCE to write psi files")

Is this really a regression from this commit? 6db12ee0456d is changing permissions of those files to be world writeable with the CAP_SYS_RESOURCE requirement. Permissions of cgroup files is not changed and the default mode is 644 (with root as an owner) so only privileged processes are allowed without any delegation.

Agree, the Fixes line here is not valid. I will remove it.

Quoted 3 lines

I think you should instead construct this slightly differently. The ultimate goal is to allow a reasonable delegation after all, no?

Yes.

Quoted 6 lines

So keep your current patch and extend it by removing the min timeout constrain and justify the change by the necessity of the granularity tuning as reported by Sudarshan Rajagopala. If this causes any regression then a revert would also return the min timeout constrain back and we will have to think about a different approach.

I think adding CAP_SYS_RESOURCE check is needed even if we keep the min timeout capped like today. Without it one could create multiple cgroups and add a trigger into each one, therefore creating an unlimited number of "psimon" kernel threads. At some point I expect them to affect system performance because even at high polling intervals they still consume some cpu resources. So, this change I think is needed regardless of the change to min polling period and I would suggest keeping them separate.

Quoted 4 lines

The consistency with the global case is a valid point only partially because different cgroups might have different owners which is not usually the case for the global psi interface, right?

Correct.

Quoted 2 lines

Makes sense?

Yes but hopefully my argument about keeping this and min period patches separate is reasonable?
Thanks,
Suren.

Quoted 3 lines

--
Michal Hocko
SUSE Labs

#3 lore raw

On Wed 01-03-23 10:05:36, Suren Baghdasaryan wrote:
[...]

Quoted 2 lines

Yes but hopefully my argument about keeping this and min period patches separate is reasonable?

I am not questioning that. The practical advantage to squash the two changes is that in case of the CAP_SYS_RESOURCE you do not have to explicitly think about reverting the min constrain as well. I do not think reverting one without the other is good.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 10 lines · On Wed, Mar 1, 2023 at 10:35 AM Michal Hocko <[email protected]> wrote:

On Wed 01-03-23 10:05:36, Suren Baghdasaryan wrote:
[...]

Quoted 2 lines

Yes but hopefully my argument about keeping this and min period patches separate is reasonable?

I am not questioning that. The practical advantage to squash the two changes is that in case of the CAP_SYS_RESOURCE you do not have to explicitly think about reverting the min constrain as well. I do not think reverting one without the other is good.

Ok, I'm fine with having both changes in the same patch. Will post v2 later today. Thanks!

Quoted 4 lines

--
Michal Hocko
SUSE Labs

#5 lore raw
Quoted 8 lines · Suren Baghdasaryan
Quoted 6 lines · Michal Hocko

On Wed 01-03-23 10:05:36, Suren Baghdasaryan wrote:
[...]

Quoted 2 lines

Yes but hopefully my argument about keeping this and min period patches separate is reasonable?

Quoted 7 lines
Quoted 4 lines

I am not questioning that. The practical advantage to squash the two changes is that in case of the CAP_SYS_RESOURCE you do not have to explicitly think about reverting the min constrain as well. I do not think reverting one without the other is good.

Ok, I'm fine with having both changes in the same patch. Will post v2 later today. Thanks!

Didn't call it v2 since the title had to be changed. The new patch is posted at: https://lore.kernel.org/all/[email protected]/

Quoted 5 lines
Quoted 5 lines

--
Michal Hocko
SUSE Labs