Skip to content

Commit

Permalink
sched/fair: Null terminate buffer when updating tunable_scaling
Browse files Browse the repository at this point in the history
This patch null-terminates the temporary buffer in sched_scaling_write()
so kstrtouint() does not return failure and checks the value is valid.

Before:
  $ cat /sys/kernel/debug/sched/tunable_scaling
  1
  $ echo 0 > /sys/kernel/debug/sched/tunable_scaling
  -bash: echo: write error: Invalid argument
  $ cat /sys/kernel/debug/sched/tunable_scaling
  1

After:
  $ cat /sys/kernel/debug/sched/tunable_scaling
  1
  $ echo 0 > /sys/kernel/debug/sched/tunable_scaling
  $ cat /sys/kernel/debug/sched/tunable_scaling
  0
  $ echo 3 > /sys/kernel/debug/sched/tunable_scaling
  -bash: echo: write error: Invalid argument

Fixes: 8a99b68 ("sched: Move SCHED_DEBUG sysctl to debugfs")
Signed-off-by: Mel Gorman <mgorman@techsingularity.net>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Vincent Guittot <vincent.guittot@linaro.org>
Link: https://lore.kernel.org/r/20210927114635.GH3959@techsingularity.net
  • Loading branch information
Mel Gorman authored and Peter Zijlstra committed Oct 1, 2021
1 parent 2630cde commit 7030661
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion kernel/sched/debug.c
Original file line number Diff line number Diff line change
@@ -173,16 +173,22 @@ static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
char buf[16];
unsigned int scaling;

if (cnt > 15)
cnt = 15;

if (copy_from_user(&buf, ubuf, cnt))
return -EFAULT;
buf[cnt] = '\0';

if (kstrtouint(buf, 10, &sysctl_sched_tunable_scaling))
if (kstrtouint(buf, 10, &scaling))
return -EINVAL;

if (scaling >= SCHED_TUNABLESCALING_END)
return -EINVAL;

sysctl_sched_tunable_scaling = scaling;
if (sched_update_scaling())
return -EINVAL;

0 comments on commit 7030661

Please sign in to comment.