Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp97599imj; Thu, 14 Feb 2019 16:09:50 -0800 (PST) X-Google-Smtp-Source: AHgI3IaoFbjlzZRVZ8BUYHM43ZwgKR9sxJAdTWw5WuY4gfMVdSP8/9+L7M04/B5MyMqDpI5LIrtl X-Received: by 2002:a17:902:704c:: with SMTP id h12mr7271149plt.30.1550189390518; Thu, 14 Feb 2019 16:09:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550189390; cv=none; d=google.com; s=arc-20160816; b=T+ZZblVuAYG/H2KGed9vIEVfsf1lSQaP4ZO/a5pFsOqYbdB/K8PHxZkz3th0us9VQk TyDvmj43vModGet7VKQlSFBfoYpEnCVDPdiwPIc7DTRhJP7kRNYs/yUwiRXUnYJKRGPU A9MCO9dmmgGGk1Y50rQbVo/A5Ubxu4IQOFjMeKsFh+YaSucCWqA7h7cfCvQsY+YWRflc RrQ7DCbmgxEwBCQegNXy2IZCOCdo15byT1nbJeZjaVLZ8/Frkkz+wwBM/TWhske2Gn8j quQimd+DAdQokjeKc4inZZvkAjR7Gg1UDFXlW4ryVREg36eGmC7c9RzXB++YLKsHkyBd Qs+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=zgIY4W12AifZiYGKPIJKvhZkVAc9WDHsN0M0ZqcY4Ag=; b=umFp6B9NBHrSyQaewao+uMtAMy+GW34+Y1jb+nYedy1ZHxz7Per9asOwmqSmmuWFBU rqsvtsvyFywksJ9qX+J24RGbjQHJxusakVAC41dg3PY4OhJ6bkAmIwXy8LHrxxNwhj3r QMwgBpPIo5wJE3mqJaVqF3nvnKdloGOvcTsoCK3qdFV6gTrgDvEMArbSxk+2VorSqFCm 7stvECN+LmIUrolL3jO/lgVGt5psWe4GKgOqNMd683lLQ+nnRhBee4xJd1P5faiw9/HP C8bDfdjvtPF1VEEN5xIxsAa56W2v9UNWOBlJ0QgWEblNdRYtnc6ZNv3HOodYsNx2Fsmk RoCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JYpPZB9y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 b3si3930321pld.282.2019.02.14.16.09.34; Thu, 14 Feb 2019 16:09:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=JYpPZB9y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S2391023AbfBNOvF (ORCPT + 99 others); Thu, 14 Feb 2019 09:51:05 -0500 Received: from mail-qt1-f180.google.com ([209.85.160.180]:35593 "EHLO mail-qt1-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407300AbfBNOtn (ORCPT ); Thu, 14 Feb 2019 09:49:43 -0500 Received: by mail-qt1-f180.google.com with SMTP id p48so7119483qtk.2 for ; Thu, 14 Feb 2019 06:49:42 -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=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=PLk1uc8/7EQ3pS1asZu8tFHtJTgXoVhGgduk7EDGymGUPiVwzJxTpFOAdWZs001h0l 8rSHCQqVCbDMv4iZ+10ywTREnoeFkMIHXg27xu71BuR7kl1Bl1aVsUhJyJTNMmrCpdjF pUhxLzy9WGk0jKNTwKdYhx3U9vIp6h/rITiumuH+5E2FsaY4xI7ZIZO0xBrIW38FARTc YlWG0xTMz8dhG78VlqFrkAJLAqz7dL3Ui6Dx0M+/0AoCfuwdh3BPbg1Pt0E6sC7kh86h fgSTX1x/zm53s/HzLiFfcwWHgif7xUTA+HDRg971CIoojQeSESEsM7/XHpWX4T1QAsHu Or4w== X-Gm-Message-State: AHQUAuZvkqR7qsCjJa514kF247ca/gFh7KE5uWI7Dckl8XNpiznk7juc d1AhF6oZm5tCInya5+SJ7n3ntg== X-Received: by 2002:aed:24d8:: with SMTP id u24mr3290346qtc.47.1550155782496; Thu, 14 Feb 2019 06:49:42 -0800 (PST) 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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(-) 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" -- 2.17.1