From patchwork Thu Jan 10 17:51:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Long X-Patchwork-Id: 155252 Delivered-To: patches@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp2176165jaa; Thu, 10 Jan 2019 09:52:04 -0800 (PST) X-Received: by 2002:a0c:9384:: with SMTP id f4mr10943982qvf.239.1547142724760; Thu, 10 Jan 2019 09:52:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547142724; cv=none; d=google.com; s=arc-20160816; b=DAJg1S37dGTdAEFgDv7jTvpQgDXbSUuW7pLorbGxhsgQsna36ESkuUySrZmsRyXE5Y oTch/EVRWqpjgkqGhfclUz0Qqi1Cw8AU3VEKeRbpliZsf5cWLldZSbGf3M2uxv6+0h3F E2v1xw2t+moKnLhNb272fb935Gv7kSFIkYixT1awWq0/VKFdRm9R8MQI6tLaB4z3SU0j 7nzwyzy1fyH5uEuHYV7O481sH2ZRfxWOfub9IihPpLUvvn1p8L5uSODpl5a/qi4oIg1l KXNy/PavqqV+j+jba55t8dpdSvP1tyBFtUR7uOX/xBjHSGdGATmIehzAG9/RZVX48XKx AYJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=39y7AjK7peQfKaqAWC/HsqOhv07u/eu+YO/gawsGjOo=; b=ecAZkUNX28N1x5+obl+y6dBzEfxYA98XwsNKQOK+Ykir8gA1+TWbfl9Amwp0E8CH7G UVnycJxv2jWUaPg/H0hvJfmJXjOYP2mVmW2KUzJYGHNktUzmQQSMpEt8wdEhEMVskini jJ05SKal9t9Rw+fOvP22Et6b94ZJMdd8OJGo9r2hW/3YRDzI2epvOTL9ZILWsZabuHCk SqIHBvAu5jOp2j194Nq6XMBRPSmlQvM9DtZodtXAItQ3js85NUAQEgUMkFCNDtMS9kp/ ARCaYmc/Qt/ixBj4mWHbsmWArW0VuI4GvIzU0pQLHOzjzJbo/SDlAIlkhhClfdNI7yE3 EwUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ccXudGro; spf=pass (google.com: domain of dave.long@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=dave.long@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id y29sor67619829qvc.49.2019.01.10.09.52.04 for (Google Transport Security); Thu, 10 Jan 2019 09:52:04 -0800 (PST) Received-SPF: pass (google.com: domain of dave.long@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ccXudGro; spf=pass (google.com: domain of dave.long@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=dave.long@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=39y7AjK7peQfKaqAWC/HsqOhv07u/eu+YO/gawsGjOo=; b=ccXudGroi2GQcoI9rK03mo+Q9Ktafwd6ciQ3lohK2nFSQUXMgEwxtxiM5lk4owrlRq AnpTk4wuefjZPeL+9iE/wuB1yRBjWMQNghxB8Fjg8xDvKP55nTyLGaFSIGJvjSUqpdqG KnTAaEhTRVfoiRNl41QuYThpLlho6E9pKrILs= 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=39y7AjK7peQfKaqAWC/HsqOhv07u/eu+YO/gawsGjOo=; b=AaJ9Cbg6u4r/ZGp2K4uTSYA9pYAUgRY77o+j+LEfKOyIz8q3npYh/yvmfbbzr/WUG+ PiHFOzzdhsTnJKw2JuC1U2vnqUBiWtfqwSCpBp0GnrdPQOYXXy9//DU25epJ+oCj2diF vJLKOmOWPh0wUno0gvD3ljPgTLBfJecacaRavZxxGDWGaQx40CJ9HgLqmvi/2UVytK4S cJVx1zHNMknFvrO3SXB7pMcHRnul4GkLhKMv5OHyIf2yW+tp8qjh7nS+eW24gI/rtPo2 OnrcSX6fAr+SshQzm5kN//MQj7Nz9z1qD2Lw1lRSUJeB1tIporAFIpXHvbijBfEYrGaW ujtQ== X-Gm-Message-State: AJcUukfL6gZHtOtUMhVh8zCG2KFZ2iOgkifcobyfpz8p6PW1E6MEZpi2 g6XLDrgyiAz91u4AbdpU5XrANxTP X-Google-Smtp-Source: ALg8bN63ztSATNuB271lxdGZmmZ5SCqyS2k3AqKJyr/9ig2yjoK8xiGGuwQtI2meqg7DkjEJtrw8WQ== X-Received: by 2002:a0c:bb82:: with SMTP id i2mr10600704qvg.159.1547142724500; Thu, 10 Jan 2019 09:52:04 -0800 (PST) Return-Path: Received: from localhost.localdomain (pool-72-71-243-63.cncdnh.fast00.myfairpoint.net. [72.71.243.63]) by smtp.googlemail.com with ESMTPSA id b8sm41815904qka.79.2019.01.10.09.52.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Jan 2019 09:52:03 -0800 (PST) From: David Long To: stable@vger.kernel.org, Russell King - ARM Linux , Florian Fainelli , Julien Thierry , Tony Lindgren , Marc Zyngier , Mark Rutland Cc: Greg KH , Mark Brown Subject: [PATCH 4.14 08/17] ARM: 8796/1: spectre-v1, v1.1: provide helpers for address sanitization Date: Thu, 10 Jan 2019 12:51:41 -0500 Message-Id: <20190110175150.5239-9-dave.long@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190110175150.5239-1-dave.long@linaro.org> References: <20190110175150.5239-1-dave.long@linaro.org> From: Julien Thierry Commit afaf6838f4bc896a711180b702b388b8cfa638fc upstream. Introduce C and asm helpers to sanitize user address, taking the address range they target into account. Use asm helper for existing sanitization in __copy_from_user(). Signed-off-by: Julien Thierry Signed-off-by: Russell King Signed-off-by: David A. Long --- arch/arm/include/asm/assembler.h | 11 +++++++++++ arch/arm/include/asm/uaccess.h | 26 ++++++++++++++++++++++++++ arch/arm/lib/copy_from_user.S | 6 +----- 3 files changed, 38 insertions(+), 5 deletions(-) -- 2.17.1 diff --git a/arch/arm/include/asm/assembler.h b/arch/arm/include/asm/assembler.h index b17ee03d280b..88286dd483ff 100644 --- a/arch/arm/include/asm/assembler.h +++ b/arch/arm/include/asm/assembler.h @@ -467,6 +467,17 @@ THUMB( orr \reg , \reg , #PSR_T_BIT ) #endif .endm + .macro uaccess_mask_range_ptr, addr:req, size:req, limit:req, tmp:req +#ifdef CONFIG_CPU_SPECTRE + sub \tmp, \limit, #1 + subs \tmp, \tmp, \addr @ tmp = limit - 1 - addr + addhs \tmp, \tmp, #1 @ if (tmp >= 0) { + subhss \tmp, \tmp, \size @ tmp = limit - (addr + size) } + movlo \addr, #0 @ if (tmp < 0) addr = NULL + csdb +#endif + .endm + .macro uaccess_disable, tmp, isb=1 #ifdef CONFIG_CPU_SW_DOMAIN_PAN /* diff --git a/arch/arm/include/asm/uaccess.h b/arch/arm/include/asm/uaccess.h index cf50b030c62c..a5807b67ca8a 100644 --- a/arch/arm/include/asm/uaccess.h +++ b/arch/arm/include/asm/uaccess.h @@ -99,6 +99,32 @@ static inline void set_fs(mm_segment_t fs) #define __inttype(x) \ __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) +/* + * Sanitise a uaccess pointer such that it becomes NULL if addr+size + * is above the current addr_limit. + */ +#define uaccess_mask_range_ptr(ptr, size) \ + ((__typeof__(ptr))__uaccess_mask_range_ptr(ptr, size)) +static inline void __user *__uaccess_mask_range_ptr(const void __user *ptr, + size_t size) +{ + void __user *safe_ptr = (void __user *)ptr; + unsigned long tmp; + + asm volatile( + " sub %1, %3, #1\n" + " subs %1, %1, %0\n" + " addhs %1, %1, #1\n" + " subhss %1, %1, %2\n" + " movlo %0, #0\n" + : "+r" (safe_ptr), "=&r" (tmp) + : "r" (size), "r" (current_thread_info()->addr_limit) + : "cc"); + + csdb(); + return safe_ptr; +} + /* * Single-value transfer routines. They automatically use the right * size if we just have the right pointer type. Note that the functions diff --git a/arch/arm/lib/copy_from_user.S b/arch/arm/lib/copy_from_user.S index a826df3d3814..6709a8d33963 100644 --- a/arch/arm/lib/copy_from_user.S +++ b/arch/arm/lib/copy_from_user.S @@ -93,11 +93,7 @@ ENTRY(arm_copy_from_user) #ifdef CONFIG_CPU_SPECTRE get_thread_info r3 ldr r3, [r3, #TI_ADDR_LIMIT] - adds ip, r1, r2 @ ip=addr+size - sub r3, r3, #1 @ addr_limit - 1 - cmpcc ip, r3 @ if (addr+size > addr_limit - 1) - movcs r1, #0 @ addr = NULL - csdb + uaccess_mask_range_ptr r1, r2, r3, ip #endif #include "copy_template.S"