Prevent rt_sigqueueinfo and rt_tgsigqueueinfo from spoofing the signal code

Userland should be able to trust the pid and uid of the sender of a
signal if the si_code is SI_TKILL.

Unfortunately, the kernel has historically allowed sigqueueinfo() to
send any si_code at all (as long as it was negative - to distinguish it
from kernel-generated signals like SIGILL etc), so it could spoof a
SI_TKILL with incorrect siginfo values.

Happily, it looks like glibc has always set si_code to the appropriate
SI_QUEUE, so there are probably no actual user code that ever uses
anything but the appropriate SI_QUEUE flag.

So just tighten the check for si_code (we used to allow any negative
value), and add a (one-time) warning in case there are binaries out
there that might depend on using other si_code values.

Signed-off-by: Julien Tinnes <jln@google.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Julien Tinnes and committed by Linus Torvalds da48524e b52307ca

+12 -4
+12 -4
kernel/signal.c
··· 2421 2421 return -EFAULT; 2422 2422 2423 2423 /* Not even root can pretend to send signals from the kernel. 2424 - Nor can they impersonate a kill(), which adds source info. */ 2425 - if (info.si_code >= 0) 2424 + * Nor can they impersonate a kill()/tgkill(), which adds source info. 2425 + */ 2426 + if (info.si_code != SI_QUEUE) { 2427 + /* We used to allow any < 0 si_code */ 2428 + WARN_ON_ONCE(info.si_code < 0); 2426 2429 return -EPERM; 2430 + } 2427 2431 info.si_signo = sig; 2428 2432 2429 2433 /* POSIX.1b doesn't mention process groups. */ ··· 2441 2437 return -EINVAL; 2442 2438 2443 2439 /* Not even root can pretend to send signals from the kernel. 2444 - Nor can they impersonate a kill(), which adds source info. */ 2445 - if (info->si_code >= 0) 2440 + * Nor can they impersonate a kill()/tgkill(), which adds source info. 2441 + */ 2442 + if (info->si_code != SI_QUEUE) { 2443 + /* We used to allow any < 0 si_code */ 2444 + WARN_ON_ONCE(info->si_code < 0); 2446 2445 return -EPERM; 2446 + } 2447 2447 info->si_signo = sig; 2448 2448 2449 2449 return do_send_specific(tgid, pid, sig, info);