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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF56EC46467 for ; Fri, 20 Jan 2023 00:55:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229787AbjATAzZ (ORCPT ); Thu, 19 Jan 2023 19:55:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbjATAzV (ORCPT ); Thu, 19 Jan 2023 19:55:21 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57C539F075 for ; Thu, 19 Jan 2023 16:55:19 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id 7so2987574pga.1 for ; Thu, 19 Jan 2023 16:55:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=NXL29dhGPGSlxhwtfxK4fw+n68wsFSVNwNnLqn1osKM=; b=jZRlzywpwITHll2KRGT8ul5zgQzLQ5haL7RX7a1uWpmxk7IpWlL0sqakiUMkrLyS00 GUweQNVDtxHmxEpAU10K778LVljDuRkRIfGg0M9wktSLRM0TM7hkTJZUF2qUVbCmrE0C qria/izb3rbVitx2JKW2eXnH48oGsSs+T9tPc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NXL29dhGPGSlxhwtfxK4fw+n68wsFSVNwNnLqn1osKM=; b=eg3ih92xF4RJ/1fJ1BWeaT+323ZNPMk5sJJMdsiwMhvUIzrUWnJ13oEbvZC6gASfoY iWKyGLIq716vLKg9x9UWO7/AB9ZnmpsuCLYeStaUWVRGrlUsb/40h6BjQDafS0kWijIJ eyX4/+/1UT7U3j60T42iNZGkHGaWLsoskEgztVOxbeuz7RiR3lYOEbGyQidNa/ia0+9v e1TQcK8Ku5YqUTQLA9J1Y/fZ1mwTXf7H4Y264IiFb+sQ7jYKRWpAeU15C9CKgLPweDpc sn2DrpdvNizRVxUrnsUESn0RNwiwYaVn1/L2td853ToL5rN8R7//mx0lOJ0r9wwkxonu yEQw== X-Gm-Message-State: AFqh2kqp7VPvf+QKu45mWbBqtIAuUFu8WOKs7cOrXfUkc+yPFmLssxWi REab48Rf4HbmtWWsJHHXOEat2Q== X-Google-Smtp-Source: AMrXdXv5B5/j+1dhLV2iD0PUpATQfFIclIsDHZkG7CDLuhZzZXjljtdmKU4p4Wj57OmeTr9d69neOA== X-Received: by 2002:a05:6a00:21c9:b0:58d:f607:5300 with SMTP id t9-20020a056a0021c900b0058df6075300mr8819113pfj.8.1674176118791; Thu, 19 Jan 2023 16:55:18 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 65-20020a621844000000b005877d374069sm22336822pfy.10.2023.01.19.16.55.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 16:55:18 -0800 (PST) Date: Thu, 19 Jan 2023 16:55:17 -0800 From: Kees Cook To: Rick Edgecombe Cc: 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 , 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, Yu-cheng Yu Subject: Re: [PATCH v5 10/39] x86/mm: Introduce _PAGE_COW Message-ID: <202301191655.97E3023EC@keescook> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> <20230119212317.8324-11-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230119212317.8324-11-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-doc@vger.kernel.org On Thu, Jan 19, 2023 at 01:22:48PM -0800, Rick Edgecombe wrote: > Some OSes have a greater dependence on software available bits in PTEs than > Linux. That left the hardware architects looking for a way to represent a > new memory type (shadow stack) within the existing bits. They chose to > repurpose a lightly-used state: Write=0,Dirty=1. So in order to support > shadow stack memory, Linux should avoid creating memory with this PTE bit > combination unless it intends for it to be shadow stack. > > The reason it's lightly used is that Dirty=1 is normally set by HW > _before_ a write. A write with a Write=0 PTE would typically only generate > a fault, not set Dirty=1. Hardware can (rarely) both set Dirty=1 *and* > generate the fault, resulting in a Write=0,Dirty=1 PTE. Hardware which > supports shadow stacks will no longer exhibit this oddity. > > So that leaves Write=0,Dirty=1 PTEs created in software. To achieve this, > in places where Linux normally creates Write=0,Dirty=1, it can use the > software-defined _PAGE_COW in place of the hardware _PAGE_DIRTY. In other > words, whenever Linux needs to create Write=0,Dirty=1, it instead creates > Write=0,Cow=1 except for shadow stack, which is Write=0,Dirty=1. > Further differentiated by VMA flags, these PTE bit combinations would be > set as follows for various types of memory: > > (Write=0,Cow=1,Dirty=0): > - A modified, copy-on-write (COW) page. Previously when a typical > anonymous writable mapping was made COW via fork(), the kernel would > mark it Write=0,Dirty=1. Now it will instead use the Cow bit. This > happens in copy_present_pte(). > - A R/O page that has been COW'ed. The user page is in a R/O VMA, > and get_user_pages(FOLL_FORCE) needs a writable copy. The page fault > handler creates a copy of the page and sets the new copy's PTE as > Write=0 and Cow=1. > - A shared shadow stack PTE. When a shadow stack page is being shared > among processes (this happens at fork()), its PTE is made Dirty=0, so > the next shadow stack access causes a fault, and the page is > duplicated and Dirty=1 is set again. This is the COW equivalent for > shadow stack pages, even though it's copy-on-access rather than > copy-on-write. > > (Write=0,Cow=0,Dirty=1): > - A shadow stack PTE. > - A Cow PTE created when a processor without shadow stack support set > Dirty=1. > > There are six bits left available to software in the 64-bit PTE after > consuming a bit for _PAGE_COW. No space is consumed in 32-bit kernels > because shadow stacks are not enabled there. > > Implement only the infrastructure for _PAGE_COW. Changes to start > creating _PAGE_COW PTEs will follow once other pieces are in place. > > Tested-by: Pengfei Xu > Tested-by: John Allen > Co-developed-by: Yu-cheng Yu > Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook -- Kees Cook