From patchwork Fri May 25 14:40:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10427677 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 757D3602D8 for ; Fri, 25 May 2018 14:41:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6173C29763 for ; Fri, 25 May 2018 14:41:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5575A29792; Fri, 25 May 2018 14:41:36 +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=-10.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5071529763 for ; Fri, 25 May 2018 14:41:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8184D6B026C; Fri, 25 May 2018 10:41:08 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7F1496B026D; Fri, 25 May 2018 10:41:08 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66B836B026E; Fri, 25 May 2018 10:41:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wm0-f70.google.com (mail-wm0-f70.google.com [74.125.82.70]) by kanga.kvack.org (Postfix) with ESMTP id 0761D6B026C for ; Fri, 25 May 2018 10:41:08 -0400 (EDT) Received: by mail-wm0-f70.google.com with SMTP id e1-v6so3443716wma.3 for ; Fri, 25 May 2018 07:41:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=KJryK/I0YpO7jpfpV165zQoiFDp/pu6pR0eoskQ6Y3c=; b=UTfd4BAVg04sS4i5zQIjZswCcHFitTOW0IcjWvQ9jOCE2vaWmY/wlABafgTr7BHqCi LM1/IIfR8FFgEWGALsgMov7eWtNCT6eEXejSibzynQJhUnDjrRuMnMyyQCZ1mMh3qnxW l6t+lIwwSb6bgExbrtbbZBi4phgwTmcTrTr1+g6ARqgrMZpUk56ico2S7A+12HgJvH1m F6vwtS0m9HEzS+pLxurfYQKtqjEisKQzxeHy3/V3zHED7c1S1ZW54LH628nfy4b1+kGI mWG72zYIpZgRpVadaUVbm5yLRN73H+Hp0jCYK/HlzdlUe3gluNMUKhAU8J7/Kf5+AWXn UL5w== X-Gm-Message-State: ALKqPwcugs/GLalNx71+rFN2fp267bgCKtRo24sZvcDrXktuYd83GHtn vHryP8ofjctJ9a9XmvSTCWDsxbc0CF/JUU2U230VdW1L93iUwKV6fDoKthTxJSqgNVdrZOMkwgB Q/Yp3g1TIuzCbY8bhrmv3P997UpDOUWnwuxwJBEAG8PNROLsFc+M1A5FmIvtIaAlJ22mtlEfahr inwuGfkEU0Bb6YC3Umza39ks/Qml+oCk8KMG1RgXHPPYCjbvRqkOqSTC9yg6n+qv5sLjMiyOBpG iUqcWYYbOnbVtXr9x5nm7H2xHt2UtF0z+zDSAtAL33Rt92MlxU2ZbgaxJ2QAX5F71+AfTSa7pCg TzSg/BbEBOkJ5kXC4Lm0CJW4elw49vVn87mwMJGCXJaoUpgaUkiPfJvsi2iQt91N8IfyHltkz9o / X-Received: by 2002:a1c:3b87:: with SMTP id i129-v6mr2204573wma.51.1527259267546; Fri, 25 May 2018 07:41:07 -0700 (PDT) X-Received: by 2002:a1c:3b87:: with SMTP id i129-v6mr2204529wma.51.1527259266548; Fri, 25 May 2018 07:41:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527259266; cv=none; d=google.com; s=arc-20160816; b=cUrEuLwJXlWJm7Aa1dtMyO+D7Nvg+rl/wg0FIxwo7UbRGQRzMvBXJNuWg17uq8lzND d37zadhLxQ9B6z0o5aSTJvzAUYqwMi+w30m7qquLVImbTaoDZbTrgwSehoDNCagUVN+p qEdqCpsULe+F1Muo2keH+i2ybChDKRk7Sg8EmV25WhOp0EDLS49lE6qAE9DynV1vR1gO Pdph3U69fTFrdggWFO8K9+2sRBdbb+ShrqtCkGu/RV6/aBEP9gWeof/jBPDEalu7AcZb R8aW7DF53KsoYxnCgodb9jHU0aBsHBLmsudpEWKDkmeSU4JHWtjkknYiGarueIDO0V55 nDzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=KJryK/I0YpO7jpfpV165zQoiFDp/pu6pR0eoskQ6Y3c=; b=HZhdSw3JMGJDLJzGv+Y853XrQF8Us05RM+z8CmTfLemynClVLJR7GT0bkgkG9L2z4B BIUkYVnLrr9ViPynVwn13kSisgifg5U9PcX1tpR/bnfPRFQ7vpSI4XqUFqA/3ozD+U6n 2pcT916H2Ogja+atH0K4EGilO5FDm/ygv96HpLg/bDXUvzWuroZmQEZaN+fWdM5qWBqK 2jn0KddqmQ05fRR4Bksf4YW7S6Fxn2zeis2a3UmqrGHkLGCwLCeeQkZ+QGk4X0XKs0Gd k48vSPrMV6uoWhz5fxYjHrKCBhHIZpV2RtRedwn4kVAYrd1Xngsf2meKLSMA9fitLTwR p4aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=XTc34E8d; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id y33-v6sor8986624wrd.59.2018.05.25.07.41.06 for (Google Transport Security); Fri, 25 May 2018 07:41:06 -0700 (PDT) Received-SPF: pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=XTc34E8d; spf=pass (google.com: domain of andreyknvl@google.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=andreyknvl@google.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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; bh=KJryK/I0YpO7jpfpV165zQoiFDp/pu6pR0eoskQ6Y3c=; b=XTc34E8dcrfUyduSqdiVMTRh5IcP4uw6wqhvn+dpBjLRJpbYQjZZZYaSlTUnABk3bN FDhFyWXfp/2zU/NIEl0e4KUSayVuyqgSr6TepGJHVoVm44rpHlCe56t+xRZMhH9vUUFg yKrLxaaQYHCpHhTFex0/ikYL2STEwiuJ5ZiJ1af/yipGzB2DaiOtftUnIg/NX7X+7Hps Mq1gV06zwon/6skfS83oK32IWHnS5BdHklYRsSBY7NvVhxuI25l2+sStmK0YChZmv/zx HJa6PoRCl3wMKoIjny+chGNrh/r4uvV0rFtj1yCRLRy7SZeyhoc5sH9jwx3cNLY5issj lpkw== X-Google-Smtp-Source: ADUXVKJUJawtpJJrUp2suHAU5B/cK27/M1UcBNYMwMJ1CkjPg3VshqfIaiHc2njSD+rekiGosRtQSw== X-Received: by 2002:adf:a925:: with SMTP id u34-v6mr2667586wrc.248.1527259265266; Fri, 25 May 2018 07:41:05 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id q2-v6sm25293885wrm.26.2018.05.25.07.41.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 May 2018 07:41:04 -0700 (PDT) From: Andrey Konovalov To: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Jonathan Corbet , Catalin Marinas , Will Deacon , Christopher Li , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Masahiro Yamada , Michal Marek , Andrey Konovalov , Mark Rutland , Nick Desaulniers , Yury Norov , Marc Zyngier , Kristina Martsenko , Suzuki K Poulose , Punit Agrawal , Dave Martin , Ard Biesheuvel , James Morse , Michael Weiser , Julien Thierry , Tyler Baicar , "Eric W . Biederman" , Thomas Gleixner , Ingo Molnar , Kees Cook , Sandipan Das , David Woodhouse , Paul Lawrence , Herbert Xu , Josh Poimboeuf , Geert Uytterhoeven , Tom Lendacky , Arnd Bergmann , Dan Williams , Michal Hocko , Jan Kara , Ross Zwisler , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Matthew Wilcox , "Kirill A . Shutemov" , Souptick Joarder , Hugh Dickins , Davidlohr Bueso , Greg Kroah-Hartman , Philippe Ombredanne , Kate Stewart , Laura Abbott , Boris Brezillon , Vlastimil Babka , Pintu Agarwal , Doug Berger , Anshuman Khandual , Mike Rapoport , Mel Gorman , Pavel Tatashin , Tetsuo Handa , 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 Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Kees Cook , Jann Horn , Mark Brand , Chintan Pandya Subject: [PATCH v2 12/16] khwasan: add bug reporting routines Date: Fri, 25 May 2018 16:40:28 +0200 Message-Id: X-Mailer: git-send-email 2.17.0.921.gf22659ad46-goog In-Reply-To: References: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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_report.c | 7 ++--- mm/kasan/khwasan_report.c | 21 +++++++++++++++ mm/kasan/report.c | 57 +++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index d7624b879d86..e209027f3b52 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -161,6 +161,9 @@ void *khwasan_set_tag(const void *addr, u8 tag); u8 khwasan_get_tag(const void *addr); void *khwasan_reset_tag(const void *ptr); +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_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); }