Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp507052rwd; Mon, 12 Jun 2023 17:45:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6B39iRKprG/ntRIz+KDW64zU7xfI30DDIWqr/bX/y67xKbFOnEE8jFIsshxLObMtV7TvUS X-Received: by 2002:a50:ed89:0:b0:514:93f0:e15a with SMTP id h9-20020a50ed89000000b0051493f0e15amr5949697edr.16.1686617107963; Mon, 12 Jun 2023 17:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686617107; cv=none; d=google.com; s=arc-20160816; b=wbYG1Mkjkq6JBWyueJU3C6j9VN6bgerkT+HIqX3Srh6kARmAR3rRbh6LDa3xZRGFWg clypmY4ZKaF9zENe+irAfanVPw8Fye/LAll5Tkwr7dsIPllDkgNPf9fdrif+uEUR3Bzc bmVtLtCJJ0kKS37RYQEDTLUQsJGs7F9IioRoWbwqkcFQWAe0GGmXdupRAIJh0b6ar4Og Cz6DtX5HDN6W1zsOLu8gS8dZKc8XEFLIgb/J2Kuhk+vXovg55qPN0jQfvk8ceaEDGNBj FnJF9aNWCX0yq/clAX2qIe5oOsIvmLr2lb+SdlqP3lxQpFnRvcg+AJxT0Wn6rBmWyE+v RNZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PXl5GqZ7V0Lo7jE7kAx6s21MXFeCFmlgpAurMlsy448=; b=LgcW3RzquP8lTfQG8TB0PLi/Y8da4wcCGeTqva2lxCp8BMOzEGgc/2rPiTZH+MYje5 t9vuwMAIydW9F9hORrgQ1qOfvAf3FdGb43mGpaKgly/5urk/eeM+rfHd9IEM9UmwscQg AH1z+PKmkGCD5Pc5lqk3aS5d8mhioV+8sdCD37+o7xF8FY8k16euNHPSZEEXLkVXMMb+ YGc0aH9K3pmuwxpeO0QzvH4UXhmKPZ59F0u38ZdsQMzpEAEsvU3JMrq3qNhK+QFToZtK q/V66jfR1H/4keSJ5F1SLEH5FDOeQZD927o8W/hYApfhKOSh+xF0e6dxCSSJIe8ze3/a aj+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=G03JU2S0; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i3-20020aa7dd03000000b00508d739f07bsi6714771edv.615.2023.06.12.17.44.42; Mon, 12 Jun 2023 17:45:07 -0700 (PDT) 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=@intel.com header.s=Intel header.b=G03JU2S0; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229513AbjFMAPs (ORCPT + 99 others); Mon, 12 Jun 2023 20:15:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233336AbjFMAOY (ORCPT ); Mon, 12 Jun 2023 20:14:24 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17AA41FD4; Mon, 12 Jun 2023 17:12:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686615176; x=1718151176; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c7sunRbrNdSwt/1KmOuIzzKZkXv73NcNzDYIVaoBldo=; b=G03JU2S0Evi/fePKI8tofmFf6jCfbNuseYNk3pBFLsEjtODG0wjoLGX+ fTtSibEDt9OAP17er1I4Y2zOUV+gLWqXTiu7hsS6hUTYyhxkDSadT8Ihk C5CDBUZgCNPaY5h5CR/sbJg/4LKePeZkxee12wEJJxmjKh4tipSR+a9Qc w5tc+YhEQtoNcTjqPXbqzwshaVvGDWwcJzqPt9OrfXmTvLLO3tyz384MH 2E5LkLl5e2e07zNSx8KSt4z1kHIfUHdPl8zbXgCZdRIuKlsGxnubCYOtY P3ghlZM2+r1GM7YyFY84GurNIdlWtTMvT2smfRg87Qwe8bKcd9xoMsz4a w==; X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="361557144" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="361557144" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10739"; a="835671052" X-IronPort-AV: E=Sophos;i="6.00,238,1681196400"; d="scan'208";a="835671052" Received: from almeisch-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.amr.corp.intel.com) ([10.209.42.242]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2023 17:12:25 -0700 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 , 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, david@redhat.com, debug@rivosinc.com, szabolcs.nagy@arm.com, torvalds@linux-foundation.org, broonie@kernel.org Cc: rick.p.edgecombe@intel.com, Pengfei Xu Subject: [PATCH v9 21/42] x86/mm: Teach pte_mkwrite() about stack memory Date: Mon, 12 Jun 2023 17:10:47 -0700 Message-Id: <20230613001108.3040476-22-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> References: <20230613001108.3040476-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 If a VMA has the VM_SHADOW_STACK flag, it is shadow stack memory. So when it is made writable with pte_mkwrite(), it should create shadow stack memory, not conventionally writable memory. Now that all the places where shadow stack memory might be created pass a VMA into pte_mkwrite(), it can know when it should do this. So make pte_mkwrite() create shadow stack memory when the VMA has the VM_SHADOW_STACK flag. Do the same thing for pmd_mkwrite(). This requires referencing VM_SHADOW_STACK in these functions, which are currently defined in pgtable.h, however mm.h (where VM_SHADOW_STACK is located) can't be pulled in without causing problems for files that reference pgtable.h. So also move pte/pmd_mkwrite() into pgtable.c, where they can safely reference VM_SHADOW_STACK. Signed-off-by: Rick Edgecombe Reviewed-by: Borislav Petkov (AMD) Reviewed-by: Kees Cook Acked-by: Mike Rapoport (IBM) Acked-by: Deepak Gupta Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook --- arch/x86/mm/pgtable.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 101e721d74aa..c4b222d3b1b4 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -883,6 +883,9 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr) pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) { + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + pte = pte_mkwrite_novma(pte); return pte_clear_saveddirty(pte); @@ -890,6 +893,9 @@ pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + pmd = pmd_mkwrite_novma(pmd); return pmd_clear_saveddirty(pmd); -- 2.34.1