From patchwork Thu Mar 1 12:53:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 130190 Delivered-To: patch@linaro.org Received: by 10.80.172.228 with SMTP id x91csp2761337edc; Thu, 1 Mar 2018 04:56:37 -0800 (PST) X-Google-Smtp-Source: AG47ELsKBEq05rvBcdBgfs+1bf6DlKfgWLZ4kVotqzKoFbbWpPNLVJl9kRHNlDTaDxDFm5yJ8vgY X-Received: by 2002:a17:902:aa87:: with SMTP id d7-v6mr1831054plr.237.1519908997626; Thu, 01 Mar 2018 04:56:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519908997; cv=none; d=google.com; s=arc-20160816; b=GzV486tuRxIiH9gAqX9HRMmreuyn6t2wppWDYEVZOy54Q6q0osIkZMr/c7bKjDpcj4 9fx5mWzOJ9ytai5BvfvxTdgT7JU6hY+nPlLQHTreECC6X73H2F4z936w2ogx/86bRiQH ey880B4ROri1Cpe5kFXZOss98RJbPIzRAc/3VdB9gaY1JYm/1vnaU2ZtyeyFZmmv6hsl pN+UAqpWPBU7iqnSzmjgW5rwYoebwHG027mnGFTx2DquoRGApaVixFBg91eIXGylsexb 6/7tdlAsm9wzFyGFXHq0C5w9diJVWiT2DKSozJuRIUX90WqFNgMEbdcYUkn+KuTlSOhp 9S3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=i4j5LLlcJTrNi6a03osuM7FMBFfhyS0MAdksfWz86sU=; b=l55OG9Clip40FMSmaGspwWNvky6d+RB77UlF4HWJ75SK+uCD3LuUwXlrghzL6DvVfC rEXWU7JrY046xt5w3rmuDNCPGHpKqB1OlxLDvEY0GzfjoMe3/nF1u8F+A7rHGL1myO5K zQXRqE4V9xt1CFo+Vm4ppRxpXY8Rdt1+5JymqTht4Xw7goJrp7FTK5/WHb5YHH/ULwGO 18N25N4NymqzCtrSsFufBKhUbwG7/4uQrfFIgaO21FScmBSUKmMq1bu4Cx+uVqlOkJ7V 3UL1GuwSG7x+FdoyOFoMMnvxDLiF7l/CQALQ14hJ65ez7DlhmZKkeKVRv4VQqOOwbDfF o4gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=R0saQ4fw; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 14-v6si3022704plb.444.2018.03.01.04.56.37; Thu, 01 Mar 2018 04:56:37 -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=@linaro.org header.s=google header.b=R0saQ4fw; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030743AbeCAM4e (ORCPT + 28 others); Thu, 1 Mar 2018 07:56:34 -0500 Received: from mail-pl0-f68.google.com ([209.85.160.68]:34518 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030577AbeCAM4b (ORCPT ); Thu, 1 Mar 2018 07:56:31 -0500 Received: by mail-pl0-f68.google.com with SMTP id u13-v6so3593880plq.1 for ; Thu, 01 Mar 2018 04:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=i4j5LLlcJTrNi6a03osuM7FMBFfhyS0MAdksfWz86sU=; b=R0saQ4fweThOggq/TobnvzHWJpLRton+5jJ0eFNYqO5Snpv2UioTyy6aGI6w4P0KOB ON5T9SkWwhlF0sSoZBtM07coOH5WOu7BJ3Wu1f+tjr6cxw9qZo+ztL/JTJLREOdqttzI 7BYcQCOeS00mxpxGtGWiH+X72LTRbi7E3+YJA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=i4j5LLlcJTrNi6a03osuM7FMBFfhyS0MAdksfWz86sU=; b=UjpEExiOXBkvuy0VqH3amxe99glcl4TPWYdK5trYpfMswF/yfvjg0GB0sW0ANtWtcQ MO/aT+5dyE9h15fAY95hXAK3Yaqq8uIH5ZQNo2Y17QAydHA2zljJ2lKcbCnzmggh0vdL pZ2AYlWYAgTElaF2ANMhcoklj+GZgyEV1nNDxcUSrIfevrZZMgh2wsLzHr+Q4U8F2Kqc jgI85AeI1lSpq+Mlrz+B9WSDdYkkzLtzkpIuzgR4trWdl7LcV1OMsKacp2NRFwTw7cds evAJWa1LgoYjXxXN+r/PJmgImDWGLoxteWitbs2dQm1Pf7Hh/VXgJaLIwgzGKG8Bs9bK NMgA== X-Gm-Message-State: APf1xPBdge0XkQFF6ZkKooqGp2TGQzxiRI3dyM2sIcyYxK5aI/u/GPCl sLKVLcEohJ+kRbkdXoZhkwWF6Q== X-Received: by 2002:a17:902:b605:: with SMTP id b5-v6mr1873928pls.354.1519908991099; Thu, 01 Mar 2018 04:56:31 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id x4sm2289655pfb.46.2018.03.01.04.56.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 04:56:30 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Robin Murphy Subject: [PATCH 10/45] arm64: Use pointer masking to limit uaccess speculation Date: Thu, 1 Mar 2018 20:53:47 +0800 Message-Id: <1519908862-11425-11-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> References: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Robin Murphy commit 4d8efc2d5ee4 upstream. Similarly to x86, mitigate speculation past an access_ok() check by masking the pointer against the address limit before use. Even if we don't expect speculative writes per se, it is plausible that a CPU may still speculate at least as far as fetching a cache line for writing, hence we also harden put_user() and clear_user() for peace of mind. Signed-off-by: Robin Murphy Signed-off-by: Will Deacon Signed-off-by: Catalin Marinas --- arch/arm64/include/asm/uaccess.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) -- 2.7.4 diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 7b1eb49..3531fec 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -170,6 +170,26 @@ static inline void uaccess_enable_not_uao(void) } /* + * Sanitise a uaccess pointer such that it becomes NULL if above the + * current addr_limit. + */ +#define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr) +static inline void __user *__uaccess_mask_ptr(const void __user *ptr) +{ + void __user *safe_ptr; + + asm volatile( + " bics xzr, %1, %2\n" + " csel %0, %1, xzr, eq\n" + : "=&r" (safe_ptr) + : "r" (ptr), "r" (current_thread_info()->addr_limit) + : "cc"); + + csdb(); + return safe_ptr; +} + +/* * The "__xxx" versions of the user access functions do not verify the address * space - it must have been done previously with a separate "access_ok()" * call. @@ -241,7 +261,7 @@ do { \ __typeof__(*(ptr)) __user *__p = (ptr); \ might_fault(); \ access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \ - __get_user((x), __p) : \ + __p = uaccess_mask_ptr(__p), __get_user((x), __p) : \ ((x) = 0, -EFAULT); \ }) @@ -307,7 +327,7 @@ do { \ __typeof__(*(ptr)) __user *__p = (ptr); \ might_fault(); \ access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \ - __put_user((x), __p) : \ + __p = uaccess_mask_ptr(__p), __put_user((x), __p) : \ -EFAULT; \ }) @@ -368,7 +388,7 @@ static inline unsigned long __must_check copy_in_user(void __user *to, const voi static inline unsigned long __must_check clear_user(void __user *to, unsigned long n) { if (access_ok(VERIFY_WRITE, to, n)) - n = __clear_user(to, n); + n = __clear_user(__uaccess_mask_ptr(to), n); return n; }