Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10640358imu; Mon, 31 Dec 2018 03:56:44 -0800 (PST) X-Google-Smtp-Source: AFSGD/Wtna+SzmmVOvqib16OcmEiVjvxBNTDGDwAv2TOGhOXvmU6QABkoqYtjSiA9xUy6+isVfuG X-Received: by 2002:a62:29c3:: with SMTP id p186mr38384385pfp.117.1546257404530; Mon, 31 Dec 2018 03:56:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546257404; cv=none; d=google.com; s=arc-20160816; b=Nmm+PatVWXFteOvOwqbsGR/0T2mSUZ038oa/dsx4IM0oILuekeaTUXEsyGsr5A+2Yw gZyFrz+Fa8sfOMp8lik/LfWHTH2jGHCnnmUEsSuT3UuRH34MHnAbnYzHUv86YGWjgXP6 sM+715aSCo35oBgvC5rPs3HMmNMZXnMg19HFH96nF1NIe76FbY2YEp76Luljl3ATVZCG Hjid9fYudYzwjtwHboghXWUVnuVvA4d/m40+0R+uRz4Dn7+/Gie0BykUqPVaCSc3jOns vX0lUDyO0HpE9pQP9VcBmbVQCdYdW3+pLkO41/Y7SH4TqAlUMY5PCdFFK/mZv/76dMYx u6Bg== 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; bh=Gc+1jehDzUB2xjFmmdmiSZpZTFQgqJ6Snak98sp4xTA=; b=G1KDTrYDTeulS5wuuxPDqVXPLrnGgx7pNntGMmb/lLiwvcDUJUu4tQCsTdMLJ23C+Y pT4m68vsQkp5kvBh1E7XOHlGc93VzZ1EFQf1H1ThwVnHIDLkNGbZtdL1IyL4B4cVMdX/ gkMtmIUmkln8ENdXzuW67UWlk0Ys/jENKrDeJXpdbNZ8s7EnAHipkpwfAI8Y34GEikTf E3mHChR1K6p5WqAi96SnO4+wYl3BwqHt3FNiQyqpQINp3KHR++4izhhpAh+ySuL7DYFT 6SQa15nMKxIFINjIrkFdCdXyx7nA2T6G3DzqNHRL0/42e6rWei6blEW9Sj6j5Y0ndbaW 3oCw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gentoo.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o32si6962032pld.407.2018.12.31.03.56.14; Mon, 31 Dec 2018 03:56:44 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gentoo.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727222AbeLaLyx (ORCPT + 99 others); Mon, 31 Dec 2018 06:54:53 -0500 Received: from smtp.gentoo.org ([140.211.166.183]:46570 "EHLO smtp.gentoo.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727078AbeLaLyw (ORCPT ); Mon, 31 Dec 2018 06:54:52 -0500 Received: from sf.home (trofi-1-pt.tunnel.tserv1.lon2.ipv6.he.net [IPv6:2001:470:1f1c:a0f::2]) (using TLSv1 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: slyfox) by smtp.gentoo.org (Postfix) with ESMTPSA id DD92E335C07; Mon, 31 Dec 2018 11:54:50 +0000 (UTC) Received: by sf.home (Postfix, from userid 1000) id F2BF022F2B8EC; Mon, 31 Dec 2018 11:54:45 +0000 (GMT) From: Sergei Trofimovich To: "Dmitry V. Levin" , Matt Turner , linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sergei Trofimovich , Richard Henderson , Ivan Kokshaysky , stable@vger.kernel.org Subject: [PATCH v2] alpha: fix page fault handling for r16-r18 targets Date: Mon, 31 Dec 2018 11:53:55 +0000 Message-Id: <20181231115355.32051-1-slyfox@gentoo.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181231014515.GA13296@altlinux.org> References: <20181231014515.GA13296@altlinux.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix page fault handling code to fixup r16-r18 registers. Before the patch code had off-by-two registers bug. This bug caused overwriting of ps,pc,gp registers instead of fixing intended r16,r17,r18 (see `struct pt_regs`). More details: Initially Dmitry noticed a kernel bug as a failure on strace test suite. Test passes unmapped userspace pointer to io_submit: ```c #include #include #include #include int main(void) { unsigned long ctx = 0; if (syscall(__NR_io_setup, 1, &ctx)) err(1, "io_setup"); const size_t page_size = sysconf(_SC_PAGESIZE); const size_t size = page_size * 2; void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (MAP_FAILED == ptr) err(1, "mmap(%zu)", size); if (munmap(ptr, size)) err(1, "munmap"); syscall(__NR_io_submit, ctx, 1, ptr + page_size); syscall(__NR_io_destroy, ctx); return 0; } ``` Running this test causes kernel to crash when handling page fault: ``` Unable to handle kernel paging request at virtual address ffffffffffff9468 CPU 3 aio(26027): Oops 0 pc = [] ra = [] ps = 0000 Not tainted pc is at sys_io_submit+0x108/0x200 ra is at sys_io_submit+0x6c/0x200 v0 = fffffc00c58e6300 t0 = fffffffffffffff2 t1 = 000002000025e000 t2 = fffffc01f159fef8 t3 = fffffc0001009640 t4 = fffffc0000e0f6e0 t5 = 0000020001002e9e t6 = 4c41564e49452031 t7 = fffffc01f159c000 s0 = 0000000000000002 s1 = 000002000025e000 s2 = 0000000000000000 s3 = 0000000000000000 s4 = 0000000000000000 s5 = fffffffffffffff2 s6 = fffffc00c58e6300 a0 = fffffc00c58e6300 a1 = 0000000000000000 a2 = 000002000025e000 a3 = 00000200001ac260 a4 = 00000200001ac1e8 a5 = 0000000000000001 t8 = 0000000000000008 t9 = 000000011f8bce30 t10= 00000200001ac440 t11= 0000000000000000 pv = fffffc00006fd320 at = 0000000000000000 gp = 0000000000000000 sp = 00000000265fd174 Disabling lock debugging due to kernel taint Trace: [] entSys+0xa4/0xc0 ``` Here `gp` has invalid value. `gp is s overwritten by a fixup for the following page fault handler in `io_submit` syscall handler: ``` __se_sys_io_submit ... ldq a1,0(t1) bne t0,4280 <__se_sys_io_submit+0x180> ``` After a page fault `t0` should contain -EFALUT and `a1` is 0. Instead `gp` was overwritten in place of `a1`. This happens due to a off-by-two bug in `dpf_reg()` for `r16-r18` (aka `a0-a2`). I think the bug went unnoticed for a long time as `gp` is one of scratch registers. Any kernel function call would re-calculate `gp`. Dmitry tracked down the bug origin back to 2.1.32 kernel version where trap_a{0,1,2} fields were inserted into struct pt_regs. And even before that `dpf_reg()` contained off-by-one error. Cc: Richard Henderson Cc: Ivan Kokshaysky Cc: Matt Turner Cc: linux-alpha@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reported-and-reviewed-by: "Dmitry V. Levin" Cc: stable@vger.kernel.org # v2.1.32+ Bug: https://bugs.gentoo.org/672040 Signed-off-by: Sergei Trofimovich --- Changes since V1: - expanded bug origin tracked down by Dmitry - added proper Dmitry's email and reviwed by tags arch/alpha/mm/fault.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/alpha/mm/fault.c b/arch/alpha/mm/fault.c index d73dc473fbb9..188fc9256baf 100644 --- a/arch/alpha/mm/fault.c +++ b/arch/alpha/mm/fault.c @@ -78,7 +78,7 @@ __load_new_mm_context(struct mm_struct *next_mm) /* Macro for exception fixup code to access integer registers. */ #define dpf_reg(r) \ (((unsigned long *)regs)[(r) <= 8 ? (r) : (r) <= 15 ? (r)-16 : \ - (r) <= 18 ? (r)+8 : (r)-10]) + (r) <= 18 ? (r)+10 : (r)-10]) asmlinkage void do_page_fault(unsigned long address, unsigned long mmcsr, -- 2.20.1