From patchwork Wed Aug 29 11:35:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10580067 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 14E055A4 for ; Wed, 29 Aug 2018 11:48:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0156E2AD6F for ; Wed, 29 Aug 2018 11:48:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E85FA2AD81; Wed, 29 Aug 2018 11:48:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 666322AD6F for ; Wed, 29 Aug 2018 11:48:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=L1hbWi5fjd/4PyCNeD9Ono3HRjTuBKQybJ3/vnXicrY=; b=HucO6WO3bLdCnc 1sOaORZCSHOifwHOh1W6bRtm9wcWxfPHG9j8egNwDwms4cIbchVa9ATqsbycvtry67NUAA3IXtxNm JnRi9NTpGp8mp0naWGDrIj4GoZbO07e0K/edchclnM1Q4Hy3ItDJRlZwxpNSkqZdVNuKFgQZ1br4M bI7CkktY8pZHWHk9QSzNQpspKshiFFfUGKagIOeahAfSuOKso1UkR0YRpQiC3K/CQuNuXjWAKs+yP 7WMCCNFK60HnXwPkxFNo0t8hsmYXbiXO9cTP6+ygw/2CSFLHYDsDbhu485F57PLY7TChRyN9Z6Pgd XJx0vzZdPDKRMpVEX2Jw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fuyxf-00048N-KX; Wed, 29 Aug 2018 11:48:35 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fuylZ-0004jY-5U for linux-arm-kernel@bombadil.infradead.org; Wed, 29 Aug 2018 11:36:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=RptGi1N09kqi5nALd3Lw1kS3KWHrO9/wmh1vLWiCPsQ=; b=X5Qu+XxWGY4GQA1i15bu382/i8 HgooCDBD1MKob/fJaQcVVB3ZICmSbedcR0kr+oIRJcw7nxOEB35A7bMvnzc9B5HLyn6rRkECIdRrP H9YFbhROKSHnw+1iioePd3r5lRlND3lBL+HDr2l3a/BRxe1p4B0IlnIzmHbTZ5B8ULUX39MrCdIw3 lHLwYGlPnWY+4t9Kfr3hjSYBCGC79gcqJVvfFF+Iv7b8fvHPB0x/TPq+ic50fpGmRsqD6j2kXZd0E 2WCw3mCn4o+C23UgYzil4Y9IUoUiMeDR4EHH55qpFhEhu3+3lxacEuCb0ONQ/bJAkefl6tJOhCsB5 MLhptfmg==; Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fuylV-00033U-7l for linux-arm-kernel@lists.infradead.org; Wed, 29 Aug 2018 11:36:03 +0000 Received: by mail-wm0-x241.google.com with SMTP id i134-v6so4268891wmf.0 for ; Wed, 29 Aug 2018 04:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RptGi1N09kqi5nALd3Lw1kS3KWHrO9/wmh1vLWiCPsQ=; b=sprGRxk69Bgq2fEeJZYij7WuPS5dd42cNx8LOGTtKuHyxeGw9qNWBhl7CaazsAv2L8 0Z5a+YfEPdFTzLPIEMD0qo3Onb3R87L1f883KaLzvddIJcQv8umtpkJKssdcauC6ACw/ L2Q2LRhzqv+ozMgzDO81VYgZMrR0AQeQbFew/ywJAN8mDBTqbD+n6xgBHehW5wYdR8AX 3TJ2uGALKi2Ff9a9/USYAZSCJWtp8SCmomSn8P5XwrObk8hFwmvXLbEkrjX3O0GIfLyL Q989jfOQUiPJoa45r149PVVlaqKZtoDfLMB5LI0YyIsYbrKIphQZgUdF1iq5rf0z9kKm ZgIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RptGi1N09kqi5nALd3Lw1kS3KWHrO9/wmh1vLWiCPsQ=; b=rcRWD07Ah3F73EFdBBR9M0mn4Prs0PSd2syIUkwgbhvavJdxONFcs+XOqeS+I2jJ+i gh6TIDMQDWVqGMRa7z+EEQTKGgqvr2xaUncAaPtKAk5eI300VRT0kIsvkVaT19URkwd3 NS7iRCWqrt4r3WWcfibqSXHuFuMJkCns4c+QiALA/nvG2rc+eWqTcCe7SNcxevu/uIXB 4TJrVwI5nbM52IheTt2ti0uGYr9zkt/rUIMFeyWvgYeNo4Ve6D4lnir6qEAJKECFIjLK xE+E6IN4r/WGvMec/fglbmOsyiWTWk3EfNW7jssm5oKm6OVlEUXBkLfzjWA3gpQPOTQu CzmQ== X-Gm-Message-State: APzg51D9E4EiLmoCeOd4QAynRiMwc9DNn0G9oCznx5P6LtxPfsFuwu/6 D9pbV7UFNp3uhft6TX2Qkw+m4A== X-Google-Smtp-Source: ANB0VdapbVuh8UM8tmw1fbsxfHLxbzYLXGvQJN/t9MYIn48sIEE2gDAiYdQOxGPzZ6wTAybzKarjqw== X-Received: by 2002:a1c:8406:: with SMTP id g6-v6mr3789441wmd.18.1535542548680; Wed, 29 Aug 2018 04:35:48 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id s10-v6sm7800454wmd.22.2018.08.29.04.35.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 04:35:47 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sparse@vger.kernel.org, linux-mm@kvack.org, linux-kbuild@vger.kernel.org Subject: [PATCH v6 13/18] khwasan: add bug reporting routines Date: Wed, 29 Aug 2018 13:35:17 +0200 Message-Id: X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180829_123601_372862_B11CE6A3 X-CRM114-Status: GOOD ( 21.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vishwath Mohan , Chintan Pandya , Jacob Bramley , Jann Horn , Ruben Ayrapetyan , Andrey Konovalov , Lee Smith , Kostya Serebryany , Mark Brand , Ramana Radhakrishnan , Evgeniy Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This commit adds rountines, that print KHWASAN error reports. Those are quite similar to KASAN, the difference is: 1. The way KHWASAN finds the first bad shadow cell (with a mismatching tag). KHWASAN compares memory tags from the shadow memory to the pointer tag. 2. KHWASAN reports all bugs with the "KASAN: invalid-access" header. This is done, so various external tools that already parse the kernel logs looking for KASAN reports wouldn't need to be changed. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 3 +++ mm/kasan/kasan.h | 7 +++++ mm/kasan/kasan_report.c | 7 ++--- mm/kasan/khwasan_report.c | 21 +++++++++++++++ mm/kasan/report.c | 57 +++++++++++++++++++++------------------ 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 1f852244e739..4424359a9dfa 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -174,6 +174,9 @@ void *khwasan_preset_slub_tag(struct kmem_cache *cache, const void *addr); void *khwasan_preset_slab_tag(struct kmem_cache *cache, unsigned int idx, const void *addr); +void kasan_report(unsigned long addr, size_t size, + bool write, unsigned long ip); + #else /* CONFIG_KASAN_HW */ static inline void khwasan_init(void) { } diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index 82672473740c..d60859d26be7 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -119,8 +119,15 @@ void kasan_poison_shadow(const void *address, size_t size, u8 value); void check_memory_region(unsigned long addr, size_t size, bool write, unsigned long ret_ip); +void *find_first_bad_addr(void *addr, size_t size); const char *get_bug_type(struct kasan_access_info *info); +#ifdef CONFIG_KASAN_HW +void print_tags(u8 addr_tag, const void *addr); +#else +static inline void print_tags(u8 addr_tag, const void *addr) { } +#endif + void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip); void kasan_report_invalid_free(void *object, unsigned long ip); diff --git a/mm/kasan/kasan_report.c b/mm/kasan/kasan_report.c index 2d8decbecbd5..fdf2d77e3125 100644 --- a/mm/kasan/kasan_report.c +++ b/mm/kasan/kasan_report.c @@ -33,10 +33,10 @@ #include "kasan.h" #include "../slab.h" -static const void *find_first_bad_addr(const void *addr, size_t size) +void *find_first_bad_addr(void *addr, size_t size) { u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); - const void *first_bad_addr = addr; + void *first_bad_addr = addr; while (!shadow_val && first_bad_addr < addr + size) { first_bad_addr += KASAN_SHADOW_SCALE_SIZE; @@ -50,9 +50,6 @@ static const char *get_shadow_bug_type(struct kasan_access_info *info) const char *bug_type = "unknown-crash"; u8 *shadow_addr; - info->first_bad_addr = find_first_bad_addr(info->access_addr, - info->access_size); - shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); /* diff --git a/mm/kasan/khwasan_report.c b/mm/kasan/khwasan_report.c index 2edbc3c76be5..51238b404b08 100644 --- a/mm/kasan/khwasan_report.c +++ b/mm/kasan/khwasan_report.c @@ -37,3 +37,24 @@ const char *get_bug_type(struct kasan_access_info *info) { return "invalid-access"; } + +void *find_first_bad_addr(void *addr, size_t size) +{ + u8 tag = get_tag(addr); + void *untagged_addr = reset_tag(addr); + u8 *shadow = (u8 *)kasan_mem_to_shadow(untagged_addr); + void *first_bad_addr = untagged_addr; + + while (*shadow == tag && first_bad_addr < untagged_addr + size) { + first_bad_addr += KASAN_SHADOW_SCALE_SIZE; + shadow = (u8 *)kasan_mem_to_shadow(first_bad_addr); + } + return first_bad_addr; +} + +void print_tags(u8 addr_tag, const void *addr) +{ + u8 *shadow = (u8 *)kasan_mem_to_shadow(addr); + + pr_err("Pointer tag: [%02x], memory tag: [%02x]\n", addr_tag, *shadow); +} diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 155247a6f8a8..e031c78f2e52 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -64,11 +64,10 @@ static int __init kasan_set_multi_shot(char *str) } __setup("kasan_multi_shot", kasan_set_multi_shot); -static void print_error_description(struct kasan_access_info *info, - const char *bug_type) +static void print_error_description(struct kasan_access_info *info) { pr_err("BUG: KASAN: %s in %pS\n", - bug_type, (void *)info->ip); + get_bug_type(info), (void *)info->ip); pr_err("%s of size %zu at addr %px by task %s/%d\n", info->is_write ? "Write" : "Read", info->access_size, info->access_addr, current->comm, task_pid_nr(current)); @@ -272,6 +271,8 @@ void kasan_report_invalid_free(void *object, unsigned long ip) start_report(&flags); pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); + print_tags(get_tag(object), reset_tag(object)); + object = reset_tag(object); pr_err("\n"); print_address_description(object); pr_err("\n"); @@ -279,41 +280,45 @@ void kasan_report_invalid_free(void *object, unsigned long ip) end_report(&flags); } -static void kasan_report_error(struct kasan_access_info *info) -{ - unsigned long flags; - - start_report(&flags); - - print_error_description(info, get_bug_type(info)); - pr_err("\n"); - - if (!addr_has_shadow(info->access_addr)) { - dump_stack(); - } else { - print_address_description((void *)info->access_addr); - pr_err("\n"); - print_shadow_for_address(info->first_bad_addr); - } - - end_report(&flags); -} - void kasan_report(unsigned long addr, size_t size, bool is_write, unsigned long ip) { struct kasan_access_info info; + void *tagged_addr; + void *untagged_addr; + unsigned long flags; if (likely(!report_enabled())) return; disable_trace_on_warning(); - info.access_addr = (void *)addr; - info.first_bad_addr = (void *)addr; + tagged_addr = (void *)addr; + untagged_addr = reset_tag(tagged_addr); + + info.access_addr = tagged_addr; + if (addr_has_shadow(untagged_addr)) + info.first_bad_addr = find_first_bad_addr(tagged_addr, size); + else + info.first_bad_addr = untagged_addr; info.access_size = size; info.is_write = is_write; info.ip = ip; - kasan_report_error(&info); + start_report(&flags); + + print_error_description(&info); + if (addr_has_shadow(untagged_addr)) + print_tags(get_tag(tagged_addr), info.first_bad_addr); + pr_err("\n"); + + if (addr_has_shadow(untagged_addr)) { + print_address_description(untagged_addr); + pr_err("\n"); + print_shadow_for_address(info.first_bad_addr); + } else { + dump_stack(); + } + + end_report(&flags); }