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 7078BC004D4 for ; Fri, 20 Jan 2023 01:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229864AbjATBBY (ORCPT ); Thu, 19 Jan 2023 20:01:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229843AbjATBBW (ORCPT ); Thu, 19 Jan 2023 20:01:22 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4687AA45CD for ; Thu, 19 Jan 2023 17:01:20 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id jm10so3948323plb.13 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=0Yf8lIDC6SkzYVNACniB0JDu1No4M4TYeyqUpFfADsvXzLKT0/Yz2Ed09dioFk8ldJ pD5uMp/u9oSkbg9Ff9NaL7qkOczgqcOee3wlr6Qa0TAcSVsJFWNkTEMlN3Bos0Xi38vt FMeidMkqq+IxSnLB+Q+ECx6mZdr1xE6E2Tgt5hmuNCxrLAE/qUpQp1p0hE0sfVVzDoHS cRs4yza9xGWaBqaw1I7H9apZU8fZ+kfSAV+prnCNN2iHVHe+1BI6mCQsYsYDV3H8ktI8 mIqQ5cq89e/YsjsUchKZ0npv16qW0wJvjY71SYikM8Ewi2ri7DJ7z96iNoyCkGgTNQF6 SHWQ== X-Gm-Message-State: AFqh2kp9aPkOMyWqTfUylGXqEW+U0SbZEFooCb1EnguV7mZa+jXC+XTb ogMY/lq22hPNC0xG52M0ci4Wbg== X-Google-Smtp-Source: AMrXdXvb9cDkGke9zAyCgTEMw6x2BXISM0o26YyZuQdkPa8eazpgLD3hpkP3EYi76Rast6AtsdUb7w== 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> Precedence: bulk List-ID: X-Mailing-List: linux-doc@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