From 2b5819446e0bda40edcd64f71f895473e5bc6411 Mon Sep 17 00:00:00 2001 From: Rick Yiu Date: Mon, 7 Oct 2024 07:00:24 +0000 Subject: [PATCH] ANDROID: sched: Make uclamp changes depend on CAP_SYS_NICE There is currently nothing preventing tasks from changing their per-task clamp values in anyway that they like. The rationale is probably that system administrators are still able to limit those clamps thanks to the cgroup interface. However, this causes pain in a system where both per-task and per-cgroup clamp values are expected to be under the control of core system components (as is the case for Android). To fix this, let's require CAP_SYS_NICE to change per-task clamp values. There are ongoing discussions upstream about more flexible approaches than this using the RLIMIT API -- see [1]. But the upstream discussion has not converged yet, and this is way too late for UAPI changes in android12-5.10 anyway, so let's apply this change which provides the behaviour we want without actually impacting UAPIs. This patch is cherry picked from commit 0bb784e62322f9f989a087c8cbc793cd1ddbdcbf because the original patch was dropped due to upstream code merge. Also it uses a new Change-Id because it conflicts with the original one. [1] https://lore.kernel.org/lkml/20210623123441.592348-4-qperret@google.com/ Bug: 187186685 Change-Id: Ib2dbd3a2fb8a8512714f36e626203898c0b8b67b Signed-off-by: Quentin Perret Signed-off-by: Rick Yiu --- kernel/sched/syscalls.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index 94c48f2ed8f9..cffb39771ede 100644 --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -514,6 +514,12 @@ static int user_check_sched_setscheduler(struct task_struct *p, if (p->sched_reset_on_fork && !reset_on_fork) goto req_priv; + if (!capable(CAP_SYS_NICE)) { + /* Can't change util-clamps */ + if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) + return -EPERM; + } + return 0; req_priv: