Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4866918ybc; Fri, 15 Nov 2019 11:19:10 -0800 (PST) X-Google-Smtp-Source: APXvYqxMnFU08T/r74fNdIonSOv49KpVjgM4ENRRQUQohEbRBNGj8uOEdyTfuQLZxpeEee6NpQxI X-Received: by 2002:a17:906:52c3:: with SMTP id w3mr3215821ejn.122.1573845550850; Fri, 15 Nov 2019 11:19:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573845550; cv=none; d=google.com; s=arc-20160816; b=mDsJc1Ma6P9tmHEof3IhnHR2NZr9p7b5IwgFLIauPERgl4jePm2NOqLuY+tu5wLSy/ hMukv1y5ykCNmxwwmULSVxqweWZ6sgxSpGl33f85xMs57B3o+QXcu/oqEnkOPGu/ccpn U36aScgpOucuC66CGGtnwWHw4CUAZP0EePOe95UCEYk9tGBdGJ1US5wV0G+LLZ1AoN+Y gdxONyix8k/KVWa7ylKNxNaJj3uZrqsHTpkrmrbowiK1HZu10Bgxw69x7sjcMU6rEiOF StdoqnC90+NTyO0lyhAiLcdw5U6RqG5+eJwRPRtrqFKgAFlColzzGZ9vcPmIICKBHm6d oHMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=Z4eVXgHWWbGRrH9BipYQn3sMDOkKflzbIVEAmfiSHc4=; b=Y1iVFvmfiObjnSH0soXV2aR9uHYKrDGXEv1PvE0UI/ouSkmceiJ65JYMqvTWUdCmRT pVSsulc1UvJw5WP7QsEqTOnhdqSo93VeZ0aAPAZlMqDJkkiHvQT4SKg1PvXm1gcseexH 5/eiJfZNSMFCoYTpAakHjdJQXDZSZ/ao2LwSsqZYOMMRHg058vePyVj5tiN6btZk/tB3 bogbAePkNc+sKjlmbFFUQcTmH2M/17ctEsFHmGsZaB4okWgKTTjVRwiMYiUP5AZHJ5yQ S+7CITmIvNOQUKqwnt6Wot9D7rN5JvQIvy8BWU6haTGYdZROXbaNN4DsBdXLzkGNwyfv 17QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qCPoUdJ0; 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 s21si6341763eju.82.2019.11.15.11.18.45; Fri, 15 Nov 2019 11:19:10 -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=qCPoUdJ0; 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 S1726881AbfKOTRp (ORCPT + 99 others); Fri, 15 Nov 2019 14:17:45 -0500 Received: from mail-vk1-f201.google.com ([209.85.221.201]:56071 "EHLO mail-vk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726466AbfKOTRo (ORCPT ); Fri, 15 Nov 2019 14:17:44 -0500 Received: by mail-vk1-f201.google.com with SMTP id n6so4556002vke.22 for ; Fri, 15 Nov 2019 11:17:44 -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=Z4eVXgHWWbGRrH9BipYQn3sMDOkKflzbIVEAmfiSHc4=; b=qCPoUdJ0wzt053R9VhJrlNxU9aqLDf1JU8N77nqnqs1UNRFbPCPE+Fkpv+Gqrpa5B1 7YuLk1nFQJ2IseEsVFRDdBoiSYMjEqCcaJzu20t0EITie6omSxbzd98Yvp5yf1vagYEB jWh+LYL6GzFRM6TbQ2hmff2Oh15gBP8EdiN8c1x7FShABhxNvnFRtl7hlZvQAInMWq2B ha60eUT3ZgCZ7xO3JjJyJHTCHQTz3qP+wbmkn4w1bxnWE4FmDfcom1FhX/0RNA6fNpei AHubL4bsK+sVFtfmX/wTWyPTHOdRrH2YxKBeOm06Gxpb6cF5AZfTnBpCrDpDmKkuRE3h 422Q== 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=Z4eVXgHWWbGRrH9BipYQn3sMDOkKflzbIVEAmfiSHc4=; b=I3cHo80MypREBRt53nhOh/CxTR8g5L1vxgNLigt2YhRRHBFKhAqlUXkBqlCjCTWob0 aWLC19QiQr7zzGMJ2mK6a8ZHkuWRDgcziKCzwX1zyh5Zgc0wwBmqJtKVuJA/sJg1tIPu XhUYQmKVVF5YGq1c3uWg3Qv3K/OHz7SHizp1moXcnBTDBlL4LKq8338CsrNa6vk3fMGn uURWPGLCFH6r1eBEM0XSz/7NEYU5k4iwieDq4dp9iuv2aBJBo7mqfkCLpsf1kBWuDXxW sWfqw55GpY3EU0mbQQeOWgizmp50YrDbYAnp7AODDC0qh0rxSGcwhvzG/fHrnPmANEHJ c0cw== X-Gm-Message-State: APjAAAV/B6xwJ4fcFwfSW0LalXDko+QacmtRhv02NFaDKCP9YdI5joOD CxeME/JWEq0Rl3WTNz8xfJ8DPGxdAA== X-Received: by 2002:a1f:9705:: with SMTP id z5mr9652185vkd.46.1573845463473; Fri, 15 Nov 2019 11:17:43 -0800 (PST) Date: Fri, 15 Nov 2019 20:17:27 +0100 In-Reply-To: <20191115191728.87338-1-jannh@google.com> Message-Id: <20191115191728.87338-2-jannh@google.com> Mime-Version: 1.0 References: <20191115191728.87338-1-jannh@google.com> X-Mailer: git-send-email 2.24.0.432.g9d3f5f5b63-goog Subject: [PATCH v2 2/3] x86/traps: Print non-canonical 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 A frequent cause of #GP exceptions are memory accesses to non-canonical addresses. Unlike #PF, #GP doesn't come with a fault address in CR2, so the kernel doesn't currently print the fault address for #GP. Luckily, we already have the necessary infrastructure for decoding X86 instructions and computing the memory address that is being accessed; hook it up to the #GP handler so that we can figure out whether the #GP looks like it was caused by a non-canonical address, and if so, print that address. While it is already possible to compute the faulting address manually by disassembling the opcode dump and evaluating the instruction against the register dump, this should make it slightly easier to identify crashes at a glance. Signed-off-by: Jann Horn --- Notes: v2: - print different message for segment-related GP (Borislav) - rewrite check for non-canonical address (Sean) - make it clear we don't know for sure why the GP happened (Andy) arch/x86/kernel/traps.c | 45 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index c90312146da0..12d42697a18e 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -56,6 +56,8 @@ #include #include #include +#include +#include #ifdef CONFIG_X86_64 #include @@ -509,6 +511,38 @@ dotraplinkage void do_bounds(struct pt_regs *regs, long error_code) do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, 0, NULL); } +/* + * On 64-bit, if an uncaught #GP occurs while dereferencing a non-canonical + * address, print that address. + */ +static void print_kernel_gp_address(struct pt_regs *regs) +{ +#ifdef CONFIG_X86_64 + u8 insn_bytes[MAX_INSN_SIZE]; + struct insn insn; + unsigned long addr_ref; + + if (probe_kernel_read(insn_bytes, (void *)regs->ip, MAX_INSN_SIZE)) + return; + + kernel_insn_init(&insn, insn_bytes, MAX_INSN_SIZE); + insn_get_modrm(&insn); + insn_get_sib(&insn); + addr_ref = (unsigned long)insn_get_addr_ref(&insn, regs); + + /* Bail out if insn_get_addr_ref() failed or we got a kernel address. */ + if (addr_ref >= ~__VIRTUAL_MASK) + return; + + /* Bail out if the entire operand is in the canonical user half. */ + if (addr_ref + insn.opnd_bytes - 1 <= __VIRTUAL_MASK) + return; + + pr_alert("probably dereferencing non-canonical address 0x%016lx\n", + addr_ref); +#endif +} + dotraplinkage void do_general_protection(struct pt_regs *regs, long error_code) { @@ -547,8 +581,15 @@ do_general_protection(struct pt_regs *regs, long error_code) return; if (notify_die(DIE_GPF, desc, regs, error_code, - X86_TRAP_GP, SIGSEGV) != NOTIFY_STOP) - die(desc, regs, error_code); + X86_TRAP_GP, SIGSEGV) == NOTIFY_STOP) + return; + + if (error_code) + pr_alert("GPF is segment-related (see error code)\n"); + else + print_kernel_gp_address(regs); + + die(desc, regs, error_code); return; } -- 2.24.0.432.g9d3f5f5b63-goog