Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7088735imu; Mon, 21 Jan 2019 23:38:26 -0800 (PST) X-Google-Smtp-Source: ALg8bN5MFCiqNvr7vG7Y9IqBxk6BMRGr5yb5JUY31Q4lDyNc+EsfSYOyyANCYMjR7zHjw4e+TF+f X-Received: by 2002:a17:902:6946:: with SMTP id k6mr33713014plt.101.1548142706748; Mon, 21 Jan 2019 23:38:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548142706; cv=none; d=google.com; s=arc-20160816; b=FXkw3rK2iQLq5NhbhpbDe8kBAWDEQByMc0CkR4SNpcFm3YkdmbVoTj6gpoQAkt67dF Bv+UJKOsOf0VaqXkTw9CPgBx9LDRWB1CC+yz2/iFcEnhIs8SGlu4D4SOdcbjNZRgWcmI O1NWAGa8Wq9RihVjpua2IWQXPQ2NDSgwo9TgyD+0GTocf0vFWM46p3nVp0qkfpQ0Ream Xan72irLFwtdwn6PzphReOdCRsHtl45NrqltKFW7R7Kw1z8fSIa4lLTgZVU9mQ1tj+/3 hT2oVCbi7m6nQDDAWRNkM+Cv+nejNtt01VDJFdRkvnh5Pp9VVESV7MJsQZiDctwKFgUR fGaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=x61w76GzNe+nOXtRz1PrU5HrR+5Sd+AN/GUuUvwosFo=; b=AJnGSkzFizrEJNXjKWj/FD7hyuPNXC0dNNadVUh9pOFBwMQc0On/T3/CKCi3vijDlq JBjPA2LOailxJKhGQesVApyVvNTthk1fceQ3O5bkS/hnU+MP62wifLrMiCxZT6kNX8or wlJZBW3IQiVho+L5Ln7o2j6qAO5oNqcHXuab9eNj4maLqX4vLAZqu3UfVgXGep0lg4dI 8MG9cc1Xb8x+1JTeG5s8MvjauRVmopnXOTfGvqzStcbEn9hTQH7CL+wAzfetB3ll/mLK Vn5L7p1FDA0guH1R9SGc9DyNspLziixDXXScweyCQvGIfLaMK3otBGx2RywdjqgfK4gq p1fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HDCNmbLR; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s5si14248882pfi.134.2019.01.21.23.38.10; Mon, 21 Jan 2019 23:38:26 -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=@gmail.com header.s=20161025 header.b=HDCNmbLR; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727323AbfAVHgX (ORCPT + 99 others); Tue, 22 Jan 2019 02:36:23 -0500 Received: from mail-it1-f196.google.com ([209.85.166.196]:33913 "EHLO mail-it1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727165AbfAVHgX (ORCPT ); Tue, 22 Jan 2019 02:36:23 -0500 Received: by mail-it1-f196.google.com with SMTP id x124so8987943itd.1; Mon, 21 Jan 2019 23:36:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x61w76GzNe+nOXtRz1PrU5HrR+5Sd+AN/GUuUvwosFo=; b=HDCNmbLRvHyvDvcpC/mJPmUr5d02Qi8LTRlU76tLz4EK3xfLKwqJAAFMwOXwMBws7n iw2TRYVvrQnVpztPcyvVICVTLZVXRvWyRJhOsXAt3PiN5gy8X7Wu1LQw/rMzZBD7QmI7 C/xHcIE/NmpGlaanUueL+mtUg8LvzbPlN95LnW0pICQi8b6Dunr3C9cZxRn3dMrEFcjU NOEEUX9jnvN70Kcuid0iBFClW0BS+TeiJX+/pYSjAqBB2gNJBq6KHPlQuc/DcbhGgMfU 36BftU2aZEK4SlpDqi+rH+/76Dswnam+H6quc5uCwfRTray5q/Nq5lf+1uvpp02HyXGD Zu9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x61w76GzNe+nOXtRz1PrU5HrR+5Sd+AN/GUuUvwosFo=; b=JUCdgsNRQO6W5vNA9uQgxIQSGIO1hU0+tH7SqYftBPyJo4gspqYG6lH7xE28a6c6KF QjHjiaZyzdpxltrZooJT3fpPos263J7esR/zPNVCDo9AQ5CBgI9yAzXGelUmZ118MEgC 1KI8c2vYCbJ7bI/6C1ieg0dzSvfOq0jhGNIkxiYMLkklG1a8q1Fe3AjpukGfZFleqYWs /lb5Y+Fo1xHbnht3WgagpVF6xWqSD8nP3235hWJIbkxYSwdhLH0uesNiYQRL/+5qrxVG btPFmgfhnxL1462M0cmGOGqL+hbJQFgrxB6YVRUwE1iJc87+ECIuzfF6ska0sxSa8XAA K5Fw== X-Gm-Message-State: AJcUukeeh+vt8094lyuyuGLVqRtd/pI2/9eNajEKP0210yLonUCGyGs7 wkCPQ2x9Ygaf/ZiaOkIWWfOehxCAHeKwHSGdC2A= X-Received: by 2002:a24:2716:: with SMTP id g22mr1334607ita.40.1548142581839; Mon, 21 Jan 2019 23:36:21 -0800 (PST) MIME-Version: 1.0 References: <20181231014515.GA13296@altlinux.org> <20181231115355.32051-1-slyfox@gentoo.org> In-Reply-To: <20181231115355.32051-1-slyfox@gentoo.org> From: Matt Turner Date: Mon, 21 Jan 2019 23:36:10 -0800 Message-ID: Subject: Re: [PATCH v2] alpha: fix page fault handling for r16-r18 targets To: Sergei Trofimovich Cc: "Dmitry V. Levin" , linux-alpha , LKML , Richard Henderson , Ivan Kokshaysky , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 31, 2018 at 3:54 AM Sergei Trofimovich wrote: > > 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. Wow, nice work. I've vacuumed the patch up and will include it in my next pull req.