Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp58600imu; Tue, 27 Nov 2018 09:00:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/WWsB/cR7pbUid4FG9jbMxWkdgHlecgo82apzm6KOTsxX0pb9jhy9uDaClfSwGEM86bDbBl X-Received: by 2002:a63:e001:: with SMTP id e1mr30098987pgh.39.1543338059641; Tue, 27 Nov 2018 09:00:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543338059; cv=none; d=google.com; s=arc-20160816; b=Bv/TwYqBLk72YPzBRTIngtSUKTmcbNaN92vgb84m4jq5Yzbpzf+dPAGQQhBK6akwrA ml6vi+8Q8K7AdF/bZtcSjkjvkb+AnmsILKcvKa/jOcswLQvL0w8aaP76NPV1hJaqYbwR h25/l30f2vFUuhlgOS9erhvK9Awl63UUy4DIHqikyPJ9FhRzruUuEcmRam9vyrn1OxFl sN1LnONjK6AHigicsHEjDi6Sy+HqZbr9jI8rMrTJOXVpC4oKnXfYusjteiwSNZutoVGV ca7wkjKor8isHtpWEuJ2hDDVoQvWKjvCHfIjTfOqsKGwX4vjNug3bEgrTcM6/J+gLien Hlew== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NGY24kEYlKV9za6j4+lLg+Mk37D+Fr2A0V+VMyaOAWQ=; b=cUzs+l/i/fRspio0H9hPumN5L8jVzAaME8gsLbiCX0gV+TfnCOGlzyXGzaNoTHxhW7 PdiUBhcMl0ocBB/vZBCJlSVK7LsjVcKlok0cAkfQ+xHGHWsXtsnINJ9LYYYKkJMtMjLm HuQtIbulhAn0DOGIPAJhk8vlUGyEpI6GdMi/dlljPRJedPzMZCvJ85ZIVpOMTd+gFch+ A03GfQvPWatAI4O3E+Z8mNMgloahLd/CcCo416A6gTeR8ujmUCGsl7mLRgysczs/mPw9 2ifKl0j6CgxTAVNTX8Z+VbHrj7SRK4yI0ua1KAJEG8H0trG1krrw0VF3ee0vjfWgVTqG uk7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Y7JqEyQS; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si4709519plb.103.2018.11.27.09.00.35; Tue, 27 Nov 2018 09:00:59 -0800 (PST) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=Y7JqEyQS; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731777AbeK1Dyx (ORCPT + 99 others); Tue, 27 Nov 2018 22:54:53 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:37950 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731722AbeK1Dyw (ORCPT ); Tue, 27 Nov 2018 22:54:52 -0500 Received: by mail-wr1-f66.google.com with SMTP id v13so19982996wrw.5 for ; Tue, 27 Nov 2018 08:56:21 -0800 (PST) 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=NGY24kEYlKV9za6j4+lLg+Mk37D+Fr2A0V+VMyaOAWQ=; b=Y7JqEyQSNecPNfIakkOPNWEwozlvQ7ETwuh0OMtbtbMYqXQfovnxd3NByCHMQnfWdE ebrdGC7fbXgpWh7tVKpiOQ3SBN0iFuxb8A6HilwShILfNEkMxIO1gsHrq8F3oY0cdw1U mt+Rayis9WOPMzHc3QMBeO3T+clBxfiO2BGANOhY7g24ixnqihd710PDAtHJNxbEoWfB UhTp/V3tfBoEDp3a4IEvOzJDd94a+ALJ8vy4G7KwlLH4epF1VTUxj7iqVGgj4KPBBfbT 5SXPz5hbzYMoZMa3xobi8WbquZxaw5JcCRwjPmT629uqE6pzCrMCBw1ojI/VEgbU71Q1 ydng== 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=NGY24kEYlKV9za6j4+lLg+Mk37D+Fr2A0V+VMyaOAWQ=; b=WRZHT7WZw85N9PA6yJK89uA1yJOvmNWd2SfmZHzR8vpoqg2BwxtZm9dJwJY3VNZUAT JF0HXrPlSAk8V7ip42QPNw5bfSvkBYRqHYq8cPIh6G2rUHmJV4PtOQPLAsQ9qlFJo4fs yTXhqab8rLgEsDMygmUFmaj21ISYsYQP3XaqjF54jE493ejRNWhegV0SfE3OD2do8I8n 55wZfdynFC3guQk/yvQElvSHWvyZpCg1ZMudkw/5Ua00ZxvfInFrxT6WPKexzIdYwRk8 oNH6CerkE5nA32PM2h9A/6sIxgOgJH+7zSkB2CqIy6a/NEjmav8wN2zLWNRdplJCcS67 +kzw== X-Gm-Message-State: AA+aEWbBcn3jzFCzRjPvwg3+H1Dq0PfFkXW9WsOxCB4xW9BG5wl0c+EJ ITEfjWX13DxxCOaRu77+MQtHHg== X-Received: by 2002:adf:c042:: with SMTP id c2mr16699964wrf.158.1543337780158; Tue, 27 Nov 2018 08:56:20 -0800 (PST) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:3180:41f8:3010:ff61]) by smtp.gmail.com with ESMTPSA id k73sm6383099wmd.36.2018.11.27.08.56.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 08:56:19 -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 v12 17/25] kasan: add bug reporting routines for tag-based mode Date: Tue, 27 Nov 2018 17:55:35 +0100 Message-Id: <996c09ec2c8f11294c106973f3b1a211417fa74e.1543337629.git.andreyknvl@google.com> X-Mailer: git-send-email 2.20.0.rc0.387.gc7a69e6b6c-goog In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 33cc3b0e017e..82a23b23ff93 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,10 +140,14 @@ static inline void quarantine_remove_cache(struct kmem_cache *cache) { } #ifdef CONFIG_KASAN_SW_TAGS +void print_tags(u8 addr_tag, const void *addr); + u8 random_tag(void); #else +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); +} -- 2.20.0.rc0.387.gc7a69e6b6c-goog