blk-ioprio: check capabilities before promoting a cgroup to RT
Subsystems (2)
PATCH 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. The other policies are left alone.
--- block/blk-ioprio.c | 4 ++++ 1 file changed, 4 insertions(+)
block/blk-ioprio.c +4 -0
@@ -14,6 +14,7 @@#include <linux/blk-mq.h> #include <linux/blk_types.h> +#include <linux/capability.h> #include <linux/kernel.h> #include <linux/module.h> #include "blk-cgroup.h"
@@ -91,6 +92,9 @@ 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 ) && + !capable (CAP_SYS_ADMIN ) && !capable (CAP_SYS_NICE )) +return -EPERM ; blkcg->prio_policy = ret;return nbytes; }
Signature · 1 line
2.43.0
Quoted 20 lines · Hungyu Lin
ioprio_set()and the per-request I/O priority of aio andio_uringonly hand outIOPRIO_CLASS_RTto tasks that haveCAP_SYS_ADMINorCAP_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_RTalthoughioprio_set()fails with-EPERMfor 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.rstexpects 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.
Quoted 7 lines
Require
CAP_SYS_ADMINorCAP_SYS_NICEfor the two policies that raise the priority class, likeioprio_check_cap()does. The other policies are left alone.Fixes 556910e39249 ("block: Introduce the ioprio rq-qos policy")Signed-off-by Hungyu Lin <[email protected]>
Quoted 26 lines
--- block/blk-ioprio.c | 4 ++++ 1 file changed, 4 insertions(+)block/blk-ioprio.c +4
-0@@ -14,6 +14,7 @@#include <linux/blk-mq.h> #include <linux/blk_types.h> +#include <linux/capability.h> #include <linux/kernel.h> #include <linux/module.h> #include "blk-cgroup.h" @@ -91,6 +92,9 @@ 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 ) && + !capable (CAP_SYS_ADMIN ) && !capable (CAP_SYS_NICE )) +return -EPERM ; blkcg->prio_policy = ret;return nbytes; }
Quoted 3 lines · Hungyu Lin
Require
CAP_SYS_ADMINorCAP_SYS_NICEfor the two policies that raise the priority class, likeioprio_check_cap()does. The other policies are left alone.
Restricting RT promotion to privileged users makes sense to me. A few comments below.
block/blk-ioprio.c static ssize_t ioprio_set_prio_policy(struct kernfs_open_file *of, char *buf, · line 97
Quoted 7 lines
block/blk-ioprio.c +3
-0@@ -91,6 +92,9 @@ 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 ) && + !capable (CAP_SYS_ADMIN ) && !capable (CAP_SYS_NICE )) +return -EPERM ;
capable() checks the credentials of the task that calls write() rather than the credentials of the task that opened the file. Checking the credentials of the caller from inside a .write() callback is discouraged because it enables confused-deputy attacks, e.g. an unprivileged user opens io.prio.class and passes the file descriptor as stdout or stderr to a setuid program. cgroup_release_agent_write() uses the file credentials for this reason. Please use something like this instead:
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;
Additionally, please update the io.prio.class documentation in Documentation/admin-guide/cgroup-v2.rst such that it mentions that CAP_SYS_ADMIN or CAP_SYS_NICE is required to select the promote-to-rt and none-to-rt policies.
Thanks,
Bart.