linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Cc: serge@hallyn.com,
	syzbot <syzbot+a9ac39bf55329e206219@syzkaller.appspotmail.com>,
	jmorris@namei.org, keescook@chromium.org,
	linux-kernel@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	syzkaller-bugs@googlegroups.com
Subject: Re: KASAN: use-after-free Read in task_is_descendant
Date: Thu, 25 Oct 2018 13:13:56 +0200	[thread overview]
Message-ID: <20181025111355.GA3725@redhat.com> (raw)
In-Reply-To: <201810250215.w9P2Fm2M078167@www262.sakura.ne.jp>

On 10/25, Tetsuo Handa wrote:
>
> Oleg Nesterov wrote:
> > On 10/22, Tetsuo Handa wrote:
> > > > And again, I do not know how/if yama ensures that child is rcu-protected, perhaps
> > > > task_is_descendant() needs to check pid_alive(child) right after rcu_read_lock() ?
> > >
> > > Since the caller (ptrace() path) called get_task_struct(child), child itself can't be
> > > released. Do we still need pid_alive(child) ?
> >
> > get_task_struct(child) can only ensure that this task_struct can't be freed.
>
> The report says that it is a use-after-free read at
>
>   walker = rcu_dereference(walker->real_parent);
>
> which means that walker was already released.

quite possibly I missed something, but I am not sure I understand your concerns...

So again, suppose that "child" is already dead. Its task_struct can't be freed,
but child->real_parent can point to the already freed memory.

This means that the 1st walker = rcu_dereference(walker->real_parent) is fine,
this simply reads the child->real_parent pointer, but on the second iteration

	walker = rcu_dereference(walker->real_parent);

reads the alredy freed memory.

> I wonder whether pid_alive() test helps.
>
> We can get
>
> [   40.620318] parent or walker is dead.
> [   40.624146] tracee is dead.
>
> messages using below patch and reproducer.

again, I do not understand, this all looks correct...

> ----------
> diff --git a/kernel/ptrace.c b/kernel/ptrace.c
> index 99cfddd..0d9d786 100644
> --- a/kernel/ptrace.c
> +++ b/kernel/ptrace.c
> @@ -385,6 +385,7 @@ static int ptrace_attach(struct task_struct *task, long request,
>  	if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
>  		goto out;
>
> +	schedule_timeout_killable(HZ);
>  	task_lock(task);
>  	retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
>  	task_unlock(task);
> diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> index ffda91a..a231ec6 100644
> --- a/security/yama/yama_lsm.c
> +++ b/security/yama/yama_lsm.c
> @@ -283,6 +283,11 @@ static int task_is_descendant(struct task_struct *parent,
>  		return 0;
>
>  	rcu_read_lock();
> +	if (!pid_alive(parent) || !pid_alive(walker)) {
> +		rcu_read_unlock();
> +		printk("parent or walker is dead.\n");

This is what we need to do, except I think we should change yama_ptrace_access_check().
And iiuc parent == current, pid_alive(parent) looks unnecessary. Although we need to
check ptracer_exception_found(), may be it needs some changes too.

And yes, task_is_descendant() can hit the dead child, if nothing else it can
be killed. This can explain the kasan report.

> @@ -315,6 +320,10 @@ static int ptracer_exception_found(struct task_struct *tracer,
>  	bool found = false;
>
>  	rcu_read_lock();
> +	if (!pid_alive(tracee)) {
> +		printk("tracee is dead.\n");
> +		goto unlock;

Sure, this is possible too.

> But since "child" has at least one reference, reading "child->real_parent" should
> be safe. Therefore, I think that bailing out due to pid_is_alive(child) == false
> (like above patch does) cannot avoid this problem...

Why?

OK. Lets ignore ptracer_exception_found() for the moment. Why do you think the
patch below can't help?

Oleg.

--- x/security/yama/yama_lsm.c
+++ x/security/yama/yama_lsm.c
@@ -368,7 +368,8 @@ static int yama_ptrace_access_check(stru
 			break;
 		case YAMA_SCOPE_RELATIONAL:
 			rcu_read_lock();
-			if (!task_is_descendant(current, child) &&
+			if (!pid_alive(child) ||
+			    !task_is_descendant(current, child) &&
 			    !ptracer_exception_found(current, child) &&
 			    !ns_capable(__task_cred(child)->user_ns, CAP_SYS_PTRACE))
 				rc = -EPERM;


  reply	other threads:[~2018-10-25 11:14 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-21  7:10 KASAN: use-after-free Read in task_is_descendant syzbot
2018-10-21  7:12 ` Tetsuo Handa
2018-10-22  9:54   ` Oleg Nesterov
2018-10-22 10:06     ` Tetsuo Handa
2018-10-22 13:46       ` Oleg Nesterov
2018-10-25  2:15         ` Tetsuo Handa
2018-10-25 11:13           ` Oleg Nesterov [this message]
2018-10-25 11:36             ` Kees Cook
2018-10-25 12:05               ` Oleg Nesterov
2018-10-25 11:47             ` Tetsuo Handa
2018-10-25 12:17               ` Oleg Nesterov
2018-10-25 13:01                 ` Oleg Nesterov
2018-10-26 16:09                   ` Kees Cook
2018-10-29 12:23                     ` Oleg Nesterov
2018-10-29 15:05                       ` yama: unsafe usage of ptrace_relation->tracer Oleg Nesterov
2019-01-10 11:05                         ` Tetsuo Handa
2019-01-10 18:47                           ` Kees Cook
2019-01-16 17:40                           ` Oleg Nesterov
2018-10-25 13:14                 ` KASAN: use-after-free Read in task_is_descendant Tetsuo Handa
2018-10-25 15:55                   ` Oleg Nesterov
2018-10-25 16:25                     ` Oleg Nesterov
2018-10-26 12:23                     ` Tetsuo Handa
2018-10-26 13:04                       ` Oleg Nesterov
2018-10-26 13:51                         ` Tetsuo Handa
2018-10-26 14:39                           ` Oleg Nesterov
2018-10-26 15:04                             ` Tetsuo Handa
2018-10-26 15:22                               ` Oleg Nesterov
2018-10-25  8:19     ` Kees Cook
2018-10-25 11:52       ` Oleg Nesterov
2018-11-10  3:25 ` syzbot
2018-11-10 11:46 ` syzbot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181025111355.GA3725@redhat.com \
    --to=oleg@redhat.com \
    --cc=jmorris@namei.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=serge@hallyn.com \
    --cc=syzbot+a9ac39bf55329e206219@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).