Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp377817rdh; Tue, 13 Feb 2024 22:22:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXPmhB73PE5MYVbFj8N0t3WyTk8Auyw5CLtnvmd/UhBQkMvephbpU5ohE9FV8oNCWg7Le4aV0+BGQUEjYzf7/5NvVrZOOonDqPygBKNXg== X-Google-Smtp-Source: AGHT+IFQ2z5UGr0rBJ2o1oU0A7prC5pq6O3AOtzU00IXDgcG5VuK9YT3y1QozZL4n36ZXlNZ9Qly X-Received: by 2002:a05:6808:f0f:b0:3c0:3d6e:59e2 with SMTP id m15-20020a0568080f0f00b003c03d6e59e2mr1926403oiw.12.1707891764890; Tue, 13 Feb 2024 22:22:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707891764; cv=pass; d=google.com; s=arc-20160816; b=hCkOV25gUlaLvKqT7jl7J2yW10i6a9tL+TAEyKjdkbQvGPwy6fk4GvjPVA6ts7oxH3 RwmaQ+rt7f8LP/jXD8DBw6llSPLWFRAFJ8Yj+xHwhX0Obts2uEUTHIoX+MF9Yx3GfpHt ina++KOukSRQD60zyhDloNPajg9tyZz4ii/Lb6hsJBa+FrSNdBTNLvLjxHoPSCmbqJ1F BoMIxu0/Ut99pPyFnxCjT2o4XrW5HRKkq4pOBioT7JXOZJeqnwP2RrsQ0XTAXoueVY4d Zc6Nuj+jxbv8X4DFUmwVh7cAkdQVj8aF8Rn3/+Mbh1fFcKpviRqruEewRbdDGbSSwUbV VYiA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=rGY8MgAjorTLmS0etZL+MbOeGy8EWjx6eeLXYxdDNas=; fh=J7feWdDYC7wLAfPBplD0oMyuwwkZUPKfcz+AvCKfp/c=; b=p6+0sd5N0FCKv4Np2PgLTFWFrUfVEX0tZ7yEj7byMlQKNDgGiyjcJ9MRoCm+o5MQaJ Czi/S6noDpkFp4p43lLwU/cRot4RZmfVbKbxBjTxjqqKipB+bCVW4Za9TXxfo3EAqYHK NKTAfVpanlI8jITUqbDbVcJOw1ulziPEEtU3CMf/5XxbjMkivOFFUpf6E5iwGGYLLZge PUBMAcU+BimrFNx3UzLhHA/toTIhzgHmVD7JgZFVYGbzpOd2UWxCXVWeUH1i3blyec9Z 9JZFAnb/j4YqOtgInj8BTNHlmOMyBYAqiS6PsaUiuOAc3CC358RAaMK7JqlCdd95CP0n JhYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=O86RzAzY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-64684-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64684-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCXjHIk7/7tR5ryNDHW7q8uSW01BX3JsRQ2tiyneu8hVwl3v2Vnoxc10okduNOigqQFAordkvem6JaJP6/xvlUD+Vbo1ol91xjxgiebgqg== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id z21-20020a656655000000b005dc81a30772si2753858pgv.301.2024.02.13.22.22.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 22:22:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-64684-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=O86RzAzY; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-64684-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-64684-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 5B909B25B50 for ; Wed, 14 Feb 2024 02:21:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1A4ABBE47; Wed, 14 Feb 2024 02:21:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="O86RzAzY" Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65F8CB664; Wed, 14 Feb 2024 02:21:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=134.134.136.20 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707877300; cv=none; b=e/fQWzPhBsZ+ShF8TbwcZk2h2WvIDChcYKItAHBE8vfVZnf52iwuTySCiq2Lx3gt1aNK5LPcj/HImRCQLXbkmR+l2QWwz7ZlVMX056POwotQ6XqegG8rGfX6JjqwVkDyb6Mp1Cj7Hmp6fGQKsIkzFh69hn7B+5WFNKDWwpIdecY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707877300; c=relaxed/simple; bh=w/Py/fKUqDJ1wrKhND+FI5QwXTp0BunLn1s0v7WFZgg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rWzVQblEq7Omx10Tfa0nGHP5tjIP1cWwDV+p44sVu+oW1AhKKH0KshfIqnGJAMMCMREoeC6TF9wmYlilnUMOzB/MWTd8pywQJ/r7xtlvmiZYhnYQmm8nYYtmQ5ySR1ujJLSDbzG58GZIN+tnaky8fVbnL5K9FcK8jZcEDXrFUcs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=O86RzAzY; arc=none smtp.client-ip=134.134.136.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1707877298; x=1739413298; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=w/Py/fKUqDJ1wrKhND+FI5QwXTp0BunLn1s0v7WFZgg=; b=O86RzAzYuaysXZG5yXbqlQbxfOQGanLdMRDXlurt4VNIWwyGTP72s9U4 AoY+CS7cnw7gGXwc01QJ7fQzIvXJvGzI+2eccAjBX95iypONQppz7VSUF 574YGNxdBDts3+A28hh491ucg52z57/AecDB+XGreNCba7sU6zzq8gJhV /GwE/wrDyQ4eW0jD0PPOJ6q5fm9QT5gVC2jwPevV19YzeATCqt+QP9S8k dRbT+Nm+MBwtWLsBgWeUPgJPU9O5rH9zgQAz2pdNqaQJidhe2tjhnAFyM oz5f1r4JpXtEumxTexoq2TXxNSoStdXRyYN5YFvMYkgGb0sX/yIlUOOTy A==; X-IronPort-AV: E=McAfee;i="6600,9927,10982"; a="396348013" X-IronPort-AV: E=Sophos;i="6.06,158,1705392000"; d="scan'208";a="396348013" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2024 18:21:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10982"; a="911976033" X-IronPort-AV: E=Sophos;i="6.06,158,1705392000"; d="scan'208";a="911976033" Received: from diegoavi-mobl.amr.corp.intel.com (HELO desk) ([10.255.230.185]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2024 18:21:35 -0800 Date: Tue, 13 Feb 2024 18:21:35 -0800 From: Pawan Gupta To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Josh Poimboeuf , Andy Lutomirski , Jonathan Corbet , Sean Christopherson , Paolo Bonzini , tony.luck@intel.com, ak@linux.intel.com, tim.c.chen@linux.intel.com, Andrew Cooper , Nikolay Borisov Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, Alyssa Milburn , Daniel Sneddon , antonio.gomez.iglesias@linux.intel.com Subject: [PATCH v8 1/6] x86/bugs: Add asm helpers for executing VERW Message-ID: <20240213-delay-verw-v8-1-a6216d83edb7@linux.intel.com> X-Mailer: b4 0.12.3 References: <20240213-delay-verw-v8-0-a6216d83edb7@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240213-delay-verw-v8-0-a6216d83edb7@linux.intel.com> MDS mitigation requires clearing the CPU buffers before returning to user. This needs to be done late in the exit-to-user path. Current location of VERW leaves a possibility of kernel data ending up in CPU buffers for memory accesses done after VERW such as: 1. Kernel data accessed by an NMI between VERW and return-to-user can remain in CPU buffers since NMI returning to kernel does not execute VERW to clear CPU buffers. 2. Alyssa reported that after VERW is executed, CONFIG_GCC_PLUGIN_STACKLEAK=y scrubs the stack used by a system call. Memory accesses during stack scrubbing can move kernel stack contents into CPU buffers. 3. When caller saved registers are restored after a return from function executing VERW, the kernel stack accesses can remain in CPU buffers(since they occur after VERW). To fix this VERW needs to be moved very late in exit-to-user path. In preparation for moving VERW to entry/exit asm code, create macros that can be used in asm. Also make VERW patching depend on a new feature flag X86_FEATURE_CLEAR_CPU_BUF. Reported-by: Alyssa Milburn Suggested-by: Andrew Cooper Suggested-by: Peter Zijlstra Cc: stable@kernel.org Signed-off-by: Pawan Gupta --- arch/x86/entry/entry.S | 22 ++++++++++++++++++++++ arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/nospec-branch.h | 13 +++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S index 582731f74dc8..d9feadffa972 100644 --- a/arch/x86/entry/entry.S +++ b/arch/x86/entry/entry.S @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include "calling.h" @@ -23,4 +26,23 @@ EXPORT_SYMBOL_GPL(entry_ibpb); .popsection +/* + * Define the VERW operand that is disguised as entry code so that + * it can be referenced with KPTI enabled. This ensure VERW can be + * used late in exit-to-user path after page tables are switched. + */ +.pushsection .entry.text, "ax" + +.align L1_CACHE_BYTES, 0xcc +SYM_CODE_START_NOALIGN(mds_verw_sel) + UNWIND_HINT_UNDEFINED + ANNOTATE_NOENDBR + .word __KERNEL_DS +.align L1_CACHE_BYTES, 0xcc +SYM_CODE_END(mds_verw_sel); +/* For KVM */ +EXPORT_SYMBOL_GPL(mds_verw_sel); + +.popsection + THUNK warn_thunk_thunk, __warn_thunk diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 315673320f6b..0343caa016a9 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -95,7 +95,7 @@ #define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */ #define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */ #define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */ -/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */ +#define X86_FEATURE_CLEAR_CPU_BUF ( 3*32+18) /* "" Clear CPU buffers using VERW */ #define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */ #define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */ #define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */ diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 8bd8ed576f91..da1b5a20dea2 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -306,6 +306,17 @@ #endif .endm +/* + * Macro to execute VERW instruction that mitigate transient data sampling + * attacks such as MDS. On affected systems a microcode update overloaded VERW + * instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF. + * + * Note: Only the memory operand variant of VERW clears the CPU buffers. + */ +.macro CLEAR_CPU_BUFFERS + ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF +.endm + #else /* __ASSEMBLY__ */ #define ANNOTATE_RETPOLINE_SAFE \ @@ -529,6 +540,8 @@ DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush); DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear); +extern u16 mds_verw_sel; + #include /** -- 2.34.1