Received: by 10.213.65.68 with SMTP id h4csp639880imn; Fri, 23 Mar 2018 12:16:16 -0700 (PDT) X-Google-Smtp-Source: AG47ELsWtBpSMtoZle9Q7XZRp/TetXIXjWDtEK5UrV0o37MnbIsvnPmgs/HElgYBZh70+CnqaJbl X-Received: by 2002:a17:902:a713:: with SMTP id w19-v6mr29849039plq.246.1521832576027; Fri, 23 Mar 2018 12:16:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521832575; cv=none; d=google.com; s=arc-20160816; b=tqxudw89TL0Ab3hHVgaFHoDT2kjPfL5wO2ABs7nv9jc5KG5uKLgTmWEE4VXucDwowq K/AkRT/EPleigshErrka6m1QydcVJiwn9X6P2Z9KnuMt4KFJAdEF70QPCYmOE6b5orh9 Y5S6JPD4v7GNKJbrZCBaIHUCoHFKsehrh/ASYtl7akYLXTrr5Bs1w6KGD+Qh3K6aFxGf e21TcCCXAHDHWo9OaWUw2SZrJiHyp2yPC6/DNVBiaSsD+ybUAwxk+nBvRkLe4EZzNoCb t/oj3m24Ov2YhKO2UZ2XSdGbuo57N45ZgVlhoRcA4aB2GjanYewa2zEpNYlBuqyTygfI aBaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=8Jt0yTjm+UIMKApEBptEx6UMysouljlISjVwHjAQMYg=; b=U0HZJieCdJWZLlR0sEO4lCAzRcUfLaYB7czTJS9VkuWRRWgC24wMt4HhtLJ9sNFkU2 p5C2Ru/Ciq9BIzWYqcJLuATMiysaHZ4p0bsSQiNkAP91V/6G86NHR3KYnC/KRrzq3zg6 4OuEFrFYmpIKEvtR37UTMiHtebM72sM1sb+NsTiMzdTIU0cA5KNFmmgVOxbOQZex1v3g nNDsXUbCrKjS1op9mvtIDcqGwe6ttLR3Vys6kpK1F58pak5JG7KgQkGZ6HjcbxvYMHVv H23LjuQbR3Ig8AdKrzHL91uGBGaCp10RAn8j5had25TFiz0IZnQR39RPHbTOmw7Rrlih LhnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=k1IxM8dA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h14si7145872pfd.404.2018.03.23.12.16.01; Fri, 23 Mar 2018 12:16:15 -0700 (PDT) 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=@google.com header.s=20161025 header.b=k1IxM8dA; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752050AbeCWTPL (ORCPT + 99 others); Fri, 23 Mar 2018 15:15:11 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:37614 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752159AbeCWTPJ (ORCPT ); Fri, 23 Mar 2018 15:15:09 -0400 Received: by mail-wr0-f194.google.com with SMTP id l49so4035124wrl.4 for ; Fri, 23 Mar 2018 12:15:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=8Jt0yTjm+UIMKApEBptEx6UMysouljlISjVwHjAQMYg=; b=k1IxM8dAieQFQdvPIxqodTq+jgeXCkBy6WLe9CfVARg+gQ2RLK4yHBBDsvGQU7IQ32 Hkb7Jvv8nNxh1CXSq4zZnvXDH71F8yvyHVEK3PNcJK9WX3kPR4C8hjNcx/O7yjGBdTxs QaqShWLEl69Hb29vaYIuXt+rjoVeaGB5e3RUZv5vVFCnCT6xaGRjv1+FH0RcF1PRzums MODIp/db9lA68kRq4w1ikKeJvqSIHhLu/zUxcCbFtwJW++otwi8riPiDnJLr43/y0khT KYggDDafZADhrEKAbzQUOKlV0OpMelxTO69PF8cgEPelv4dtD7qgpOUzDGBWMvUfaNc0 Uu8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=8Jt0yTjm+UIMKApEBptEx6UMysouljlISjVwHjAQMYg=; b=JsSglJymBBABTzICe1U8VLbjzvdtBV54aVMsmYLk0RvGiFpY9us0yo2OM5fDj8fHRM VeIewwCmIC5tDxWj6YsWg8scGALxgPt00qSkZk3nyOZdXnQpLxx17dI1/SqhpNmJfdqA nHuyHUxct0qFVQIt88vqOa0IbCszkeQ4oFV1rlQEg4M2DjWi/lV0L6D9o6s5Bqm4Hpbh PqYg6FRvEJ9LvxiuQTZW0cNXdy8374gRV8tOsOADOO+lZFd0/ygPg532N67JBz4rrg9m 8EHpm0kpdfnMuH/lN22MorNZ+9dzxFKx1e5jBFC0QwcseycsbzsjrHyGuairCyl25iOX ZYlA== X-Gm-Message-State: AElRT7Hi24oVCrturrowS1VIM7utUppf6mMkifeUKYyWE4fUJDBweGWa Mo+7TEwC2T3Zv8Fc8LR8YgrNSU52GGwvUPMavq1Ff0Z1 X-Received: by 10.223.141.143 with SMTP id o15mr25393714wrb.272.1521832507567; Fri, 23 Mar 2018 12:15:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.28.184.12 with HTTP; Fri, 23 Mar 2018 12:15:06 -0700 (PDT) In-Reply-To: <20180323180911.E43ACAB8@viggo.jf.intel.com> References: <20180323180903.33B17168@viggo.jf.intel.com> <20180323180911.E43ACAB8@viggo.jf.intel.com> From: Shakeel Butt Date: Fri, 23 Mar 2018 12:15:06 -0700 Message-ID: Subject: Re: [PATCH 4/9] x86, pkeys: override pkey when moving away from PROT_EXEC To: Dave Hansen Cc: LKML , Linux MM , linuxram@us.ibm.com, Thomas Gleixner , Dave Hansen , mpe@ellerman.id.au, Ingo Molnar , Andrew Morton , shuah@kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 23, 2018 at 11:09 AM, Dave Hansen wrote: > > From: Dave Hansen > > I got a bug report that the following code (roughly) was > causing a SIGSEGV: > > mprotect(ptr, size, PROT_EXEC); > mprotect(ptr, size, PROT_NONE); > mprotect(ptr, size, PROT_READ); > *ptr = 100; > > The problem is hit when the mprotect(PROT_EXEC) > is implicitly assigned a protection key to the VMA, and made > that key ACCESS_DENY|WRITE_DENY. The PROT_NONE mprotect() > failed to remove the protection key, and the PROT_NONE-> > PROT_READ left the PTE usable, but the pkey still in place > and left the memory inaccessible. > > To fix this, we ensure that we always "override" the pkee > at mprotect() if the VMA does not have execute-only > permissions, but the VMA has the execute-only pkey. > > We had a check for PROT_READ/WRITE, but it did not work > for PROT_NONE. This entirely removes the PROT_* checks, > which ensures that PROT_NONE now works. > > Reported-by: Shakeel Butt > > Signed-off-by: Dave Hansen Should there be a 'Fixes' tag? Also should this patch go to stable? > Cc: Ram Pai > Cc: Thomas Gleixner > Cc: Dave Hansen > Cc: Michael Ellermen > Cc: Ingo Molnar > Cc: Andrew Morton > Cc: Shuah Khan > --- > > b/arch/x86/include/asm/pkeys.h | 12 +++++++++++- > b/arch/x86/mm/pkeys.c | 19 ++++++++++--------- > 2 files changed, 21 insertions(+), 10 deletions(-) > > diff -puN arch/x86/include/asm/pkeys.h~pkeys-abandon-exec-only-pkey-more-aggressively arch/x86/include/asm/pkeys.h > --- a/arch/x86/include/asm/pkeys.h~pkeys-abandon-exec-only-pkey-more-aggressively 2018-03-21 15:47:49.810198922 -0700 > +++ b/arch/x86/include/asm/pkeys.h 2018-03-21 15:47:49.816198922 -0700 > @@ -2,6 +2,8 @@ > #ifndef _ASM_X86_PKEYS_H > #define _ASM_X86_PKEYS_H > > +#define ARCH_DEFAULT_PKEY 0 > + > #define arch_max_pkey() (boot_cpu_has(X86_FEATURE_OSPKE) ? 16 : 1) > > extern int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, > @@ -15,7 +17,7 @@ extern int __execute_only_pkey(struct mm > static inline int execute_only_pkey(struct mm_struct *mm) > { > if (!boot_cpu_has(X86_FEATURE_OSPKE)) > - return 0; > + return ARCH_DEFAULT_PKEY; > > return __execute_only_pkey(mm); > } > @@ -56,6 +58,14 @@ bool mm_pkey_is_allocated(struct mm_stru > return false; > if (pkey >= arch_max_pkey()) > return false; > + /* > + * The exec-only pkey is set in the allocation map, but > + * is not available to any of the user interfaces like > + * mprotect_pkey(). > + */ > + if (pkey == mm->context.execute_only_pkey) > + return false; > + > return mm_pkey_allocation_map(mm) & (1U << pkey); > } > > diff -puN arch/x86/mm/pkeys.c~pkeys-abandon-exec-only-pkey-more-aggressively arch/x86/mm/pkeys.c > --- a/arch/x86/mm/pkeys.c~pkeys-abandon-exec-only-pkey-more-aggressively 2018-03-21 15:47:49.812198922 -0700 > +++ b/arch/x86/mm/pkeys.c 2018-03-21 15:47:49.816198922 -0700 > @@ -94,15 +94,7 @@ int __arch_override_mprotect_pkey(struct > */ > if (pkey != -1) > return pkey; > - /* > - * Look for a protection-key-drive execute-only mapping > - * which is now being given permissions that are not > - * execute-only. Move it back to the default pkey. > - */ > - if (vma_is_pkey_exec_only(vma) && > - (prot & (PROT_READ|PROT_WRITE))) { > - return 0; > - } > + > /* > * The mapping is execute-only. Go try to get the > * execute-only protection key. If we fail to do that, > @@ -113,7 +105,16 @@ int __arch_override_mprotect_pkey(struct > pkey = execute_only_pkey(vma->vm_mm); > if (pkey > 0) > return pkey; > + } else if (vma_is_pkey_exec_only(vma)) { > + /* > + * Protections are *not* PROT_EXEC, but the mapping > + * is using the exec-only pkey. This mapping was > + * PROT_EXEC and will no longer be. Move back to > + * the default pkey. > + */ > + return ARCH_DEFAULT_PKEY; > } > + > /* > * This is a vanilla, non-pkey mprotect (or we failed to > * setup execute-only), inherit the pkey from the VMA we > _