From patchwork Fri Jul 12 05:27:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 11041459 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6AFB8912 for ; Fri, 12 Jul 2019 05:31:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5344F28B80 for ; Fri, 12 Jul 2019 05:31:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 43B9728B7E; Fri, 12 Jul 2019 05:31:13 +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=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED 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 CE66C28B7E for ; Fri, 12 Jul 2019 05:31:12 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=BzPMhrFXGXOlk8TywbHUNZU5N0LGvRG5V1LZIu9o7EM=; b=hphmUoIYxdpSFu /hd1YfEHPsfI5RosKOSOG9Bih3+sXA+Zg72AsDd/B3OtoB/RxUkj/9KfI9RSTTDbGzi6WFKwdbFNg zTpBV1iCJxkV4/1CdxT0lgDUbIhBSrn8y2oylcpBfNYJCI7sg/UClkKep29CuJCQExABwQ0Lxvxvq LuHepSZ+i0YqNoXAcd2WWQPY+GOIzUWKXMjgOQSMjneC+TxbrhRMWf5J0XU8J1tOzYDhRyuQE679Q 4+LRea5FHwTeF/dTTpqPELLxmBvtJuEavf750fBKU0j0O3uz0TY5TS+0+Lr6Sh5Ivnx/w2id23Z3z gr1bp766D9ivyl7B0ymg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hlo9E-0002yb-Hj; Fri, 12 Jul 2019 05:31:08 +0000 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hlo7O-0000PN-2y for linux-arm-kernel@lists.infradead.org; Fri, 12 Jul 2019 05:29:15 +0000 Received: by mail-pg1-x543.google.com with SMTP id o13so3984773pgp.12 for ; Thu, 11 Jul 2019 22:29:13 -0700 (PDT) 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 :mime-version:content-transfer-encoding; bh=oplH30x1zLW/kw5XTQuFI7s72sVvdi1RahKBb4zAIVI=; b=eaes5zcgp44lXt6A5g4+fUxCC1ls9h/X8hAy28QuZtZMD6AhLT94Fh11hOM2uLjsaT obGqKrqcKmP6ZW+32H2nZ7w2JwPPtS4yBtSNQMCAAQD8Pu+SGM2kfXPurwyUrM25/bX6 LvSH7O2WsNuUTX6AmZ2BWi4PlLRGTfdvNTmGZIyL6k4ZgbDqhRRrqbYaWBXOv+Df2pTn LMbeKC/td9b+cPHPvrv173y8fJ6gCLUUKudhxCo09n1btwp/ZFR1i7tQLMGpyqqs/6S8 2akxlq9oUecJlXcLc/wWJtAjUbaPZQpg+3BrBAHeQR/PGKy380/KeFy2yB/E2mvylW1o VlGg== 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:mime-version:content-transfer-encoding; bh=oplH30x1zLW/kw5XTQuFI7s72sVvdi1RahKBb4zAIVI=; b=bNkpqB64h8peBFZ4bqv/JH3O15AMfyt1GbBCGwA+WNr/cTNyDhOgCUKR5cV+51fvBq 96NNLz8YhRAGiF/sFSY4xKuHoqCEVWnJYMJaNaolssSqCyAdXvkbeFY7uIf2wCkVLN7a scIeisFhwJ7gKRBhBcoxADhPjaGT3TQZnNxKhziHe70dtIu0zrdqXJuRnCkDePbQvUye zBA4j9sjLqo4dUAjvMscE2eBJIVgfzZww5nDIodYVzv6uWKCq3ex0PH2qoPqhKoM/bns mio5+y+dbyCVViTZFt3PzxW9fkjEav4+bTu4HP1HQ7WJyNOTSxRravHJKgo1FM5PIXmH YFQA== X-Gm-Message-State: APjAAAVSBkJTL+1DIUTOgjjLuQ9gMI5DJCWBCzWdmYh25JzO3/GNSxg7 Vz53C/LWRjH+wmMVf5NfDrKatA== X-Google-Smtp-Source: APXvYqwrNthjqBlV/RL3WReAfg5hBYhOgQ1sTUVcjfhA0FjZyQO3ukp4Tkq5AsBP1XdlXm5bE9jM2Q== X-Received: by 2002:a17:90a:d151:: with SMTP id t17mr9328073pjw.60.1562909353263; Thu, 11 Jul 2019 22:29:13 -0700 (PDT) Received: from localhost ([122.172.28.117]) by smtp.gmail.com with ESMTPSA id r9sm10746373pjq.3.2019.07.11.22.29.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jul 2019 22:29:12 -0700 (PDT) From: Viresh Kumar To: stable@vger.kernel.org, Julien Thierry Subject: [PATCH v4.4 V2 05/43] arm64: Use pointer masking to limit uaccess speculation Date: Fri, 12 Jul 2019 10:57:53 +0530 Message-Id: <99d86496bf2e822479ec7f26faa6a6d31d4e5524.1562908075.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.21.0.rc0.269.g1a574e7a288b In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190711_222914_429925_3014EA70 X-CRM114-Status: GOOD ( 13.57 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Marc Zyngier , Viresh Kumar , Will Deacon , mark.brown@arm.com, Catalin Marinas , Russell King , linux-arm-kernel@lists.infradead.org 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 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: Viresh Kumar --- 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; }