Commit 602fcf52 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Stefan Bader

signal/ptrace: Don't leak unitialized kernel memory with PTRACE_PEEK_SIGINFO

BugLink: https://bugs.launchpad.net/bugs/1836666

[ Upstream commit f6e2aa91 ]

Recently syzbot in conjunction with KMSAN reported that
ptrace_peek_siginfo can copy an uninitialized siginfo to userspace.
Inspecting ptrace_peek_siginfo confirms this.

The problem is that off when initialized from args.off can be
initialized to a negaive value.  At which point the "if (off >= 0)"
test to see if off became negative fails because off started off
negative.

Prevent the core problem by adding a variable found that is only true
if a siginfo is found and copied to a temporary in preparation for
being copied to userspace.

Prevent args.off from being truncated when being assigned to off by
testing that off is <= the maximum possible value of off.  Convert off
to an unsigned long so that we should not have to truncate args.off,
we have well defined overflow behavior so if we add another check we
won't risk fighting undefined compiler behavior, and so that we have a
type whose maximum value is easy to test for.

Cc: Andrei Vagin <avagin@gmail.com>
Cc: stable@vger.kernel.org
Reported-by: syzbot+0d602a1b0d8c95bdf299@syzkaller.appspotmail.com
Fixes: 84c751bd ("ptrace: add ability to retrieve signals without removing from a queue (v4)")
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent dc1f07b8
...@@ -673,6 +673,10 @@ static int ptrace_peek_siginfo(struct task_struct *child, ...@@ -673,6 +673,10 @@ static int ptrace_peek_siginfo(struct task_struct *child,
if (arg.nr < 0) if (arg.nr < 0)
return -EINVAL; return -EINVAL;
/* Ensure arg.off fits in an unsigned long */
if (arg.off > ULONG_MAX)
return 0;
if (arg.flags & PTRACE_PEEKSIGINFO_SHARED) if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
pending = &child->signal->shared_pending; pending = &child->signal->shared_pending;
else else
...@@ -680,18 +684,20 @@ static int ptrace_peek_siginfo(struct task_struct *child, ...@@ -680,18 +684,20 @@ static int ptrace_peek_siginfo(struct task_struct *child,
for (i = 0; i < arg.nr; ) { for (i = 0; i < arg.nr; ) {
siginfo_t info; siginfo_t info;
s32 off = arg.off + i; unsigned long off = arg.off + i;
bool found = false;
spin_lock_irq(&child->sighand->siglock); spin_lock_irq(&child->sighand->siglock);
list_for_each_entry(q, &pending->list, list) { list_for_each_entry(q, &pending->list, list) {
if (!off--) { if (!off--) {
found = true;
copy_siginfo(&info, &q->info); copy_siginfo(&info, &q->info);
break; break;
} }
} }
spin_unlock_irq(&child->sighand->siglock); spin_unlock_irq(&child->sighand->siglock);
if (off >= 0) /* beyond the end of the list */ if (!found) /* beyond the end of the list */
break; break;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment