From patchwork Thu Apr 12 11:11:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 133200 Delivered-To: patch@linaro.org Received: by 10.46.84.29 with SMTP id i29csp1571197ljb; Thu, 12 Apr 2018 04:12:08 -0700 (PDT) X-Google-Smtp-Source: AIpwx49WF2MMDjO38OwFT1uAUfUnWIM6vnahrXkdfrceEdth1DjgjFIMrQ9LlxutGJ5xe+I+JF0n X-Received: by 2002:a17:902:b94c:: with SMTP id h12-v6mr520992pls.321.1523531528586; Thu, 12 Apr 2018 04:12:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523531528; cv=none; d=google.com; s=arc-20160816; b=T6SAhe+1HESFggwzMWQxQ/nD492CbHVq7De3AUGhAjl16ImeWmwMiyDKAwlVO2rXCP wj052/T6X4zGWhQBYA3tXO5QvNR7BZu0FGF+cOyuC6hgBljkmXOkdo3tBeeVPBwM+LYd ytxSBkYvqd7ox0OKelHBlQGQ1RFDAyOu7fWsjLqIWCXNQQp6UsQsqnPSD0mXzwm+1meD bjVA7ME8jZFZo/L1IsbbKr4QsclCbeHgKNbUzJckDc9YBWStyhqCjL4v48aC7GlKpCm7 CYw5p+FWxni97PcTQc89MwjOL8WaCoH7t24S6pfY8XbUqwoQ52i/4NC/1JcjCFHOoupD FNFA== 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:arc-authentication-results; bh=fH5DCYavYRQjZgA+1K538hgwRHfPAwJlvBO46x3s5Vg=; b=0jGXfq+XEFx2HgfezaKcn+NFPug5rL8B5UiZcRjBtIB/3dhyST1RFymRJXhcxQLCA5 0IiD5yGmRTOIv1XqOYezFLfDTBUVAyA9Mh+6oM7dtNINkb1ODcEg6L7huzN5rNeEWWLU UU0xWNET/4WZX2l3BK35J6S96W/1ADTyffo4AXsZEtQ9RodDOfnB5vNLGlPVzhIyMJSz HR4IGEfpqZLBtbVsfN5ecmZHkMuNHfadKTs7EAymzdd4LB1AkET5qsIhN/qi4/Z0APWB Egl+07ZWy3ag+8cS5hMalPtFiBMb1oVH7eUeWNzqumAOwubWP+umosPgw5FJSzdxd7Yk Pwww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-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 bg3-v6si3001807plb.118.2018.04.12.04.12.08; Thu, 12 Apr 2018 04:12:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of stable-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 stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752763AbeDLLMH (ORCPT + 11 others); Thu, 12 Apr 2018 07:12:07 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:59334 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752428AbeDLLMH (ORCPT ); Thu, 12 Apr 2018 07:12:07 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B24A780D; Thu, 12 Apr 2018 04:12:06 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 0E9603F24A; Thu, 12 Apr 2018 04:12:04 -0700 (PDT) From: Mark Rutland To: stable@vger.kernel.org Cc: mark.brown@linaro.org, ard.biesheuvel@linaro.org, marc.zyngier@arm.com, will.deacon@arm.com, catalin.marinas@arm.com, ghackmann@google.com, shankerd@codeaurora.org Subject: [PATCH v4.9.y 05/42] arm64: Use pointer masking to limit uaccess speculation Date: Thu, 12 Apr 2018 12:11:01 +0100 Message-Id: <20180412111138.40990-6-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180412111138.40990-1-mark.rutland@arm.com> References: <20180412111138.40990-1-mark.rutland@arm.com> Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Robin Murphy commit 4d8efc2d5ee4c9ccfeb29ee8afd47a8660d0c0ce 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 Signed-off-by: Mark Rutland [v4.9 backport] --- arch/arm64/include/asm/uaccess.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 0f05ba51531c..a9db051b967f 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -129,6 +129,26 @@ static inline unsigned long __range_ok(unsigned long addr, unsigned long size) " .popsection\n" /* + * 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. @@ -202,7 +222,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); \ }) @@ -270,7 +290,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; \ }) @@ -331,7 +351,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; }