Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp867674ybe; Wed, 11 Sep 2019 06:04:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqwEY3Az0WkWKcrRZGNlj/bp4EapL7wVaheIW56AesJOAcNQgfLaKhkpMjp6D4Q2gTECzr3b X-Received: by 2002:a17:906:6dc1:: with SMTP id j1mr29694014ejt.85.1568207090092; Wed, 11 Sep 2019 06:04:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568207090; cv=none; d=google.com; s=arc-20160816; b=to6ZG52qsRWfj8yRiwl6B4BjN/TaQaNraebkksHZ3IBqtQ3IK+uixd4O/+l4FWRGjz ye/ZHW7palPuSClMjnv1p/ysWKWfIImlkD5LHWr2rcKFrACRRJwAOaYit5mqF+D493sI mW5ZwYX8bKEM/5Qfo86ml8qzqLjCCPjqEyiFavI2hO2ktnUj/2+DetnFpZ5joZGIaih7 2U7dleSFF2JqiVHQnBOOR8OX6vJ6cXCidTjvyLUvF+MndWok9muYi4Cvyh2vXvkPkE9x Hy7rirmt7ckSd2Hmv2cdFo20Nv0+KC86K1troibTcSZlcWHv8HmAz6DrjBFD6edsGmkA IypQ== 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=oIVbbJqRkDE/eNyd9GZNdppqWHsKj44BIopnURfqM0o=; b=cmu5u2tgvGGtP45CFrMZbyKFc0WX1nkJvJ0hTv/3980WgDDQdZgdTUXtELQUxXtkuK QYSR/v0HAA97SDJGAfR76Ik3mWunVGjMvHsdZKhTzDDqhZCo3UYFzOXVMvnT1fYSm4qO Zs077sndtd8xq8lcUjhKVK7DT2tyN5LHgM3SzkQtIYdV/MyvmDb5aAJSoaB5goWbUx94 NVEVyhVx02lxU7/o0NCcnGC94n7NM0OVAU0eof1Q2C4G82IrVGXkhzIejUfzYw/1t47B EnrJ3U7vI8sy+ugA0+Dq0FMGmFEmWnGVUT3xDYIvgeWJ5iCvlPYSFENZ4uNsP0ng0I86 ToqA== 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 z33si12120322edz.314.2019.09.11.06.04.23; Wed, 11 Sep 2019 06:04:50 -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 S1727945AbfIKNCG (ORCPT + 99 others); Wed, 11 Sep 2019 09:02:06 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:23969 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727659AbfIKNCG (ORCPT ); Wed, 11 Sep 2019 09:02:06 -0400 X-UUID: e7bb7d6d9123418ab85de4128f4f1d7a-20190911 X-UUID: e7bb7d6d9123418ab85de4128f4f1d7a-20190911 Received: from mtkcas07.mediatek.inc [(172.21.101.84)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 1948053234; Wed, 11 Sep 2019 21:02:00 +0800 Received: from mtkcas08.mediatek.inc (172.21.101.126) by mtkmbs07n2.mediatek.inc (172.21.101.141) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Wed, 11 Sep 2019 21:01:58 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas08.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Wed, 11 Sep 2019 21:01:58 +0800 From: Walter Wu To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , Andrew Morton , Martin Schwidefsky , Andrey Konovalov , Qian Cai , Vlastimil Babka , Arnd Bergmann CC: , , , , , , Walter Wu Subject: [PATCH v4] mm/kasan: dump alloc and free stack for page allocator Date: Wed, 11 Sep 2019 21:01:56 +0800 Message-ID: <20190911130156.12628-1-walter-zh.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain 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's report adds the alloc/free stack for page allocator in order to help programmer to see memory corruption caused by the page. By default, KASAN doesn't record alloc or free stack for page allocator. It is difficult to fix up the page use-after-free or double-free issue. We add the following changing: 1) KASAN enable PAGE_OWNER by default to get the alloc stack of the page. 2) Add new feature option to get the free stack of the page. The new feature KASAN_DUMP_PAGE depends on DEBUG_PAGEALLOC, it will help to record free stack of the page, it is very helpful for solving the page use-after-free or double-free issue. When KASAN_DUMP_PAGE is enabled then KASAN's report will show the last alloc and free stack of the page, it should be: BUG: KASAN: use-after-free in kmalloc_pagealloc_uaf+0x70/0x80 Write of size 1 at addr ffffffc0d60e4000 by task cat/115 ... prep_new_page+0x1c8/0x218 get_page_from_freelist+0x1ba0/0x28d0 __alloc_pages_nodemask+0x1d4/0x1978 kmalloc_order+0x28/0x58 kmalloc_order_trace+0x28/0xe0 kmalloc_pagealloc_uaf+0x2c/0x80 page last free stack trace: __free_pages_ok+0x116c/0x1630 __free_pages+0x50/0x78 kfree+0x1c4/0x250 kmalloc_pagealloc_uaf+0x38/0x80 Changes since v1: - slim page_owner and move it into kasan - enable the feature by default Changes since v2: - enable PAGE_OWNER by default - use DEBUG_PAGEALLOC to get page information Changes since v3: - correct typo cc: Andrey Ryabinin cc: Vlastimil Babka cc: Andrey Konovalov Signed-off-by: Walter Wu --- lib/Kconfig.kasan | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan index 4fafba1a923b..a3683e952b10 100644 --- a/lib/Kconfig.kasan +++ b/lib/Kconfig.kasan @@ -41,6 +41,7 @@ config KASAN_GENERIC select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER help Enables generic KASAN mode. Supported in both GCC and Clang. With GCC it requires version 4.9.2 @@ -63,6 +64,7 @@ config KASAN_SW_TAGS select SLUB_DEBUG if SLUB select CONSTRUCTORS select STACKDEPOT + select PAGE_OWNER help Enables software tag-based KASAN mode. This mode requires Top Byte Ignore support by the CPU and therefore @@ -135,6 +137,19 @@ 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 last allocation and freeing stack of the page" + depends on KASAN + select DEBUG_PAGEALLOC + help + By default, KASAN enable PAGE_OWNER only to record alloc stack + for page allocator. It is difficult to fix up page use-after-free + or double-free issue. + The feature depends on DEBUG_PAGEALLOC, it will extra record + free stack of the page. It is very helpful for solving the page + use-after-free or double-free issue. + The feature will have a small memory overhead. + config TEST_KASAN tristate "Module for testing KASAN for bug detection" depends on m && KASAN -- 2.18.0