From patchwork Wed Oct 12 09:57:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 77536 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp373516qge; Wed, 12 Oct 2016 02:58:46 -0700 (PDT) X-Received: by 10.99.36.65 with SMTP id k62mr336780pgk.88.1476266326443; Wed, 12 Oct 2016 02:58:46 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u5si8514288pfa.187.2016.10.12.02.58.46; Wed, 12 Oct 2016 02:58:46 -0700 (PDT) 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; 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 S932654AbcJLJ6o (ORCPT + 27 others); Wed, 12 Oct 2016 05:58:44 -0400 Received: from foss.arm.com ([217.140.101.70]:56246 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932591AbcJLJ6g (ORCPT ); Wed, 12 Oct 2016 05:58:36 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3080216; Wed, 12 Oct 2016 02:57:09 -0700 (PDT) Received: from e104818-lin.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2F6023F318; Wed, 12 Oct 2016 02:57:08 -0700 (PDT) From: Catalin Marinas To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Andy Lutomirski , CAI Qian Subject: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning Date: Wed, 12 Oct 2016 10:57:03 +0100 Message-Id: <1476266223-14325-1-git-send-email-catalin.marinas@arm.com> X-Mailer: git-send-email 2.1.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning. Fixes: 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") Cc: Andrew Morton Cc: Andy Lutomirski Cc: CAI Qian Reported-by: CAI Qian Signed-off-by: Catalin Marinas --- This was reported in a subsequent comment here: https://bugzilla.kernel.org/show_bug.cgi?id=173901 However, the original bugzilla entry doesn't look related to task stack freeing as it was first reported on 4.8-rc8. Andy, sorry for cc'ing you to bugzilla, please feel free to remove your email from the bug above (I can't seem to be able to do it). mm/kmemleak.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index a5e453cf05c4..e5355a5b423f 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void) read_lock(&tasklist_lock); do_each_thread(g, p) { - scan_block(task_stack_page(p), task_stack_page(p) + - THREAD_SIZE, NULL); + void *stack = try_get_task_stack(p); + if (stack) { + scan_block(stack, stack + THREAD_SIZE, NULL); + put_task_stack(p); + } } while_each_thread(g, p); read_unlock(&tasklist_lock); }