Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4799330ybl; Wed, 22 Jan 2020 05:02:19 -0800 (PST) X-Google-Smtp-Source: APXvYqznf8ObA9EZLK9kv7AhNSV7+pSJGyWjRqr5D73ScLzfZc1zfw8FAU8i2Btr5cWCzlHzF/oy X-Received: by 2002:a9d:518b:: with SMTP id y11mr6871602otg.349.1579698139609; Wed, 22 Jan 2020 05:02:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579698139; cv=none; d=google.com; s=arc-20160816; b=KxZfJ0i5gncpwsNDCF0JewwfaijpplUgQI5XjiItfTGCKPP0UStt1ZCPVBK1B3uGrj oQ6kMvgD8zszc4VeDTQJrBE1fGEp7NTJ67qlbzDOV2EmahKPKWjdYkGtlQp1T0QA624h rYNg5aNpYqyq/Q90K5eQyZxnj7I1RjCyQd01ylSp2NZZ9m8sr4Z5JVjyXRyjPS7s7s3x iEiGsw0wGQJU+IveUvxUn/v6wmVZk5oXgchq0k2B2vm13HJMGwCGjkf2xcZMAJD+vCry AgsJgK/Hjok2K/adch1aAiO5uxXu6I0fNwOy8g1XDvknZ2q4s3eWG6n+T6D90ua+icdh SyyQ== 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=iFbImthIOlCPbURK2OGGbKh/yT9m4I8aqMlWfZTcU+E=; b=C9T9KxYXP9RRXup8/702xe4cKDgSsEzjweeAQy7BltCTYSaroAvRM+lYIZqTv41r4O 7w7hBQf5B4rsRfSFiFEoJPkhGzxYkLI0oX9B096oEWGJwwQvvsD5ZJKKAdSDbj7016ik /tV7hq24j6im1GEk/TKaBUw1ZLuOngEQCEM8FzeJc4bCWXPdRVlsqZplFyssbK8KmDLG Sur/nGO6JhmS2AI414Xp+qH4EMgmQwq8Z2iORB29PgWvU5StGG9E+M2RCK4BJ+kxgL9m q3FJbVU+liMsgjFkK73oWNaga69Nyx6FRolahqDtYdgYpYgmppalApE9SayhRaRMVAnd ovuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=O5WSDQYU; 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 l6si13822742otk.134.2020.01.22.05.02.07; Wed, 22 Jan 2020 05:02:19 -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=O5WSDQYU; 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 S1729232AbgAVNAw (ORCPT + 99 others); Wed, 22 Jan 2020 08:00:52 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:16160 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729093AbgAVNAm (ORCPT ); Wed, 22 Jan 2020 08:00:42 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 482lrR2trSz9v4T6; Wed, 22 Jan 2020 14:00:39 +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=O5WSDQYU; 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 4nbWYbYP4Gr5; Wed, 22 Jan 2020 14:00:39 +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 482lrR1szqz9v4T4; Wed, 22 Jan 2020 14:00:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1579698039; bh=iFbImthIOlCPbURK2OGGbKh/yT9m4I8aqMlWfZTcU+E=; h=In-Reply-To:References:From:Subject:To:Cc:Date:From; b=O5WSDQYUFN5WmBLlV7O7THG41xuhILXKO98/NXe+lxig1aGv17e6ZnyrB6MRXu2jG NKawLj1k3m9pU27WuQZl9Hxp658ypN/7l88qegRA3KkX1sbFpaYV32f6js0KjkO8M0 JSprLI+a3mhuKSTS0CsmbIguNDPym/7m/92wTCsE= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 4E6698B7FC; Wed, 22 Jan 2020 14:00:40 +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 eOSvAb_poZnv; Wed, 22 Jan 2020 14:00:40 +0100 (CET) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.230.100]) by messagerie.si.c-s.fr (Postfix) with ESMTP id C78FE8B7FA; Wed, 22 Jan 2020 14:00:39 +0100 (CET) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id BF986651E0; Wed, 22 Jan 2020 13:00:39 +0000 (UTC) Message-Id: <36561aa79bfe4c6b8085ebd87a1967ea639fb878.1579697910.git.christophe.leroy@c-s.fr> In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v1 5/6] powerpc/32s: prepare prevent_user_access() for user_access_end() To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Date: Wed, 22 Jan 2020 13:00:39 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation of implementing user_access_begin and friends on powerpc, the book3s/32 version of prevent_user_access() need to be prepared for user_access_end(). user_access_end() doesn't provide the address and size which were passed to user_access_begin(), required by prevent_user_access() to know which segment to modify. The list of segments which where unprotected by allow_user_access() are available in current->kuap. But we don't want prevent_user_access() to read this all the time, especially everytime it is 0 (for instance because the access was not a write access). Implement a special direction case named KUAP_SELF. In this case only, the addr and end are retrieved from current->kuap. Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/book3s/32/kup.h | 25 ++++++++++++++++++------ arch/powerpc/include/asm/kup.h | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/32/kup.h b/arch/powerpc/include/asm/book3s/32/kup.h index 3c1798e56b55..a99fc3428ac9 100644 --- a/arch/powerpc/include/asm/book3s/32/kup.h +++ b/arch/powerpc/include/asm/book3s/32/kup.h @@ -117,6 +117,7 @@ static __always_inline void allow_user_access(void __user *to, const void __user return; end = min(addr + size, TASK_SIZE); + current->thread.kuap = (addr & 0xf0000000) | ((((end - 1) >> 28) + 1) & 0xf); kuap_update_sr(mfsrin(addr) & ~SR_KS, addr, end); /* Clear Ks */ } @@ -127,15 +128,27 @@ static __always_inline void prevent_user_access(void __user *to, const void __us u32 addr, end; BUILD_BUG_ON(!__builtin_constant_p(dir)); - if (!(dir & KUAP_W)) - return; - addr = (__force u32)to; + if (dir == KUAP_SELF) { + u32 kuap = current->thread.kuap; - if (unlikely(addr >= TASK_SIZE || !size)) - return; + if (unlikely(!kuap)) + return; + + addr = kuap & 0xf0000000; + end = kuap << 28; + } else { + if (!(dir & KUAP_W)) + return; + + addr = (__force u32)to; + + if (unlikely(addr >= TASK_SIZE || !size)) + return; + + end = min(addr + size, TASK_SIZE); + } - end = min(addr + size, TASK_SIZE); current->thread.kuap = 0; kuap_update_sr(mfsrin(addr) | SR_KS, addr, end); /* set Ks */ } diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h index 49fd977a498e..0676df6f3f86 100644 --- a/arch/powerpc/include/asm/kup.h +++ b/arch/powerpc/include/asm/kup.h @@ -5,6 +5,7 @@ #define KUAP_R 1 #define KUAP_W 2 #define KUAP_RW (KUAP_R | KUAP_W) +#define KUAP_SELF 4 #ifdef CONFIG_PPC64 #include -- 2.25.0