Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6018880ybc; Wed, 27 Nov 2019 13:30:41 -0800 (PST) X-Google-Smtp-Source: APXvYqywl97iooewurTOvw046BkyiFdwqddsg+9Jv5C/lSeeicoIumSmNZkkjcQrK79cke+KNcTx X-Received: by 2002:a05:6402:1802:: with SMTP id g2mr31705900edy.253.1574890241465; Wed, 27 Nov 2019 13:30:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574890241; cv=none; d=google.com; s=arc-20160816; b=wme5CM/rihIA1cUMGDmfC6Gg/SPFRJB+w7oXEQOAqYQ8e+X99P7HT3hOQeyEppB154 GTwz5PqlIVqqSzcUdELubLYh//OyjhkS6ndd4xBRqPoScp9SSeh89DmcfcMcQqaLrI8S DWx68e/tcX3GIn5kuL3TlZnfMO871kJyD36dthQv8VErisGGVXtnitm4NZCIB3/z8YDj b5bhBzMrwaHoFXzW6ZBI5x6N0FQHIgmdGtzZbpuTDDSRjv4rk6cY4gaAQ/7H/xFPnDkC P/F5qRHCvbzSVL64LiMdMBjTy4iIw0kgN0+fUvzb6S9SBuFsDTKuIgyPMdYLt9AiEbHP YECA== 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=UJTHZ77IEvS3yFM/aky+/YaxIMAN3JzUR/9YWCS9pGo=; b=wPvuxKJsG9oYByzKLWsod+V9ca+Yl088Od3w3lGzquDC4aDkXaica8LqSpSGUNTCPI JTTAZNH++IiD7yAj1YM9kz2jNGLgwg8mEZ0AF/L8QJkL6vOg+K5oVqV9eOCxqYIlCm9F HXMpIfoIlNr+NX7myQezXO4dJGVCIdBZbF/2Y7/Ea8CsRnbp8QJdwsrX/4sRZWgLsefl 0bIJLmohvPj0c4UBmp6wub5xZNWrPagsKvUNyNqZrXhA/e4Li+T0NXOH3vXwfcMGkcoU dfFzXMlHFS+l5JUQVzt7L29tdWNoxu7dT5Hp6xl/6tgL6U9AcHBLhFOYNGyliweqBZ2I +lDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nNnhhFe+; 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 n9si665893ejc.119.2019.11.27.13.30.18; Wed, 27 Nov 2019 13:30:41 -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=nNnhhFe+; 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 S1727901AbfK0V2E (ORCPT + 99 others); Wed, 27 Nov 2019 16:28:04 -0500 Received: from mail.kernel.org ([198.145.29.99]:50206 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731350AbfK0U6u (ORCPT ); Wed, 27 Nov 2019 15:58:50 -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 9655B2084D; Wed, 27 Nov 2019 20:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574888329; bh=BMFHvk5a88FT5YP9mQsEHlbl8cKcHQllCzfUDXjL5Qg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nNnhhFe+uHY7Pry/lssMSbjZ5w2wE253aTl4gzK7hrOrA8x31pd06Drw66rJvvtoV vIsHj33x01hRYQqnKJZ1Tsv7Ao0uccrceq8js9FNqEU6ITKMSnHcTVHN0tfVhVpcx1 rrYuTissvM1k91xYgGyNp4PACKcHePg0FVkLdEJU= 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.19 090/306] kprobes, x86/ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack Date: Wed, 27 Nov 2019 21:29:00 +0100 Message-Id: <20191127203121.491777539@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127203114.766709977@linuxfoundation.org> References: <20191127203114.766709977@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 6de1fd3d00974..ee696efec99fd 100644 --- a/arch/x86/include/asm/ptrace.h +++ b/arch/x86/include/asm/ptrace.h @@ -236,24 +236,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