Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3579622pxv; Mon, 26 Jul 2021 07:13:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcusWxc8X5Clu5c0y3QlJpG2F72tYBRS1jpgGM3N0vspY6CrxcuT5aOcoan6dM9tpOCfTA X-Received: by 2002:a17:906:9bf0:: with SMTP id de48mr17325262ejc.260.1627308823609; Mon, 26 Jul 2021 07:13:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627308823; cv=none; d=google.com; s=arc-20160816; b=gsvBFaIkwuTDhyP8Nv8ciRvxFoFNB07jJtbneHPEDSR4WFcM8JY+MUOfr9Rc2HFvdz FBxvDa0p1D90TM8tKn/7utrUxQFDx67WVVp2T3HSz6+nXK7FHisao20cjgNQKbmHRcyW BIKhN+rhUdvyfjAYSZMhcW04iJKIjubZXblpUzSdNReCb5JtzvpwstNaZA5mk8pClMQw dRrIAvUrFfxEqDJTwfQejQtgl4oSC/L2qTuHgZrx8o/x1RPuV8eHz5uZaxtZAWfd+FZX x7cFDoiX5Ak5o9lnhyjfZX+TyqOQh8lBiAMqm3vjuPqy3vjBHKJSoa7IT0WhNt/Fn2lc pxYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4UtBKiyOTfuRPnWjihzMIRAZxhrtpmfHukMTc0HrRhw=; b=xbwSeytoFQ4K/LO3pYdkpXDg03hx5ROdDbMQ5hGQ6mECxSV1VFu6c+MF+x86+m36hW vcwhrIZEXRCesjD2Zy97dhGQOyiNI/izP+Gu+CNgUJ8sE8ab4f3aAeGritJczQRrbIjW xyz42JlDSDbpFS/dBMh++P9vSfe4Vz8nUz2u5w6CsH2d8Uu9a1Ut8BLJXWeE7dCJaYoE nRaEroBdObQpOpVHls25ov1adiFdGeuzCEKfTEAK6DOYyPIbVm/KdnpHwa6jELnuQUo1 GQ5qOuUDWItvpoTs4OIZWmTeGDaddG8/CnIGbZyVpSR9VVLCJCBVsiKBe+tdKC210exx ge1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JC54lRSu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id y21si45829eje.91.2021.07.26.07.13.20; Mon, 26 Jul 2021 07:13:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JC54lRSu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234331AbhGZNb2 (ORCPT + 99 others); Mon, 26 Jul 2021 09:31:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:42864 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234214AbhGZNbW (ORCPT ); Mon, 26 Jul 2021 09:31:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3FD1A60F55; Mon, 26 Jul 2021 14:11:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627308711; bh=en+PAmlM0caG8kJ2VCv1dbeWPj0LXJV1wbRzu3p7AmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JC54lRSurzWqC72r0vaMWfndqofRq/mZ/p1HUHrNHldVfSI7yepI+BOFTrQgDVxWs i5G1nmc4x2hCZz7ufzcSHUzWZTuj9M6b2I5eyQHTByVNe9AYawmlafA/hJyGi7+RRk Sq7R/07wWvPCeakSLBAiNh8/+Iqmqg9w03wdDqBzogYHg0wbwxSZG6ZJFUS/b1ve6o JHtNN19vPYzbxyr02SfNNDJ9f6ZIRzpSTiVIph+tuo6qMpURr1DNf6pMmORnJhf4VM PaRpsbWoo/+zTeXIByNRzjTHTwu5IjZJ3bG3OH637j9sgfBIeWCqZa5vakwdDToR+J ZEa0dJ+ki/IrQ== From: Arnd Bergmann To: Russell King Cc: Arnd Bergmann , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, Alexander Viro , Linus Walleij , Christoph Hellwig Subject: [PATCH v5 02/10] ARM: traps: use get_kernel_nofault instead of set_fs() Date: Mon, 26 Jul 2021 16:11:33 +0200 Message-Id: <20210726141141.2839385-3-arnd@kernel.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210726141141.2839385-1-arnd@kernel.org> References: <20210726141141.2839385-1-arnd@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnd Bergmann 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. Reviewed-by: Christoph Hellwig Reviewed-by: Linus Walleij Signed-off-by: Arnd Bergmann --- 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 64308e3a5d0c..10dd3ef1f398 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) -- 2.29.2