Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp3667715ima; Mon, 4 Feb 2019 03:07:45 -0800 (PST) X-Google-Smtp-Source: AHgI3IZuAfXod8FC+VC42/qRvu/nP23bIAgd4jdC5eFyLyndvgF8XdKcDK43Lt2aAzRTpH4qz7E9 X-Received: by 2002:a63:e447:: with SMTP id i7mr8678146pgk.273.1549278465187; Mon, 04 Feb 2019 03:07:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549278465; cv=none; d=google.com; s=arc-20160816; b=ZEMFNouZsVfME+pV3lwUZL95tmsEVYg0dTG+J6G6NTAVbZml4HgbEIiG/pCW/bTUAw ussdEsVirutOpbKODXQ5DWT4+XsieB9GjUXsvbHRRQHGgMcMi5z63V7CcH4XUJi7Flbk w0gPzbU+omIrMZCa5WCrCrvR1i3rVO/5njanqIKyXL4twOivpp05WyjNf9zNvjseJSb2 xa4zhHv5hLg1jhYehOOt8u8cgCFal5EKPkb5RtD6Kg5A3DWzWPcczoHxS7oQdKi7EuqQ dTBuV62CS+FA+rxQe9NypS5CWZzQqaufRUBlWmGmGIquX5p+ATesjMhwBXIn8HlX6rYA THmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=O9kwz9Nox4W+8DKKvB4HYtlHTun1NsmJkL0/6/12e5o=; b=piTlUBTCNPNK1Vfxt5HCGbWLq2jhY8JbK7FchXVrqh1hYrHO/vHmAznoS5f+GnVmIz M1w+CELn7+bEw9ZkdIMWCU2grSmEUBSNWvSgFTUUNRwP7zMkdk3alGH9Q+by7kpHJTmB FPybZ3nJM0YrizhufZ8/PYX71csNydFkmcHc4cU5yWwrHecrytF4vqjLubTOAbEBY22m G/MjCE8dn6bsU+7F14jBnajLBc77RhyByi5UnI7Fwni4XDLZvveexCnR6CzS4IKbpx5W cbExd+aDhQJeXksLxloAfrCfs1ChvVJ6QZlWFSZsqZPAEU2vWMKTHHkrg8hOiAhsIxnj 9MBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2aAeIevC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b3si9369565pld.282.2019.02.04.03.07.28; Mon, 04 Feb 2019 03:07:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2aAeIevC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730881AbfBDKoC (ORCPT + 99 others); Mon, 4 Feb 2019 05:44:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:41728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730863AbfBDKn6 (ORCPT ); Mon, 4 Feb 2019 05:43:58 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B87112075B; Mon, 4 Feb 2019 10:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549277037; bh=NU4hriu1C264Y4njVsccF64wn8K47m7xIDmj8xelh3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2aAeIevCfGT8wt4Ve1pwSDdAGsVb/3gtOLJB/jFlmV5DG/FwjVPGM+PZD4vBxmg9k R/rvR1t3XFYFv+JZ20MYIGhK2dDna/G2jMTUTxSmB3PCi7l5Bm2JzK8VsjzY0GWDcJ 5URi4q2XoTiqMGx0xYC7iWxQ3MsmWveNZDibSS8k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+7fbbfa368521945f0e3d@syzkaller.appspotmail.com, Shakeel Butt , Roman Gushchin , Michal Hocko , David Rientjes , Johannes Weiner , Tetsuo Handa , Andrew Morton , Linus Torvalds Subject: [PATCH 4.9 24/30] mm, oom: fix use-after-free in oom_kill_process Date: Mon, 4 Feb 2019 11:37:02 +0100 Message-Id: <20190204103609.741477620@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190204103605.271746870@linuxfoundation.org> References: <20190204103605.271746870@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shakeel Butt commit cefc7ef3c87d02fc9307835868ff721ea12cc597 upstream. Syzbot instance running on upstream kernel found a use-after-free bug in oom_kill_process. On further inspection 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. Now the next question is should we continue with the oom-kill as the previously selected task has exited? However before adding more complexity and heuristics, let's answer why we even look at the children of oom-kill selected task? The select_bad_process() has already selected the worst process in the system/memcg. Due to race, the selected process might not be the worst at the kill time but does that matter? The userspace can use the oom_score_adj interface to prefer children to be killed before the parent. I looked at the history but it seems like this is there before git history. Link: http://lkml.kernel.org/r/20190121215850.221745-1-shakeelb@google.com Reported-by: syzbot+7fbbfa368521945f0e3d@syzkaller.appspotmail.com Fixes: 6b0c81b3be11 ("mm, oom: reduce dependency on tasklist_lock") Signed-off-by: Shakeel Butt Reviewed-by: Roman Gushchin Acked-by: Michal Hocko Cc: David Rientjes Cc: Johannes Weiner Cc: Tetsuo Handa Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/oom_kill.c | 8 ++++++++ 1 file changed, 8 insertions(+) --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -861,6 +861,13 @@ static void oom_kill_process(struct oom_ * 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; @@ -880,6 +887,7 @@ static void oom_kill_process(struct oom_ } } } + put_task_struct(p); read_unlock(&tasklist_lock); p = find_lock_task_mm(victim);