Received: by 10.223.185.116 with SMTP id b49csp4172886wrg; Mon, 19 Feb 2018 12:31:39 -0800 (PST) X-Google-Smtp-Source: AH8x227ITumFCrbYY1R56xxnZbhGZ3pYcJqGkj2NrEpz5Uq5ruQrJ5o5Osyq70GTiGPjzB7fTulr X-Received: by 10.99.44.22 with SMTP id s22mr6920234pgs.111.1519072299754; Mon, 19 Feb 2018 12:31:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519072299; cv=none; d=google.com; s=arc-20160816; b=Wjd8hCZfFNUTOOubBkxdyZh/+PytjqpqbMA0QZ1pq6QH6yYQcQESjcEjUIjW/PYpiy 8wkJ8pwBo5BQTPLhTwrYcwoKPRDatnZXpfS68QmxZn78FzVsOqh6LzJK6nL2LxWciNjX Rxmg40dhj6/GfKURIq24csMFpbEh0fvCroO+R2ATbDKtDJ3EfA9GhyvHkphKArxydt// XGf0r/JnJ1YLGSRWFdUWvrqMx4JVu/gRAHY++0Y7HRnwjJK1970PtyfTRaR+D11HBE5F KT68KlhS12AGUBjEriQgCEwgQ0dDfNi76NopNIIvMlKcheJVuIwSBCauHR4mxFZofDKI YJ7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=bqszpFsE/GeWwDstaIN7vjnr76u9ntscIpCzq5yGqBo=; b=uAVWwSh7x37PlV8fgR1T/QWZO/hYaeZsTx/Uyu8a48KCN2mKFGAjEd4a1fzLyIvE3x IdJQ0aDAQs2Eqf1i3OipfXMGvRVEQomDc1+Mxo9YneFe0PrkPBWm09SLEJai5G3nvbES mUsAUNVD2LWJuOPMHuHlIn/G5y20VIQCptkDTCd5j1cAXZuA8/Rq77satrh1eFaOlbKy oZ6GlaJPIvfs0XQpvJfkbNl+jgv08cJyQBJgTo569XizYBrjdLwQRIVaqScTty/TGdCT aidkA1Gw2VsD404S6WsKTdYAOUXIYXYCWPpN4bM3rs0r79M4C7g77fgSL4ct6gUQt30a 1ChA== 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 w5si9710991pgs.259.2018.02.19.12.31.25; Mon, 19 Feb 2018 12:31:39 -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 S932391AbeBSU24 (ORCPT + 99 others); Mon, 19 Feb 2018 15:28:56 -0500 Received: from mail.skyhub.de ([5.9.137.197]:37760 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932232AbeBSU2x (ORCPT ); Mon, 19 Feb 2018 15:28:53 -0500 X-Virus-Scanned: Nedap ESD1 at mail.skyhub.de Received: from mail.skyhub.de ([127.0.0.1]) by localhost (blast.alien8.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id b3dVpJHekRqE; Mon, 19 Feb 2018 21:28:51 +0100 (CET) Received: from pd.tnic (p200300EC2BC81D00F5823ED58EA92312.dip0.t-ipconnect.de [IPv6:2003:ec:2bc8:1d00:f582:3ed5:8ea9:2312]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 740D51EC08BF; Mon, 19 Feb 2018 21:28:51 +0100 (CET) From: Borislav Petkov To: X86 ML Cc: Peter Zijlstra , Josh Poimboeuf , Andy Lutomirski , LKML Subject: [PATCH 3/5] x86/dumpstack: Improve opcodes dumping in the Code: section Date: Mon, 19 Feb 2018 21:28:24 +0100 Message-Id: <20180219202826.19797-4-bp@alien8.de> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20180219202826.19797-1-bp@alien8.de> References: <20180219202826.19797-1-bp@alien8.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov The code used to iterate byte-by-byte over the bytes around RIP and that is expensive: disabling pagefaults around it, copy_from_user, etc... Make it read the whole buffer of code_bytes size in one go. By default use a statically allocated 64 bytes buffer. If "code_bytes=" is supplied on the cmdline a new buffer gets allocated. Also, do the PAGE_OFFSET check outside of the function because latter will be reused in other context. Signed-off-by: Borislav Petkov --- arch/x86/kernel/dumpstack.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c index 954b1dd1c04a..aef0d98cc282 100644 --- a/arch/x86/kernel/dumpstack.c +++ b/arch/x86/kernel/dumpstack.c @@ -22,9 +22,13 @@ #include #include +#define OPCODE_BUFSIZE 64 + int panic_on_unrecovered_nmi; int panic_on_io_nmi; -static unsigned int code_bytes = 64; +static unsigned int code_bytes = OPCODE_BUFSIZE; +static u8 __opc[OPCODE_BUFSIZE]; +static u8 *opcodes = __opc; static int die_counter; bool in_task_stack(unsigned long *stack, struct task_struct *task, @@ -358,8 +362,8 @@ void die(const char *str, struct pt_regs *regs, long err) static int __init code_bytes_setup(char *s) { - ssize_t ret; unsigned long val; + ssize_t ret; if (!s) return -EINVAL; @@ -372,35 +376,37 @@ static int __init code_bytes_setup(char *s) if (code_bytes > 8192) code_bytes = 8192; + if (code_bytes > OPCODE_BUFSIZE) { + u8 *new_buf = kzalloc(code_bytes, GFP_KERNEL); + if (!new_buf) + return -EINVAL; + + opcodes = new_buf; + } + return 1; } __setup("code_bytes=", code_bytes_setup); static void show_opcodes(u8 *rip) { - unsigned int code_prologue = code_bytes * 43 / 64; - unsigned int code_len = code_bytes; - unsigned char c; + unsigned int code_prologue = code_bytes * 43 / OPCODE_BUFSIZE; u8 *ip; int i; printk(KERN_DEFAULT "Code: "); ip = (u8 *)rip - code_prologue; - if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { - /* try starting at IP */ - ip = (u8 *)rip; - code_len = code_len - code_prologue + 1; + if (probe_kernel_read(opcodes, ip, code_bytes)) { + pr_cont(" Bad RIP value.\n"); + return; } - for (i = 0; i < code_len; i++, ip++) { - if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { - pr_cont(" Bad RIP value."); - break; - } + + for (i = 0; i < code_bytes; i++, ip++) { if (ip == (u8 *)rip) - pr_cont("<%02x> ", c); + pr_cont("<%02x> ", opcodes[i]); else - pr_cont("%02x ", c); + pr_cont("%02x ", opcodes[i]); } pr_cont("\n"); } @@ -422,6 +428,10 @@ void show_regs(struct pt_regs *regs) */ if (!user_mode(regs)) { show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT); - show_opcodes((u8 *)regs->ip); + + if (regs->ip < PAGE_OFFSET) + pr_cont(" Bad RIP value.\n"); + else + show_opcodes((u8 *)regs->ip); } } -- 2.13.0