From patchwork Mon Sep 7 15:36:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 11761289 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5E18C59D for ; Mon, 7 Sep 2020 15:39:00 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2CEDD2078E for ; Mon, 7 Sep 2020 15:39:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Vh6U88Ns" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2CEDD2078E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arndb.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8Q2BnWBdKN/pvjlTZvL68x+Fn/ezsNT/Ug4eWxBIb9w=; b=Vh6U88NsaZW2GkLSg31ouORC3 E0LTY2B3H/CMTUIOMjKw4XmZDdMys/7I2u5rB1jHdEmjfkS+mNQGR2Dy7sdLEGFCk+YET4gz77E2a pWzdQkp1EOl5zBEGg3wDn/duODiRCd46VrQnxLCwRWIAihkS540CugAntko4hlayutcCrtU2cwHgK VzAtbaticZvYjffMNtmotbqgSg766NZQJaZ/MrRsYW7rkhV9TojeP/M8/U+0QCTv9XUqCVPrUj82n MvfhUQEe7Vu8V9uW/1nyd4d0BCZ/F379NoeNHjB6s24MO4UFmNi2UEW0kXQE9Z9BmwT+j57HyBa4v ly0IA2/wA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFJDQ-0003M3-Lz; Mon, 07 Sep 2020 15:37:56 +0000 Received: from mout.kundenserver.de ([217.72.192.74]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFJDM-0003L1-Jx for linux-arm-kernel@lists.infradead.org; Mon, 07 Sep 2020 15:37:53 +0000 Received: from threadripper.lan ([149.172.98.151]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.145]) with ESMTPA (Nemesis) id 1MKsax-1jz1mO2Uaj-00LArU; Mon, 07 Sep 2020 17:37:45 +0200 From: Arnd Bergmann To: Christoph Hellwig , Russell King Subject: [PATCH 2/9] ARM: traps: use get_kernel_nofault instead of set_fs() Date: Mon, 7 Sep 2020 17:36:43 +0200 Message-Id: <20200907153701.2981205-3-arnd@arndb.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200907153701.2981205-1-arnd@arndb.de> References: <20200907153701.2981205-1-arnd@arndb.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:bqL9TqSlLxoRFFmGo0QZlOzE4loGzIV2al/StD8nh0dRqmEo/zl r64AuU+j3zZIcpJ6aJRTDhH/hcUIRPN0v5Ce+f7pq0+Pp8u9hWU8oEVPjpBBwogE+XZO5No yCZBEAexPXzjyNtW8kovGgln9eIgE+krcRPWvUn/+8dWvPjezJSouRZiuckvQOO9eR1YOws VWiVkFvqGHaxoJfst5xjA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:CSTeWP/Fmbs=:E8ArNtSucnOwkJb173KkVn IFhVpT/SayTL0XFhu8YBu2d7K0bL9QnhCHK4fB2CFn0Zj2g3TMFjdioycUx2V4BaIKupaQDIs wI8mBHmGHVyoVjvlcdlZKLSdfELYqVeHF4kmrwxRpDKJvcnOCJME7Rej/KDkpE6ag+Ku18u+S yd5YNp33A9tdwdJii9LINoc87+IuZ8zULpSiWFjGT21fQAbi2xLWVJitEDlOpFnANCjlFLXWo b3LLS4keCz7+qN55MsLOE0S2XYC2Ysbw7ubVOFa+rkW55p+wPt4i/ksIxd4nQWX5qC5FKKVCE AB4ZmIdysWUCXUadYMP2Zemix0HlSGwjRIFgkVaqVCcd3rrUY17VT3dRZQr4ZUUe5mzItwpQV vmVNrXP59siGL3uPhFAlQt+SqGN1gQnL2bE1lisYTdl8TsepO6sI3UQslea4PhVZ4hnbn8N65 jZeoWlAB8KyTCeFQsJxCfzZisUI4uigMcagsHFoDxwLdGQDdNdU4NVx49T2eWg6mlWPLsBnGy 7GFt3Qm4BIQdDXksjITSF7oh5hslqPcyZ9W6JFvoxNEGUs89KFfPsJ1ckPAEqgZ35a7RC+UVj JODqUytSWQ4zgyD1qO+Pdsl8SCVlDnUaibLrcEPawVKITJ90Mo+8rlFAn7IOM4ITlIwbATXZ6 P0l1KLrLU85ifgidC5vUKDxqq0+jv/cdc8O46AzDbsmqfld29FWhTZo6Vy2L0ZFDCkhMnUDw7 OsZTugwqnbpd/JGAfSwg31t+KdQOl+uCFV8vCxlBfxS0Vi2wY3bW7ToTBVFZ8qcTG4Prq1Gus YX+14gkcMYMMyV9PS2ocYDIj1r+hUM9KDPzRnhOLzrwatKBB8vtTe4ANv9ofKnBAKb49ftq X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200907_113752_879900_19FC93AA X-CRM114-Status: GOOD ( 24.78 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [217.72.192.74 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [217.72.192.74 listed in wl.mailspike.net] 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, Arnd Bergmann , Dmitry Safonov <0x7f454c46@gmail.com>, linus.walleij@linaro.org, kernel@vger.kernel.org, Russell King , linux-kernel@vger.kernel.org, Alexander Viro , Andrew Morton , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org The stack dumping code needs to work for both kernel and user mode, and currently this works by using set_fs() and then calling get_user() to carefully access a potentially invalid pointer. Change both locations to handle user and kernel mode differently, using get_kernel_nofault() in case of kernel pointers. I change __get_user() to get_user() here for consistency, as user space stacks should not point into kernel memory. In dump_backtrace_entry() I assume that dump_mem() can only operate on kernel pointers when in_entry_text(from) is true, rather than checking the mode register. Signed-off-by: Arnd Bergmann --- arch/arm/kernel/traps.c | 69 ++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/arch/arm/kernel/traps.c b/arch/arm/kernel/traps.c index 17d5a785df28..ebed261b356f 100644 --- a/arch/arm/kernel/traps.c +++ b/arch/arm/kernel/traps.c @@ -60,7 +60,7 @@ static int __init user_debug_setup(char *str) __setup("user_debug=", user_debug_setup); #endif -static void dump_mem(const char *, const char *, unsigned long, unsigned long); +static void dump_mem(const char *, const char *, unsigned long, unsigned long, bool kernel_mode); void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame, const char *loglvl) @@ -76,7 +76,7 @@ void dump_backtrace_entry(unsigned long where, unsigned long from, #endif if (in_entry_text(from) && end <= ALIGN(frame, THREAD_SIZE)) - dump_mem(loglvl, "Exception stack", frame + 4, end); + dump_mem(loglvl, "Exception stack", frame + 4, end, true); } void dump_backtrace_stm(u32 *stack, u32 instruction, const char *loglvl) @@ -119,20 +119,11 @@ static int verify_stack(unsigned long sp) * Dump out the contents of some memory nicely... */ static void dump_mem(const char *lvl, const char *str, unsigned long bottom, - unsigned long top) + unsigned long top, bool kernel_mode) { unsigned long first; - mm_segment_t fs; int i; - /* - * We need to switch to kernel mode so that we can use __get_user - * to safely read from kernel space. Note that we now dump the - * code first, just in case the backtrace kills us. - */ - fs = get_fs(); - set_fs(KERNEL_DS); - printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top); for (first = bottom & ~31; first < top; first += 32) { @@ -144,20 +135,25 @@ static void dump_mem(const char *lvl, const char *str, unsigned long bottom, for (p = first, i = 0; i < 8 && p < top; i++, p += 4) { if (p >= bottom && p < top) { - unsigned long val; - if (__get_user(val, (unsigned long *)p) == 0) - sprintf(str + i * 9, " %08lx", val); + u32 val; + int err; + + if (kernel_mode) + err = get_kernel_nofault(val, (u32 *)p); + else + err = get_user(val, (u32 *)p); + + if (!err) + sprintf(str + i * 9, " %08x", val); else sprintf(str + i * 9, " ????????"); } } printk("%s%04lx:%s\n", lvl, first & 0xffff, str); } - - set_fs(fs); } -static void __dump_instr(const char *lvl, struct pt_regs *regs) +static void dump_instr(const char *lvl, struct pt_regs *regs) { unsigned long addr = instruction_pointer(regs); const int thumb = thumb_mode(regs); @@ -173,10 +169,20 @@ static void __dump_instr(const char *lvl, struct pt_regs *regs) for (i = -4; i < 1 + !!thumb; i++) { unsigned int val, bad; - if (thumb) - bad = get_user(val, &((u16 *)addr)[i]); - else - bad = get_user(val, &((u32 *)addr)[i]); + if (!user_mode(regs)) { + if (thumb) { + u16 val16; + bad = get_kernel_nofault(val16, &((u16 *)addr)[i]); + val = val16; + } else { + bad = get_kernel_nofault(val, &((u32 *)addr)[i]); + } + } else { + if (thumb) + bad = get_user(val, &((u16 *)addr)[i]); + else + bad = get_user(val, &((u32 *)addr)[i]); + } if (!bad) p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ", @@ -189,20 +195,6 @@ static void __dump_instr(const char *lvl, struct pt_regs *regs) printk("%sCode: %s\n", lvl, str); } -static void dump_instr(const char *lvl, struct pt_regs *regs) -{ - mm_segment_t fs; - - if (!user_mode(regs)) { - fs = get_fs(); - set_fs(KERNEL_DS); - __dump_instr(lvl, regs); - set_fs(fs); - } else { - __dump_instr(lvl, regs); - } -} - #ifdef CONFIG_ARM_UNWIND static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, const char *loglvl) @@ -276,6 +268,7 @@ static int __die(const char *str, int err, struct pt_regs *regs) struct task_struct *tsk = current; static int die_counter; int ret; + bool kernel_mode = !user_mode(regs); pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP S_ISA "\n", str, err, ++die_counter); @@ -290,9 +283,9 @@ static int __die(const char *str, int err, struct pt_regs *regs) pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n", TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk)); - if (!user_mode(regs) || in_interrupt()) { + if (kernel_mode || in_interrupt()) { dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp, - THREAD_SIZE + (unsigned long)task_stack_page(tsk)); + THREAD_SIZE + (unsigned long)task_stack_page(tsk), kernel_mode); dump_backtrace(regs, tsk, KERN_EMERG); dump_instr(KERN_EMERG, regs); }