Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5017532imm; Tue, 7 Aug 2018 11:08:59 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcnKatxhgWHZqrbMOhybvw5rEe0OPpH1PMnilzMHhPLpkkiV3cNFe97SULxxMWt/N9MmA0x X-Received: by 2002:a63:b349:: with SMTP id x9-v6mr19602426pgt.337.1533665339594; Tue, 07 Aug 2018 11:08:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533665339; cv=none; d=google.com; s=arc-20160816; b=gNdcywTjWPeagy7dT92L0fjPImtzZI2GSWtR/CVTtNKI7cZbGK52BY/el0AJsSlajC z3hHx/dhdqKFFElHR6Eow9cnLXY/MReMw7WdG3hxtiX7FM5FgUMH50TUNTgu/GtCSbuJ iKCEkBF5gtE1C6spiK5agmzmDwAt1UGvqFY5vPPCOPkcLpWq+7ZQl2IMR3c93E1rJquD cwPPyXweEb00VDP+bKuc2OPCzPoGqDPXuPBbHzKgzba7RdiMCJbWJ269AkerElMwUGTe Y7GlWiVgYLiTDom6SaJzkYtLzLg7l2zc328aZ83cHqXB9CAeovnMR3ZRK5I6Aik5Tms1 13mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=hT6lKNGjxTuMa6TyoqlR/hs6VGzltGZn9pdd7kX0wb4=; b=nzrv5J8H2mh2ZerclFCPMIexvQKkjf0ATd0S8K9/PJsUd91AAlWRGaV4K0jNqSe5lz 4U97PFm65s84IAZu2sIlvmp3HCVxoLxRNipV4YD0Fh5I5RVpKUfb01Kr67sPdbqoFNTO ODiYQHs5jCYJ4mOSr5iVkDWnHCUpc0+OPYW9RuAWa08f4wBaaLbjxjXSfuZ5O2AIu61h kaB9CY8YOymWFOUyAHB17y1/YOluFtR58g+QPJmmgr9XuewpqM5XBsCGAmHdnAtxLCRG qeP0jtB+LCT68ntmlqGMknO5unyXvcUpjDNt/dLPuLKpsToPDaXc9JzwwFuP85Av5PU5 C35g== ARC-Authentication-Results: i=1; mx.google.com; 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 31-v6si1496528plg.260.2018.08.07.11.08.44; Tue, 07 Aug 2018 11:08:59 -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; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390268AbeHGTpG (ORCPT + 99 others); Tue, 7 Aug 2018 15:45:06 -0400 Received: from mga11.intel.com ([192.55.52.93]:52649 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388865AbeHGTpF (ORCPT ); Tue, 7 Aug 2018 15:45:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Aug 2018 10:29:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,456,1526367600"; d="scan'208";a="64258384" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.132]) by orsmga006.jf.intel.com with ESMTP; 07 Aug 2018 10:29:44 -0700 From: Sean Christopherson To: linux-kernel@vger.kernel.org Cc: sean.j.christopherson@intel.com, Dave Hansen , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH] x86/pkeys: Explicitly treat PK #PF on kernel address as a bad area Date: Tue, 7 Aug 2018 10:29:20 -0700 Message-Id: <20180807172920.8766-1-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.18.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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