Skip to contentSkip to conversation

sched/rt: check integer overflow at usec to nsec conversion

PATCH New Konstantin Khlebnikov 2 participants 6 messages Feb 27, 2019 – Apr 19, 2019 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 sched/rt: check integer overflow at usec to nsec conversion

Example of unhandled overflows:

# echo 18446744073709651 > cpu.rt_runtime_us
# cat cpu.rt_runtime_us
99
# echo 18446744073709900 > cpu.rt_period_us
# cat cpu.rt_period_us
348

After this patch they will fail with -EINVAL.

---
 kernel/sched/rt.c |    5 +++++
 1 file changed, 5 insertions(+)
kernel/sched/rt.c +5 -0
index e4f398ad9e73..aa7ee3a0bf90 100644
@@ -2555,6 +2555,8 @@ int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
 	if (rt_runtime_us < 0)
 		rt_runtime = RUNTIME_INF;
+	else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
 
 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
 }
@@ -2575,6 +2577,9 @@ int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
 {
 	u64 rt_runtime, rt_period;
 
+	if (rt_period_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
+
 	rt_period = rt_period_us * NSEC_PER_USEC;
 	rt_runtime = tg->rt_bandwidth.rt_runtime;
 
#1 lore raw

PATCH 2/3 sched/core: handle overflow in cpu_shares_write_u64

Bit shift in scale_load() could overflow shares. This patch saturates it to MAX_SHARES like following sched_group_set_shares().

Example:

# echo 9223372036854776832 > cpu.shares
# cat cpu.shares

Before patch: 1024
After pattch: 262144

---
 kernel/sched/core.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/sched/core.c +2 -0
index d8d76a65cfdd..a626fbd9fdc7 100644
@@ -6502,6 +6502,8 @@ static void cpu_cgroup_attach(struct cgroup_taskset *tset)
 static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
 				struct cftype *cftype, u64 shareval)
 {
+	if (shareval > scale_load_down(ULONG_MAX))
+		shareval = MAX_SHARES;
 	return sched_group_set_shares(css_tg(css), scale_load(shareval));
 }
 
#2 lore raw

[tip:sched/core] sched/core: Handle overflow in cpu_shares_write_u64

sched/core: Handle overflow in cpu_shares_write_u64
Commit-ID:  5b61d50ab4ef590f5e1d4df15cd2cea5f5715308
Gitweb:     https://git.kernel.org/tip/5b61d50ab4ef590f5e1d4df15cd2cea5f5715308
Author:     Konstantin Khlebnikov <[email protected]>
AuthorDate: Wed, 27 Feb 2019 11:10:18 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 19 Apr 2019 13:42:10 +0200

sched/core: Handle overflow in cpu_shares_write_u64

Bit shift in scale_load() could overflow shares. This patch saturates it to MAX_SHARES like following sched_group_set_shares().

Example:

 # echo 9223372036854776832 > cpu.shares
 # cat cpu.shares

Before patch: 1024
After pattch: 262144

---
 kernel/sched/core.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/sched/core.c +2 -0
index fb09eaad1d3a..685b1541ce51 100644
@@ -6507,6 +6507,8 @@ static void cpu_cgroup_attach(struct cgroup_taskset *tset)
 static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
 				struct cftype *cftype, u64 shareval)
 {
+	if (shareval > scale_load_down(ULONG_MAX))
+		shareval = MAX_SHARES;
 	return sched_group_set_shares(css_tg(css), scale_load(shareval));
 }
 
#3 lore raw

PATCH 3/3 sched/core: check quota and period overflow at usec to nsec conversion

In reply to Konstantin Khlebnikov

Large values could overflow u64 and pass following sanity checks.

# echo 18446744073750000 > cpu.cfs_period_us
# cat cpu.cfs_period_us
40448
# echo 18446744073750000 > cpu.cfs_quota_us
# cat cpu.cfs_quota_us
40448

After this patch they will fail with -EINVAL.

---
 kernel/sched/core.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/core.c +6 -1
index a626fbd9fdc7..5f46aa335b28 100644
@@ -6606,8 +6606,10 @@ int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
 	period = ktime_to_ns(tg->cfs_bandwidth.period);
 	if (cfs_quota_us < 0)
 		quota = RUNTIME_INF;
-	else
+	else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
 		quota = (u64)cfs_quota_us * NSEC_PER_USEC;
+	else
+		return -EINVAL;
 
 	return tg_set_cfs_bandwidth(tg, period, quota);
 }
@@ -6629,6 +6631,9 @@ int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
 {
 	u64 quota, period;
 
+	if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
+
 	period = (u64)cfs_period_us * NSEC_PER_USEC;
 	quota = tg->cfs_bandwidth.quota;
 
#4 lore raw

[tip:sched/core] sched/core: Check quota and period overflow at usec to nsec conversion

sched/core: Check quota and period overflow at usec to nsec conversion
Commit-ID:  1a8b4540db732ca16c9e43ac7c08b1b8f0b252d8
Gitweb:     https://git.kernel.org/tip/1a8b4540db732ca16c9e43ac7c08b1b8f0b252d8
Author:     Konstantin Khlebnikov <[email protected]>
AuthorDate: Wed, 27 Feb 2019 11:10:20 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 19 Apr 2019 13:42:10 +0200

sched/core: Check quota and period overflow at usec to nsec conversion

Large values could overflow u64 and pass following sanity checks.

 # echo 18446744073750000 > cpu.cfs_period_us
 # cat cpu.cfs_period_us
 40448
 # echo 18446744073750000 > cpu.cfs_quota_us
 # cat cpu.cfs_quota_us
 40448

After this patch they will fail with -EINVAL.

---
 kernel/sched/core.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/core.c +6 -1
index 685b1541ce51..de8ab411826c 100644
@@ -6611,8 +6611,10 @@ static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
 	period = ktime_to_ns(tg->cfs_bandwidth.period);
 	if (cfs_quota_us < 0)
 		quota = RUNTIME_INF;
-	else
+	else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
 		quota = (u64)cfs_quota_us * NSEC_PER_USEC;
+	else
+		return -EINVAL;
 
 	return tg_set_cfs_bandwidth(tg, period, quota);
 }
@@ -6634,6 +6636,9 @@ static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
 {
 	u64 quota, period;
 
+	if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
+
 	period = (u64)cfs_period_us * NSEC_PER_USEC;
 	quota = tg->cfs_bandwidth.quota;
 
#5 lore raw

[tip:sched/core] sched/rt: Check integer overflow at usec to nsec conversion

In reply to Konstantin Khlebnikov
sched/rt: Check integer overflow at usec to nsec conversion
Commit-ID:  1a010e29cfa00fee2888fd2fd4983f848cbafb58
Gitweb:     https://git.kernel.org/tip/1a010e29cfa00fee2888fd2fd4983f848cbafb58
Author:     Konstantin Khlebnikov <[email protected]>
AuthorDate: Wed, 27 Feb 2019 11:10:17 +0300
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 19 Apr 2019 13:42:09 +0200

sched/rt: Check integer overflow at usec to nsec conversion

Example of unhandled overflows:

 # echo 18446744073709651 > cpu.rt_runtime_us
 # cat cpu.rt_runtime_us
 99
 # echo 18446744073709900 > cpu.rt_period_us
 # cat cpu.rt_period_us
 348

After this patch they will fail with -EINVAL.

---
 kernel/sched/rt.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/sched/rt.c +5 -0
index 90fa23d36565..1e6b909dca36 100644
@@ -2555,6 +2555,8 @@ int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
 	if (rt_runtime_us < 0)
 		rt_runtime = RUNTIME_INF;
+	else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
 
 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
 }
@@ -2575,6 +2577,9 @@ int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
 {
 	u64 rt_runtime, rt_period;
 
+	if (rt_period_us > U64_MAX / NSEC_PER_USEC)
+		return -EINVAL;
+
 	rt_period = rt_period_us * NSEC_PER_USEC;
 	rt_runtime = tg->rt_bandwidth.rt_runtime;