From patchwork Tue May 8 17:20:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 10386637 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 9800360353 for ; Tue, 8 May 2018 17:22:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8200C290B3 for ; Tue, 8 May 2018 17:22:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 75C5E290B9; Tue, 8 May 2018 17:22:01 +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 AE111290B3 for ; Tue, 8 May 2018 17:22:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07A066B02D3; Tue, 8 May 2018 13:21:39 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 02C846B02D5; Tue, 8 May 2018 13:21:38 -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 DBE876B02D6; Tue, 8 May 2018 13:21:38 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f199.google.com (mail-wr0-f199.google.com [209.85.128.199]) by kanga.kvack.org (Postfix) with ESMTP id 83DC16B02D3 for ; Tue, 8 May 2018 13:21:38 -0400 (EDT) Received: by mail-wr0-f199.google.com with SMTP id w14-v6so21774398wrk.22 for ; Tue, 08 May 2018 10:21:38 -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:in-reply-to:references; bh=eBYOTNDzOJ/AEFeyEKP9NQWRFTjBXtEDfLLt3CwLefI=; b=g0c80N+/PUXR6Tcwc+opcEsq+TLpYcgCbNDdep+l0EBWciR+e3xeuJ78pB+QoZ3jS0 1qiSS9ejyVrsvc2R+wTJuOjTGQx/QPy8LwnvEPNaTrUZw5xZq4FY21u/EWbVgvuOkdKa O7p3acnq1iip9KKjyzMDJQe8PfwQyRa5DJEke8NzTGzD2SyQIMesPtq4zhtsI7vt2smX vJw/xEQjFw7+2fsu7yMfgyQe9O1idv3xlexjxLvvwh0z1e6zOKOqk94aqtUb9IRKLGr/ 54kOpeTyJeZaxFtEZNbeCjDYwti3Iy4xJQ8zPywKQOlexFgn9O3Muw1qlDp2mHcW9zFt rmvg== X-Gm-Message-State: ALQs6tDsZOr88SUY8PG+rWoq4VArOMHEmPoITN4CaqmPaAu9qbDaz9sV eRKK74TnKfUYUTovn5+anqdRjXNbGGMEGoTWFRxajpM4b6Zeivs8vMJLt+5s3L5lOSPFZaBdRCj W5Mym/H3h3vjxRGvbqL+GOFaeGoE1jwUgw3Y8xyjlIijdVNrXBwWVwBG10bBU8SM/WQef+YMlLv E4bxhh8FOYWKv1xqmcIRDock2TUSRNJsEdbFfmCYB07UiAPiZdmm0llj4jjyeAc8dDyX37LuaRt xPrX21FTFx/b+V2kM6Gz/9Gt/zMeTw0yb/Iewm/alaNXSIH3e42pkX6O1FAUCkA/8OJjFKTdfty jMejsfp3apBHqJfsWxr0ho0Ss75Us67rUp02gxsG5Bo8OjBnPcro+TXygyolN0yCaGNUf8u83/s r X-Received: by 2002:adf:aead:: with SMTP id y42-v6mr35601639wrc.23.1525800098083; Tue, 08 May 2018 10:21:38 -0700 (PDT) X-Received: by 2002:adf:aead:: with SMTP id y42-v6mr35601586wrc.23.1525800097204; Tue, 08 May 2018 10:21:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525800097; cv=none; d=google.com; s=arc-20160816; b=tCDF5EdtM1vJvVRcZ8zLISoEs6XcoP1LgPkystj1Urta2hwgJF/geRVzk5gA8rOJTr wLu79lNVGhosjJALwn1+1IhTN07oL3D1GIuAWN7znvtCakhE/3rcLy3DSoG0Zva59gQX bcQMvwwsHoNNULffzoaiKsuryidR9uarLntJfH8/YgpFNQtq72P659jNiCil67swbYht vCmog2zxQy7coANWNOSYiCVD3z1OQLK9LBB7d+Qrzu8n8blUVZNTF1eUJVJFpMduQE3z ER/l5sYh3x6wUgrxo4+4UYJpciShICHVJXF0yQ3RRzpDlrcUZ2aSBq0Sj4IkkWijl5QT jyEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=eBYOTNDzOJ/AEFeyEKP9NQWRFTjBXtEDfLLt3CwLefI=; b=HQCaT3EQa1lpCDI6LFzwG76EHKRHL9J78Ww4Z6yhFYY1j/LXg002C6x+poz0XLaFgW 7rvYiJe+tifByeSCEwVxbWMreIBht0BmbcYFncEbqodsy4ZKmV/frbHExW9wttl4O+T/ VLrwvy7qfu21NNMRY9KQSV20xz949OXfE1UKj8l25buuUIsVM4wuu4Ihe08lhY5f2GYl yZ7znVZJ7DUrwxPvY/cXRiTStAcQx0s7171BP0GMWgpihNNvRndUhvR/Ip6ltqH1EYcO np6lvUCv7z/vBAbh+nOzXQD39ySnYotUYRvfdPTBJHULMfuHHAf9knzqQzHbTt9VE3db iYTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hDtcMEva; 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 t21-v6sor57356wrb.77.2018.05.08.10.21.37 for (Google Transport Security); Tue, 08 May 2018 10:21:37 -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=hDtcMEva; 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 :in-reply-to:references; bh=eBYOTNDzOJ/AEFeyEKP9NQWRFTjBXtEDfLLt3CwLefI=; b=hDtcMEvaR7ZWAgrRfCh1VWJUIl1sGXGmx4fPZiKngWsGb5/0HrMpNqk1Bqt/nOsPR1 uKk+LvYK3ObPjeRR8PF9Eunesqz2rdWsp3ET+SlqbmAawf9tD7IZzYc6z+fbh793fu80 l4O6c3raDGvoY7iurayS5C/xBNYkNLTX5GkraGeiyAW21V9Q+0kHTwJTvnWRqauMHY+3 s41xIFJqSzZcSxSN9HJ0q5yKvRQU+SrFGZX9cO9KSJoe4Ti2sGDm1GbXzdhxl6LMvV+O Ix41a5Cw2iKIKn96Sga89Uo2fwGeIO1wl24q59/IRd2L67g0RBolgH+TVo76ojfQElFt v9pQ== X-Google-Smtp-Source: AB8JxZqG+rEh7klQ+p6hE5DkJTRBX+DH3QTdOSw2sQcEXnuH68j8QvMwP85HAYkUicv1u3eHeb+Mkw== X-Received: by 2002:adf:a00d:: with SMTP id k13-v6mr33124423wrk.39.1525800096161; Tue, 08 May 2018 10:21:36 -0700 (PDT) Received: from andreyknvl0.muc.corp.google.com ([2a00:79e0:15:10:84be:a42a:826d:c530]) by smtp.gmail.com with ESMTPSA id m134sm14178311wmg.4.2018.05.08.10.21.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 May 2018 10:21:35 -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 v1 12/16] khwasan: add bug reporting routines Date: Tue, 8 May 2018 19:20:58 +0200 Message-Id: <0474f59eee0b14a7e72264efa2fcff276f03839f.1525798754.git.andreyknvl@google.com> X-Mailer: git-send-email 2.17.0.441.gb46fe60e1d-goog In-Reply-To: References: 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.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 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.h b/mm/kasan/kasan.h index a76aee9e095f..620941d1e84f 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -128,8 +128,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); }