Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp331516ybe; Tue, 3 Sep 2019 23:52:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqz0cAh2slcBUmZ+aPcD4I9jL7/kxtA0Y2DOYXlzfmDizlvOCHgGHcvK15u9L8Y4wetjfsaX X-Received: by 2002:a62:ee0e:: with SMTP id e14mr45594015pfi.31.1567579979247; Tue, 03 Sep 2019 23:52:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567579979; cv=none; d=google.com; s=arc-20160816; b=UuljGDlCC8MUzkXgybNkdQ9+i8DmeQKJE42KgY/rfaKaLzTd2cRKKdmfq7yW/Wo5mM kO4VU/w4PTbRer/HGXT0WCl9R72BD8R9U3h6Ko9FKhcvs9Z3TpbL8E+vT7J9QZTqfKXG Co9AMQ9WT/eAxvY/Plz08DXQhwK1IesEYpNAX+HNQZf5gereqMYqUpxXM7W6BdJQZe4d momZHAFSHH1FuULBmgZT1GXZZI8fsoGhPWh9k+l6CX9F8D5KO46jdX9b/YqV7UQ5OT66 OPFBqKfWghO3wFOHNqQN6QtfiZYoIGYtHGXhsaRaf9FUmucraKvdc4KlGu1PaM/fA7C4 Zypw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=mCrWLrvxNNwo0KPUL54vUucBEotR+pBRYrc2zuw/3wM=; b=VJ1SR679PgQYm/fBrvCWXBNyquckos4D8OXL9eib9kzWmfjms/y78skJNn59bTGHQl SyEM1tw3u4NK4Yj6N0AWIt5V4EJcHe2sbQTcya4cBl8ffJg5lWut9MDLNOyAoPhQbvu5 5IwpfLQeiw1+/TqxWxwjXF1rxt1I9z/6EUq1OODbdzapgOlaJyuGGt6N0FDryA5tS6mU uI1HAWto73G1Aj+PSVuCSBdWM9i2r1fEnBgoonDnfJVRd4sUzcKv7WhWgXdKNAmIiGXv i+27QWgLTwgSg9oIh2dBw/6BN/yrryLiN8fZWm4uFulbMcu98AGZBgHegi+vJcSKCzAX QatQ== ARC-Authentication-Results: i=1; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l184si16492542pge.286.2019.09.03.23.52.42; Tue, 03 Sep 2019 23:52:59 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728700AbfIDGvn (ORCPT + 99 others); Wed, 4 Sep 2019 02:51:43 -0400 Received: from mailgw02.mediatek.com ([210.61.82.184]:5894 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725267AbfIDGvn (ORCPT ); Wed, 4 Sep 2019 02:51:43 -0400 X-UUID: d6949eddd6d848458f0e91f382beab44-20190904 X-UUID: d6949eddd6d848458f0e91f382beab44-20190904 Received: from mtkexhb02.mediatek.inc [(172.21.101.103)] by mailgw02.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 4046662; Wed, 04 Sep 2019 14:51:38 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Wed, 4 Sep 2019 14:51:35 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Wed, 4 Sep 2019 14:51:35 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , Andrew Morton , Martin Schwidefsky , Arnd Bergmann CC: , , , , , , Walter Wu Subject: [PATCH 1/2] mm/kasan: dump alloc/free stack for page allocator Date: Wed, 4 Sep 2019 14:51:33 +0800 Message-ID: <20190904065133.20268-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-TM-SNTS-SMTP: E693BB34C42D4B73B3B2B12EEB54C8F30BF059021EA46B52BEF177F393CF9F522000:8 X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch is KASAN report adds the alloc/free stacks for page allocator in order to help programmer to see memory corruption caused by page. By default, KASAN doesn't record alloc/free stack for page allocator. It is difficult to fix up page use-after-free issue. This feature depends on page owner to record the last stack of pages. It is very helpful for solving the page use-after-free or out-of-bound. KASAN report will show the last stack of page, it may be: a) If page is in-use state, then it prints alloc stack. It is useful to fix up page out-of-bound issue. BUG: KASAN: slab-out-of-bounds in kmalloc_pagealloc_oob_right+0x88/0x90 Write of size 1 at addr ffffffc0d64ea00a by task cat/115 ... Allocation stack of page: prep_new_page+0x1a0/0x1d8 get_page_from_freelist+0xd78/0x2748 __alloc_pages_nodemask+0x1d4/0x1978 kmalloc_order+0x28/0x58 kmalloc_order_trace+0x28/0xe0 kmalloc_pagealloc_oob_right+0x2c/0x90 b) If page is freed state, then it prints free stack. It is useful to fix up page use-after-free issue. BUG: KASAN: use-after-free in kmalloc_pagealloc_uaf+0x70/0x80 Write of size 1 at addr ffffffc0d651c000 by task cat/115 ... Free stack of page: kasan_free_pages+0x68/0x70 __free_pages_ok+0x3c0/0x1328 __free_pages+0x50/0x78 kfree+0x1c4/0x250 kmalloc_pagealloc_uaf+0x38/0x80 This has been discussed, please refer below link. https://bugzilla.kernel.org/show_bug.cgi?id=203967 Signed-off-by: Walter Wu --- lib/Kconfig.kasan | 9 +++++++++ mm/kasan/common.c | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index 4fafba1a923b..ba17f706b5f8 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -135,6 +135,15 @@ config KASAN_S390_4_LEVEL_PAGING to 3TB of RAM with KASan enabled). This options allows to force 4-level paging instead. +config KASAN_DUMP_PAGE + bool "Dump the page last stack information" + depends on KASAN && PAGE_OWNER + help + By default, KASAN doesn't record alloc/free stack for page allocator. + It is difficult to fix up page use-after-free issue. + This feature depends on page owner to record the last stack of page. + It is very helpful for solving the page use-after-free or out-of-bound. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASAN diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 2277b82902d8..2a32474efa74 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "kasan.h" #include "../slab.h" @@ -227,6 +228,11 @@ void kasan_alloc_pages(struct page *page, unsigned int order) void kasan_free_pages(struct page *page, unsigned int order) { +#ifdef CONFIG_KASAN_DUMP_PAGE + gfp_t gfp_flags = GFP_KERNEL; + + set_page_owner(page, order, gfp_flags); +#endif if (likely(!PageHighMem(page))) kasan_poison_shadow(page_address(page), PAGE_SIZE << order, -- 2.18.0