Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp5977436ybc; Wed, 27 Nov 2019 12:45:50 -0800 (PST) X-Google-Smtp-Source: APXvYqwI3r5A0019XEgC7TmF8gRBIH4E80h2kkK31NVitof4sm+50pqS5mw6UzyPDHk9pnExFnZs X-Received: by 2002:aa7:d6c8:: with SMTP id x8mr4478102edr.300.1574887550035; Wed, 27 Nov 2019 12:45:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574887550; cv=none; d=google.com; s=arc-20160816; b=qMTRItlCIr3FpreYftzq+ClFEza5Gld2L1IOWVz4Lcv+L9edLecaGapc61thUbp4rP NJr9sVO19YB6ZydQ5E+xdU/BoA/WSxbco4ZZolagyFEuMLzOqLurDpVmwqkQoQqa7HGe DWrFuG6ENT7e+4oL6gv84AIR6VDaUxuKqjdUGix7mdcdRETPb0jeAAJ6MODena6HYs4u C5ACCmP9htaFUoeeWApp12Dr1SVlo3Fju0dwIJGKC1eRUwfHznzdOKRsfPW1rZ2yIlBQ ywVdrxGZSPvDP2QPe/DBaC6d7iXG+Uua5JU0kBAdh0ec/ddV51bEg93v82kh6ciuiWKZ Xd3A== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=E6UHCr8CRcAcX0hdRp5/PMHgzbtd3FmKPSVhKTays7Q=; b=NddCz8IJ54MXbyeafNmrQ0AoCVrf6YrO9puvgUa4WhxbUlPW9FTaZ1EMqWrOQweMsp V6wxrdGlmUgtvt3bpwq6gC9OASiVDYUd61fRiJwDz3nA2Aj5ebESOPZDLXWSDXIgriIW /VPncpl7dnXBS2xdhgZUvDv7OY1iJzYNwFO28IZl8B2gs/K8PaiRPt5uocB7zCl5p3T+ Bi/ktDjcRdbDrKluet9TDba0ByL9mGSI7Lk0HFKm1wavWUp/O5ai3GoMAWc2hTz6Xglj f40EhCki60Gih8w+Y0956OOPaTP2tm+T4cKRKOtLYU99baZtSF5X4drVf1XcTVh3dyix YOCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2O6y5+F4; 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 bx7si4051529edb.403.2019.11.27.12.45.25; Wed, 27 Nov 2019 12:45:50 -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=@kernel.org header.s=default header.b=2O6y5+F4; 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 S1727685AbfK0UlA (ORCPT + 99 others); Wed, 27 Nov 2019 15:41:00 -0500 Received: from mail.kernel.org ([198.145.29.99]:45800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729108AbfK0Uk4 (ORCPT ); Wed, 27 Nov 2019 15:40:56 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EE135215A5; Wed, 27 Nov 2019 20:40:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574887256; bh=nZxJ00STl8IMFOGnrMdWwzfFhRdOIpZHkd0P/ot4qpM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2O6y5+F4uJMZXc6dQTfNAwVwRLSGmFZZ51dnzdIS5gSW0/KQkJDQeP73TLQJ7XJ/o 8JvTFKcDZquHbfeXoQPzV7dCVGntbQ3Hv0Itb+0hPiB/CHYYZQn1OrGk3jmY+joogY oeXQv+kba2mfV/ayQ0vz0RCG3dEZL3G5HAjTotpY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Lutomirski , "Steven Rostedt (VMware)" , "Joel Fernandes (Google)" , Borislav Petkov , Josh Poimboeuf , Linus Torvalds , Masami Hiramatsu , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 4.9 040/151] kprobes, x86/ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack Date: Wed, 27 Nov 2019 21:30:23 +0100 Message-Id: <20191127203025.510308881@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203000.773542911@linuxfoundation.org> References: <20191127203000.773542911@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Steven Rostedt (VMware) [ Upstream commit c2712b858187f5bcd7b042fe4daa3ba3a12635c0 ] Andy had some concerns about using regs_get_kernel_stack_nth() in a new function regs_get_kernel_argument() as if there's any error in the stack code, it could cause a bad memory access. To be on the safe side, call probe_kernel_read() on the stack address to be extra careful in accessing the memory. A helper function, regs_get_kernel_stack_nth_addr(), was added to just return the stack address (or NULL if not on the stack), that will be used to find the address (and could be used by other functions) and read the address with kernel_probe_read(). Requested-by: Andy Lutomirski Signed-off-by: Steven Rostedt (VMware) Reviewed-by: Joel Fernandes (Google) Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Josh Poimboeuf Cc: Linus Torvalds Cc: Masami Hiramatsu Cc: Peter Zijlstra Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/20181017165951.09119177@gandalf.local.home Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- arch/x86/include/asm/ptrace.h | 42 +++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h index ea78a8438a8af..fb489cd848faa 100644 --- a/arch/x86/include/asm/ptrace.h +++ b/arch/x86/include/asm/ptrace.h @@ -199,24 +199,52 @@ static inline int regs_within_kernel_stack(struct pt_regs *regs, (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))); } +/** + * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack + * @regs: pt_regs which contains kernel stack pointer. + * @n: stack entry number. + * + * regs_get_kernel_stack_nth() returns the address of the @n th entry of the + * kernel stack which is specified by @regs. If the @n th entry is NOT in + * the kernel stack, this returns NULL. + */ +static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n) +{ + unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); + + addr += n; + if (regs_within_kernel_stack(regs, (unsigned long)addr)) + return addr; + else + return NULL; +} + +/* To avoid include hell, we can't include uaccess.h */ +extern long probe_kernel_read(void *dst, const void *src, size_t size); + /** * regs_get_kernel_stack_nth() - get Nth entry of the stack * @regs: pt_regs which contains kernel stack pointer. * @n: stack entry number. * * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which - * is specified by @regs. If the @n th entry is NOT in the kernel stack, + * is specified by @regs. If the @n th entry is NOT in the kernel stack * this returns 0. */ static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) { - unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); - addr += n; - if (regs_within_kernel_stack(regs, (unsigned long)addr)) - return *addr; - else - return 0; + unsigned long *addr; + unsigned long val; + long ret; + + addr = regs_get_kernel_stack_nth_addr(regs, n); + if (addr) { + ret = probe_kernel_read(&val, addr, sizeof(val)); + if (!ret) + return val; + } + return 0; } #define arch_has_single_step() (1) -- 2.20.1