Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1087634imj; Thu, 14 Feb 2019 00:45:38 -0800 (PST) X-Google-Smtp-Source: AHgI3IaATNHatOs4GGHRnTlGZw1zNXWD+psPBm3Tb++NGXdIkezMqHV4ci0cgpjRJ5Myd6s0gzbA X-Received: by 2002:a17:902:b190:: with SMTP id s16mr2980395plr.262.1550133938301; Thu, 14 Feb 2019 00:45:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550133938; cv=none; d=google.com; s=arc-20160816; b=rcIvBp66fL+yatwbTfJCz++ET6eKWkjPRzY1yuA0sE8PGS3Zu/IhZdOv6ZAgzp5v82 3cw45ZO5AgSp8q3L1fmNPNJvBofFN9UoXdR12mUhc52wHhvf1BVfL7d0dXnTiUR8XmQ+ W/pD8YZTnyfdvK90c3t5Pfek1zLhid7+6TY+18Uqao2zBG2ykbuJiCX0fGEPOW15V1aR EnZ4pDTW7LO+5QQrUURbzWEf6Lg9CRHVjticFCxa4i3ztZQh/SFSG2hDQx7y4snFNmeQ WH3Zgf8Axa0jZsDA7nnnIvLDcdc2a8uiS3qaRvujitiKz0Sr8NPQfGYjCt1QkBHlfZad 2TjQ== 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=rK+V5Bt5jiMQbW7MEfKBAMfmgwoZSjPrByJHM+oVMeo=; b=yyHB9moZE7IX1Oz6trIrFmq/USfwNty449Tjq03cHTOVOZF6n/rIPUo0r3S//13Qmj iQcEI9094trrlV3lSJn58gajHb4zTl5Y/gS/sfZnk5sjGXHRXhfhtWFYiqDzELAIRZue ErRN0uMEvKoaO76DZhpaGjRAdnReWPzUFugMhwCCD/ILLiUZmHDnkQg9JagSMPQ91uu4 HrZvpmrVLu25jrP4nvuHoySChjwqi3GjqOqJSsRw1x1igYCvjE3jaFNS3vQFQq3c1Sj+ +zzc0daIRJ62PUdqQ4yJkWMcSzfmgObACPD+8H50lB2a/ZC33ja6MJmPj2WQbAgfSicl AW6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="GTJ/bm4f"; 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 b74si1865726pfe.47.2019.02.14.00.45.21; Thu, 14 Feb 2019 00:45:38 -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="GTJ/bm4f"; 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 S2436785AbfBMVcm (ORCPT + 99 others); Wed, 13 Feb 2019 16:32:42 -0500 Received: from mail-qt1-f172.google.com ([209.85.160.172]:38639 "EHLO mail-qt1-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436757AbfBMVci (ORCPT ); Wed, 13 Feb 2019 16:32:38 -0500 Received: by mail-qt1-f172.google.com with SMTP id 2so4478730qtb.5 for ; Wed, 13 Feb 2019 13:32:37 -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=rK+V5Bt5jiMQbW7MEfKBAMfmgwoZSjPrByJHM+oVMeo=; b=GTJ/bm4fEru5IpP4vE+G1cANhMJFH2iFjAYzU/wHNNGNCwI+nz6rQOavzMJnf+tdmz Ap1od0Qvoqs1tG719Cp3mvlcOMz8/tjSGFQmxmFns3VpNswa7Y/etgTGt7uJVxT/rvEy 5n2hMb6jBYK4122o8N1rIHPGv6Y4ID/+IxcCm/swWZxPYAb/zbxXrG4/vCSIjDTPUutc AQEbWgTmEuyoKkWaXWLpZuQVHxGthkZbN0XxqwgmGW7lyZGU9KHse1r5L31bOfkBcWuF 5gFGrsP8/N76vlwfkFAaJtunv5NFkicZIyxvzZ+Xhssaefi+LF4jDyAmzmUE9BI3Bk69 D4Og== 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=rK+V5Bt5jiMQbW7MEfKBAMfmgwoZSjPrByJHM+oVMeo=; b=RGx4I/H7tJyCdkG0r28uYwm9yFr4Rs665HuI2Jq5d0RRqxh23zcQPAhTp/06nPvMzu MiswYvmNUmO38fQJG31uJzo7hKtvz46wpVgu6KX2Idf/ZxjW85VBwB1eLlLnzdR5KcfE R2wsJTviBSyRk7jjmD8PmNe/vauVfbGu8BGO2sw1rBlJR9VCOhSfqzqwuBgq8vmxHTKS 4fabEz6EsYTuICjsgM+k+xiS9gOA+RLjxEB6gL1JktOA/F3RXhIBaiOWBYTOzav+UGnJ WTeOkJ9NWwKT4UrYxFrC8+kY2HCxUsIWbrrNgvhDak9mXNSCaLblhXixoVk2AthMj86G uqcw== X-Gm-Message-State: AHQUAubCdT2vjBZjwbT0U7UEEnG0/Bkg3gUXnwcHv7XE7IJM1cU0GfTe AVjbGset2lPHPY8gs6aoYQqKJA== X-Received: by 2002:ac8:1e04:: with SMTP id n4mr224614qtl.378.1550093556875; Wed, 13 Feb 2019 13:32:36 -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 8sm382675qtr.7.2019.02.13.13.32.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 13:32:36 -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.19 08/17] ARM: 8796/1: spectre-v1,v1.1: provide helpers for address sanitization Date: Wed, 13 Feb 2019 16:32:14 -0500 Message-Id: <20190213213223.916-9-dave.long@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190213213223.916-1-dave.long@linaro.org> References: <20190213213223.916-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 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 1191e7da8fab..c136eef8f690 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" -- 2.17.1