Skip to contentSkip to conversation

sched/core: check format and overflows in cgroup2 cpu.max

PATCH In review Konstantin Khlebnikov 3 participants 6 messages Feb 27, 2019 – Mar 6, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/core: check format and overflows in cgroup2 cpu.max

Cgroup2 interface for cpu bandwidth limit has some flaws:

- on stack buffer overflow
- no checks for valid format or trailing garbage
- no checks for integer overflows

This patch fixes all these flaws.

Fixes 0d5936344f30 ("sched: Implement interface for cgroup unified hierarchy")
---
 kernel/sched/core.c |   29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
kernel/sched/core.c +22 -7
index 5f46aa335b28..123b1910bc2e 100644
@@ -6947,19 +6947,34 @@ static int __maybe_unused cpu_period_quota_parse(char *buf,
 						 u64 *periodp, u64 *quotap)
 {
 	char tok[21];	/* U64_MAX */
+	int ret, len;
 
-	if (!sscanf(buf, "%s %llu", tok, periodp))
+	if (sscanf(buf, "%20s %n", tok, &len) != 1)
 		return -EINVAL;
 
-	*periodp *= NSEC_PER_USEC;
-
-	if (sscanf(tok, "%llu", quotap))
-		*quotap *= NSEC_PER_USEC;
-	else if (!strcmp(tok, "max"))
+	ret = kstrtou64(tok, 10, quotap);
+	if (ret) {
+		if (strcmp(tok, "max"))
+			return ret;
 		*quotap = RUNTIME_INF;
-	else
+	} else if (*quotap <= U64_MAX / NSEC_PER_USEC) {
+		*quotap *= NSEC_PER_USEC;
+	} else
 		return -EINVAL;
 
+	buf += len;
+	if (*buf) {
+		if (sscanf(buf, "%20s %n", tok, &len) != 1 || buf[len])
+			return -EINVAL;
+		ret = kstrtou64(tok, 10, periodp);
+		if (ret)
+			return ret;
+		if (*periodp > U64_MAX / NSEC_PER_USEC)
+			return -EINVAL;
+	}
+
+	*periodp *= NSEC_PER_USEC;
+
 	return 0;
 }
 
maintainer
#1 lore raw

Hello,

Quoted 7 lines · Konstantin Khlebnikov

Cgroup2 interface for cpu bandwidth limit has some flaws:

- on stack buffer overflow
- no checks for valid format or trailing garbage
- no checks for integer overflows

This patch fixes all these flaws.

Ditto as the blkio patch. Unless there is a correctness problem, my preference is towards keeping the parsing functions simple and I don't think the kernel needs to play the role of strict input verifier here as long as the only foot getting shot is the user's own.

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 9 lines · Tejun Heo

Hello,

Quoted 6 lines · Konstantin Khlebnikov

Cgroup2 interface for cpu bandwidth limit has some flaws:

- on stack buffer overflow
- no checks for valid format or trailing garbage
- no checks for integer overflows

Quoted 6 lines
Quoted 1 line

This patch fixes all these flaws.

Ditto as the blkio patch. Unless there is a correctness problem, my preference is towards keeping the parsing functions simple and I don't think the kernel needs to play the role of strict input verifier here as long as the only foot getting shot is the user's own.

IMHO non-strict interface more likely hides bugs and could cause problems for future changes.

Here is only only one fatal bug - buffer overflow in sscanf because %s has no limit.

Strict validation could be done as more strict sscanf variant or some kind of extension for format string.

Quoted 3 lines

Thanks.

maintainer
#3 lore raw

Hello, Konstantin.

Quoted 10 lines · Konstantin Khlebnikov
Quoted 4 lines

Ditto as the blkio patch. Unless there is a correctness problem, my preference is towards keeping the parsing functions simple and I don't think the kernel needs to play the role of strict input verifier here as long as the only foot getting shot is the user's own.

IMHO non-strict interface more likely hides bugs and could cause problems for future changes.

Here is only only one fatal bug - buffer overflow in sscanf because %s has no limit.

Ah, indeed. Can you please post a patch to fix that problem first?

Quoted 2 lines

Strict validation could be done as more strict sscanf variant or some kind of extension for format string.

I don't necessarily disagree with you; however, what often ends up with these manually crafted parsing approach are 1. code which is unnecessarily difficult to follow 2. different subset of validations and parsing bugs (of course) everywhere.

Given the above, I tend to lean towards dump sscanf() parsing. If we wanna improve the situation, I think the right thing to do is either improving sscanf or introducing new helpers to parse these things rather than hand-crafting each site. It is really error-prone.

Thanks.

Signature · 1 line
tejun
maintainer
#4 lore raw
Quoted 24 lines · Tejun Heo

Hello, Konstantin.

Quoted 10 lines · Konstantin Khlebnikov
Quoted 4 lines

Ditto as the blkio patch. Unless there is a correctness problem, my preference is towards keeping the parsing functions simple and I don't think the kernel needs to play the role of strict input verifier here as long as the only foot getting shot is the user's own.

IMHO non-strict interface more likely hides bugs and could cause problems for future changes.

Here is only only one fatal bug - buffer overflow in sscanf because %s has no limit.

Ah, indeed. Can you please post a patch to fix that problem first?

Quoted 2 lines

Strict validation could be done as more strict sscanf variant or some kind of extension for format string.

I don't necessarily disagree with you; however, what often ends up with these manually crafted parsing approach are 1. code which is unnecessarily difficult to follow 2. different subset of validations and parsing bugs (of course) everywhere.

Quoted 4 lines

Given the above, I tend to lean towards dump sscanf() parsing. If we wanna improve the situation, I think the right thing to do is either improving sscanf or introducing new helpers to parse these things rather than hand-crafting each site. It is really error-prone.

Always use a field width specifier with %s. Which is exactly what the proposed patch did IIRC.

Maybe that's something checkpatch could warn about.

#5 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Tejun Heo

Hello, Konstantin.

Quoted 5 lines · Konstantin Khlebnikov
Quoted 4 lines

Ditto as the blkio patch. Unless there is a correctness problem, my preference is towards keeping the parsing functions simple and I don't think the kernel needs to play the role of strict input verifier here as long as the only foot getting shot is the user's own.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

IMHO non-strict interface more likely hides bugs and could cause problems for future changes.

Here is only only one fatal bug - buffer overflow in sscanf because %s has no limit.

Ah, indeed. Can you please post a patch to fix that problem first?

Done.
Please see [PATCH] sched/core: fix buffer overflow in cgroup2 property cpu.max

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Strict validation could be done as more strict sscanf variant or some kind of extension for format string.

I don't necessarily disagree with you; however, what often ends up with these manually crafted parsing approach are 1. code which is unnecessarily difficult to follow 2. different subset of validations and parsing bugs (of course) everywhere.

Quoted 4 lines
Quoted 4 lines

Given the above, I tend to lean towards dump sscanf() parsing. If we wanna improve the situation, I think the right thing to do is either improving sscanf or introducing new helpers to parse these things rather than hand-crafting each site. It is really error-prone.

I'm playing with sscanf right now.

Both problems (integer overflows and matching end of string) are relatively easy to fix without breaking sane compatibility.

Quoted 6 lines

Always use a field width specifier with %s. Which is exactly what the proposed patch did IIRC.

Maybe that's something checkpatch could warn about.

This could be done mandatory.
In-kernel sscanf always requires width for "%[...]".