From patchwork Fri Mar 9 09:06:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 10270109 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ED42860236 for ; Fri, 9 Mar 2018 09:12:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E6DD829D9E for ; Fri, 9 Mar 2018 09:12:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DB26D29DA0; Fri, 9 Mar 2018 09:12:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 483BA29D9E for ; Fri, 9 Mar 2018 09:12:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7kbblTwMnRBQIsgxVgihCSr2+ngNeZSDQI6hO15DX5g=; b=kG00df349IiEflmAIjmgjqfJhQ qrSLcprMNr9puisIIIkkAqpSaf5qWEx3K8S4sl75XqGK8Hmxv26usoKF6yuBNA8XzEvnx2AaJ4Sz8 QqCQkFAjk7tvW87GhBze5B92xt0gLQcZpK6O1qB3cZPRs+gBTOGttw6W/ni25fdls0VtPF+U5D+aE VbjuyxvoT51wcEmvO96GzCcJ/fxl+CcqioSAy/j0qB5Ay+EkIn4QZfwTm2ilZxkcGcXbq5e4a8aVl JZ0+Nm5CkUrxYN6BRy7S3StYRM9Zpn/qCp4SBNsuQwMrJUXrclkeigm+7ougR9TkYfcQ/IJcFWy/F tZ6TsglA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1euE4N-0006Zp-MO; Fri, 09 Mar 2018 09:12:07 +0000 Received: from mail-pg0-x242.google.com ([2607:f8b0:400e:c05::242]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1euE1P-0003ZH-1v for linux-arm-kernel@lists.infradead.org; Fri, 09 Mar 2018 09:09:56 +0000 Received: by mail-pg0-x242.google.com with SMTP id s15so696100pgv.8 for ; Fri, 09 Mar 2018 01:08:52 -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=f0NBTGFifdEQeVSLOqVTvrw1dF89sNHFqOLvXJVum9s=; b=CYo37zvvnfBuQiG84cfuG91AQlnShyWq5mEe4x+Ya4yiCUNCMUYzgJ9C5giFE3zINT /yh1P8FzIqlpBRYoNrHAPRtKC4iyKpZ4UXW6gkgfG4LFQmkB7hu9YFksnTWo4tHwlJVM dgA9EudqghkRBo8issJIqm01IcO0OF0D2XIHU= 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=f0NBTGFifdEQeVSLOqVTvrw1dF89sNHFqOLvXJVum9s=; b=CpgMHgu4LGvE4vW8T0AMCI0kzQpY7xxPZ+uoAmUOmlKconx6HXlrf95n+zLEWjbrU/ aOlFpcChy9BEXxIPjUnWd7RPeK4FXFPNO5ELgeDWW62r/09LVQQvh2bU7Q3JVOBVCIj4 0q4/hQacXwxvFZz0IdQjC+IKH1Ud+pWUuGD7aYaycFsOK8BDYkRBPYh70V5HNUkAp5DV hiFpH+qsCicYV6QHIii3qux1o7dOMlnzoxG/K4BDq1WBbizC9YI+vzjaTc8X2xTQKbtJ 0D6as0ZSSc70H8m9fD011q9aLUnapPxu/YLb4/kEUgDZqV1hnDZwgG1IJsgfNLQepyAK XaAw== X-Gm-Message-State: AElRT7EWvwClmZW8Wq2UeNAVJOW++l2QPMCgGOmH2zigsQa+bwC8l/vU c3sHh6yfdm5IHRJBGvWkGW6xBQ== X-Google-Smtp-Source: AG47ELuR80jMEuldsncj85RpRRgr6t+mYvuzAT3RWDbWCRjnoGMqkV4//0wbj0BZ4I/+F/FzUxQ+Rw== X-Received: by 10.98.165.4 with SMTP id v4mr24465736pfm.51.1520586532000; Fri, 09 Mar 2018 01:08:52 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id c7sm1961752pfg.36.2018.03.09.01.08.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 01:08:51 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , broonie@linaro.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH 07/39] arm64: Use pointer masking to limit uaccess speculation Date: Fri, 9 Mar 2018 17:06:50 +0800 Message-Id: <20180309090722.26279-8-alex.shi@linaro.org> X-Mailer: git-send-email 2.16.2.440.gc6284da In-Reply-To: <20180309090722.26279-1-alex.shi@linaro.org> References: <20180309090722.26279-1-alex.shi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180309_010903_947393_C11F836F X-CRM114-Status: GOOD ( 14.10 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alex Shi , Robin Murphy MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Alex Shi --- arch/arm64/include/asm/uaccess.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index c625cc5531fc..75363d723262 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -121,6 +121,26 @@ static inline unsigned long __range_ok(unsigned long addr, unsigned long size) #define access_ok(type, addr, size) __range_ok((unsigned long)(addr), size) #define user_addr_max get_fs +/* + * 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()" @@ -193,7 +213,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); \ }) @@ -259,7 +279,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; \ }) @@ -297,7 +317,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; }