From patchwork Tue Nov 6 17:30:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10670965 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 3A1C313BF for ; Tue, 6 Nov 2018 17:31:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 215442ABA2 for ; Tue, 6 Nov 2018 17:31:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1432E2ABDD; Tue, 6 Nov 2018 17:31:45 +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 576DF2ABDC for ; Tue, 6 Nov 2018 17:31:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C35856B036F; Tue, 6 Nov 2018 12:31:10 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BBD856B0371; Tue, 6 Nov 2018 12:31:10 -0500 (EST) 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 A852D6B0373; Tue, 6 Nov 2018 12:31:10 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by kanga.kvack.org (Postfix) with ESMTP id 520A96B036F for ; Tue, 6 Nov 2018 12:31:10 -0500 (EST) Received: by mail-wr1-f70.google.com with SMTP id u8-v6so11935252wrn.17 for ; Tue, 06 Nov 2018 09:31:10 -0800 (PST) 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:mime-version :content-transfer-encoding; bh=PqZ4W4abhuiWrYBf7dbVlb6J0CRQ5VwTcMgO0APUlT4=; b=BCXzeVVt6F6tVLmDRzD+t89gVroya0MRIRkbehDpUUhhqOthrDc9Sg0Mw867HDSINZ rC4psmunxZxDfZg/lkuO17nJFfD+kpfM6pKMzw6+qxrrsRECsAENdN5L/NzW+1/5OtaO uLjmuTIQCtVSvsgMUTjyo+R/vGTI08zuVyzAc/KIPy+NSvXZUVNJMmhqfhitXyKUKKLm r0Pf02BMaolS8TBskPddJ9+SmGU5fcf/to73jyCXtqOglwGD6FikSmIvDK3tdbCvZCFQ FnUR9MD5eyRZ2s9u+Dn/XdUk4FfQXohtG3prN7MJ7PO0dSR8aVSSzXzvKhjUaAmzEw+c kcMg== X-Gm-Message-State: AGRZ1gLf7+zy+L5q+s4/fxprldBCdEKFjSCCvxC/Xtipwb4l0CVEhVSo 9Pm6sXzqhFlgRda6pqDVA9QjNpff6+hLhmeuXCmxc5F++eWyaXbYchsuxYAbxhaeT7pHgq/np0d 4z0Hz7w2tCNYpUVm6xK6g1FsX0+kG0JnduKfRV7Z/CdIACPjzhCnSzQaBwM1JRUsFyc6aOw/FJX 0QyOQ4zikuisKd9YoE6PqDDjgd6kxR2vAR2PoSvThj/Jz9CvAoD9EtHf+vq6lgH+dpdCbThvl2N ZyokElGnk54MmaxKV7JSona59/d2dvbwldAFia+7g36CFfQ8PiNCcw09BDwccoFBNTlU4MxHAKm HT3x2KgmjP/fmU2zmOx47NEPmzDI/RAgnmgTuHQ9Z11EF619ZrZqJXLfyF2aTt28qkZAWqIZOCk g X-Received: by 2002:adf:fd49:: with SMTP id h9-v6mr25104869wrs.280.1541525469841; Tue, 06 Nov 2018 09:31:09 -0800 (PST) X-Received: by 2002:adf:fd49:: with SMTP id h9-v6mr25104791wrs.280.1541525468711; Tue, 06 Nov 2018 09:31:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541525468; cv=none; d=google.com; s=arc-20160816; b=kPBuobr77twcieT5FaxsrE9c6IOxGus8c2zvy65bUdgm7f0QAAVQZ638+mPy4cbmGY A3ToGdHLvp2mPmx+7jk2Cp6LV4uiSQxeL4jGRjAFnmzWqfWZPbMV9WSH+qm10qieeU7w L7NY1xx4ysVgD0724I1FMaxH/wYd/52YVTCTzU8Ib2goQ053lq+ZCxe5g9eBjLlRxR/C Hlx9/aednE1+qu5sGy6Kdnd+7b7xwBzKAQJ6vzwOGjo4WJ4YDLcKBJT/nQWCl4z8803c YO31D2s5st7ZUNM3Z3XAB/MAjB1XSd6LtieCMA/U0QHsybiNDE9bMmEemMOv/Jv+y1SG Xcuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=PqZ4W4abhuiWrYBf7dbVlb6J0CRQ5VwTcMgO0APUlT4=; b=tqTR/yJaQDVkzTcy2Q3tO2K1GpLHiJXdPITGRqW8tWNndY9gUHp8AGXB56T+WwNQNs 5bsYeFB/X37MWwfByp/tuuKoFDTw6WvHo8PP0YAht44HL0AMOQ2HkQbff2tvAQivyNZf tFfys/Lvgdg0efavirOdTuxFlDGY2DwPZRwPJZVzXaQo3BE710jrRVI4/lB0+DWxaImj SMzaw+QgM6PIJj1PUoD38im/G1fb64OUAYBeK9shJo4sRAkbg0ZxS0Q51MzSsl3nwMHu JdqpnpBxxlt+AY0xFQ3A0N+6pX7icBMOvfzqEIunsyrfjFL2JiHe6f283SijpFyoAicy XxxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=e9NIR9oz; 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 j10sor13552220wrx.1.2018.11.06.09.31.08 for (Google Transport Security); Tue, 06 Nov 2018 09:31:08 -0800 (PST) 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=e9NIR9oz; 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 :mime-version:content-transfer-encoding; bh=PqZ4W4abhuiWrYBf7dbVlb6J0CRQ5VwTcMgO0APUlT4=; b=e9NIR9ozLa1hZiKs+YKqZ6JfopiH2CQLRj0DtzXk9rGh9eC5tEoHQMpt6odCi502Cb yJa7wWAhVx1XTUQNl2jST/MXE7EQSq9OggekRcJAaTxYKxCZI6Ql89+UTOOMBoWh9q47 +3vL/lw05/Hnw80UmllX6OFiVE/hWaB4257zLGTAO7by+xk3wEze8BLJF+9ZUEH4ooRY u6Qd6mPCbR2mrFto/v9H8OLXtsga5a6tDNO9fO0ZXQYVV84jGfMELJmtNL35eIwsoIfK 6scEgS18N4s195E1kRfIb+N5FpQnr1wRyXZujecm1KRELMwzNuIgG9YTdLBIxZp+Sh2T qQ8w== X-Google-Smtp-Source: AJdET5fvwZJYEj2YXgKRWAOEHHdvBWsS4JO9Y0Zaa7TtohiWchaSF3ehPThS3UYqVY/8GH0sO1tDVQ== X-Received: by 2002:adf:f542:: with SMTP id j2-v6mr15247056wrp.70.1541525468219; Tue, 06 Nov 2018 09:31:08 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id z17-v6sm16328416wrm.65.2018.11.06.09.31.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Nov 2018 09:31:07 -0800 (PST) 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 Cc: Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Jann Horn , Mark Brand , Chintan Pandya , Vishwath Mohan , Andrey Konovalov Subject: [PATCH v10 16/22] kasan: add bug reporting routines for tag-based mode Date: Tue, 6 Nov 2018 18:30:31 +0100 Message-Id: X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-goog In-Reply-To: References: MIME-Version: 1.0 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 tag-based KASAN error reports. Those are quite similar to generic KASAN, the difference is: 1. The way tag-based KASAN finds the first bad shadow cell (with a mismatching tag). Tag-based KASAN compares memory tags from the shadow memory to the pointer tag. 2. Tag-based KASAN reports all bugs with the "KASAN: invalid-access" header. Also simplify generic KASAN find_first_bad_addr. Reviewed-by: Andrey Ryabinin Reviewed-by: Dmitry Vyukov Signed-off-by: Andrey Konovalov --- mm/kasan/generic_report.c | 16 ++++------- mm/kasan/kasan.h | 5 ++++ mm/kasan/report.c | 57 +++++++++++++++++++++------------------ mm/kasan/tags_report.c | 18 +++++++++++++ 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/mm/kasan/generic_report.c b/mm/kasan/generic_report.c index 5201d1770700..a4604cceae59 100644 --- a/mm/kasan/generic_report.c +++ b/mm/kasan/generic_report.c @@ -33,16 +33,13 @@ #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 *p = addr; - while (!shadow_val && first_bad_addr < addr + size) { - first_bad_addr += KASAN_SHADOW_SCALE_SIZE; - shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); - } - return first_bad_addr; + while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p))) + p += KASAN_SHADOW_SCALE_SIZE; + return p; } static const char *get_shadow_bug_type(struct kasan_access_info *info) @@ -50,9 +47,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/kasan.h b/mm/kasan/kasan.h index 50adcab463f2..9b567f742539 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -119,6 +119,7 @@ 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); void kasan_report(unsigned long addr, size_t size, @@ -139,6 +140,8 @@ static inline void quarantine_remove_cache(struct kmem_cache *cache) { } #ifdef CONFIG_KASAN_SW_TAGS +void print_tags(u8 addr_tag, const void *addr); + #define KASAN_PTR_TAG_SHIFT 56 #define KASAN_PTR_TAG_MASK (0xFFUL << KASAN_PTR_TAG_SHIFT) @@ -166,6 +169,8 @@ static inline void *reset_tag(const void *addr) #else /* CONFIG_KASAN_SW_TAGS */ +static inline void print_tags(u8 addr_tag, const void *addr) { } + static inline u8 random_tag(void) { return 0; diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 64a74f334c45..214d85035f99 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); } diff --git a/mm/kasan/tags_report.c b/mm/kasan/tags_report.c index 8af15e87d3bc..573c51d20d09 100644 --- a/mm/kasan/tags_report.c +++ b/mm/kasan/tags_report.c @@ -37,3 +37,21 @@ 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 *p = reset_tag(addr); + void *end = p + size; + + while (p < end && tag == *(u8 *)kasan_mem_to_shadow(p)) + p += KASAN_SHADOW_SCALE_SIZE; + return p; +} + +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); +}