Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1562905imm; Wed, 17 Oct 2018 23:49:33 -0700 (PDT) X-Google-Smtp-Source: ACcGV60LGiptc77jnqd+/ygF8pU9dK5LS7BJCavKO0FNIrmQVKUshmnwHGxZhLo+JM1SvYapfcGW X-Received: by 2002:a63:9c3:: with SMTP id 186-v6mr27528377pgj.249.1539845373491; Wed, 17 Oct 2018 23:49:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539845373; cv=none; d=google.com; s=arc-20160816; b=zpV5DlYqbclUSP4q0CF70Lt1LxKezkTZm3odeZgs0SZDEusaVLQHCH5s0UkDyfDoKx 8Si8bNjNwxdUsZow3f1TNXcc1ltxR/8nTqEd7N/tc761/iq2Qih0VCIz0EUfyfdkPD1S +mUgvuFCaDaGa4Df0r8ujOGuv+e1WDfd5+kf5Fg4uPeIW+fb+OVAVfLCvMGo+FAn2vyb HvclsAT4II+2p5JMR6RbQQyNmxMhjQAK9m7f8NDlC4Ai8bKmMTS0FVW6j+u/ZeOJ2gJZ 052onqLbM+ZV/ObN42O2um3xZwgoe/fZGExTJcqnR3+HWxIcQ9PXPBwNVLeJFL32bSmZ +sPw== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=jBS4G+/z5xKz5iekJqYoKcJjYviAf44kkLRjBkPSnwA=; b=O/co5Jby9+h15mQwMxHLLwxC5lfDKSMk+KCsNluirFZBDrrT4lh1lNZJdSDoKBJB1W KNUALqze/jvmXjY08HjtiT+y6vuRUI9YxcTHfn5rhU+H7ojdMAlTeeiCqjwrvdjismot 7BFn9CIfDnzkGQJEhjGMdctFB05beu0GvqoFcz61u+bFNq2ckQIIRTnBn0p4uNr+fo8U wuXE707i2EYUV9BggvphqgIwe7UM0ikFrkXh0XoFv+lrcBpmE+pf61AeOftjluF2dAUo gxNwp8khF70DCqtEVoGZDKfWZMxOrIyLjk6wBiyiXecOi0SuX2mg+3/sHnsrOv7+BbRp uMCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=W4oOx8Wd; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t90-v6si20552261pfi.221.2018.10.17.23.49.15; Wed, 17 Oct 2018 23:49:33 -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; dkim=pass header.i=@kernel.org header.s=default header.b=W4oOx8Wd; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727498AbeJROsV (ORCPT + 99 others); Thu, 18 Oct 2018 10:48:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:36432 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbeJROsV (ORCPT ); Thu, 18 Oct 2018 10:48:21 -0400 Received: from devbox (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (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 EFB0421473; Thu, 18 Oct 2018 06:48:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539845330; bh=yUBR8uWXBdESI9vTVsRfMmfIuy8cgWJmA5JK7nKpzXU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=W4oOx8WdDZ0Nq2jlmoOMO09bX1Z7ByNCc+x1uR6923IINe3eMfILaPaFrh4XLxy8y pSW9NZav6oMDBsbrpYLJRJ5ajcfRqeeXnxVxpWVrq85SrZJhrBWrA3xX3Ok1loycYK xIFgjT6eNwLND3KBpzfWpHoxuuqUrgGmmgNGK4xM= Date: Thu, 18 Oct 2018 15:48:46 +0900 From: Masami Hiramatsu To: Steven Rostedt Cc: Andy Lutomirski , LKML , Masami Hiramatsu , Joel Fernandes , Thomas Gleixner , Peter Zijlstra , "H. Peter Anvin" , Ingo Molnar , Josh Poimboeuf , Borislav Petkov Subject: Re: [PATCH v2] x86: ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack Message-Id: <20181018154846.8d055466316359525b94a87a@kernel.org> In-Reply-To: <20181017165951.09119177@gandalf.local.home> References: <20181017165951.09119177@gandalf.local.home> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 17 Oct 2018 16:59:51 -0400 Steven Rostedt wrote: > From: "Steven Rostedt (VMware)" > > 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(). > > Link: http://lkml.kernel.org/r/CALCETrXn9zKTb9i1LP3qoFcpqZHF34BdkuZ5D3N0uCmRr+VnbA@mail.gmail.com > Requested-by: Andy Lutomirski > Signed-off-by: Steven Rostedt (VMware) Looks good to me. Reviewed-by: Masami Hiramatsu Thank you, > --- > > Changes since v1: > > - Make regs_get_kernel_stack_nth() not fault, and not have a > separate function. Only tracing uses it anyway. > > arch/x86/include/asm/ptrace.h | 43 ++++++++++++++++++++++++++++++++++++------- > 1 file changed, 36 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h > index c2304b25e2fd..055f632ce711 100644 > --- a/arch/x86/include/asm/ptrace.h > +++ b/arch/x86/include/asm/ptrace.h > @@ -237,23 +237,52 @@ static inline int regs_within_kernel_stack(struct pt_regs *regs, > } > > /** > + * 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; > } > > /** > -- > 2.13.6 > -- Masami Hiramatsu