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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E902C05027 for ; Mon, 20 Feb 2023 12:57:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3DA36B0072; Mon, 20 Feb 2023 07:57:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEDA66B0073; Mon, 20 Feb 2023 07:57:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8F126B0074; Mon, 20 Feb 2023 07:57:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C60756B0072 for ; Mon, 20 Feb 2023 07:57:18 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9A0F2AA123 for ; Mon, 20 Feb 2023 12:57:18 +0000 (UTC) X-FDA: 80487670956.24.E67BF83 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf14.hostedemail.com (Postfix) with ESMTP id 5682B100021 for ; Mon, 20 Feb 2023 12:57:16 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VVBiqfhs; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676897836; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zemW7aNWRwAi+p4sGKW/xJ1ltsTq0KCUp+kk3nYuOkc=; b=eOZONTxFSjf3qYmFZJcYvqOKZ3WyDvBTTg8+S/KkIOAXdPZZNN3IMY29PzIpQSCAE0e6QM Ix2qz9nqvhvxcmZZRq80XaIASfWP3hJjlfe8YLWjvr+S6njJm9wi4mdUebqlwbxKJ6KUWq At2144iOpa4n7aZ19shurmPnyrA8lcA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=VVBiqfhs; spf=pass (imf14.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676897836; a=rsa-sha256; cv=none; b=SLhPzxk4KhoMnF08lkZVysB479ddPYYAu2U2B9DBL8kxSYRw58rkf95lKnjuJhLOyxSl1G X1NyhnbyQfCups81aAiwruWfy8U6BibCL+SBBzUMfr2pyOs1An/640S8kszZ46kCwuLKzU h2D6NRj6SHpi4bOUcd3rhIFyc2z/Y2o= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676897835; h=from:from: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: in-reply-to:in-reply-to:references:references; bh=zemW7aNWRwAi+p4sGKW/xJ1ltsTq0KCUp+kk3nYuOkc=; b=VVBiqfhsxg9D3rwM+WEpMpgqAHDA3VJq22YPMsmL9PEM7JFXUAHWhs2ZEcq1xh0F5yQfAo 0V4TT1/oAvP4f43PksewX1lrDUwagQ4iyk5qr2cGh8shqLdY7hoiE8uR6lTLEEIgZnibTw 0emS4dYv6rakaIamTQcp538UQ+VVRgw= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-543-A8FDytqROWWBSwm9FsB79w-1; Mon, 20 Feb 2023 07:57:11 -0500 X-MC-Unique: A8FDytqROWWBSwm9FsB79w-1 Received: by mail-wm1-f70.google.com with SMTP id f14-20020a7bcc0e000000b003dd41ad974bso560535wmh.3 for ; Mon, 20 Feb 2023 04:57:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zemW7aNWRwAi+p4sGKW/xJ1ltsTq0KCUp+kk3nYuOkc=; b=zt3WYRAxvTTiT69ipZ8e8V373LR3YXOkwBwiyBpzuQUR62NzzfKDX62LKC/XYTnCXT yVdF3S4nbMveX19OU4Z0jeU7RRi7KYIekUk5jSIzPf6GoDtCnvNMvB8m+c6GG5HkXnKK twO4PUpMxMvrp03OPuYpkQ7r+kPP//CXWHlXuN91ebp335rIYSc6LNQsASruR0zct6fB ACnltVkUcqUv22oH5Ws2MBhKFPf4Pxln2TuLeFwcdsO59hme63XolHJg+e82R+/8qxiY DhSFcKNr7NhlQ7dE/ewHXTqQbw8jmHDOizNFi5p1tdo/36r/kakdqKCJcWqdKiFFIJv4 22QQ== X-Gm-Message-State: AO0yUKVUlsg93lxAZlT279KiKEZ1/FYglQGnBErVrDsDgvomgm4CUP2a AMP+xo0pR5BfnE0yaPDQtffHC8lBG6FVGX/iQVFn3rLsvCyTAcvbDa/N7NqJL9AdRax0+FStCDh M1n08Ms3JKJw= X-Received: by 2002:a5d:6544:0:b0:2c5:953c:696f with SMTP id z4-20020a5d6544000000b002c5953c696fmr171989wrv.30.1676897830804; Mon, 20 Feb 2023 04:57:10 -0800 (PST) X-Google-Smtp-Source: AK7set+/84V+aDeHrLh5AwgyrTahMkPSaK64yS70UPPno5zO+r6bOD3NKj93IjGMJQae+rzSOfSbzA== X-Received: by 2002:a5d:6544:0:b0:2c5:953c:696f with SMTP id z4-20020a5d6544000000b002c5953c696fmr171966wrv.30.1676897830380; Mon, 20 Feb 2023 04:57:10 -0800 (PST) Received: from ?IPV6:2003:cb:c705:8300:e519:4218:a8b5:5bec? (p200300cbc7058300e5194218a8b55bec.dip0.t-ipconnect.de. [2003:cb:c705:8300:e519:4218:a8b5:5bec]) by smtp.gmail.com with ESMTPSA id w15-20020adfec4f000000b002c54241b4fesm5961541wrn.80.2023.02.20.04.57.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Feb 2023 04:57:09 -0800 (PST) Message-ID: <458b3d39-ddce-c0f2-fe80-4e0cc5b101bd@redhat.com> Date: Mon, 20 Feb 2023 13:57:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.2 Subject: Re: [PATCH v6 19/41] x86/mm: Check shadow stack page fault errors To: Rick Edgecombe , 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 , Weijiang Yang , "Kirill A . Shutemov" , John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com, akpm@linux-foundation.org, Andrew.Cooper3@citrix.com, christina.schimpe@intel.com, debug@rivosinc.com Cc: Yu-cheng Yu References: <20230218211433.26859-1-rick.p.edgecombe@intel.com> <20230218211433.26859-20-rick.p.edgecombe@intel.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230218211433.26859-20-rick.p.edgecombe@intel.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5682B100021 X-Stat-Signature: dfa7mndidbdah7prt5bot114uuh7nqxc X-HE-Tag: 1676897836-990065 X-HE-Meta: U2FsdGVkX1+aseQwqBmUPBcXdfuNpNykVpiFDZug8jA+AuGDwofGpu5zoFqL9Im7r9ZwVeIgFWB2sdPtXL0Q4LVvuHg7/U20s4h/INzM9hfzhr97FUbIaz9pIqXw9spWB1bW2+r68YgllazzIK8wnP8imr8Wyi/HuLE0XOT//aAvKi7uimn16f3tQCs6aKpFYvZ0HF4FLcsnmhVnJElbHA6A6QWZiGXDoeSAXdD8c40iV54mBHWAjGVSI7ClmaY0S3+FP+VkbE4VqTmwiGv9ti4OeQNDXc5fVBJU7FudxQ8j+Yh7yL0ds9lGl9s40eXdNz6h00kLTFNO8dCJgE7SLrPwHImf14OqevS0TTr9rQfe6ii8ZcNU4Cxsia9GdRFFxPN5SnjEUyFv12hQOJu+Yf1A3DnjTRzmIaUAeBqTiSCYyp99lu9p9X67fpMmzwPDjO4MZ4elr0Pk1DhniZUaVTidW3pRMJ0sEblPC166iETCIHkaoiYu4oocZysGVmLfJAJPpGg++3A9Sbv3yyahvPgJDRt7qp+ihsyXUGrYnGL48FyxnQ732kk7pXv9CFG0UoiJuRATPKe2czR/RW2hY7wJRfLDFo98qBD96ROctQrtVS7uAdhLhQ2Vg57OzRWQDt0YYiFuY3LMnRXsYOShzg8LB5Rs9TsSDmX0RpW7mvB8XSkKL+bjTaXXmAOH0ixNwpZf1Ud/Ci0nCH5cu8TKBL/C7eyYogZOAitP4EzrpkWNDQMvyH9BJbEHuR66aqCqBfv8sUza99/8Lp80AvyhLGilBPniwIBm0dw+vI6+xUEN1iPR3mHeLjlP7cpLlYsY8K2SrGVvytNi2mDLb/RnbJM3WVrMG7g0OqNPgq3C5X79DDWMenzSqthIpFnpqWhsgkKk2HsAMvOI5LBMCMVjCLMvhHhGqdnVeR1KV9y1biNMqX3nbH1/4IIrXuqd/BFTySQ9NJiGjDxx70rswNd oTimSVfB N/spTsDQ+Eu13clpcnn2wlB/G41v+q4fn+PAEvrLWtilr5xrZEriUN1iG4BHVTwj65Ql9KzinOEJl16rAlKDc39QnZ1CxM/F8krfBxnsZdXIl5fhq+HJM0XQQevo1xCFBLRUjvNxohVUVtt5Hi4PZLVV58729ySYfz3q+vaZE7+X16obhGgaUqCZunEKozwHqoVXUx0usvFG8OfZCL3wtsm2r3cvzIlYYIoSexVPdmcuHz5uY3VdAK+LdW1XakgTKHRWSumOCoDvq1l6dUvP0/jzlcIs/HdBFDrUXGgfBfgjXJZ785R2AuRvbAV/2MTje38Jm2KjzCymQp+ZPUvqKMI6Z/HuEXUIq8Amm3WSUnPpHsC62AxButfzYp+TwlFQUq6s5KHtIGDmOS0eCtUMw3KMXJoR4uw/YHittEa8iwagCJS5UDa+d9d5WB7vk30JAXsaXuTOJ7tQaGdVqkExG3QPyAaIiGioNrgnP X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 18.02.23 22:14, Rick Edgecombe wrote: > From: Yu-cheng Yu > > 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. > > Tested-by: Pengfei Xu > Tested-by: John Allen > Reviewed-by: Kees Cook > Signed-off-by: Yu-cheng Yu > Co-developed-by: Rick Edgecombe > Signed-off-by: Rick Edgecombe > > --- > v6: > - Update comment due to rename of Cow bit to SavedDirty > > v5: > - Add description of COW example (Boris) > - Replace "permissioned" (Boris) > - Remove capitalization of shadow stack (Boris) > > v4: > - Further improve comment talking about FAULT_FLAG_WRITE (Peterz) > > v3: > - Improve comment talking about using FAULT_FLAG_WRITE (Peterz) > --- > arch/x86/include/asm/trap_pf.h | 2 ++ > arch/x86/mm/fault.c | 38 ++++++++++++++++++++++++++++++++++ > 2 files changed, 40 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 7b0d4ab894c8..42885d8e2036 100644 > --- a/arch/x86/mm/fault.c > +++ b/arch/x86/mm/fault.c > @@ -1138,8 +1138,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; > @@ -1331,6 +1345,30 @@ void do_user_addr_fault(struct pt_regs *regs, > > perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); > > + /* > + * When a page becomes COW it changes from a shadow stack permission > + * page (Write=0,Dirty=1) to (Write=0,Dirty=0,SavedDirty=1), which is simply > + * read-only to the CPU. When shadow stack is enabled, a RET would > + * normally pop the shadow stack by reading it with a "shadow stack > + * read" access. However, in the COW case the shadow stack memory does > + * not have shadow stack permissions, it is read-only. So it will > + * generate a fault. > + * > + * For conventionally writable pages, a read can be serviced with a > + * read only PTE, and COW would not have to happen. But for shadow > + * stack, there isn't the concept of read-only shadow stack memory. > + * If it is shadow stack permission, it can be modified via CALL and > + * RET instructions. So COW needs to happen before any memory can be > + * mapped with shadow stack permissions. > + * > + * Shadow stack accesses (read or write) need to be serviced with > + * shadow stack permission memory, so in the case of a shadow stack > + * read access, treat it as a WRITE fault so both COW will happen and > + * the write fault path will tickle maybe_mkwrite() and map the memory > + * shadow stack. > + */ Again, I suggest dropping all details about COW from this comment and from the patch description. It's just one such case that can happen. -- Thanks, David / dhildenb