From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83C81C43334 for ; Tue, 4 Sep 2018 19:50:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 495B520659 for ; Tue, 4 Sep 2018 19:50:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 495B520659 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=intel.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728046AbeIEARI (ORCPT ); Tue, 4 Sep 2018 20:17:08 -0400 Received: from mga07.intel.com ([134.134.136.100]:63929 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726749AbeIEARH (ORCPT ); Tue, 4 Sep 2018 20:17:07 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Sep 2018 12:50:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,330,1531810800"; d="scan'208";a="259816882" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.20]) by fmsmga005.fm.intel.com with ESMTP; 04 Sep 2018 12:50:30 -0700 Date: Tue, 4 Sep 2018 12:50:30 -0700 From: Sean Christopherson To: linux-kernel@vger.kernel.org Cc: Dave Hansen , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, Andy Lutomirski , Jann Horn Subject: Re: [PATCH] x86/pkeys: Explicitly treat PK #PF on kernel address as a bad area Message-ID: <20180904195030.GA6269@linux.intel.com> References: <20180807172920.8766-1-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180807172920.8766-1-sean.j.christopherson@intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Cc-ing Jann and Andy. On Tue, Aug 07, 2018 at 10:29:20AM -0700, Sean Christopherson wrote: > Kernel addresses are always mapped with _PAGE_USER=0, i.e. PKRU > isn't enforced, and so we should never see X86_PF_PK set on a > kernel address fault. WARN once to capture the issue in case we > somehow don't die, e.g. the access originated in userspace. > > Remove a similar check and its comment from spurious_fault_check(). > The intent of the comment (and later code[1]) was simply to document > that spurious faults due to protection keys should be impossible, but > that's irrelevant and a bit of a red herring since we should never > get a protection keys fault on a kernel address regardless of the > kernel's TLB flushing behavior. > > [1] http://lists-archives.com/linux-kernel/28407455-x86-pkeys-new-page-fault-error-code-bit-pf_pk.html > > Signed-off-by: Sean Christopherson > Cc: Dave Hansen > --- > There's no indication that this condition has ever been encountered. > I came across the code in spurious_fault_check() and was confused as > to why we would unconditionally treat a protection keys fault as > spurious when the comment explicitly stated that such a case should > be impossible. > > Dave Hansen suggested adding a WARN_ON_ONCE in spurious_fault_check(), > but it seemed more appropriate to freak out on any protection keys > fault on a kernel address since that would imply a hardware issue or > kernel bug. I omitted a Suggested-by since this isn't necessarily > what Dave had in mind. > > arch/x86/mm/fault.c | 16 ++++++++++------ > 1 file changed, 10 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c > index 2aafa6ab6103..f19a55972136 100644 > --- a/arch/x86/mm/fault.c > +++ b/arch/x86/mm/fault.c > @@ -1040,12 +1040,6 @@ static int spurious_fault_check(unsigned long error_code, pte_t *pte) > > if ((error_code & X86_PF_INSTR) && !pte_exec(*pte)) > return 0; > - /* > - * Note: We do not do lazy flushing on protection key > - * changes, so no spurious fault will ever set X86_PF_PK. > - */ > - if ((error_code & X86_PF_PK)) > - return 1; > > return 1; > } > @@ -1241,6 +1235,14 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code, > * protection error (error_code & 9) == 0. > */ > if (unlikely(fault_in_kernel_space(address))) { > + /* > + * We should never encounter a protection keys fault on a > + * kernel address as kernel address are always mapped with > + * _PAGE_USER=0, i.e. PKRU isn't enforced. > + */ > + if (WARN_ON_ONCE(error_code & X86_PF_PK)) > + goto bad_kernel_address; > + > if (!(error_code & (X86_PF_RSVD | X86_PF_USER | X86_PF_PROT))) { > if (vmalloc_fault(address) >= 0) > return; > @@ -1253,6 +1255,8 @@ __do_page_fault(struct pt_regs *regs, unsigned long error_code, > /* kprobes don't want to hook the spurious faults: */ > if (kprobes_fault(regs)) > return; > + > +bad_kernel_address: > /* > * Don't take the mm semaphore here. If we fixup a prefetch > * fault we could otherwise deadlock: > -- > 2.18.0 >