Received: by 10.223.176.46 with SMTP id f43csp310828wra; Thu, 18 Jan 2018 17:59:33 -0800 (PST) X-Google-Smtp-Source: ACJfBotx5oFRfeVvcs5VuFCPNhyz1y35malDhR33kjpsxYrm6IETe73Uh0Pps2f7SzXF9LL8yLc7 X-Received: by 10.99.111.71 with SMTP id k68mr11722193pgc.360.1516327173278; Thu, 18 Jan 2018 17:59:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516327173; cv=none; d=google.com; s=arc-20160816; b=y0p8fYlV02GFB0gHUy5hr4C6K1iTA2+0cwO4kHRWUsauXWa6807TIuxB0AVFZoRcMg X2dTm38+lfxpdBAJiFiSC5H9sRmhk5VdTV/HX1tomqSa2DT4MfPRPoBAZ/ZVN/Rv4nwX 3roFpltzxrMnGsroWB/rcmT4CU3hcCqIgYCY6k8swv/InBRseka7hONATBFYpSkQ1ShG UUpoRuZtwhl3LhA7JLRlwOY2xJF02pr1zPk31cKdRw/ZFsYzkiUj4MMLOHEXAiK6ZTQ6 SH7hhlg7bXGDxWT2GSll5j46kN5h8nkTBmU2pGy6IYntR6mtArdJ7wiyxffqqtHElk6+ zoug== 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:arc-authentication-results; bh=KegGDsf3yAcsmD0yzv1+oYEqEFIo7268OLlmInSquj0=; b=JANV+riqY+1PCLSxa2NRXbb2ZyloDBCCG8IdRqcQuL7ijpae+iM8MOojAdsbAuMlzR n3NA8C8NSocalXopuPhnnDxkjOwbCMItmPQ7nqS+FF2iQLCd7kqarbts9GFWdbruF5U+ +IA03WBT48hzZGCVg3PXI/BklRWMlThPVyM6GeWCIHAz9WxD1n1vKjs8n5hXtMjVVYkK iJxp8f1pO0l0+OeMMk9jPjaEmvqLifo21uDoeXqFEV/GAh3yVSunebXx0I+cfRV/Mijh AoV7bgfVpfrwSdLsPzVw0ffmbMAK+CJYpldgsIhT7YVomi/TVccTMVhVu82XcOrZTefB nPfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=J7wE+mpd; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o2si7343589pge.93.2018.01.18.17.59.19; Thu, 18 Jan 2018 17:59:33 -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=fail header.i=@gmail.com header.s=20161025 header.b=J7wE+mpd; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755695AbeASB6a (ORCPT + 99 others); Thu, 18 Jan 2018 20:58:30 -0500 Received: from mail-qt0-f195.google.com ([209.85.216.195]:42237 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755363AbeASBwM (ORCPT ); Thu, 18 Jan 2018 20:52:12 -0500 Received: by mail-qt0-f195.google.com with SMTP id c2so427673qtn.9; Thu, 18 Jan 2018 17:52:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=KegGDsf3yAcsmD0yzv1+oYEqEFIo7268OLlmInSquj0=; b=J7wE+mpdNYsbxeU3/+yS6wCy6RcZxPAGtF38jVNa6aH021fCHha4JSNkYvDzOGR/P7 OuuAJo0oSK+W9DaKrVUAD7Xx9rmzk+IFOw1VZp+VzRvRikgKsbqFT5C2EQ2DTVJR5y+9 5K82ySEKBbkLAGlP4d00AvQPepCm2A9wDhZFZG/clCLzuO+I9DvR9dF42uYw1I5q3B3M uSN56p03XPsw3M08+JgEBrytGtyXAaAxt5F5KkBU/IayWlMyBIH+Ch9pKqIdeczijZ1o CYW4juOzPhY2U8BswPZ6QW89yEvRaDP2b0MHtpXQBGWyiyMWgipyOmkFR+KZS6cEueuK eCsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=KegGDsf3yAcsmD0yzv1+oYEqEFIo7268OLlmInSquj0=; b=tqjm3bM1ET8JH7Z0UfuxMGYlZkgwcGHjNsWyzWOw+Y+nZ0Co1wr2pNLBEcpRPnRfm5 piq8XKt4yWudWK401Z8ZbToSmSfkjSq2/76RYmQ2G2Jl5AktQzA+1eXcsbtzRNpsEXJ0 gCPMEZzJ0QkKs/CE5+jE55/F5EJkb/qqIxzg6GO1NrhxwnMbyP7jb/KMaJhSzhP/eIaO xf5G7hWWDDWJUHdSo7chJ3KTi8xFvFp7ZTjYLIGPUKrXojmzOxU1xHn0pbvaydOcS18q 2xe1S46tekYljMoHmc8jOkkoUVf6PQfGOywS/aNyXL/eXnu7XiDN5CGPm9P0YzCnGRU3 7WkQ== X-Gm-Message-State: AKwxytfENBb5M2+Y1tvrShRG5u0NHfPasfe2G4j6nXpyea13/xLi6Nvo OXBsCOhqxJD3eq/a+S945QU= X-Received: by 10.237.58.102 with SMTP id n93mr51361973qte.49.1516326731181; Thu, 18 Jan 2018 17:52:11 -0800 (PST) Received: from localhost.localdomain (50-39-100-161.bvtn.or.frontiernet.net. [50.39.100.161]) by smtp.gmail.com with ESMTPSA id c127sm5483161qke.78.2018.01.18.17.52.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 17:52:10 -0800 (PST) From: Ram Pai To: mpe@ellerman.id.au, mingo@redhat.com, akpm@linux-foundation.org, corbet@lwn.net, arnd@arndb.de Cc: linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, benh@kernel.crashing.org, paulus@samba.org, khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com, bsingharora@gmail.com, hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, ebiederm@xmission.com, linuxram@us.ibm.com Subject: [PATCH v10 11/27] powerpc: introduce execute-only pkey Date: Thu, 18 Jan 2018 17:50:32 -0800 Message-Id: <1516326648-22775-12-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1516326648-22775-1-git-send-email-linuxram@us.ibm.com> References: <1516326648-22775-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch provides the implementation of execute-only pkey. The architecture-independent layer expects the arch-dependent layer, to support the ability to create and enable a special key which has execute-only permission. Acked-by: Balbir Singh Signed-off-by: Ram Pai --- arch/powerpc/include/asm/book3s/64/mmu.h | 1 + arch/powerpc/include/asm/pkeys.h | 6 +++- arch/powerpc/mm/pkeys.c | 58 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletions(-) diff --git a/arch/powerpc/include/asm/book3s/64/mmu.h b/arch/powerpc/include/asm/book3s/64/mmu.h index 37ef23c..0abeb0e 100644 --- a/arch/powerpc/include/asm/book3s/64/mmu.h +++ b/arch/powerpc/include/asm/book3s/64/mmu.h @@ -116,6 +116,7 @@ struct patb_entry { * bit unset -> key available for allocation */ u32 pkey_allocation_map; + s16 execute_only_pkey; /* key holding execute-only protection */ #endif } mm_context_t; diff --git a/arch/powerpc/include/asm/pkeys.h b/arch/powerpc/include/asm/pkeys.h index 3def5af..2b5bb35 100644 --- a/arch/powerpc/include/asm/pkeys.h +++ b/arch/powerpc/include/asm/pkeys.h @@ -128,9 +128,13 @@ static inline int mm_pkey_free(struct mm_struct *mm, int pkey) * Try to dedicate one of the protection keys to be used as an * execute-only protection key. */ +extern int __execute_only_pkey(struct mm_struct *mm); static inline int execute_only_pkey(struct mm_struct *mm) { - return 0; + if (static_branch_likely(&pkey_disabled)) + return -1; + + return __execute_only_pkey(mm); } static inline int arch_override_mprotect_pkey(struct vm_area_struct *vma, diff --git a/arch/powerpc/mm/pkeys.c b/arch/powerpc/mm/pkeys.c index 7dfcf2d..b466a2c 100644 --- a/arch/powerpc/mm/pkeys.c +++ b/arch/powerpc/mm/pkeys.c @@ -96,6 +96,8 @@ void pkey_mm_init(struct mm_struct *mm) if (static_branch_likely(&pkey_disabled)) return; mm_pkey_allocation_map(mm) = initial_allocation_mask; + /* -1 means unallocated or invalid */ + mm->context.execute_only_pkey = -1; } static inline u64 read_amr(void) @@ -260,3 +262,59 @@ void thread_pkey_regs_init(struct thread_struct *thread) write_iamr(read_iamr() & pkey_iamr_mask); write_uamor(read_uamor() & pkey_amr_uamor_mask); } + +static inline bool pkey_allows_readwrite(int pkey) +{ + int pkey_shift = pkeyshift(pkey); + + if (!is_pkey_enabled(pkey)) + return true; + + return !(read_amr() & ((AMR_RD_BIT|AMR_WR_BIT) << pkey_shift)); +} + +int __execute_only_pkey(struct mm_struct *mm) +{ + bool need_to_set_mm_pkey = false; + int execute_only_pkey = mm->context.execute_only_pkey; + int ret; + + /* Do we need to assign a pkey for mm's execute-only maps? */ + if (execute_only_pkey == -1) { + /* Go allocate one to use, which might fail */ + execute_only_pkey = mm_pkey_alloc(mm); + if (execute_only_pkey < 0) + return -1; + need_to_set_mm_pkey = true; + } + + /* + * We do not want to go through the relatively costly dance to set AMR + * if we do not need to. Check it first and assume that if the + * execute-only pkey is readwrite-disabled than we do not have to set it + * ourselves. + */ + if (!need_to_set_mm_pkey && !pkey_allows_readwrite(execute_only_pkey)) + return execute_only_pkey; + + /* + * Set up AMR so that it denies access for everything other than + * execution. + */ + ret = __arch_set_user_pkey_access(current, execute_only_pkey, + PKEY_DISABLE_ACCESS | + PKEY_DISABLE_WRITE); + /* + * If the AMR-set operation failed somehow, just return 0 and + * effectively disable execute-only support. + */ + if (ret) { + mm_pkey_free(mm, execute_only_pkey); + return -1; + } + + /* We got one, store it and use it from here on out */ + if (need_to_set_mm_pkey) + mm->context.execute_only_pkey = execute_only_pkey; + return execute_only_pkey; +} -- 1.7.1