From patchwork Fri Jun 16 19:17:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 109335 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1569577vqr; Fri, 16 Jun 2023 12:32:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4gGv+zMfM+h4h5QawKbO4DdZNMeQ4L1wu14aYkJWEqWYZaiU805QjdEdwEZ4b6hz4Qo3N3 X-Received: by 2002:a17:902:ea05:b0:1ac:d03a:9702 with SMTP id s5-20020a170902ea0500b001acd03a9702mr2983482plg.67.1686943962634; Fri, 16 Jun 2023 12:32:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686943962; cv=none; d=google.com; s=arc-20160816; b=sVa7lYmT0RwxqRKiESmYlKRsloBLZflcxPO8ewsQXpY0ayWpFbppEQIqrBB8Jmaova pRH/YUwqwRdsYlPrE0a4VGSYkWI0+Clu52UyOxnlMXM8o9A+JEnbw5FOiuKUZXD1JbNE elyPXiZPW81F9VREgIiK4TWVrc4BQPlAy/Z+dSA4UMpBAZCu4MsSUZZAugajM9njssvI t5k51QG0ql1XKyLNxr8ELVUltUV2D/O294lH93ls0YTLNtpmnCTgMQJvbD+p7easTzh9 d06XUDfrwDxGOnE1TyuCKA45we+36FiIietVlA/LAOHgIROM0kxwaqYeNkVPTMlo2qcY H4Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=2+XWb39eYEq67XlTsQ89FEsjM8qAnO34h6tfzMegr2s=; b=xLNJU+HWbFR9GZBbjg0nJTL8i1Fd/uTGABEli3CcjmREEKkNSJ5ZMFlixF4VnLAtrv gGli/clxsz5r0HiUVfJSiSExy7PM+IH/6tRmxsNkoaXiXj5BphMBGgt/eW6YHdbmiYPk bJFHCYyY+vUvW21S13xopZF+7KVQjyLuPaG04rSZZV7n3+is9gY54jNLU1QesHBlYTTr X4O+upLqQS3NXBG+dn1YyriWxnhFn4bMNVqoqQh4V6rZcM7mBZj4SQE/wwGnk156nEGq qKjc9vCRzKxpKYvucXBMEcSqpn6qsuz9xYwsK79uB2Kc7uMCsDhUbeZyCVQQ3sEdAmOo QtKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Mh97hloT; dkim=neutral (no key) header.i=@linutronix.de header.b="YtVOU/ij"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w17-20020a170902e89100b001b3d9943500si9758612plg.356.2023.06.16.12.32.28; Fri, 16 Jun 2023 12:32:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Mh97hloT; dkim=neutral (no key) header.i=@linutronix.de header.b="YtVOU/ij"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346145AbjFPTTA (ORCPT + 99 others); Fri, 16 Jun 2023 15:19:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345995AbjFPTRL (ORCPT ); Fri, 16 Jun 2023 15:17:11 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEC053AB9; Fri, 16 Jun 2023 12:17:03 -0700 (PDT) Date: Fri, 16 Jun 2023 19:17:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686943022; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2+XWb39eYEq67XlTsQ89FEsjM8qAnO34h6tfzMegr2s=; b=Mh97hloTe1kk6UKlzKV260Nr1OFt5k1Hb65qbCMtBx6TAWthb6EvadJmzg+SycT6F8VKQI yQhAcWAEXgIioD9pP3i7QUTnqRWZB5kSXxCVsTUXZikH4gH4ENoPT8ljXU8WIhKzKaSD80 8hupo/vMfMp1Gp+fMDVdjX3n2eTELBBR5QVCkiIUfdKY08KYNiL1QZg70fOaysVWrpug62 KGe6Uo26Y+Iwy1uxB8easYJFa/SL1Q0EDi/7GJJ9I2ET8izbShC8BiQO0YrwIx1I/T5Oko ujrUzwCnNH/lEKDZxPpHFvHGHbNPcv4IreYbU/GX/OrcpmyxOuYp4xgGN50M2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686943022; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2+XWb39eYEq67XlTsQ89FEsjM8qAnO34h6tfzMegr2s=; b=YtVOU/ijl/YLF5IwXOnTi6Q9YenLFLbAD1Ex396mGghsJl0EnV9Gwnb5ApCXqy/fqgazjB RPX26iZzqZSX6OBA== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86/mm: Check shadow stack page fault errors Cc: "Yu-cheng Yu" , Rick Edgecombe , Dave Hansen , "Borislav Petkov (AMD)" , Kees Cook , "Mike Rapoport (IBM)" , Pengfei Xu , John Allen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <168694302175.404.6136165645103196908.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768888952118678961?= X-GMAIL-MSGID: =?utf-8?q?1768888952118678961?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 06617c18cda97b83f21f62d474db812985a25b7e Gitweb: https://git.kernel.org/tip/06617c18cda97b83f21f62d474db812985a25b7e Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:41 -07:00 Committer: Dave Hansen CommitterDate: Thu, 15 Jun 2023 16:31:33 -07:00 x86/mm: Check shadow stack page fault errors The CPU performs "shadow stack accesses" when it expects to encounter shadow stack mappings. These accesses can be implicit (via CALL/RET instructions) or explicit (instructions like WRSS). Shadow stack accesses to shadow-stack mappings can result in faults in normal, valid operation just like regular accesses to regular mappings. Shadow stacks need some of the same features like delayed allocation, swap and copy-on-write. The kernel needs to use faults to implement those features. The architecture has concepts of both shadow stack reads and shadow stack writes. Any shadow stack access to non-shadow stack memory will generate a fault with the shadow stack error code bit set. This means that, unlike normal write protection, the fault handler needs to create a type of memory that can be written to (with instructions that generate shadow stack writes), even to fulfill a read access. So in the case of COW memory, the COW needs to take place even with a shadow stack read. Otherwise the page will be left (shadow stack) writable in userspace. So to trigger the appropriate behavior, set FAULT_FLAG_WRITE for shadow stack accesses, even if the access was a shadow stack read. For the purpose of making this clearer, consider the following example. If a process has a shadow stack, and forks, the shadow stack PTEs will become read-only due to COW. If the CPU in one process performs a shadow stack read access to the shadow stack, for example executing a RET and causing the CPU to read the shadow stack copy of the return address, then in order for the fault to be resolved the PTE will need to be set with shadow stack permissions. But then the memory would be changeable from userspace (from CALL, RET, WRSS, etc). So this scenario needs to trigger COW, otherwise the shared page would be changeable from both processes. Shadow stack accesses can also result in errors, such as when a shadow stack overflows, or if a shadow stack access occurs to a non-shadow-stack mapping. Also, generate the errors for invalid shadow stack accesses. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Link: https://lore.kernel.org/all/20230613001108.3040476-16-rick.p.edgecombe%40intel.com --- arch/x86/include/asm/trap_pf.h | 2 ++ arch/x86/mm/fault.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de5..afa5243 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -11,6 +11,7 @@ * bit 3 == 1: use of reserved bit detected * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access + * bit 6 == 1: shadow stack access fault * bit 15 == 1: SGX MMU page-fault */ enum x86_pf_error_code { @@ -20,6 +21,7 @@ enum x86_pf_error_code { X86_PF_RSVD = 1 << 3, X86_PF_INSTR = 1 << 4, X86_PF_PK = 1 << 5, + X86_PF_SHSTK = 1 << 6, X86_PF_SGX = 1 << 15, }; diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index e439998..fe68119 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1118,8 +1118,22 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Shadow stack accesses (PF_SHSTK=1) are only permitted to + * shadow stack VMAs. All other accesses result in an error. + */ + if (error_code & X86_PF_SHSTK) { + if (unlikely(!(vma->vm_flags & VM_SHADOW_STACK))) + return 1; + if (unlikely(!(vma->vm_flags & VM_WRITE))) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ + if (unlikely(vma->vm_flags & VM_SHADOW_STACK)) + return 1; if (unlikely(!(vma->vm_flags & VM_WRITE))) return 1; return 0; @@ -1311,6 +1325,14 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * Read-only permissions can not be expressed in shadow stack PTEs. + * Treat all shadow stack accesses as WRITE faults. This ensures + * that the MM will prepare everything (e.g., break COW) such that + * maybe_mkwrite() can create a proper shadow stack PTE. + */ + if (error_code & X86_PF_SHSTK) + flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_WRITE) flags |= FAULT_FLAG_WRITE; if (error_code & X86_PF_INSTR)