Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp651323ybe; Wed, 4 Sep 2019 05:50:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqyjtKhVcVhDMYh/Ib372hLDXOqEIEG1stD1wNzRh5o+gHbydslylyPTO/egKe3IPIh1FLqR X-Received: by 2002:a17:90b:8ca:: with SMTP id ds10mr4716138pjb.105.1567601424152; Wed, 04 Sep 2019 05:50:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567601424; cv=none; d=google.com; s=arc-20160816; b=SHJoctdIqO9HQIJJO38suawRMQC0eVZ1p8p9g8QpHlafrUxXspoVFhJeuko/EG44Pr MZyPL1Vh4uZBnGAdFLj/PhmyiiiiqKBnReAuBNumIrS7qlgLZ/ywnKODsk63NMBM3389 59BOBYZL2Uh0KgeQRh6MBS99dyahtuAfMhe+5gTJyigbKvci0VuAahjKiIa9EAf3cYOh ITBjdep7yeLF7sdPXnCrUEXJAXfRYw4v2OsaHa74phcn/5NGp1NlF9tVu0TFIsZ5dDbK 4Da+VofpjaNS3cgolNpyjbPzK2LxL5taEFjjY3C7ij/MajAcWOUl40XOn/lRrLZLLap8 ozVQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=PC5IFzzSR7B/D3u/zLTll22794H9JBcYpW4N8jNkrT8=; b=Ln+mnNyty2SwUJuKg6CyRyvJXSPaCqP+qhG3b7R5HSTZEcpbt/kQdXE2elgmFpD0Uk dJOXx0jCmHe0rP/NLCg59Twi1lCMubYNe3a8SjoPjewv1aa04tCt9sVS3mTrA1EdZztv JschoF2aCAdVvtQtqonW5TG/xU7cB8SW3PLAP0UC0qLvfgWFIOg41QYiyxx6w27lDzug R9AnuRmvA1LyGvzV1epbvPo3zS3uHEd0tOiBH3qrgGMGWEFhbzohz7q4AlHI0Rb+v63z WzZcnlIWCmFPLqqW88iwcSan2BiuYC5bcQakRmQud3tfijAoSFC8Nv5vkECR5028hS5x ygfw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e4si8943927pln.193.2019.09.04.05.50.07; Wed, 04 Sep 2019 05:50:24 -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 S1730087AbfIDMtK (ORCPT + 99 others); Wed, 4 Sep 2019 08:49:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:47098 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730065AbfIDMtK (ORCPT ); Wed, 4 Sep 2019 08:49:10 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id F03BCAF47; Wed, 4 Sep 2019 12:49:07 +0000 (UTC) Subject: Re: [PATCH 1/2] mm/kasan: dump alloc/free stack for page allocator To: Walter Wu , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , Andrew Morton , Martin Schwidefsky , Arnd Bergmann Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com References: <20190904065133.20268-1-walter-zh.wu@mediatek.com> From: Vlastimil Babka Message-ID: <401064ae-279d-bef3-a8d5-0fe155d0886d@suse.cz> Date: Wed, 4 Sep 2019 14:49:06 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190904065133.20268-1-walter-zh.wu@mediatek.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/4/19 8:51 AM, Walter Wu wrote: > 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. I expect this will conflict both in syntax and semantics with my series [1] that adds the freeing stack to page_owner when used together with debug_pagealloc, and it's now in mmotm. Glad others see the need as well :) Perhaps you could review the series, see if it fulfils your usecase (AFAICS the series should be a superset, by storing both stacks at once), and perhaps either make KASAN enable debug_pagealloc, or turn KASAN into an alternative enabler of the functionality there? Thanks, Vlastimil [1] https://lore.kernel.org/linux-mm/20190820131828.22684-1-vbabka@suse.cz/t/#u > 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, >