Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp774124ybc; Sat, 16 Nov 2019 08:27:11 -0800 (PST) X-Google-Smtp-Source: APXvYqzHOEMRYttq83CXHRwyV11MRZoBPbPo7UXBEN0dFqOfsxZXg8oFflmYgm0SayX8FWMl6mA0 X-Received: by 2002:a17:906:1cd8:: with SMTP id i24mr10846638ejh.149.1573921631488; Sat, 16 Nov 2019 08:27:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573921631; cv=none; d=google.com; s=arc-20160816; b=hDP9g4F9lc1PdRd/ysO4z7caBCmAuzLSB0eD73LEMrFqALWyiNNnSMolnkCLaAn9WQ Y58fqh5rtcPWGaesy7BDkwkJGpGtm/KV1Js/mrx1bAjeZ2WJk8Hbd92W+lapldBE6ZqN 7B8d3aamtJDA4sOUteZbUjKG+KzxwMo2mnLc/2b5vNadGk/K8eO32MCsI/GH2y2m7J8y m9hTd4oDHnNJNWVEaSt4OGaeuewUDEdunZK+6U/GxO9NlvhE9pLEUt4ZPdUYrRv2Y88b ZdhbVC8wn6Cs74/Avlp/hpI/iS+pBKWlZwl7izp+EDLaM4o8PQmiZFJl/I7RKDrvekCn BKyg== 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:date:subject:cc:to:from :dkim-signature; bh=M6hxspTH1zncgmcbKuFX84ZgeTyAgjjW5ZAIXhPiz3o=; b=EJbDpjSD7l/AH0flo5A6SSKiPfeuTR655rDpgObAz1gEh/QdqdpKdi9n307owI6V90 8MkD1JQ9YjlpUlml0mHtpYrI7FIdbbzy7G1tw7+tn2DwdFTPH45fjUeXY0KyOLi4XIue hTD/G39jtcaU5V7BX+iWhn2pyacRaWyDLgRAPCvRs74KSA3g5/WWVF9rA2FAmgZMQc1D q5q6A7BLWQOQ04QIYT7jjaiPO4TNC21BR6CZm63Bw+29pdz0r+pvHfVXSSSjJg2tixyv x3b0/QsWP+ZyrEsK0APu8zbt6mtJJ94uVS7mMBCDB5i3/EKSY2gyL5BHrQNETPtt25Qh XjfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tEpM0gCK; 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 q25si7979717edw.222.2019.11.16.08.26.46; Sat, 16 Nov 2019 08:27:11 -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=tEpM0gCK; 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 S1728348AbfKPPmf (ORCPT + 99 others); Sat, 16 Nov 2019 10:42:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:46102 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728289AbfKPPma (ORCPT ); Sat, 16 Nov 2019 10:42:30 -0500 Received: from sasha-vm.mshome.net (unknown [50.234.116.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 67B8F2077B; Sat, 16 Nov 2019 15:42:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573918949; bh=tU1Y2b+GPi4XWwAl76MIU7DlHKBpL0erqN5yzCh2sGM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tEpM0gCK6sce4is3DZZ1pQOWT2HGidHNIwOZrpNgesNGsbtit8m5ubsAoWI/0gEzS MR66bPwG2C5a1DtaJGVYWkhK6Arp9vkfd5PrNOlGPAmmdWGIg7wlleEmJsNZZ1TZfC 3X++1RAt7C5+HOsu3vqZ2AALnDEBzKJIyQU5SA9E= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: "Steven Rostedt (VMware)" , Andy Lutomirski , Joel Fernandes , Borislav Petkov , Josh Poimboeuf , Linus Torvalds , Masami Hiramatsu , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 4.19 071/237] kprobes, x86/ptrace.h: Make regs_get_kernel_stack_nth() not fault on bad stack Date: Sat, 16 Nov 2019 10:38:26 -0500 Message-Id: <20191116154113.7417-71-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191116154113.7417-1-sashal@kernel.org> References: <20191116154113.7417-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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