Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1055967rdb; Wed, 24 Jan 2024 03:40:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjSF6Rz1pTqTf9wGP6edv+Sf9lj0WlCaPoiO0ueCq4pgRfWdkBJVu2MyC8m8dNFMCtcjO4 X-Received: by 2002:a05:6122:995:b0:4b7:b414:f268 with SMTP id g21-20020a056122099500b004b7b414f268mr3608881vkd.13.1706096454937; Wed, 24 Jan 2024 03:40:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706096454; cv=pass; d=google.com; s=arc-20160816; b=f+FXnMlXrOFRePgJNhjWWiewbyRIRg0QVDIoad8pAwE9bwdMg5BGPVIE/MYhf17pyD 9hopqAjXb3bdWYxkR3BYL03oBYjKQtSYXEiS634Xsgo6QQZGBO++ducDhooJW8WThoAX BsncJ8IAgfZXs3jpImmOAxa84H1LSFDLPgnOs+B+Vzr7Vd+j0s71u6JIIU246LCoCvhW Z/7/9sS0nGKo9gyPJm5iB4zp+aYI7xcT9RuAhbTcGrjKONd7xFrgqpMFzie/FvfvTx0Y buqwiWiPft1e+SLNVpyavyGNWGtAISVa7mU9w3cK7jwtnQBIObr368wrHasJkgasHmXT ESwA== 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=AAWYqQo49rSUP/liphSBHP0is1wvO5riamGNWMX1lTw=; fh=mM7j0AJ/S24jynU3+f8ZuQxBzhzn0ovRqHTZp+moQSw=; b=FSml4l/owgBTibR1X34GVAfiWkbOmNDRkxSNz2uEHsc/6jCJIjZoB+HsgIljzmdW1z 0sxMXXQlmG3mVJh75H7kiEU47sFALPZJkPjc1927EDs7AaCHk+7yEy7PuP3eExmRgpFI S3b8iAOWVxQF7qAA747ld+kEm+BJNKoDpEEPYnMV9cOWAG0gf6OKohVSrx7aNbLoOov3 gTIegXQox2fslnKIeOqBVujLFTasIj5qZZ6x35GjRn5BlzZq/ouAR9TTQckFmg8fz+BX 1SvyqvoU/CMdjPR9VIzXLbv2hkI219U4c8enAdNeJB0VzrCjxQw9hE5dUl9faTy/S0PT WA9w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GGfVKPzj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36574-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36574-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id e6-20020a0cf346000000b0067f60f66a13si10111639qvm.581.2024.01.24.03.40.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 03:40:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36574-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GGfVKPzj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36574-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36574-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C38E91C24A70 for ; Wed, 24 Jan 2024 07:42:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CCF0C1799C; Wed, 24 Jan 2024 07:41:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="GGfVKPzj" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.13]) (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 4DE7E171DD; Wed, 24 Jan 2024 07:41:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706082064; cv=none; b=hfZzMspZkTTuBowdORqgwUU0SQfaC+WFfY1I6e6mkuS+6+kEO+z2DTd7mKJMP02rLoGRq5AHoF9Sj9PqnSJf5cpcLOyasBMoWAcb18QjkA4wTLcJK9ToNlsM6nmk9EHLYBdYs7csINaQjhMK5xduabExo296oXEYY2GR4PJoWV8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706082064; c=relaxed/simple; bh=/z2uYKhSyYcTU8/yd1Vq7zQ21LOb4cUygjDFi2E+u98=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mgv+bus5RFefcks1fCuQH/I+vIXjctunmY1lGEDiRJ7F99tvgSRnQ3NXQKmE70Jt/FRFURQIInXrH3iej9LG3kjoQQb0KHJrG72CHabVT0L2fNqU9ugRF/tfu0EpQT+Wy9knb9XQKjRnXtb/QrZGiBHt0oz6/x7yWJpV3VxFe6c= 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=GGfVKPzj; arc=none smtp.client-ip=198.175.65.13 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=1706082063; x=1737618063; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=/z2uYKhSyYcTU8/yd1Vq7zQ21LOb4cUygjDFi2E+u98=; b=GGfVKPzjWZIQTqaxOLw6JNkgtRoLVqGWls97u8ZZCJGC6MTX2yxFVL9Q TNyyl5xq4ziSr4xhRSUBVOcUhKQ60VMVgoQ2SFVpAjZgdguQoWh+LvpMS j3bdESyDjQIsmYBxBiNSN/gsiF7KoaLvvf4gg3jOyFnRh3bqh9D9Qf0DT mKEEgBEUSA07unmKKwFiw70KSsIQc7zS2VPM7VxnaDtmO1WO0TUlwoUzo Kp7gklIAy8mPEiHR0sWo44WsYeA553aVOrfyUzrctftcUWU67k8VrSw8F qnrNXzC1uWO8SjH8cZtbo5nSxocUkFelNL4ZgpG+zbekYQOoLiu8Vb2d2 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="8880160" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="8880160" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orvoesa105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 23:41:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="786326523" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="786326523" Received: from bbaidya-mobl.amr.corp.intel.com (HELO desk) ([10.209.53.134]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 23:41:01 -0800 Date: Tue, 23 Jan 2024 23:41:01 -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, Pawan Gupta , Alyssa Milburn Subject: [PATCH v6 1/6] x86/bugs: Add asm helpers for executing VERW Message-ID: <20240123-delay-verw-v6-1-a8206baca7d3@linux.intel.com> X-Mailer: b4 0.12.3 References: <20240123-delay-verw-v6-0-a8206baca7d3@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: <20240123-delay-verw-v6-0-a8206baca7d3@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 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 | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S index 8c8d38f0cb1d..bd8e77c5a375 100644 --- a/arch/x86/entry/entry.S +++ b/arch/x86/entry/entry.S @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include .pushsection .noinstr.text, "ax" @@ -20,3 +23,22 @@ SYM_FUNC_END(entry_ibpb) EXPORT_SYMBOL_GPL(entry_ibpb); .popsection + +/* + * Defines the VERW operand that is disguised as entry code so that + * it can be referenced with KPTI enabled. This ensures 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 diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 4af140cf5719..79a7e81b9458 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -308,10 +308,10 @@ #define X86_FEATURE_SMBA (11*32+21) /* "" Slow Memory Bandwidth Allocation */ #define X86_FEATURE_BMEC (11*32+22) /* "" Bandwidth Monitoring Event Configuration */ #define X86_FEATURE_USER_SHSTK (11*32+23) /* Shadow stack support for user mode applications */ - #define X86_FEATURE_SRSO (11*32+24) /* "" AMD BTB untrain RETs */ #define X86_FEATURE_SRSO_ALIAS (11*32+25) /* "" AMD BTB untrain RETs through aliasing */ #define X86_FEATURE_IBPB_ON_VMEXIT (11*32+26) /* "" Issue an IBPB only on VMEXIT */ +#define X86_FEATURE_CLEAR_CPU_BUF (11*32+27) /* "" Clear CPU buffers using VERW */ /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4) /* AVX VNNI instructions */ diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index f93e9b96927a..4ea4c310db52 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -315,6 +315,21 @@ #endif .endm +/* + * Macros 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 EXEC_VERW + verw _ASM_RIP(mds_verw_sel) +.endm + +.macro CLEAR_CPU_BUFFERS + ALTERNATIVE "", __stringify(EXEC_VERW), X86_FEATURE_CLEAR_CPU_BUF +.endm + #else /* __ASSEMBLY__ */ #define ANNOTATE_RETPOLINE_SAFE \ -- 2.34.1