From patchwork Fri Sep 18 12:46:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 11784995 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 CD29E618 for ; Fri, 18 Sep 2020 12:50:11 +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 154892311A for ; Fri, 18 Sep 2020 12:50:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="odqnedd7" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 154892311A 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=TX8zYra+uWx3bRtion3mjrwhk00E77byywHF/mnsbd0=; b=odqnedd7k4kuf3DHw7NftaJtQ s+C51XT9gXOj+DVto6jUe3KPgk7oNrlTHlkUT/XgxSHz1GzDHfh0PkzeXQjmlxgrVaiY2CozJab7p UFKyhAFqpQXECt9+KRXwlgvciqoOst2+jB9ik43gA4odxTAvkkNjWXOszbehxK44dvHJZuisX6yft +oSKvQT/+Kyau2UUy56qWmxf0FQk2PfozL/ZH4WS2lzBDgXowfP0szhKZuOeqoDiTamQYE4VxJwZr tBw0CG2Ljz1M0JVzlmsZwnaZzOd7x8X5+VH9ZAySob8+A1NgFsNWLnKBRVQ5QYt4Lxi/X8SqFsgZJ ZhG8HDVuw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kJFpp-0007ZJ-Mw; Fri, 18 Sep 2020 12:49:53 +0000 Received: from mout.kundenserver.de ([212.227.126.134]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kJFms-0006Ck-VA for linux-arm-kernel@lists.infradead.org; Fri, 18 Sep 2020 12:46:58 +0000 Received: from threadripper.lan ([149.172.98.151]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.129]) with ESMTPA (Nemesis) id 1MTRAS-1jtycg3laG-00Ti0L; Fri, 18 Sep 2020 14:46:34 +0200 From: Arnd Bergmann To: Christoph Hellwig , Russell King , Alexander Viro Subject: [PATCH v2 2/9] ARM: traps: use get_kernel_nofault instead of set_fs() Date: Fri, 18 Sep 2020 14:46:17 +0200 Message-Id: <20200918124624.1469673-3-arnd@arndb.de> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200918124624.1469673-1-arnd@arndb.de> References: <20200918124624.1469673-1-arnd@arndb.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:Gy/6r3hP6+Xw0VZ1kPuBOfmudRbDMtnh6jF154kk+qnOsu9JUCQ cQK0bo1hDNYXTK9JI2Na6UKfmHCGL9OWP+7LGyS/kOe6Uc9yJbO9xExzSioQ0OF5BPd/C1V nmlIEcSh3DpIrVLDUZebSlnT9QnSnvqCMQd9I2RYk49O2SSMFV8/sb0VnF6GCOz06mOntBv vtRVwI+wzeyGhICf15/8w== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:DMblnyBKhY4=:9xYkhBDiYrvF2PKMOZ5/jd 7itoiVV6ScsHcK8wPs3xmfxSSrugMrRfii48gNLwyhVjnLhHsCHeIToONufxWhSJtWjqp50Rd dIhEumG+L/4r73Q7OyhK/FrDYOmZctzBbZbyaVJ4wQqNud37Mn9XLeJ8YXD11XGCYC0p6i9Ge aOPWKJtnHknCvEXZxHrj6X8nCWU097zi0wIqq8hmcU+urPAHVQ7Y29zGk/ELMlP4oGdhPlU52 6I+dJDkdUkKfntHzL5sCVutRX7EueaRKoTpxc2faPKlwJQxnGn7NyNFxjeypshD9UPmkv0qIz uylSZc/nektnaiR3D6IRhQZJMwJDykBoIe8C3jk0bbNnCInQBu4lL/0wQ2RHfnusS4U0glwC8 SD2DQohnsrGmHMGnbqEnN7XTejaUW+b9phi85MVOtGtIEuN4980eeaFVIsMTkklxbhhq3oHCf L/0jbGnUehzANInlLiBFQiyHWJ6O4vsTWFpWso1N3zvn1ePT3/CLrnhkUTYZrdnoAHZ+GQAbH 3ZfCZ13w4NlZrcq9AiIZ93tkymhgVfkaxx4TATAiRc5BNr+tkMSj0wWdKy4ArwL6Sk95+LKBK GxcEz1kzhIFPShl/11Yi7N2GGdiN0cZntPvNNU8dwnwdzcSzUnJS8OjxdNgYRBjR8O6eyP7Cr dlsU4otbC8UOXe3bQ6WT2de5wyXNscPVST3gquGhFq+RSacYFwmiUJq4YwmSjeapSR6o3uXck zD+Ip58BziCarZCj5aMUGzFfn/jRUcoejVakvpvdBJ3pK87EOfqMQYxnulXte+co+uheW/4yI FNjRnYXt1zm6T5mNTFcR7DRZP8334b7k9fdnpA8MGg/BI710j6z9SMcAT4z3EUHAhn6bPeJ X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200918_084651_569726_8FADE275 X-CRM114-Status: GOOD ( 20.53 ) 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_MSPIKE_H2 RBL: Average reputation (+2) [212.227.126.134 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [212.227.126.134 listed in list.dnswl.org] 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, linux-mm@kvack.org, Arnd Bergmann , linux-kernel@vger.kernel.org, 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 ARM uses set_fs() and __get_user() to allow the stack dumping code to access possibly invalid pointers carefully. These can be changed to the simpler get_kernel_nofault(), and allow the eventual removal of set_fs(). dump_instr() will print either kernel or user space pointers, depending on how it was called. For dump_mem(), I assume we are only interested in kernel pointers, and the only time that this is called with user_mode(regs)==true is when the regs themselves are unreliable as a result of the condition that caused the trap. Signed-off-by: Arnd Bergmann Reviewed-by: Christoph Hellwig --- arch/arm/kernel/traps.c | 47 ++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/arch/arm/kernel/traps.c b/arch/arm/kernel/traps.c index 17d5a785df28..c3964a283b63 100644 --- a/arch/arm/kernel/traps.c +++ b/arch/arm/kernel/traps.c @@ -122,17 +122,8 @@ static void dump_mem(const char *lvl, const char *str, unsigned long bottom, unsigned long top) { 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) { @@ -145,7 +136,7 @@ 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) + if (get_kernel_nofault(val, (unsigned long *)p)) sprintf(str + i * 9, " %08lx", val); else sprintf(str + i * 9, " ????????"); @@ -153,11 +144,9 @@ static void dump_mem(const char *lvl, const char *str, unsigned long bottom, } 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 +162,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 +188,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)