Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1836424imm; Mon, 3 Sep 2018 10:41:35 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb1T5GKwmcMbpKXNOVwA/PwjWKc2S2hKfU8K9Kq7XcP2bttgUJwB9Tl72/Tw0Ijp96TKqo8 X-Received: by 2002:a63:8241:: with SMTP id w62-v6mr25960846pgd.230.1535996495281; Mon, 03 Sep 2018 10:41:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535996495; cv=none; d=google.com; s=arc-20160816; b=H6HqyKI9nuCa5V20JJFT8+jRl5aGHhloMwjS7ExDleJXoeuek/BP8EryubUSzUIiNZ YKLDEWBErG39fmB7V0p3C1M9Bps6dHZErHfJDgkRBPG9U1/5TDTBZvYLbWv0Au3twaXR eiXFScli1OSpl46gQweXNbhKYOZPf4T5CSJWV4vCHcyNxPrmLRe1sNFM9GOgDpS+PGbR d/+flcdUas4H3T4gUGa6ANdS9N4FNLqgDBK+mO9FxvC61a7z0cA62x8UwFKdpUeDGJXs q4AYqSmpeSVCkb5eJBlVsQvkhkMiINsZfEoMLKAoIMPUQBR8ZjV3UwscGMk2dgEuP/u2 xG5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=Wimc0pE+Zc0xg38RPyocCcxoHoOO36KoKkGNzumZt48=; b=qnx6gzZMk+L8skyHBQFp3DCqlVGX5pfCOOat0Nu2vzXs1F+8HYHirXtq4dBOveQqCR HdDJ5hoPZJDgMZx9SLukL/QQdjts+RbAq2TlrNXooC9ii5e4FaD4hYPByCIJ54ba27Be fKqa+9p2/VMtAPLHUrhA3+F9JPaVBn1FMIjcw3QiyBVWmALTyTUCSPPPSzb+85YoUD4K afKeZWvE89Jijzn6SRo7tRFl4+wcqNPN04btQhWBrjJ7qpAFFKeKYCtaw+dkb7QpjTA9 AiNg280gnhqUMS0xIEabzNz7/BI2hRHDjiyZbcsaknskdTBiMDqe/W2WSXhCh5o2laQM SORw== 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 2-v6si20165699pfd.39.2018.09.03.10.41.20; Mon, 03 Sep 2018 10:41:35 -0700 (PDT) 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 S1731998AbeICWBF (ORCPT + 99 others); Mon, 3 Sep 2018 18:01:05 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49330 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729170AbeICWBF (ORCPT ); Mon, 3 Sep 2018 18:01:05 -0400 Received: from localhost (ip-213-127-74-90.ip.prioritytelecom.net [213.127.74.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 4013BC75; Mon, 3 Sep 2018 17:39:53 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Thomas Gleixner , Kees Cook , Borislav Petkov , "H. Peter Anvin" , Andy Lutomirski , security@kernel.org Subject: [PATCH 4.18 122/123] x86/dumpstack: Dont dump kernel memory based on usermode RIP Date: Mon, 3 Sep 2018 18:57:46 +0200 Message-Id: <20180903165724.647650510@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180903165719.499675257@linuxfoundation.org> References: <20180903165719.499675257@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 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 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit 342db04ae71273322f0011384a9ed414df8bdae4 upstream. show_opcodes() is used both for dumping kernel instructions and for dumping user instructions. If userspace causes #PF by jumping to a kernel address, show_opcodes() can be reached with regs->ip controlled by the user, pointing to kernel code. Make sure that userspace can't trick us into dumping kernel memory into dmesg. Fixes: 7cccf0725cf7 ("x86/dumpstack: Add a show_ip() function") Signed-off-by: Jann Horn Signed-off-by: Thomas Gleixner Reviewed-by: Kees Cook Reviewed-by: Borislav Petkov Cc: "H. Peter Anvin" Cc: Andy Lutomirski Cc: security@kernel.org Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20180828154901.112726-1-jannh@google.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/stacktrace.h | 2 +- arch/x86/kernel/dumpstack.c | 21 +++++++++++++++------ arch/x86/mm/fault.c | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) --- a/arch/x86/include/asm/stacktrace.h +++ b/arch/x86/include/asm/stacktrace.h @@ -111,6 +111,6 @@ static inline unsigned long caller_frame return (unsigned long)frame; } -void show_opcodes(u8 *rip, const char *loglvl); +void show_opcodes(struct pt_regs *regs, const char *loglvl); void show_ip(struct pt_regs *regs, const char *loglvl); #endif /* _ASM_X86_STACKTRACE_H */ --- a/arch/x86/kernel/dumpstack.c +++ b/arch/x86/kernel/dumpstack.c @@ -92,23 +92,32 @@ static void printk_stack_address(unsigne * Thus, the 2/3rds prologue and 64 byte OPCODE_BUFSIZE is just a random * guesstimate in attempt to achieve all of the above. */ -void show_opcodes(u8 *rip, const char *loglvl) +void show_opcodes(struct pt_regs *regs, const char *loglvl) { unsigned int code_prologue = OPCODE_BUFSIZE * 2 / 3; u8 opcodes[OPCODE_BUFSIZE]; - u8 *ip; + unsigned long ip; int i; + bool bad_ip; printk("%sCode: ", loglvl); - ip = (u8 *)rip - code_prologue; - if (probe_kernel_read(opcodes, ip, OPCODE_BUFSIZE)) { + ip = regs->ip - code_prologue; + + /* + * Make sure userspace isn't trying to trick us into dumping kernel + * memory by pointing the userspace instruction pointer at it. + */ + bad_ip = user_mode(regs) && + __chk_range_not_ok(ip, OPCODE_BUFSIZE, TASK_SIZE_MAX); + + if (bad_ip || probe_kernel_read(opcodes, (u8 *)ip, OPCODE_BUFSIZE)) { pr_cont("Bad RIP value.\n"); return; } for (i = 0; i < OPCODE_BUFSIZE; i++, ip++) { - if (ip == rip) + if (ip == regs->ip) pr_cont("<%02x> ", opcodes[i]); else pr_cont("%02x ", opcodes[i]); @@ -123,7 +132,7 @@ void show_ip(struct pt_regs *regs, const #else printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip); #endif - show_opcodes((u8 *)regs->ip, loglvl); + show_opcodes(regs, loglvl); } void show_iret_regs(struct pt_regs *regs) --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -838,7 +838,7 @@ show_signal_msg(struct pt_regs *regs, un printk(KERN_CONT "\n"); - show_opcodes((u8 *)regs->ip, loglvl); + show_opcodes(regs, loglvl); } static void