From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 911DDC432C0 for ; Wed, 27 Nov 2019 23:50:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5961220870 for ; Wed, 27 Nov 2019 23:50:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W5y6tSeM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727581AbfK0Xuk (ORCPT ); Wed, 27 Nov 2019 18:50:40 -0500 Received: from mail-wr1-f73.google.com ([209.85.221.73]:55060 "EHLO mail-wr1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727433AbfK0Xuj (ORCPT ); Wed, 27 Nov 2019 18:50:39 -0500 Received: by mail-wr1-f73.google.com with SMTP id f8so7663738wrw.21 for ; Wed, 27 Nov 2019 15:50:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=+1lx5ocSVx6un1v0Cw2ClVXDDkVMc3TsX0GOf0k847E=; b=W5y6tSeM7/OGLQ1ZTWtsKW5uLMtBKd6X/tgxB0w7/yCSuKXW01xfNqOyaxRu1ju7tQ 1Bdx1x/Lg6+FJA2j2dMX6DUJBoBmBiXn523rrnhIP0L/EQTThVq5B2FfVh4C+dS2xX4K JgpYwNSckA3YgU2tcANqkmbYVldrMk9JDbeq5kxamUxoY3+UznOme8dDTh1fxhBMWJ26 epjjM6e7WUEV723/YTm9UTwbCgE+shXYnja9l7x+UQIy9NWWsrg8xg5xPW9L/VJa/onS mmN0Tu8JZgbp+fXhf8CQP6ejc9Fj/Y2/vG7MisU2G3byzdEFbfyK6abmcnlzh6wCyO0Q nJaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=+1lx5ocSVx6un1v0Cw2ClVXDDkVMc3TsX0GOf0k847E=; b=Gn5UI9Rot4+uHK1l0k7geeO13LSnByYNCNaw5pHuaB9I76WBZl1S+heEXhRjzq+y9S 2ZTYaBRSHP5iLFN8s7Bhp3MHYCIpecnTGLqHxvxkx7c0KNUMLK9fueUZ5DciZnbaVCTM 2iK8M4IuXBKO1vgQs+nMgO48tVGrqLg57IjenPJ5fpVAPiBUbm7zWw3HqF+FsvoUNOTu CKhqRjUqRex5Ve7I0PENCckeSyQ4aQFq86T+49M6iAJrkABs7ryKapaN7vQQteaOTMrI ejUJoX7x3Gj5WBR2Cdj5EvMw180WMQYY8Xb+xNBJtONdHmJ60X8t+BH71eODhhxzrC3h +5cA== X-Gm-Message-State: APjAAAWybtnzlAV7bi6sQApSFAZ3KuJYtktW+jWsOY71JSb0nMFCFfQW 7jXVypUS6I2E7bscTa9DE955p1MeDQ== X-Google-Smtp-Source: APXvYqxlu1HIfDw7/3Xxj1fhARq97fWvjzsFvMco68IF8ULTTCoyyN3NAePBR4emBb+51+5go4AJKqEqMw== X-Received: by 2002:adf:e944:: with SMTP id m4mr10084533wrn.49.1574898636363; Wed, 27 Nov 2019 15:50:36 -0800 (PST) Date: Thu, 28 Nov 2019 00:49:16 +0100 In-Reply-To: <20191127234916.31175-1-jannh@google.com> Message-Id: <20191127234916.31175-4-jannh@google.com> Mime-Version: 1.0 References: <20191127234916.31175-1-jannh@google.com> X-Mailer: git-send-email 2.24.0.432.g9d3f5f5b63-goog Subject: [PATCH v5 4/4] x86/kasan: Print original address on #GP From: Jann Horn To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , kasan-dev@googlegroups.com, jannh@google.com Cc: linux-kernel@vger.kernel.org, Andrey Konovalov , Andy Lutomirski , Sean Christopherson Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make #GP exceptions caused by out-of-bounds KASAN shadow accesses easier to understand by computing the address of the original access and printing that. More details are in the comments in the patch. This turns an error like this: kasan: CONFIG_KASAN_INLINE enabled kasan: GPF could be caused by NULL-ptr deref or user memory access general protection fault probably for non-canonical address 0xe017577ddf75b7dd: 0000 [#1] PREEMPT SMP KASAN PTI into this: general protection fault probably for non-canonical address 0xe017577ddf75b7dd: 0000 [#1] PREEMPT SMP KASAN PTI KASAN: maybe wild-memory-access in range [0x00badbeefbadbee8-0x00badbeefbadbeef] The hook is placed in architecture-independent code, but is currently only wired up to the X86 exception handler because I'm not sufficiently familiar with the address space layout and exception handling mechanisms on other architectures. Signed-off-by: Jann Horn --- Notes: v2: - move to mm/kasan/report.c (Dmitry) - change hook name to be more generic - use TASK_SIZE instead of TASK_SIZE_MAX for compiling on non-x86 - don't open-code KASAN_SHADOW_MASK (Dmitry) - add "KASAN: " prefix, but not "BUG: " (Andrey, Dmitry) - use same naming scheme as get_wild_bug_type (Andrey) - this version was "Reviewed-by: Dmitry Vyukov " v3: - adjusted example output in commit message based on changes in preceding patch - ensure that KASAN output happens after bust_spinlocks(1) - moved hook in arch/x86/kernel/traps.c such that output appears after the first line of KASAN-independent error report v4: - adjust patch to changes in x86/traps patch v5: - adjust patch to changes in x86/traps patch - fix bug introduced in v3: remove die() call after oops_end() arch/x86/kernel/traps.c | 12 ++++++++++- arch/x86/mm/kasan_init_64.c | 21 ------------------- include/linux/kasan.h | 6 ++++++ mm/kasan/report.c | 40 +++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 9b6e4d04112a..a7dade19783a 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -589,6 +590,8 @@ do_general_protection(struct pt_regs *regs, long error_code) if (!user_mode(regs)) { enum kernel_gp_hint hint = GP_NO_HINT; unsigned long gp_addr; + unsigned long flags; + int sig; if (fixup_exception(regs, X86_TRAP_GP, error_code, 0)) return; @@ -621,7 +624,14 @@ do_general_protection(struct pt_regs *regs, long error_code) "maybe for address", gp_addr); - die(desc, regs, error_code); + flags = oops_begin(); + sig = SIGSEGV; + __die_header(desc, regs, error_code); + if (hint == GP_NON_CANONICAL) + kasan_non_canonical_hook(gp_addr); + if (__die_body(desc, regs, error_code)) + sig = 0; + oops_end(flags, regs, sig); return; } diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index 296da58f3013..69c437fb21cc 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -245,23 +245,6 @@ static void __init kasan_map_early_shadow(pgd_t *pgd) } while (pgd++, addr = next, addr != end); } -#ifdef CONFIG_KASAN_INLINE -static int kasan_die_handler(struct notifier_block *self, - unsigned long val, - void *data) -{ - if (val == DIE_GPF) { - pr_emerg("CONFIG_KASAN_INLINE enabled\n"); - pr_emerg("GPF could be caused by NULL-ptr deref or user memory access\n"); - } - return NOTIFY_OK; -} - -static struct notifier_block kasan_die_notifier = { - .notifier_call = kasan_die_handler, -}; -#endif - void __init kasan_early_init(void) { int i; @@ -298,10 +281,6 @@ void __init kasan_init(void) int i; void *shadow_cpu_entry_begin, *shadow_cpu_entry_end; -#ifdef CONFIG_KASAN_INLINE - register_die_notifier(&kasan_die_notifier); -#endif - memcpy(early_top_pgt, init_top_pgt, sizeof(early_top_pgt)); /* diff --git a/include/linux/kasan.h b/include/linux/kasan.h index cc8a03cc9674..7305024b44e3 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -194,4 +194,10 @@ static inline void *kasan_reset_tag(const void *addr) #endif /* CONFIG_KASAN_SW_TAGS */ +#ifdef CONFIG_KASAN_INLINE +void kasan_non_canonical_hook(unsigned long addr); +#else /* CONFIG_KASAN_INLINE */ +static inline void kasan_non_canonical_hook(unsigned long addr) { } +#endif /* CONFIG_KASAN_INLINE */ + #endif /* LINUX_KASAN_H */ diff --git a/mm/kasan/report.c b/mm/kasan/report.c index 621782100eaa..5ef9f24f566b 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -512,3 +512,43 @@ void __kasan_report(unsigned long addr, size_t size, bool is_write, unsigned lon end_report(&flags); } + +#ifdef CONFIG_KASAN_INLINE +/* + * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high + * canonical half of the address space) cause out-of-bounds shadow memory reads + * before the actual access. For addresses in the low canonical half of the + * address space, as well as most non-canonical addresses, that out-of-bounds + * shadow memory access lands in the non-canonical part of the address space. + * Help the user figure out what the original bogus pointer was. + */ +void kasan_non_canonical_hook(unsigned long addr) +{ + unsigned long orig_addr; + const char *bug_type; + + if (addr < KASAN_SHADOW_OFFSET) + return; + + orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT; + /* + * For faults near the shadow address for NULL, we can be fairly certain + * that this is a KASAN shadow memory access. + * For faults that correspond to shadow for low canonical addresses, we + * can still be pretty sure - that shadow region is a fairly narrow + * chunk of the non-canonical address space. + * But faults that look like shadow for non-canonical addresses are a + * really large chunk of the address space. In that case, we still + * print the decoded address, but make it clear that this is not + * necessarily what's actually going on. + */ + if (orig_addr < PAGE_SIZE) + bug_type = "null-ptr-deref"; + else if (orig_addr < TASK_SIZE) + bug_type = "probably user-memory-access"; + else + bug_type = "maybe wild-memory-access"; + pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type, + orig_addr, orig_addr + KASAN_SHADOW_MASK); +} +#endif -- 2.24.0.432.g9d3f5f5b63-goog