Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp2117027imm; Thu, 18 Oct 2018 09:16:39 -0700 (PDT) X-Google-Smtp-Source: ACcGV62cRA+aix0uPAT6N9CrGMi4+Em364s6Bf/k+hFssTLiPswNo4GHB+wdhsvaAvX3Fg2qH/IC X-Received: by 2002:a17:902:b696:: with SMTP id c22-v6mr30783914pls.37.1539879399379; Thu, 18 Oct 2018 09:16:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539879399; cv=none; d=google.com; s=arc-20160816; b=yL3jBUo36/5gSeuohnCJ2JvPdFOpiqxDez/9/c6cpb8v8zeKItR/8QDgvGPxAaefO5 cRM1P5C0agLifc7z0JNr56IKnBCZr6tJXqkA3OiPCi6NhVWt59FQR8mZbgyQ7mQeB2QQ YD181FgGF7T1Ud8d25P/OOy1+5A1Z7J+zDJcQuS3geucs+xkfKdTQQ/ADLwi42vyU8Cl 2dcCcNkcQaogqhOusWdlQX9pQXe5nmY91XKtit5PQVQcq0CcpEmeTa2OSw4d6QhPNs3q m7sAV0IlX0OwPh4yX43ykeIeTxv63lxAb02/ywkliQQcfjxxbLXf4kmcgBEXK/93AsCM yE/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=c+0w4TQFXALKjzVtplGnw98zDzfDC/hGTKH5PEVO/Bo=; b=FXV74+TFsFDQZCijo0PXrlWbRX6Zsqd3ypaH5QtrKRz8GQ46EjlbusPIUORXZZpR7T W5olDMS+9HVFHs5f97wfY3ZWl/+ja+jQYNbw65MnYV6YrpYueO7ILYKTeo+mQV0We1X5 uLv0Q+So/6nrbFje4U7kKUzhQxFnXiYO8A6U1L5F8/lazuFf/Q/JteQAy1oltmmxTouK m851SR5h85NgHTjUOa8cnO88YXUVE+Os/NXRbPT3XHJ7SaJ1P62MtJ+UIkEmSSmbn+w3 bFEwvdSBEkCs+wQiLHX2RfLJBuNcfYlmNspvj4x70xo+ffFdC/4xeP5O7svFjrGPsyDr n7CQ== 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 i21-v6si22041073pgj.51.2018.10.18.09.16.23; Thu, 18 Oct 2018 09:16:39 -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 S1728549AbeJSAPi (ORCPT + 99 others); Thu, 18 Oct 2018 20:15:38 -0400 Received: from terminus.zytor.com ([198.137.202.136]:35099 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728427AbeJSAPi (ORCPT ); Thu, 18 Oct 2018 20:15:38 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id w9IGCs5L882770 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 18 Oct 2018 09:12:54 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w9IGCsGY882767; Thu, 18 Oct 2018 09:12:54 -0700 Date: Thu, 18 Oct 2018 09:12:54 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: "tip-bot for Steven Rostedt (VMware)" Message-ID: Cc: hpa@zytor.com, mhiramat@kernel.org, joel@joelfernandes.org, tglx@linutronix.de, bp@alien8.de, luto@amacapital.net, rostedt@goodmis.org, torvalds@linux-foundation.org, mingo@kernel.org, jpoimboe@redhat.com, linux-kernel@vger.kernel.org, peterz@infradead.org Reply-To: rostedt@goodmis.org, luto@amacapital.net, bp@alien8.de, tglx@linutronix.de, joel@joelfernandes.org, mhiramat@kernel.org, hpa@zytor.com, peterz@infradead.org, jpoimboe@redhat.com, linux-kernel@vger.kernel.org, mingo@kernel.org, torvalds@linux-foundation.org In-Reply-To: <20181017165951.09119177@gandalf.local.home> References: <20181017165951.09119177@gandalf.local.home> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] kprobes, x86/ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack Git-Commit-ID: c2712b858187f5bcd7b042fe4daa3ba3a12635c0 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_96_Q autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: c2712b858187f5bcd7b042fe4daa3ba3a12635c0 Gitweb: https://git.kernel.org/tip/c2712b858187f5bcd7b042fe4daa3ba3a12635c0 Author: Steven Rostedt (VMware) AuthorDate: Wed, 17 Oct 2018 16:59:51 -0400 Committer: Ingo Molnar CommitDate: Thu, 18 Oct 2018 08:28:35 +0200 kprobes, x86/ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack 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 --- 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 6de1fd3d0097..ee696efec99f 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)