Skip to contentSkip to conversation

blk-ioprio: check capabilities before promoting a cgroup to RT

PATCH Superseded Hungyu Lin 2 participants 2 messages Nov 17

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v2 1/1 blk-ioprio: check capabilities before promoting a cgroup to RT

ioprio_set() and the per-request I/O priority of aio and io_uring only hand out IOPRIO_CLASS_RT to tasks that have CAP_SYS_ADMIN or CAP_SYS_NICE. The io.prio.class cgroup attribute has no such check. Writing "promote-to-rt", or its alias "none-to-rt", to it changes the requests of the cgroup to the RT class.

The attribute exists in every cgroup that has the io controller enabled and it is not hierarchical. A user that got a cgroup delegated owns the files of the child cgroups it creates there, so it can create a child cgroup, move into it and write "promote-to-rt" to its io.prio.class. Its requests are then issued with IOPRIO_CLASS_RT although ioprio_set() fails with -EPERM for the same task, and mq-deadline dispatches them before the best-effort requests of everybody else.

The delegation model in Documentation/admin-guide/cgroup-v2.rst expects the settings of all resource controllers to be hierarchical, so that a delegatee can only distribute further what it received from its parent. This attribute lets it raise the priority class of its own requests instead.

Require CAP_SYS_ADMIN or CAP_SYS_NICE for the two policies that raise the priority class, like ioprio_check_cap() does. Check the credentials of the task that opened the file, like cgroup_release_agent_write() does, so that the check cannot be bypassed by passing the file descriptor to a privileged program. The other policies are left alone.

Fixes 556910e39249 ("block: Introduce the ioprio rq-qos policy")

---
Changes in v2:
- Check the credentials of the task that opened the file with file_ns_capable() instead of the credentials of the writer (Bart).
- Document the requirement in cgroup-v2.rst (Bart).

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

 Documentation/admin-guide/cgroup-v2.rst | 3 +++
 block/blk-ioprio.c                      | 6 ++++++
 2 files changed, 9 insertions(+)
Documentation/admin-guide/cgroup-v2.rst +3 -0
index 9e3c67b02ae2..4424f9cf583e 100644
@@ -2409,6 +2409,9 @@ that attribute:
   none-to-rt
 	Deprecated. Just an alias for promote-to-rt.
 
+Selecting promote-to-rt or none-to-rt requires CAP_SYS_ADMIN or CAP_SYS_NICE
+in the initial user namespace.
+
 The following numerical values are associated with the I/O priority policies:
 
 +----------------+---+
block/blk-ioprio.c +6 -0
index 8fa8bca35062..acec62ee94bd 100644
@@ -14,6 +14,8 @@
 
 #include <linux/blk-mq.h>
 #include <linux/blk_types.h>
+#include <linux/capability.h>
+#include <linux/cred.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include "blk-cgroup.h"
@@ -91,6 +93,10 @@ static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf,
 	ret = sysfs_match_string(policy_name, buf);
 	if (ret < 0)
 		return ret;
+	if ((ret == POLICY_PROMOTE_TO_RT || ret == POLICY_NONE_TO_RT) &&
+	    !file_ns_capable(of->file, &init_user_ns, CAP_SYS_ADMIN) &&
+	    !file_ns_capable(of->file, &init_user_ns, CAP_SYS_NICE))
+		return -EPERM;
 	blkcg->prio_policy = ret;
 	return nbytes;
 }
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw

block/blk-ioprio.c line 18

Quoted 2 lines · Hungyu Lin
block/blk-ioprio.c +2 -0
+#include <linux/capability.h>
+#include <linux/cred.h>

file_ns_capable() is declared in <linux/capability.h> and init_user_ns is declared in <linux/user_namespace.h>. Shouldn't <linux/cred.h> be changed into <linux/user_namespace.h>? Anyway: