Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp619928pxb; Tue, 1 Feb 2022 07:08:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsY4RNjYT803hkWHd5zEe0RnOjwrkN0mOgt08X1ax7Q9tXoxjpToJZPQ5umB3S2wSZQgHF X-Received: by 2002:aa7:c258:: with SMTP id y24mr25810131edo.288.1643728099013; Tue, 01 Feb 2022 07:08:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728099; cv=none; d=google.com; s=arc-20160816; b=FkDpUuzKRUiBQZuib07Btr+dNLr8ZYm/ttgrfuM2V015vBc8LfoqsA21a1M/20uNLm a3wxr4VU0UbtKbXxNi51EtUDtdRgMk+D4j3m7zsFV4R6tMwgs17jx6DtdHVAmYC1zQcU arKssYPcoTOnthvJlVAFsCVK2Uo6Ulh0bnlyEdI3FDi/diYZenKVtA+ONo1gTzGgBP/o LlOkknLGxK6CSTioo613OUL2NdDH8C4IAibJok+Umk0L1KZZG1QSdXpJculDVvEUVFM+ fP/xW98rZ+bhhf6m62gdie5NwjB4nHDBXWCcOIIAeIJgCClIDPX5d3RsyqoKVkyQeHjw JTQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=yNEUVq7G2o49guRA3rV0zuLjAcslKibpKFL18YVNVNI=; b=RDPltRsYsmpQf9AXWKHqHgQWboA5UgqFuNrUaAZYWGTZn+p7Y8huSDnxs81JnZYB6P HzTBrCuUkkF4BKwNxPGL6BFx2gkL4lJweJi+eE+9hCbo95tDG+CkLWi8E52Hh3aYZCEs l4JyE646svfTrPyH7V0VkKeBDieV91lcxh6fsW8bls2Mlslm/uWc5PvVOVgPY+iZOvS0 OVxOL0uAprMB7l37crZoI69Ot6VZ9oedKSK0tjmHtqMRUI8vWjxzGGYNu4jNn2q3J8Is rwh0iZp5hLdsOZ55C+Mpj0CEHE2OgtLfplFzxbrw/F6ml4r3vXpCSh+vu63DF21AHSDD WtrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I57QP6fK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hb35si9718428ejc.871.2022.02.01.07.07.52; Tue, 01 Feb 2022 07:08:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I57QP6fK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356846AbiA3VXd (ORCPT + 99 others); Sun, 30 Jan 2022 16:23:33 -0500 Received: from mga06.intel.com ([134.134.136.31]:52029 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356503AbiA3VV6 (ORCPT ); Sun, 30 Jan 2022 16:21:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577718; x=1675113718; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=95Ekd9pFZXWEBY+wGysVOoEb3gOtJAhC5Dzmrl7nHJo=; b=I57QP6fKotv4XIttfrK/8ON1wasZfGohsoQoTNZq2kRKXg5s4DFKqNW3 bXcUaDefGJg47xFI1vTd4hp2WCDaYjFAidUfu0/fZ1bnEWHwXO3/VL3pC 5xoc1vN4jPIsF5izqOb9pa8ynTou9U7XwolTBEcq6gdegmd1O1i0REaCc 9PRyfh+KfeVGof1vBW4o2KCNicVWNo0bhzMht3odiEdmTmWYjcT/7SW2a /aTs7yZv9cIuPo+Z2CpmEgQx2p7Etwotp2nEd1kn7mM9Z0BBlK7D/VhSP Z/r6wbySoZVxK8p70AFN0x5z+IocFFqzVvXxjJaER01+bF1epxmkw6Ikp w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104933" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104933" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:58 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856787" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:57 -0800 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 15/35] x86/mm: Check Shadow Stack page fault errors Date: Sun, 30 Jan 2022 13:18:18 -0800 Message-Id: <20220130211838.8382-16-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu Shadow stack accesses are those that are performed by the CPU where it expects to encounter a shadow stack mapping. These accesses are performed implicitly by CALL/RET at the site of the shadow stack pointer. These accesses are made explicitly by shadow stack management instructions like WRUSSQ. Shadow stacks accesses to shadow-stack mapping can see 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. 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. In handling a shadow stack page fault, verify it occurs within a shadow stack mapping. It is always an error otherwise. For valid shadow stack accesses, set FAULT_FLAG_WRITE to effect copy-on-write. Because clearing _PAGE_DIRTY (vs. _PAGE_RW) is used to trigger the fault, shadow stack read fault and shadow stack write fault are not differentiated and both are handled as a write access. Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe --- Yu-cheng v30: - Update Subject line and add a verb. arch/x86/include/asm/trap_pf.h | 2 ++ arch/x86/mm/fault.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..afa524325e55 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 d0074c6ed31a..6769134986ec 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1107,6 +1107,17 @@ access_error(unsigned long error_code, struct vm_area_struct *vma) (error_code & X86_PF_INSTR), foreign)) return 1; + /* + * Verify a shadow stack access is within a shadow stack VMA. + * It is always an error otherwise. Normal data access to a + * shadow stack area is checked in the case followed. + */ + if (error_code & X86_PF_SHSTK) { + if (!(vma->vm_flags & VM_SHADOW_STACK)) + return 1; + return 0; + } + if (error_code & X86_PF_WRITE) { /* write, present and write, not present: */ if (unlikely(!(vma->vm_flags & VM_WRITE))) @@ -1300,6 +1311,14 @@ void do_user_addr_fault(struct pt_regs *regs, perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); + /* + * Clearing _PAGE_DIRTY is used to detect shadow stack access. + * This method cannot distinguish shadow stack read vs. write. + * For valid shadow stack accesses, set FAULT_FLAG_WRITE to effect + * copy-on-write. + */ + 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) -- 2.17.1