linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
To: Shakeel Butt <shakeelb@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	David Rientjes <rientjes@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Roman Gushchin <guro@fb.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] mm, oom: fix use-after-free in oom_kill_process
Date: Sat, 19 Jan 2019 12:35:47 +0900	[thread overview]
Message-ID: <02f74c47-4f35-3d59-f767-268844cb875e@i-love.sakura.ne.jp> (raw)
In-Reply-To: <20190119005022.61321-1-shakeelb@google.com>

On 2019/01/19 9:50, Shakeel Butt wrote:
> On looking further it seems like the process selected to be oom-killed
> has exited even before reaching read_lock(&tasklist_lock) in
> oom_kill_process(). More specifically the tsk->usage is 1 which is due
> to get_task_struct() in oom_evaluate_task() and the put_task_struct
> within for_each_thread() frees the tsk and for_each_thread() tries to
> access the tsk. The easiest fix is to do get/put across the
> for_each_thread() on the selected task.

Good catch. p->usage can become 1 while printk()ing a lot at dump_header().

> @@ -981,6 +981,13 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
>  	 * still freeing memory.
>  	 */
>  	read_lock(&tasklist_lock);
> +
> +	/*
> +	 * The task 'p' might have already exited before reaching here. The
> +	 * put_task_struct() will free task_struct 'p' while the loop still try
> +	 * to access the field of 'p', so, get an extra reference.
> +	 */
> +	get_task_struct(p);
>  	for_each_thread(p, t) {
>  		list_for_each_entry(child, &t->children, sibling) {
>  			unsigned int child_points;
> @@ -1000,6 +1007,7 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
>  			}
>  		}
>  	}
> +	put_task_struct(p);

Moving put_task_struct(p) to after read_unlock(&tasklist_lock) will reduce
latency of a write_lock(&tasklist_lock) waiter.

>  	read_unlock(&tasklist_lock);
>  
>  	/*
> 

By the way, p->usage is already 1 implies that p->mm == NULL due to already
completed exit_mm(p). Then, process_shares_mm(child, p->mm) might fail to
return true for some of children. Not critical but might lead to unnecessary
oom_badness() calls for child selection. Maybe we want to use same logic
__oom_kill_process() uses (i.e. bail out if find_task_lock_mm(p) failed)?

  parent reply	other threads:[~2019-01-19  3:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-19  0:50 [RFC PATCH] mm, oom: fix use-after-free in oom_kill_process Shakeel Butt
2019-01-19  1:58 ` Roman Gushchin
2019-01-20 20:20   ` Shakeel Butt
2019-01-19  3:35 ` Tetsuo Handa [this message]
2019-01-20 20:23   ` Shakeel Butt
2019-01-19  7:09 ` Michal Hocko
2019-01-20 20:24   ` Shakeel Butt
2019-01-21  9:19   ` Michal Hocko

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=02f74c47-4f35-3d59-f767-268844cb875e@i-love.sakura.ne.jp \
    --to=penguin-kernel@i-love.sakura.ne.jp \
    --cc=akpm@linux-foundation.org \
    --cc=guro@fb.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=rientjes@google.com \
    --cc=shakeelb@google.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).