From patchwork Thu Feb 14 14:49:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Long X-Patchwork-Id: 158401 Delivered-To: patches@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp1462230jaa; Thu, 14 Feb 2019 06:49:42 -0800 (PST) X-Received: by 2002:ac8:1735:: with SMTP id w50mr3380504qtj.361.1550155782786; Thu, 14 Feb 2019 06:49:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550155782; cv=none; d=google.com; s=arc-20160816; b=OW3FU0T8OBwNT0hKyz3Zk0ehw+FXuJ3lIH6O2Oe2M4kHg9Wii6W572MS1s1YG7/9J9 QJvOxpES6ukUOxiqohHtIgjYqZIvwnz5FlSN4Sp7265IE9p25jNBqRv0lz4DJ5k0M+iz 2lA/M24Xi6yGXmgzoww9WoxTT5SorqtDZO8wYmMmxy9+4Pgd3VZGBo8J/jh5ZUUfYSsK m6qcHgmXSLu3LLp6Gn6j3Y7jX42Wdk224jW4fL+F9/a7ChFCkaaQMZwwGBQDG5jKR7kW bLlQG2u4HMsS0P8blSnUx0zoopAYjLPEOVWHWP0WsJK74wBT1e7CF/5Y+3BcruHV8Fvx rHgQ== 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=zgIY4W12AifZiYGKPIJKvhZkVAc9WDHsN0M0ZqcY4Ag=; b=B1AZrtoBbev9RTJHM2ffHNUHZUljfm1Sj2l588zKxxdIAX8Ul/EoPsXnaEPP8NJ4wn iZtYdBZbn9zM5lKzvuz058y61em2BMNcDeqr5UkRDKCUfcfFuUEtJKZtcB5FNwMAoDBw nec7Tl6W3DZ0e8AA3nGvH3LOdzVRD+K8w4chUAsEn1Owwhy38Gufj5PUuHqFMXLhyFH6 T5lSKogBVoUc5OU+yFx1SnQIDoY564V+Y+Ype7oXbdGLqIy6wUc6pCMXQ5OPNBt2QykO 6oQIvcUuDGNQpCllT+SeuTuOp1KBgQdearHOasyY10n8Qb1sXfhtZtqpdFNvHv9yU1xk +u9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JYpPZB9y; 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 n45sor3230924qtc.52.2019.02.14.06.49.42 for (Google Transport Security); Thu, 14 Feb 2019 06:49:42 -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=JYpPZB9y; 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=zgIY4W12AifZiYGKPIJKvhZkVAc9WDHsN0M0ZqcY4Ag=; b=JYpPZB9yAsehQJ8kX2b9s5gS/ATc4yN+Z7ukFXwBX827q0jQldoOMgxHkkSofgTJ43 pCxVRZRsMMX5jka9RpzcT00sHG4QvKtkKUYJIIB8WF9AAPHXRdOPMm/BVhbCwj3oxQRl oYiOwZQg4vGq9kNokwivpbmcc7AfCtHy1BHjWz+gjxFDM+qsWTqsu9kyD6EYmJ87Ppq7 F08nsPalvOnqVY5Ncsvqsn33CgHh6e5/oRb+0c00vwESTAmpD9LSB8b0OfAG648lC4pg SEBuDxd+6Zed8aGYyQOLhNXCTlFbARJ3gmZyRGDwz/4/KrgrVtNjv841KkayKTKQSSix ZnBw== 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=zgIY4W12AifZiYGKPIJKvhZkVAc9WDHsN0M0ZqcY4Ag=; b=YD6+5uQylyuhQuAtKdUTrSMS7ddyxLk/XDmpc+XsP7hPtSb/kYVCJ9CS2dzt8ojgOI TT52NVrAtjmhEPpv7QtUz6lmqAzHO7MwBg8/pCC+doVNMy1wuCXHuc459isEoCPNn+hI r9iV6vc8jyTRQ/mD9cxzvmNL2zYzdxDeRnGapJVULrNhkVUNbAwr0/PQOnSaLKmtd/u5 y7VNsCEZWLngntzLgH5wSE30+zx/3Eyn9hUBOucfCvmBYkMjyJy1ZSpVpKq5l4wxALJV ONYYSQGlZrqANEmHl/uezsZABZMK3HK8FTAlfhFgxgukDk3XEaPshLX+jM3Td3kaQdSM Yyrw== X-Gm-Message-State: AHQUAuakcm7Clf4M1fPps2BM2lEEuKOZzPmXerClVNqzizI060jL31Y5 IbIArcIFaIE7FRKinlGQpGHJa76Q X-Google-Smtp-Source: AHgI3IZ0UvZbDTKpgh144q3MZAuV7zh79/AGn+9KJdoBj3hITiStWAv+2M2d+N7jtyEYkvhHLc4teA== X-Received: by 2002:aed:24d8:: with SMTP id u24mr3290346qtc.47.1550155782496; Thu, 14 Feb 2019 06:49:42 -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 k66sm1498919qkc.25.2019.02.14.06.49.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Feb 2019 06:49:41 -0800 (PST) From: David Long To: stable@vger.kernel.org, Russell King - ARM Linux , Florian Fainelli , Julien Thierry , Tony Lindgren , Marc Zyngier , Greg KH , Mark Rutland Cc: Will Deacon , Mark Brown , linux-kernel@vger.kernel.org Subject: [PATCH 4.9 07/16] ARM: 8796/1: spectre-v1, v1.1: provide helpers for address sanitization Date: Thu, 14 Feb 2019 09:49:21 -0500 Message-Id: <20190214144930.27539-8-dave.long@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190214144930.27539-1-dave.long@linaro.org> References: <20190214144930.27539-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 e616f61f859d..7d727506096f 100644 --- a/arch/arm/include/asm/assembler.h +++ b/arch/arm/include/asm/assembler.h @@ -465,6 +465,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 b61acd62cffb..0f6c6b873bc5 100644 --- a/arch/arm/include/asm/uaccess.h +++ b/arch/arm/include/asm/uaccess.h @@ -129,6 +129,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"