LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter
@ 2015-10-12 15:33 Amanieu d'Antras
  2015-10-12 15:54 ` Oleg Nesterov
  0 siblings, 1 reply; 5+ messages in thread
From: Amanieu d'Antras @ 2015-10-12 15:33 UTC (permalink / raw
  To: linux-kernel
  Cc: Oleg Nesterov, Andrew Morton, Paul E. McKenney,
	Amanieu d'Antras

rt_sigqueueinfo and rt_tgsigqueueinfo check the value of si_code to
prevent a process from spoofing a kernel-generated signal or one
generated by kill/tgkill.

Unfortunately this check failed to take into account the fact that
the si_code value seen by a user process is only the low 16 bits of
the value in the kernel. It was still possible to spoof any si_code
by ORing 0xffff into the top 16 bits.

The check is tightened by checking the value of si_code that will
be seen by a user program instead of the one in the kernel.

Signed-off-by: Amanieu d'Antras <amanieu@gmail.com>
---
 kernel/signal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/signal.c b/kernel/signal.c
index 0f6bbbe..f3d4f39 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2989,7 +2989,7 @@ static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
 	/* Not even root can pretend to send signals from the kernel.
 	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
 	 */
-	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
+	if (((short)info->si_code >= 0 || (short)info->si_code == SI_TKILL) &&
 	    (task_pid_vnr(current) != pid))
 		return -EPERM;
 
@@ -3037,7 +3037,7 @@ static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
 	/* Not even root can pretend to send signals from the kernel.
 	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
 	 */
-	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
+	if (((short)info->si_code >= 0 || (short)info->si_code == SI_TKILL) &&
 	    (task_pid_vnr(current) != pid))
 		return -EPERM;
 
-- 
2.6.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter
  2015-10-12 15:33 [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter Amanieu d'Antras
@ 2015-10-12 15:54 ` Oleg Nesterov
  2015-10-12 16:37   ` Amanieu d'Antras
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2015-10-12 15:54 UTC (permalink / raw
  To: Amanieu d'Antras; +Cc: linux-kernel, Andrew Morton, Paul E. McKenney

On 10/12, Amanieu d'Antras wrote:
>
> rt_sigqueueinfo and rt_tgsigqueueinfo check the value of si_code to
> prevent a process from spoofing a kernel-generated signal or one
> generated by kill/tgkill.
>
> Unfortunately this check failed to take into account the fact that
> the si_code value seen by a user process is only the low 16 bits of
> the value in the kernel. It was still possible to spoof any si_code
> by ORing 0xffff into the top 16 bits.

Confused...

> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -2989,7 +2989,7 @@ static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
>  	/* Not even root can pretend to send signals from the kernel.
>  	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
>  	 */
> -	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
> +	if (((short)info->si_code >= 0 || (short)info->si_code == SI_TKILL) &&
>  	    (task_pid_vnr(current) != pid))
>  		return -EPERM;
>
> @@ -3037,7 +3037,7 @@ static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
>  	/* Not even root can pretend to send signals from the kernel.
>  	 * Nor can they impersonate a kill()/tgkill(), which adds source info.
>  	 */
> -	if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
> +	if (((short)info->si_code >= 0 || (short)info->si_code == SI_TKILL) &&
>  	    (task_pid_vnr(current) != pid))
>  		return -EPERM;

Yes, copy_siginfo_to_user() does __put_user((short)from->si_code).
But SI_FROMUSER/SI_FROMKERNEL are internal kernel checks, we mostly
use them in copy_siginfo_to_user().

And note that if ->si_code < 0 we simply do __copy_to_user(), so
userspace can't see something which looks like "from kernel", in
this case we do not truncate ->si_code.

So I do not think this patch is right.

Oleg.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter
  2015-10-12 15:54 ` Oleg Nesterov
@ 2015-10-12 16:37   ` Amanieu d'Antras
  2015-10-13 14:54     ` Oleg Nesterov
  0 siblings, 1 reply; 5+ messages in thread
From: Amanieu d'Antras @ 2015-10-12 16:37 UTC (permalink / raw
  To: Oleg Nesterov; +Cc: linux-kernel, Andrew Morton, Paul E. McKenney

On Mon, Oct 12, 2015 at 4:54 PM, Oleg Nesterov <oleg@redhat.com> wrote:
> Yes, copy_siginfo_to_user() does __put_user((short)from->si_code).
> But SI_FROMUSER/SI_FROMKERNEL are internal kernel checks, we mostly
> use them in copy_siginfo_to_user().
>
> And note that if ->si_code < 0 we simply do __copy_to_user(), so
> userspace can't see something which looks like "from kernel", in
> this case we do not truncate ->si_code.

Ah my bad, I seem to have missed that case. So copy_siginfo_to_user
seems to be safe, but it is still an issue for copy_siginfo_to_user32
which doesn't have this check.

Maybe this should be fixed in the compat code instead?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter
  2015-10-12 16:37   ` Amanieu d'Antras
@ 2015-10-13 14:54     ` Oleg Nesterov
  2015-10-13 15:40       ` Amanieu d'Antras
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2015-10-13 14:54 UTC (permalink / raw
  To: Amanieu d'Antras; +Cc: linux-kernel, Andrew Morton, Paul E. McKenney

On 10/12, Amanieu d'Antras wrote:
>
> On Mon, Oct 12, 2015 at 4:54 PM, Oleg Nesterov <oleg@redhat.com> wrote:
> > Yes, copy_siginfo_to_user() does __put_user((short)from->si_code).
> > But SI_FROMUSER/SI_FROMKERNEL are internal kernel checks, we mostly
> > use them in copy_siginfo_to_user().
> >
> > And note that if ->si_code < 0 we simply do __copy_to_user(), so
> > userspace can't see something which looks like "from kernel", in
> > this case we do not truncate ->si_code.
>
> Ah my bad, I seem to have missed that case. So copy_siginfo_to_user
> seems to be safe, but it is still an issue for copy_siginfo_to_user32
> which doesn't have this check.
>
> Maybe this should be fixed in the compat code instead?

I agree, this doesn't look right... But I'm afraid it is too late to
change this, this logic predates the git history.

And unless I missed something copy_siginfo_to_user32() assumes that
"from" was filled by copy_siginfo_from_user32(), but this is not true
if the sender is 64bit task. And this all doesn't match the behaviour
with 32bit kernel.

Personally, I think we should leave this ancient code alone. But I
agree that something like below looks right... I dunno.

And I have no idea why copy_siginfo_to_user32() abuses '>> 16', it
seems that "si_code & __SI_MASK" could work just fine.

Oleg.

--- x/arch/x86/kernel/signal_compat.c
+++ x/arch/x86/kernel/signal_compat.c
@@ -17,13 +17,14 @@
 		   3 ints plus the relevant union member.  */
 		put_user_ex(from->si_signo, &to->si_signo);
 		put_user_ex(from->si_errno, &to->si_errno);
-		put_user_ex((short)from->si_code, &to->si_code);
 
 		if (from->si_code < 0) {
+			put_user_ex(from->si_code, &to->si_code);
 			put_user_ex(from->si_pid, &to->si_pid);
 			put_user_ex(from->si_uid, &to->si_uid);
 			put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
 		} else {
+			put_user_ex((short)from->si_code, &to->si_code);
 			/*
 			 * First 32bits of unions are always present:
 			 * si_pid === si_band === si_tid === si_addr(LS half)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter
  2015-10-13 14:54     ` Oleg Nesterov
@ 2015-10-13 15:40       ` Amanieu d'Antras
  0 siblings, 0 replies; 5+ messages in thread
From: Amanieu d'Antras @ 2015-10-13 15:40 UTC (permalink / raw
  To: Oleg Nesterov; +Cc: linux-kernel, Andrew Morton, Paul E. McKenney

On Tue, Oct 13, 2015 at 3:54 PM, Oleg Nesterov <oleg@redhat.com> wrote:
> I agree, this doesn't look right... But I'm afraid it is too late to
> change this, this logic predates the git history.
>
> And unless I missed something copy_siginfo_to_user32() assumes that
> "from" was filled by copy_siginfo_from_user32(), but this is not true
> if the sender is 64bit task. And this all doesn't match the behaviour
> with 32bit kernel.
>
> Personally, I think we should leave this ancient code alone. But I
> agree that something like below looks right... I dunno.

I am currently working on another patch set that updates
copy_siginfo_{to,from}_user32 to match the behavior of 32-bit kernels.
I will fix the si_code encoding there with a change similar to yours.

Amanieu d'Antras

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-10-13 15:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-12 15:33 [PATCH] signal: Make the si_code check in rt_[tg]sigqueueinfo stricter Amanieu d'Antras
2015-10-12 15:54 ` Oleg Nesterov
2015-10-12 16:37   ` Amanieu d'Antras
2015-10-13 14:54     ` Oleg Nesterov
2015-10-13 15:40       ` Amanieu d'Antras

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).