Skip to contentSkip to conversation

cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops

PATCH In review Chris Down 2 participants 4 messages Nov 26

Subsystems (2)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

v3 cover cgroup: Fix cgroup_mutex deadlock through cgroup_parse_float() oops

With CONFIG_UCLAMP_TASK_GROUP=y on x86_64 it is possible to get cgroup_parse_float() to oops by writing 0.00... to cpu.uclamp.min.

This alone is not great, but it's even worse because the dead writer leaves the kernfs active reference held, so cgroup_mutex gets stuck completely. It's also triggerable by unprivileged users on delegated subtrees.

v3:

- First public version
- Rebase onto current cgroup/for-7.4-fixes, no code changes

v2:

- Only parse the digits after the dot (Tejun)
- Drop the selftest cases for whitespace and signs
- Rebase onto cgroup/for-7.4-fixes

v1 and v2 were sent off-list, so they are not on lore.

Chris Down (2):
cgroup: Fix divide error on long fractions in cgroup_parse_float() selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min

 kernel/cgroup/cgroup.c                    | 29 ++++++---
 tools/testing/selftests/cgroup/config     |  4 ++
 tools/testing/selftests/cgroup/test_cpu.c | 71 +++++++++++++++++++++++
 3 files changed, 95 insertions(+), 9 deletions(-)

base-commit: c99f50e4acf86bfbbe313d79f281d83be1763260

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v3 1/2 cgroup: Fix divide error on long fractions in cgroup_parse_float()

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()")
---
 kernel/cgroup/cgroup.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
kernel/cgroup/cgroup.c +20 -9
index b3657e72db99..f6df19241772 100644
@@ -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;
Signature · 1 line
2.56.1
author
#2 lore raw

PATCH v3 2/2 selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min

In reply to Chris Down

Add a test to test_cpu that writes values to cpu.uclamp.min and reads them back, checking that they are rounded to two decimal places and that malformed or out of range values are rejected.

---
 tools/testing/selftests/cgroup/config     |  4 ++
 tools/testing/selftests/cgroup/test_cpu.c | 71 +++++++++++++++++++++++
 2 files changed, 75 insertions(+)
tools/testing/selftests/cgroup/config +4 -0
index 39f979690dd3..c8b2c95a186e 100644
@@ -2,5 +2,9 @@ CONFIG_CGROUPS=y
 CONFIG_CGROUP_CPUACCT=y
 CONFIG_CGROUP_FREEZER=y
 CONFIG_CGROUP_SCHED=y
+CONFIG_CPU_FREQ=y
+CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y
 CONFIG_MEMCG=y
 CONFIG_PAGE_COUNTER=y
+CONFIG_UCLAMP_TASK=y
+CONFIG_UCLAMP_TASK_GROUP=y
tools/testing/selftests/cgroup/test_cpu.c +71 -0

83 lines not on this page · raw message

Signature · 1 line
2.56.1
maintainer
#3 lore raw
In reply to Chris Down
Quoted 3 lines

Chris Down (2):
cgroup: Fix divide error on long fractions in cgroup_parse_float() selftests/cgroup: Test parsing of long fractions in cpu.uclamp.min

Applied 1-2 to cgroup/for-7.4-fixes.

Thanks.

--
tejun