From patchwork Fri Jun 14 03:07:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 166744 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp1505340ilk; Thu, 13 Jun 2019 20:12:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqxLrAjiGVsgw2f/gH9OZR0ID2oHslgB5NbKtIloQdxjeUbD8vSyKkvGNwcCgpOn6gPtbnv6 X-Received: by 2002:a17:902:e312:: with SMTP id cg18mr24143589plb.212.1560481931649; Thu, 13 Jun 2019 20:12:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560481931; cv=none; d=google.com; s=arc-20160816; b=C7hOY/JXaoF6r0SDJjqkLdYAbFPvTUaXjfKIjkrnZTLtPA0UcSr0aZw5mZVqFZD1ET 2NYWHJcjlQUDgbT/Knj5s73nw+fXIvW+3oQ5lJfPG4BgNdXp31ZExMleYVFPMSFM0VaD 3YsdbuV5GONL0WzliKv5NMe4EDF83rrEEYHNnyp+QvcrWW2hKresoM6DjN2rimFUuI8q Z2CFIAfWtR8wjm70cxskbvhbRU+Icj6Krw4MxBiwgG+ibgJHh0vg07MzFsMxKwLY42C/ eaf2BCZsG4woi1c8wIh9LWs++CqqDnUHk7dbMPmfQOHI7bg340HM2zssCBYF3aIadVlr GYsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oplH30x1zLW/kw5XTQuFI7s72sVvdi1RahKBb4zAIVI=; b=IhiVjzQcC6CHEdFOBjGJEpNywJeOtVVUpDNfaNrdg3CoFVt/UrTSQPQcnmoZyMCO17 1stMk3wNuUZ91TfMMkJ/RWSv16IPt//jx0GxkJhrBpn1tnHaX+JDoze22SMbdHyV7tS5 2m04OsfGTIWpZDn3T85hFHVOcC4t4N4ZrBIhevdl/18pdRtgk6wEUpVO+wAIN3IX8+yb 9/w1mR7rr1blU80IaknbofdPVgx3h/7xV3ZB2lsHngrJzs7IInrDdMFqtBPhDwLh8JTw Q4lf0MM7oYSJ3t020D6LIcMlYK1hJc4GJCyAmBnM65UyDQyv/27ucnJPJGEr7JUELe+p H84w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yzltk8JB; 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; 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 e8si1073936plb.420.2019.06.13.20.12.11; Thu, 13 Jun 2019 20:12:11 -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; dkim=pass header.i=@linaro.org header.s=google header.b=Yzltk8JB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725942AbfFNDML (ORCPT + 14 others); Thu, 13 Jun 2019 23:12:11 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:35932 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725819AbfFNDMK (ORCPT ); Thu, 13 Jun 2019 23:12:10 -0400 Received: by mail-pf1-f193.google.com with SMTP id r7so499782pfl.3 for ; Thu, 13 Jun 2019 20:12:10 -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=Yzltk8JB8mBXf0jZiiOn54g5/T0KcQ7kKV+UgQ9lpUwSlLs7Pt5kZUz7O8VRM5Gywr a6759AUaffVtNH/TQGwSHY5oA17fJKXMnvPoFyntQBTfIscQNfo17nFck1DVJjrDtGmA +OsNTrb2LaV2WQoX5sd/qPiJek55dGs814vb4bBwWTlq1ot6VHkqG7m98hfa/cY12zOZ NVjM5NrthLLacQzOfmEqJDxiugx32DoJGjZw/qboBQpDb5iGkEpwkxHwzJmyDijrFmnK zlGHBFAbNvl6JKVjnyBkeDwcgVbmq34wRQMm7GRnJ59Qelhe/pLC0cpTAsYd2dtksl3m bQxg== 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=gxbg4wdEJPL7LAHOOen286EmXDz1Ot08WA4YDWzkDYAksMftUWLDhJ1jfbehxqRUkj 2z9yzeuBRyzbNMfM+aijXiK+Z43rjqS6enhmBx+Aa5W7rEk/clYkZsq561BYZve+hFzh xhYBu1vz0NxEVXH8iqO257kZGGk0Cleauy5C+2Giq13wukkI8C13RnWpwG5l5+aYcjQZ xUk/CskgK/ZDIjRHepBb0qouvxirPKn/w/bI7rqnmgiOd7WhnVMCFUuDhP5wpSFctxvt s7GgOB+awEQYkV/mww7iG2/FMvs/8nnXZ7SvQaYeSNJt2fFg7eOLOXC5+34B/F3LPBZ6 GGpw== X-Gm-Message-State: APjAAAW0CLpmvCxZcRE47ZVbVJtKFzjQCaCHJ1nvkGkW4boegOxx8621 6PeDgE/jCFKa42fX1AzbPhkfnA== X-Received: by 2002:a63:dc15:: with SMTP id s21mr34323601pgg.215.1560481930079; Thu, 13 Jun 2019 20:12:10 -0700 (PDT) Received: from localhost ([122.172.66.84]) by smtp.gmail.com with ESMTPSA id y1sm1198391pjw.5.2019.06.13.20.12.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Jun 2019 20:12:09 -0700 (PDT) From: Viresh Kumar To: linux-arm-kernel@lists.infradead.org, Julien Thierry Cc: Viresh Kumar , stable@vger.kernel.org, Catalin Marinas , Marc Zyngier , Mark Rutland , Will Deacon , Russell King , Vincent Guittot , mark.brown@arm.com Subject: [PATCH v4.4 06/45] arm64: Use pointer masking to limit uaccess speculation Date: Fri, 14 Jun 2019 08:37:49 +0530 Message-Id: <33a351b8683ca17c3d6ed3711d2c6fe2ae1a36f3.1560480942.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.21.0.rc0.269.g1a574e7a288b In-Reply-To: References: MIME-Version: 1.0 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: Viresh Kumar --- arch/arm64/include/asm/uaccess.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) -- 2.21.0.rc0.269.g1a574e7a288b 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; }