Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1896332rwb; Thu, 19 Jan 2023 17:26:14 -0800 (PST) X-Google-Smtp-Source: AMrXdXtFSB4IYZhzkfKN7SUij2shU3CoLpblFCw2AfGnF1vxqURkDjOFiXpBJ71Mwt0eR82vzUsI X-Received: by 2002:a17:90a:f30e:b0:229:27a2:d80f with SMTP id ca14-20020a17090af30e00b0022927a2d80fmr13659045pjb.23.1674177973978; Thu, 19 Jan 2023 17:26:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674177973; cv=none; d=google.com; s=arc-20160816; b=vXFW5g9aj/czewWsBkRZ9xpL+9kxEDf2r9RSMFzfUDttfeu18UWqjklVrf6MabhB0I RxOlvwUG1AvCLeJhV3JxYWZfCJ+qpjsWy7Y5GqXH+rLzWXoripVgC8u94l0Sz9e6Z5is 1wqefIDOCArxfwbcPfz3cE2U4XYQmjQnn57VtTZt1jmO9VR2wn18GQRJcnRhEUmqw1Ut sLAqfBVE+wcUGkPk3QkTyBc6vuqIEoxzPHjbveTior/SnFsfXbR2f9e0eoDwj+wdTaZA ih96SrbBZ+ssSPff85oqa6K3DP8J917gY5R4Xo5mUrMFLk9G+XasKKJHy/cFFAjNwIZs gsSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QFsgarPNbICqKMSVZVyYMOF+g+3QXwsXtzOcayGvoi4=; b=mgaqndBkFk0NRBntolxIDT2te3sF4cb5PDMgnyHKDMzx80rnX68088Vg6x3oxkNGxG 9inXgoWBe+ev8ruNJHKYNpZaOX2fnKurfWU9rxmk1vl37YAUPIk6b2Q5GcCwGoaYM46H VR7qVsMEFa7tA2A3Ki7VQwXnmc1K6mqjTWOVKCsJ2010SmW25UY8Nly2CcSBhLJZDoLe T6gEEiBYxkM3laVUHlQyvtQDqaB1HIa4XXXVnYbG7NVv0zVxfdc7zxMxpDmfTZqzI0ju z556rhp2OO5ztO7BjxwEB85lKDjrjcD1IiA44Sa4TYlrZce0vUxG7/NnBUgrHZpOQi8H A5aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=A9WplzbO; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11-20020a63234b000000b004a68aefb7adsi41084213pgm.215.2023.01.19.17.26.07; Thu, 19 Jan 2023 17:26:13 -0800 (PST) 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=@chromium.org header.s=google header.b=A9WplzbO; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbjATBBe (ORCPT + 46 others); Thu, 19 Jan 2023 20:01:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbjATBBZ (ORCPT ); Thu, 19 Jan 2023 20:01:25 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48A49A45EC for ; Thu, 19 Jan 2023 17:01:20 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id v23so4012114plo.1 for ; Thu, 19 Jan 2023 17:01:20 -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=QFsgarPNbICqKMSVZVyYMOF+g+3QXwsXtzOcayGvoi4=; b=A9WplzbOSCG6EUOsIJ7kol0hQb5oYSpseab3h1cbCZlz1ZHfA317/hf0oOx902fJYd pHxs1xgWKKIz2MDnngJf2LmYabDdDddSS796KfBajBl5UhpiBrj6VMDoPHJU5IfhqNLp 8983YP9wsnceP1umAtc4itnxZVRMm3gbrbjGI= 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=QFsgarPNbICqKMSVZVyYMOF+g+3QXwsXtzOcayGvoi4=; b=cAUCI/lRUbz3tPK1EKga8+jCUS7ILR4Q7WlIQkJ0jxqORsKQv0PKRUSv2WeEztiehY 9/SZDXE4d+zgKXVKrliQJW3qDttSpudFK0KcgznnQoR4Eh+E3MgYlJ6jW/a9/8usQKFA jT0gGfF5R0DWx14TX/Cypl1os80ZtkLUG0LGVbMf4ZmgEJV2lcyZJfLO2Qt6U2uvvgX7 sJsGn3F42IUA1f8nC4cEMTultEAFFAtqhthdBVQxyTmnmvO4yaZjY3/EHLpYLXWtb8bJ uu3qVoD9Se+9m73wNocBFSS/TV2lT5YyTg7JYhbsFEB/IiF//jn8w333o+rpgvcj8ncd JW3Q== X-Gm-Message-State: AFqh2krh34N7+Bed387wBYuM9+99i0dVwu/jAmqdLO7sPiWDrIyt1Gsb /3KIUO0Us7ObZddnvmNNTyjbsg== X-Received: by 2002:a05:6a20:9c8b:b0:b8:bc13:c838 with SMTP id mj11-20020a056a209c8b00b000b8bc13c838mr13594445pzb.53.1674176479784; Thu, 19 Jan 2023 17:01:19 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h4-20020aa79f44000000b0056d7cc80ea4sm10783612pfr.110.2023.01.19.17.01.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 17:01:19 -0800 (PST) Date: Thu, 19 Jan 2023 17:01:18 -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 19/39] mm: Fixup places that call pte_mkwrite() directly Message-ID: <202301191701.ADBD82708A@keescook> References: <20230119212317.8324-1-rick.p.edgecombe@intel.com> <20230119212317.8324-20-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230119212317.8324-20-rick.p.edgecombe@intel.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 On Thu, Jan 19, 2023 at 01:22:57PM -0800, Rick Edgecombe wrote: > From: Yu-cheng Yu > > The x86 Control-flow Enforcement Technology (CET) feature includes a new > type of memory called shadow stack. This shadow stack memory has some > unusual properties, which requires some core mm changes to function > properly. > > With the introduction of shadow stack memory there are two ways a pte can > be writable: regular writable memory and shadow stack memory. > > In past patches, maybe_mkwrite() has been updated to apply pte_mkwrite() > or pte_mkwrite_shstk() depending on the VMA flag. This covers most cases > where a PTE is made writable. However, there are places where pte_mkwrite() > is called directly and the logic should now also create a shadow stack PTE > in the case of a shadow stack VMA. > > - do_anonymous_page() and migrate_vma_insert_page() check VM_WRITE > directly and call pte_mkwrite(). Teach it about pte_mkwrite_shstk() > > - When userfaultfd is creating a PTE after userspace handles the fault > it calls pte_mkwrite() directly. Teach it about pte_mkwrite_shstk() > > To make the code cleaner, introduce is_shstk_write() which simplifies > checking for VM_WRITE | VM_SHADOW_STACK together. > > In other cases where pte_mkwrite() is called directly, the VMA will not > be VM_SHADOW_STACK, and so shadow stack memory should not be created. > - In the case of pte_savedwrite(), shadow stack VMA's are excluded. > - In the case of the "dirty_accountable" optimization in mprotect(), > shadow stack VMA's won't be VM_SHARED, so it is not necessary. > > Tested-by: Pengfei Xu > Tested-by: John Allen > Signed-off-by: Yu-cheng Yu Reviewed-by: Kees Cook -- Kees Cook