Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp472447ybl; Fri, 24 Jan 2020 03:56:48 -0800 (PST) X-Google-Smtp-Source: APXvYqzm+dpE4/xZb/rBIbUBZpPX7To8yFy1FY9ZQGuR4llcIzT+H9p/whTc7hywa+6xEvajMKRF X-Received: by 2002:a9d:6f11:: with SMTP id n17mr2368745otq.126.1579867008735; Fri, 24 Jan 2020 03:56:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579867008; cv=none; d=google.com; s=arc-20160816; b=yYYqnsWGh05fk4mLDGBqoYTvXUzkw2xVGKNZf2OaLK4+j1sOdw5i9c+CbCoM/2SFE7 1/nI8WQrpF9gp6uPwpkYoMwCEy/yULHcohDp3BJuh1ICpsNFckFSZL/8z97wblKtP4tW D6/rPqN8Ix50wxk6v0ve2TxqWdBXza+RTr14Rzdx139vdlypYzhDjCQtAwYOFpfe0vFP rdu7KrcdYjhGT7cJHKlwRddhhiXaw/afX95xHjNwAxFI9xC0rTwrnYUtquaPkFhn2FLa TRdRf/XHntmDCD3CauiCLLLsWCChKvqOZReEd1namkabObCpCeLzBE6impPPb1WQWu8v qsDg== 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=JXDaBnd7mgdJWDIpZlE9SEvwyWmEoszznADrTkNkdwM=; b=l6qZlTrILCwK5fDut07eAwEfcYnkX5BABa/HUglF68VgyK8DSOvQ8UTcNlaTn7Fv54 2jgJcX6Uab82u2WvDq4MMhcN3FE2fIBY1zmGMJrq8gycXqbbWjYApOgPfa8OUQoOJIeS NgyfWMrV25/OQe05GLTmg4I4bDdEU9SGYNxBVDiNlRdobjHt98z7v8P0YmHnweuXdw7X xNJUzEYfEvjvOJC2kFLBJvKKXtyfOq8zHhMlUIlruUovmLaEF3MGietdN+PwsmFDd15Q +U+njgpDqS4oI9mvvwPl4cLPENCk5zgUbMq3HOLUeBG82rX0LZKvr3Wqnx7f4dW5G45P UuZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@c-s.fr header.s=mail header.b=gf3TbqMx; 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 p5si2959495oto.116.2020.01.24.03.56.36; Fri, 24 Jan 2020 03:56:48 -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=gf3TbqMx; 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 S2387809AbgAXLy6 (ORCPT + 99 others); Fri, 24 Jan 2020 06:54:58 -0500 Received: from pegase1.c-s.fr ([93.17.236.30]:7048 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733102AbgAXLyp (ORCPT ); Fri, 24 Jan 2020 06:54:45 -0500 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 483yHQ428Pz9tyNC; Fri, 24 Jan 2020 12:54:42 +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=gf3TbqMx; 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 1fODUHn7HhAt; Fri, 24 Jan 2020 12:54:42 +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 483yHQ2zh0z9tyMv; Fri, 24 Jan 2020 12:54:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=c-s.fr; s=mail; t=1579866882; bh=JXDaBnd7mgdJWDIpZlE9SEvwyWmEoszznADrTkNkdwM=; h=In-Reply-To:References:From:Subject:To:Cc:Date:From; b=gf3TbqMxn4RpFN3iRulSVmL4SOIOE5qfoyd7wuIo2yD2J3XV0pBONcYt/padeKotW ADF45oxIjvSzVf6rktruAL9T7/Fwj+w67rB/gzC0WshJ/Mad/Azx7x5fyE8/dy+5W1 LpIY/grk/X+52KDi+JmeWtKdVcusrZL/hxD96GZc= Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id AC3C98B85C; Fri, 24 Jan 2020 12:54:43 +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 1M8UtqIa11cZ; Fri, 24 Jan 2020 12:54:43 +0100 (CET) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.230.111]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 879518B84A; Fri, 24 Jan 2020 12:54:43 +0100 (CET) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id 69317651F0; Fri, 24 Jan 2020 11:54:43 +0000 (UTC) Message-Id: <55bcc1f25d8200892a31f67a0b024ff3b816c3cc.1579866752.git.christophe.leroy@c-s.fr> In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v4 5/7] 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-mm@kvack.org Date: Fri, 24 Jan 2020 11:54:43 +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 named KUAP_CURRENT. In this case only, the addr and end are retrieved from current->kuap. Signed-off-by: Christophe Leroy --- v2: No change v3: This patch was not in v3 v4: Added build protection against bad use of KUAP_CURRENT. --- arch/powerpc/include/asm/book3s/32/kup.h | 23 +++++++++++++++---- .../powerpc/include/asm/book3s/64/kup-radix.h | 4 +++- arch/powerpc/include/asm/kup.h | 11 +++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/32/kup.h b/arch/powerpc/include/asm/book3s/32/kup.h index de29fb752cca..17e069291c72 100644 --- a/arch/powerpc/include/asm/book3s/32/kup.h +++ b/arch/powerpc/include/asm/book3s/32/kup.h @@ -108,6 +108,8 @@ 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); + if (!(dir & KUAP_WRITE)) return; @@ -117,6 +119,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 +130,25 @@ 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_WRITE)) - return; - addr = (__force u32)to; + if (dir == KUAP_CURRENT) { + u32 kuap = current->thread.kuap; - if (unlikely(addr >= TASK_SIZE || !size)) + if (unlikely(!kuap)) + return; + + addr = kuap & 0xf0000000; + end = kuap << 28; + } else if (dir & KUAP_WRITE) { + addr = (__force u32)to; + end = min(addr + size, TASK_SIZE); + + if (unlikely(addr >= TASK_SIZE || !size)) + return; + } else { return; + } - 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/book3s/64/kup-radix.h b/arch/powerpc/include/asm/book3s/64/kup-radix.h index c8d1076e0ebb..a0263e94df33 100644 --- a/arch/powerpc/include/asm/book3s/64/kup-radix.h +++ b/arch/powerpc/include/asm/book3s/64/kup-radix.h @@ -86,8 +86,10 @@ static __always_inline void allow_user_access(void __user *to, const void __user set_kuap(AMR_KUAP_BLOCK_WRITE); else if (dir == KUAP_WRITE) set_kuap(AMR_KUAP_BLOCK_READ); - else + else if (dir == KUAP_READ_WRITE) set_kuap(0); + else + BUILD_BUG(); } static inline void prevent_user_access(void __user *to, const void __user *from, diff --git a/arch/powerpc/include/asm/kup.h b/arch/powerpc/include/asm/kup.h index 94f24928916a..c3ce7e8ae9ea 100644 --- a/arch/powerpc/include/asm/kup.h +++ b/arch/powerpc/include/asm/kup.h @@ -5,6 +5,12 @@ #define KUAP_READ 1 #define KUAP_WRITE 2 #define KUAP_READ_WRITE (KUAP_READ | KUAP_WRITE) +/* + * For prevent_user_access() only. + * Use the current saved situation instead of the to/from/size params. + * Used on book3s/32 + */ +#define KUAP_CURRENT 4 #ifdef CONFIG_PPC64 #include @@ -88,6 +94,11 @@ static inline void prevent_read_write_user(void __user *to, const void __user *f prevent_user_access(to, from, size, KUAP_READ_WRITE); } +static inline void prevent_current_access_user(void) +{ + prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT); +} + #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_KUAP_H_ */ -- 2.25.0