mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Down <chris@chrisdown.name>
To: tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com,
	shakeel.butt@linux.dev
Cc: cgroups@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 1/2] cgroup: Fix divide error on long fractions in cgroup_parse_float()
Date: Wed, 25 Nov 2026 21:41:15 -0800	[thread overview]
Message-ID: <27716decb1032bb5d00ab8a4560179e082d8a2c0.1795671629.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795671629.git.chris@chrisdown.name>

cgroup_parse_float() parses the decimal values written to
cpu.uclamp.min, cpu.uclamp.max and io.cost.qos. When there are too many
digits, it rounds the fraction with:

  frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));

do_div() takes a 32-bit divisor, so power_of_ten() is
truncated to its low 32 bits. That gives the wrong divisor from
10^10 onwards and zero at 10^32, because 2^32 divides 10^32.

This means that as it is now, writing "0." followed by 34 zeros to
cpu.uclamp.min or cpu.uclamp.max divides by zero.

The files belong to whoever owns the cgroup, so the owner of a
delegated subtree can do the same without any privilege:

  # mkdir /sys/fs/cgroup/d
  # cd /sys/fs/cgroup/d
  # chown 1000:1000 . cgroup.procs cgroup.subtree_control cgroup.threads
  # echo +cpu > cgroup.subtree_control
  # setpriv --reuid=1000 --regid=1000 --clear-groups sh
  $ mkdir x
  $ echo 0.0000000000000000000000000000000000 > x/cpu.uclamp.min

  Oops: divide error: 0000 [#1] SMP KASAN NOPTI
  CPU: 3 UID: 1000 PID: 178 Comm: sh Not tainted 7.4.0-rc1
  RIP: 0010:cgroup_parse_float (kernel/cgroup/cgroup.c:7553)
  RCX: 0000000000000000
  Call Trace:
   cpu_uclamp_write (kernel/sched/core.c:9677 kernel/sched/core.c:9700)
   kernfs_fop_write_iter (fs/kernfs/file.c:345)
   vfs_write (fs/read_write.c:595 fs/read_write.c:687)
   ksys_write (fs/read_write.c:739)
   do_syscall_64 (arch/x86/entry/syscall_64.c:84)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)

Unfortunately the writer dies holding the kernfs active reference on
cpu.uclamp.min, so subsequent operations deadlock in kernfs_drain(),
even in unrelated cgroups:

  task:rmdir state:D
   kernfs_drain (fs/kernfs/dir.c:539)
   __kernfs_remove (fs/kernfs/dir.c:1578)
   kernfs_remove_by_name_ns (fs/kernfs/dir.c:1796)
   cgroup_addrm_files (kernel/cgroup/cgroup.c:4596)
   css_clear_dir (kernel/cgroup/cgroup.c:1770)
   kill_css_sync (kernel/cgroup/cgroup.c:6274)
   cgroup_destroy_locked (kernel/cgroup/cgroup.c:6394)
   cgroup_rmdir (kernel/cgroup/cgroup.c:6442)
   [...]
  task:mkdir state:D
   __mutex_lock (kernel/locking/mutex.c:821)
   cgroup_kn_lock_live (kernel/cgroup/cgroup.c:1713)
   cgroup_mkdir (kernel/cgroup/cgroup.c:6161)
   [...]

To fix this, stop scanning the fraction as a number, and leave any
trailing digits alone. That avoids needing to divide, and thus the risk
of overflow.

(Dividing with div64_u64() and capping the exponent would also solve
this, but it would keep scanning the fraction into an s64, which long
fractions can still overflow. kstrtodec64() truncates rather than
rounds and is inconveniently too new for most stable trees, so let's do
it this way.)

Fixes: a5e112e6424a ("cgroup: add cgroup_parse_float()")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Down <chris@chrisdown.name>
---
 kernel/cgroup/cgroup.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index b3657e72db99..f6df19241772 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -29,6 +29,7 @@
 
 #include <linux/bpf-cgroup.h>
 #include <linux/cred.h>
+#include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/init_task.h>
 #include <linux/kernel.h>
@@ -7541,18 +7542,28 @@ static u64 power_of_ten(int power)
 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v)
 {
 	s64 whole, frac = 0;
-	int fstart = 0, fend = 0, flen;
+	int wlen = 0;
+	unsigned int i;
+	const char *p;
 
-	if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend))
-		return -EINVAL;
-	if (frac < 0)
+	if (sscanf(input, "%lld%n", &whole, &wlen) != 1)
 		return -EINVAL;
 
-	flen = fend > fstart ? fend - fstart : 0;
-	if (flen < dec_shift)
-		frac *= power_of_ten(dec_shift - flen);
-	else
-		frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift));
+	p = input + wlen;
+	if (*p == '.')
+		p++;
+
+	for (i = 0; i <= dec_shift; i++) {
+		int digit = 0;
+
+		if (isdigit(*p))
+			digit = *p++ - '0';
+
+		if (i < dec_shift)
+			frac = frac * 10 + digit;
+		else if (digit >= 5)
+			frac++;
+	}
 
 	*v = whole * power_of_ten(dec_shift) + frac;
 	return 0;
-- 
2.56.1


  reply	other threads:[~2026-11-26  5:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  5:41 [PATCH v3 0/2] cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops Chris Down
2026-11-26  5:41 ` Chris Down [this message]
2026-11-26  5:41 ` [PATCH v3 2/2] selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min Chris Down

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=27716decb1032bb5d00ab8a4560179e082d8a2c0.1795671629.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®