Skip to contentSkip to conversation

sched: Disallow sched_attr::sched_policy < 0

DISCUSSION Dave Jones 5 participants 7 messages Jun 2, 2014 – Jun 3, 2014 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Dave Jones

author
#1 lore raw
Quoted 15 lines · Linux Kernel

sched: Disallow sched_attr::sched_policy < 0

The scheduler uses policy=-1 to preserve the current policy state to implement sys_sched_setparam(), this got exposed to userspace by accident through sys_sched_setattr(), cure this.

---
 kernel/sched/core.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/sched/core.c +0 -0
index f2205f0..cdefcf7 100644

kernel/sched/core.c SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, · line 3670

Quoted 9 lines
kernel/sched/core.c +3 -0
@@ -3662,6 +3662,9 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
 	if (retval)
 		return retval;
 
+	if (attr.sched_policy < 0)
+		return -EINVAL;
+
 	rcu_read_lock();
 	retval = -ESRCH;
 	p = find_process_by_pid(pid);

Todays coverity run picked up..

3687

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1219934: Unsigned compared against 0 (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "attr.sched_policy < 0U".

3688 if (attr.sched_policy < 0)
3689 return -EINVAL;

#2 lore raw
Quoted 29 lines · Dave Jones
Quoted 15 lines · Linux Kernel

sched: Disallow sched_attr::sched_policy < 0

The scheduler uses policy=-1 to preserve the current policy state to implement sys_sched_setparam(), this got exposed to userspace by accident through sys_sched_setattr(), cure this.

---
 kernel/sched/core.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/sched/core.c +0 -0
index f2205f0..cdefcf7 100644

kernel/sched/core.c SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, · line 3670

Quoted 9 lines
kernel/sched/core.c +3 -0
@@ -3662,6 +3662,9 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
 	if (retval)
 		return retval;
 
+	if (attr.sched_policy < 0)
+		return -EINVAL;
+
 	rcu_read_lock();
 	retval = -ESRCH;
 	p = find_process_by_pid(pid);

Todays coverity run picked up..

Quoted 6 lines

3687

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1219934: Unsigned compared against 0 (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "attr.sched_policy < 0U".

3688 if (attr.sched_policy < 0)
3689 return -EINVAL;

Once upon a time GCC also did warns like that, but my compiler is silent :-(

Yes, that needs fixing..

#3 lore raw
Quoted 33 lines · Peter Zijlstra
Quoted 32 lines · Dave Jones
Quoted 15 lines · Linux Kernel

sched: Disallow sched_attr::sched_policy < 0

The scheduler uses policy=-1 to preserve the current policy state to implement sys_sched_setparam(), this got exposed to userspace by accident through sys_sched_setattr(), cure this.

---
 kernel/sched/core.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/sched/core.c +0 -0
index f2205f0..cdefcf7 100644

kernel/sched/core.c SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, · line 3670

Quoted 9 lines
kernel/sched/core.c +3 -0
@@ -3662,6 +3662,9 @@ SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
   if (retval)
           return retval;
 
+  if (attr.sched_policy < 0)
+          return -EINVAL;
+
   rcu_read_lock();
   retval = -ESRCH;
   p = find_process_by_pid(pid);

Todays coverity run picked up..

3687

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 1219934: Unsigned compared against 0 (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "attr.sched_policy < 0U".

Quoted 8 lines
Quoted 3 lines

3688 if (attr.sched_policy < 0)
3689 return -EINVAL;

Once upon a time GCC also did warns like that, but my compiler is silent :-(

Yes, that needs fixing..

I sent already a patch for that...

Signature · 2 lines
Thanks,
//richard
#4 lore raw
Quoted 6 lines · Richard Weinberger
Quoted 4 lines

Once upon a time GCC also did warns like that, but my compiler is silent :-(

Yes, that needs fixing..

I sent already a patch for that...

I found it.. thanks again :-)

#5 lore raw

Am 03.06.2014 10:32, schrieb Peter Zijlstra:

Quoted 3 lines · Richard Weinberger
Quoted 2 lines
Quoted 2 lines

Once upon a time GCC also did warns like that, but my compiler is silent :-(

-Wtype-limits is what you're looking for.

/me currently builds some kernel configs to find out how much noise it triggers...

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Yes, that needs fixing..

I sent already a patch for that...

I found it.. thanks again :-)

:-)

Thanks,
//richard

author
#6 lore raw
Quoted 8 lines · Richard Weinberger

Am 03.06.2014 10:32, schrieb Peter Zijlstra:

Quoted 3 lines · Richard Weinberger
Quoted 2 lines
Quoted 2 lines

Once upon a time GCC also did warns like that, but my compiler is silent :-(

-Wtype-limits is what you're looking for.

/me currently builds some kernel configs to find out how much noise it triggers...

Probably quite a bit I'd bet. There's a load of similar bugs in coverity's db. Some of them look benign, and are probably there just in case someone ever changes the type of a var, but it's non-obvious sometimes if the values a function receives can ever actually be < 0

Dave

#7 lore raw
In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra

Once upon a time GCC also did warns like that, but my compiler is silent :-(

You should be happy. The gcc warnings were shit.

Iirc, gcc literally at one point warned about things like

unsigned int i;

if (i < 5)

because that's comparing an unsigned type ("i") with an expression having a signed type ("5"). Yes, technically true, but it's not actually a useful warning.

That got fixed pretty quickly, but I think gcc *still* warns about things like

    unsigned int i;

    if (i >= 0 && i <= 6)
        ...

which is actually a very valid thing to do, and is commonly the result of using a range-checking macro, or in general writing code so that it is robust and doesn't care about the actual underlying type.

Warnings about robust code are f*cking broken, and easily worse than not having the warning at all. Because it results in people removing the range check.

Btw, -Wsign-compare still complains about

int i;

   if (i < 0 || i > sizeof(i))
       return error;

which is another example of a f*cking broken warning. There is no way to avoid that warning without making the code worse. That code is _correct_, dammit, and anybody who thinks it should warn (or the programmer should cast the sizeof to "int") is a tool and a moron.

End result: disabling "-Wsign-compare" is thus the only correct thing to do. Sadly compiler writers don't seem to care too deeply about the sanity of their warnings.

Linus