Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1115997imu; Sat, 15 Dec 2018 14:51:55 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xkxg2LgYwhMJg0DpQpPFiQMB1JNlUdv9B7fIxLL5dWRzVyvmhty88ateLUHyruOZ2v4LtX X-Received: by 2002:a17:902:14b:: with SMTP id 69mr7978942plb.52.1544914315583; Sat, 15 Dec 2018 14:51:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544914315; cv=none; d=google.com; s=arc-20160816; b=KVBkCDzf3+spuQwJq8+UKTiV8N69x3R3DGPskSD9gzVMfn3cO8qNrG0hIrnLy6gX53 DDp5Zk3G0Q8Pe5mKu95ruGR1dzqqmsE9Aq88jEvM6eQY2iAx5aKNES+2z719QtecpPH7 nFvefAz+MON3y9hGc6oBby6TDWhlA9E+AyQvQxQOI+30R2ctG6QMUUTIn5d34aiqXh5I b+uIBeYg7RJPZcbigwCZJAbTmUpS+QR6ZIxmnLu3UGCKYzYrgEVMTzou03SRuyco2P0G 2X2gZIFz1ngMR2hqJ4PQx9n4pB1za1wview1ZtJmsZ1dp3UdafIrrgMsONJ0sYaIvjXB 9mWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=u/XjLQfT0BPK+I7tMwKMrruqEjsQFTUyAwPmKkKVcvA=; b=qIt6Vp5p2OhXks7wa9mPQwDVDygEqsOX7q1iJz4K39hs/IgxYlspze4/r8sS2/z7pX djTx3Pf36evDyHAdTAnGq8oOwrIXEgBQIDSALfoBFx4UK+RCmHHJiMdlkeS7RZol4lov PcHqqV6sJ9z4vdYY2hzoMy3caC8swWkaklnQAlJrYhnFP3NvU6Y78WpHFTPo9zVD16FK 85MKIx3qf1vqO3R7jBt/XS0scBC9BStCyRbzmIOk/dVHJmHWib7UqlLGzyBFzHm4T/Jt 45DnLxYUyz+64pqkvxHEhBe0YWe+kOfquREJn2XsnNwOL2fRPYGOwkzv8XoG2o/dPe6e N5YQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j29si7148530pgm.554.2018.12.15.14.51.38; Sat, 15 Dec 2018 14:51:55 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728718AbeLOWuR (ORCPT + 99 others); Sat, 15 Dec 2018 17:50:17 -0500 Received: from 216-12-86-13.cv.mvl.ntelos.net ([216.12.86.13]:59104 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727229AbeLOWuR (ORCPT ); Sat, 15 Dec 2018 17:50:17 -0500 Received: from dalias by brightrain.aerifal.cx with local (Exim 3.15 #2) id 1gYIl8-0001dl-00; Sat, 15 Dec 2018 22:50:10 +0000 Date: Sat, 15 Dec 2018 17:50:10 -0500 From: Rich Felker To: Andy Lutomirski Cc: Linux MIPS Mailing List , LKML , Paul Burton , David Daney , Ralf Baechle , Paul Burton , James Hogan Subject: Re: Fixing MIPS delay slot emulation weakness? Message-ID: <20181215225009.GB23599@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Dec 15, 2018 at 11:19:37AM -0800, Andy Lutomirski wrote: > Hi all- > > Some security researchers pointed out that writing to the delay slot > emulation page is a great exploit technique on MIPS. It was > introduced in: > > commit 432c6bacbd0c16ec210c43da411ccc3855c4c010 > Author: Paul Burton > Date: Fri Jul 8 11:06:19 2016 +0100 > > MIPS: Use per-mm page to execute branch delay slot instructions > > With my vDSO hat on, I hereby offer a couple of straightforward > suggestions for fixing it. The offending code is: > > base = mmap_region(NULL, STACK_TOP, PAGE_SIZE, > VM_READ|VM_WRITE|VM_EXEC| > VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, > 0, NULL); > > VM_WRITE | VM_EXEC is a big no-no, especially at a fixed address. > > The really simple but possibly suboptimal fix is to get rid of > VM_WRITE and to use get_user_pages(..., FOLL_FORCE) to write to it. > > A possibly nicer way to accomplish more or less the same thing would > be to allocate the area with _install_special_mapping() and arrange to > keep a reference to the struct page around. > > The really nice but less compatible fix would be to let processes or > even the whole system opt out by promising not to put anything in FPU > branch delay slots, of course. As I noted on Twitter when Mudge brought this topic back up, there's a much more compatible, elegant, and safe fix possible that does not involve any W+X memory. Emulate the delay slot in kernel-space. This is trivial to do safely for pretty much everything but loads/stores. For loads/stores, where you want them to execute with user privilege level, what you do is compute the effective address in kernel-space, then return to a fixed instruction in the vdso page that performs a generic load/store using the register the kernel put the effective address result in, then restores registers off the stack and jumps to the branch destination. Rich