[PATCH v3 1/2] cgroup: Fix divide error on long fractions in cgroup_parse_float()
From: Chris Down
Date: Thu Nov 26 2026 - 00:41:25 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
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