Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4674403ybl; Mon, 3 Feb 2020 23:39:52 -0800 (PST) X-Google-Smtp-Source: APXvYqxj2nIPLlwb8zkxVD9NMxLvy+xu9gjN+AdJ9a7EyY1Y8HXJ8h4nslLwfKLAxHpzHC3ofBU0 X-Received: by 2002:a05:6808:658:: with SMTP id z24mr2607440oih.91.1580801992778; Mon, 03 Feb 2020 23:39:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580801992; cv=none; d=google.com; s=arc-20160816; b=pY6k+qiLhg1rtS82IVdbmEuzvxznfFl6zMJgRdp4Q3IpgNUVGmYyZ1IFf5dkmd6qkW mp0itYZ3oY4AhFXORmcdTrUAAKO9CAc9wtyCbcVZciC6AYAyZgrsS6sOsCUn36qW6huE VprPOXYYAeddbj3GE7coWa6oDJPxUmxPQU7GtRn3iGoClIqmARBLr65dDTudVkgRTHj9 x8PmPU3YeUSUo1+8ZjESYmMGLWSLiHqkJz/qKrgSb8ptjzIZY+DvwcolVZN+tkd4zV6J Tr1EAlVI0m3XRRf2zPu0Nyz9M3GqDhIVPkGR3LoFEPkJyT0i+aZk954ywyPYzrAaMlT6 E4AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:dkim-signature; bh=IXjiKlijLtzQuR/CNi0kS3j7+7PM/7fqUO8AWUmnzeg=; b=Btnt7A8lSBR9zt+FocRkVda+XxOJkAeF0gkEL3n4lGXnpAv0QV+S15E0xOphHOMz/b JCHRM/o16hX4aLM+hWe5H/2gezFXgnb7/AznwqY9mdWjcI19lwQpRVceJp1pWFl2CzeS HcAhXjcCM9SsjHI8hO05abr2wZ1pjHgDumT0ub5962THcNEhs0lhn7hOVFd+bYxi3l6/ hObVTC/vdt7cQMAgO3IEWYmcGdTiTFR0EOYNQo2wHBPE3M0dPV6nldPgt/lNVBEf2Y2b bRoD5ywmGitce07DUlhMHzawUaRFD7CjRxTRZTP4c1ycnsMPQW5D777e7ygCgcwPrAFa 5ptw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=hSRMu9wz; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j2si10966401otr.255.2020.02.03.23.39.41; Mon, 03 Feb 2020 23:39:52 -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=@c-s.fr header.s=mail header.b=hSRMu9wz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727151AbgBDHiA (ORCPT + 99 others); Tue, 4 Feb 2020 02:38:00 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:35179 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727129AbgBDHh6 (ORCPT ); Tue, 4 Feb 2020 02:37:58 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 48Bc432sGMz9vC1g; Tue, 4 Feb 2020 08:37:55 +0100 (CET) Authentication-Results: localhost; dkim=pass reason="1024-bit key; insecure key" header.d=c-s.fr header.i=@c-s.fr header.b=hSRMu9wz; dkim-adsp=pass; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id IWa7axmcAhxe; Tue, 4 Feb 2020 08:37:55 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 48Bc431n5hz9vC1c; Tue, 4 Feb 2020 08:37:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1580801875; bh=IXjiKlijLtzQuR/CNi0kS3j7+7PM/7fqUO8AWUmnzeg=; h=In-Reply-To:References:From:Subject:To:Cc:Date:From; b=hSRMu9wznvfD+qbck0wOIk87YeVy3P7ZkgPtDiAnur09tpGBUEosTAAmr8VsL7vyr x/zwOKFKm6fKOd2TnHGotd6uDnkaDr+NqKtNlK8FQDDE5NIg8tCFrZwJXzp0hl8qxi V0MdkHXXn+CXa8fUDjK9UzDcdYrupgzkjLxCktt4= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 2B7018B7B6; Tue, 4 Feb 2020 08:37:56 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id RM1G0Aq0u8hX; Tue, 4 Feb 2020 08:37:56 +0100 (CET) Received: from po14934vm.idsi0.si.c-s.fr (unknown [192.168.4.90]) by messagerie.si.c-s.fr (Postfix) with ESMTP id D1AEC8B755; Tue, 4 Feb 2020 08:37:55 +0100 (CET) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id 9C3F0652B3; Tue, 4 Feb 2020 07:37:55 +0000 (UTC) Message-Id: In-Reply-To: References: From: Christophe Leroy Subject: [PATCH 4/4] powerpc/uaccess: Implement user_read_access_begin and user_write_access_begin To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Date: Tue, 4 Feb 2020 07:37:55 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add support for selective read or write user access with user_read_access_begin/end and user_write_access_begin/end. Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/book3s/32/kup.h | 4 ++-- arch/powerpc/include/asm/kup.h | 14 +++++++++++++- arch/powerpc/include/asm/uaccess.h | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/32/kup.h b/arch/powerpc/include/asm/book3s/32/kup.h index 3c0ba22dc360..1617e73bee30 100644 --- a/arch/powerpc/include/asm/book3s/32/kup.h +++ b/arch/powerpc/include/asm/book3s/32/kup.h @@ -108,7 +108,7 @@ static __always_inline void allow_user_access(void __user *to, const void __user u32 addr, end; BUILD_BUG_ON(!__builtin_constant_p(dir)); - BUILD_BUG_ON(dir == KUAP_CURRENT); + BUILD_BUG_ON(dir & ~KUAP_READ_WRITE); if (!(dir & KUAP_WRITE)) return; @@ -131,7 +131,7 @@ static __always_inline void prevent_user_access(void __user *to, const void __us BUILD_BUG_ON(!__builtin_constant_p(dir)); - if (dir == KUAP_CURRENT) { + if (dir & KUAP_CURRENT_WRITE) { u32 kuap = current->thread.kuap; if (unlikely(!kuap)) diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h index 92bcd1a26d73..c745ee41ad66 100644 --- a/arch/powerpc/include/asm/kup.h +++ b/arch/powerpc/include/asm/kup.h @@ -10,7 +10,9 @@ * Use the current saved situation instead of the to/from/size params. * Used on book3s/32 */ -#define KUAP_CURRENT 4 +#define KUAP_CURRENT_READ 4 +#define KUAP_CURRENT_WRITE 8 +#define KUAP_CURRENT (KUAP_CURRENT_READ | KUAP_CURRENT_WRITE) #ifdef CONFIG_PPC64 #include @@ -101,6 +103,16 @@ static inline void prevent_current_access_user(void) prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT); } +static inline void prevent_current_read_from_user(void) +{ + prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ); +} + +static inline void prevent_current_write_to_user(void) +{ + prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE); +} + #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_KUAP_H_ */ diff --git a/arch/powerpc/include/asm/uaccess.h b/arch/powerpc/include/asm/uaccess.h index 2f500debae21..4427d419eb1d 100644 --- a/arch/powerpc/include/asm/uaccess.h +++ b/arch/powerpc/include/asm/uaccess.h @@ -468,6 +468,28 @@ static __must_check inline bool user_access_begin(const void __user *ptr, size_t #define user_access_save prevent_user_access_return #define user_access_restore restore_user_access +static __must_check inline bool +user_read_access_begin(const void __user *ptr, size_t len) +{ + if (unlikely(!access_ok(ptr, len))) + return false; + allow_read_from_user(ptr, len); + return true; +} +#define user_read_access_begin user_read_access_begin +#define user_read_access_end prevent_current_read_from_user + +static __must_check inline bool +user_write_access_begin(const void __user *ptr, size_t len) +{ + if (unlikely(!access_ok(ptr, len))) + return false; + allow_write_to_user((void __user *)ptr, len); + return true; +} +#define user_write_access_begin user_write_access_begin +#define user_write_access_end prevent_current_write_to_user + #define unsafe_op_wrap(op, err) do { if (unlikely(op)) goto err; } while (0) #define unsafe_get_user(x, p, e) unsafe_op_wrap(__get_user_allowed(x, p), e) #define unsafe_put_user(x, p, e) unsafe_op_wrap(__put_user_allowed(x, p), e) -- 2.25.0