Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp16290026ybl; Wed, 1 Jan 2020 02:09:58 -0800 (PST) X-Google-Smtp-Source: APXvYqzVTSiSnRegd97SLrtELmax03Bwsz8SaChXqW67eq9Q7w42xlmT0usm8VK59B4RtImxwAlc X-Received: by 2002:a17:906:8250:: with SMTP id f16mr81462788ejx.304.1577873398664; Wed, 01 Jan 2020 02:09:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577873398; cv=none; d=google.com; s=arc-20160816; b=aSLytKmYdBkgJZDABEMGJzfoyn0beXNKq41xj++3MEbY0vlMxUcVEMR0VC/cGRiqpi HiljwDqWYFcAajyRJBOlOAqb5mFE2b4XFEKZVs43u8jB3r7QEsL7rhNmiysUvI2jeNDz Fif6UJt8wTwPTHr2bX6B1v+DS8LdOTtPmUsVnsRi2YO5exyuflRZVzdJpIWARopXCnir 4z0THlGSm+85jezUuLhyHLyP41rFzUQxOZ40gbbMb3Ye8GTpdp0exhmXvzRqG3z5EgoT 1xzuTP0RDVKHz9JeObKvPTNScmJdhaAj1wTjXm4jG54xuvVmRfAgg5W7/YB6qb6w5iAl KoJQ== 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 :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=7x6ZxTeX/T9IRYDy8G4cz8njUXnLYigPjIJD7YHmwec=; b=bPWMHKdN6+GRkb9JvjzSAqzXUE05HoBf5aV5gTkxcXhNyZFrmd3jHbH/90k+HVWkdl tEIpAOF1nFqdM+vgb8o8WyFIu8+7jJLAh/wYwefTDTP8HkbWTgcfnNEp9LFONBLrNS3K uwPeUjs40HLKT1tsvOngfWjuCnzmGeFBWtSrH6dp0uuqeWKJNhPf8rO7dVtHhFNZoAki KSDt4Y//cVvDbJczy3FrxWbO5xP7KW7w8zVZvwJWn38umvyGFnX8hl7gZheY6CIHV7Cn PgcvCadMdiur3AFHuNZew2xCorTVZRJt9svWV9XmQ2C6+1TI5MaTXhVpMvMj79Xc4eSv O2MA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j57si32733471ede.415.2020.01.01.02.09.21; Wed, 01 Jan 2020 02:09:58 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727132AbgAAKHc (ORCPT + 99 others); Wed, 1 Jan 2020 05:07:32 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:52325 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726687AbgAAKHb (ORCPT ); Wed, 1 Jan 2020 05:07:31 -0500 Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1imauH-0004P0-MQ; Wed, 01 Jan 2020 11:07:14 +0100 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id 169A81C2BFF; Wed, 1 Jan 2020 11:07:13 +0100 (CET) Date: Wed, 01 Jan 2020 10:07:12 -0000 From: "tip-bot2 for Jann Horn" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/core] x86/kasan: Print original address on #GP Cc: Jann Horn , Borislav Petkov , Dmitry Vyukov , Alexander Potapenko , Andrew Morton , Andrey Konovalov , Andrey Ryabinin , Andy Lutomirski , Dave Hansen , "H. Peter Anvin" , Ingo Molnar , kasan-dev@googlegroups.com, "linux-mm" , Peter Zijlstra , Sean Christopherson , Thomas Gleixner , "x86-ml" , LKML In-Reply-To: <20191218231150.12139-4-jannh@google.com> References: <20191218231150.12139-4-jannh@google.com> MIME-Version: 1.0 Message-ID: <157787323296.30329.6279558999926427913.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/core branch of tip: Commit-ID: 2f004eea0fc8f86b45dfc2007add2d4986de8d02 Gitweb: https://git.kernel.org/tip/2f004eea0fc8f86b45dfc2007add2d4986de8d02 Author: Jann Horn AuthorDate: Thu, 19 Dec 2019 00:11:50 +01:00 Committer: Borislav Petkov CommitterDate: Tue, 31 Dec 2019 13:15:38 +01:00 x86/kasan: Print original address on #GP 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 Signed-off-by: Borislav Petkov Reviewed-by: Dmitry Vyukov Cc: Alexander Potapenko Cc: Andrew Morton Cc: Andrey Konovalov Cc: Andrey Ryabinin Cc: Andy Lutomirski Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Ingo Molnar Cc: kasan-dev@googlegroups.com Cc: linux-mm Cc: Peter Zijlstra Cc: Sean Christopherson Cc: Thomas Gleixner Cc: x86-ml Link: https://lkml.kernel.org/r/20191218231150.12139-4-jannh@google.com --- arch/x86/kernel/dumpstack.c | 2 ++- arch/x86/mm/kasan_init_64.c | 21 +------------------- include/linux/kasan.h | 6 +++++- mm/kasan/report.c | 40 ++++++++++++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c index 8995bf1..ae64ec7 100644 --- a/arch/x86/kernel/dumpstack.c +++ b/arch/x86/kernel/dumpstack.c @@ -427,6 +427,8 @@ void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr) int sig = SIGSEGV; __die_header(str, regs, err); + if (gp_addr) + kasan_non_canonical_hook(gp_addr); if (__die_body(str, regs, err)) sig = 0; oops_end(flags, regs, sig); diff --git a/arch/x86/mm/kasan_init_64.c b/arch/x86/mm/kasan_init_64.c index cf5bc37..763e71a 100644 --- a/arch/x86/mm/kasan_init_64.c +++ b/arch/x86/mm/kasan_init_64.c @@ -288,23 +288,6 @@ static void __init kasan_shallow_populate_pgds(void *start, void *end) } while (pgd++, addr = next, addr != (unsigned long)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; @@ -341,10 +324,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 e18fe54..5cde9e7 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -228,4 +228,10 @@ static inline void kasan_release_vmalloc(unsigned long start, unsigned long free_region_end) {} #endif +#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 6217821..5ef9f24 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