Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2305830rda; Tue, 24 Oct 2023 21:02:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiB1fC+khKIGV03/cUMwlo0t481g3PgeXXmPfBxdBs+MaPDBM2G4HcH/X+70XuK57Sni+9 X-Received: by 2002:a54:4706:0:b0:3b2:ecab:900e with SMTP id k6-20020a544706000000b003b2ecab900emr15828699oik.15.1698206537921; Tue, 24 Oct 2023 21:02:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698206537; cv=none; d=google.com; s=arc-20160816; b=rFEReuWNGM+CreqOR1EpWdxxx2j8efhTS05I/KedLcv7Uarr8E5+bHbNCG4vWomv2F fFQUCA6pSa8zUJ1H+frNSYTOJT8TJArBkiV8ZogJQFOsf0JZusLHHWaa65Ry52YmVlGZ uXJDAtTq3TjfjNqbi3NzZtSKm1WPz4bOyqc9cLLA1vhNi0eIp2vb8InoUuFnQvCQaFBs UbpbjrYKrQl11MRDLN1J2wule/St9QLXZYepgMyJsnmz1BfeOQhTnzxW5kYJ3EOl1N94 u9Bdz92KGGCvC94Oy+4nVHL1mT94qAuo9bHBLcjh9dwrXJv8KbAgR4nemC9Xt/pxg0c0 o5mg== 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=HG6JEVGwuuLc7fwtWxYUT0wW0EgtPL83q5p103w7AZw=; fh=q22FrE+3HPFqgIfGSDy85ckjMt4+7YeUp/9thyDx+2k=; b=uNL24R/FOWWU8um7uC58lmG3DGMXpSUHgFv3ndZm261/AWuniltnB6kTKKiKKynYbp 551vuwP0GfRSSawpj/073IGQ+4SLZEh81YMBxMTuCXYSSqrIADcD/ijWexVJda5tAYrg u9wd7fX0b3PCa/sbPr5ZS9lJHKQkrC9jgFEJjKJMgLFgaPDif7nzzdM3STQ2cse6sD1F dLsfOL/UUqaYXc8FDpFQud607yT+JiTnhSw7uFz582Kf6lxrpFWh3bew7ACufBa/JGqO /aA1Y4KKHjZUkmxresgU90Qd53jadYYrJsPbGxsy2pieWPQeQdkb319Fc+3IYa0rKlGY Vo7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Oq5qBdu1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f17-20020a25b091000000b00d77f78ab3a7si9890812ybj.479.2023.10.24.21.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 21:02:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Oq5qBdu1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 1F39D808577F; Tue, 24 Oct 2023 21:01:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232386AbjJYEBL (ORCPT + 99 others); Wed, 25 Oct 2023 00:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbjJYEBJ (ORCPT ); Wed, 25 Oct 2023 00:01:09 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CBD8B3; Tue, 24 Oct 2023 21:01:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698206465; x=1729742465; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=1dvC3N7YrfARLKq5nUqFeK9TE0Y0MO6GfZ1KuchckAY=; b=Oq5qBdu1WHx3kidmX/k+oKWX3+2vt4jcUjf4sfZp8s2vVHCfCdVh7Xyj 72Q41ndGBeWoZx/UYbdQhrMKa4FcM0uLfD0XcDxHXMwtRjGUDVzzKSHIt GrzdigM6dLB5aA2f5GcRVWY9fjqWsYfhbkxkdgKE9BoDaArUHvd4jZ7V/ TAZmiY74/siYZgMyNgJExRSwYBM7VXk6YK+L3qrkMZb2m356uWh97V+SH Pdjpj2pnKizTP+VIyKjbrDeRteVCyPZrew/U2oXOquNwKPlRmgdxX6zOy HUOi2EVTlhD1+UOP8P5fI3QaJDDmpGlLlKaNI7voKzsCTCWebEqWSBBi0 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="372287488" X-IronPort-AV: E=Sophos;i="6.03,249,1694761200"; d="scan'208";a="372287488" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 21:01:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="824528049" X-IronPort-AV: E=Sophos;i="6.03,249,1694761200"; d="scan'208";a="824528049" Received: from mhans-mobl3.amr.corp.intel.com (HELO desk) ([10.252.132.200]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 21:01:03 -0700 Date: Tue, 24 Oct 2023 21:00:29 -0700 From: Pawan Gupta To: Peter Zijlstra Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Josh Poimboeuf , Andy Lutomirski , Jonathan Corbet , Sean Christopherson , Paolo Bonzini , tony.luck@intel.com, ak@linux.intel.com, tim.c.chen@linux.intel.com, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, Alyssa Milburn , Daniel Sneddon , antonio.gomez.iglesias@linux.intel.com, Alyssa Milburn Subject: Re: [PATCH v2 1/6] x86/bugs: Add asm helpers for executing VERW Message-ID: <20231025040029.uglv4dwmlnfhcjde@desk> References: <20231024-delay-verw-v2-0-f1881340c807@linux.intel.com> <20231024-delay-verw-v2-1-f1881340c807@linux.intel.com> <20231024103601.GH31411@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231024103601.GH31411@noisy.programming.kicks-ass.net> X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, 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 agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 21:01:42 -0700 (PDT) On Tue, Oct 24, 2023 at 12:36:01PM +0200, Peter Zijlstra wrote: > On Tue, Oct 24, 2023 at 01:08:21AM -0700, Pawan Gupta wrote: > > > +.macro CLEAR_CPU_BUFFERS > > + ALTERNATIVE "jmp .Lskip_verw_\@;", "jmp .Ldo_verw_\@", X86_FEATURE_CLEAR_CPU_BUF > > + /* nopl __KERNEL_DS(%rax) */ > > + .byte 0x0f, 0x1f, 0x80, 0x00, 0x00; > > +.Lverw_arg_\@: .word __KERNEL_DS; > > +.Ldo_verw_\@: verw _ASM_RIP(.Lverw_arg_\@); > > +.Lskip_verw_\@: > > +.endm > > Why can't this be: > > ALTERNATIVE "". "verw _ASM_RIP(mds_verw_sel)", X86_FEATURE_CLEAR_CPU_BUF > > And have that mds_verw_sel thing be out-of-line ? That gives much better > code for the case where we don't need this. Overall the code generated with this approach is much better. But, in my testing I am seeing an issue with runtime patching in 32-bit mode, when mitigations are off. Instead of NOPs I am seeing random instruction. I don't see any issue with 64-bit mode. config1: mitigations=on, 32-bit mode, post-boot entry_SYSENTER_32: ... 0xc1a3748e <+222>: pop %eax 0xc1a3748f <+223>: verw 0xc1a38240 0xc1a37496 <+230>: sti 0xc1a37497 <+231>: sysexit --------------------------------------------- config2: mitigations=off, 32-bit mode, post-boot entry_SYSENTER_32: ... 0xc1a3748e <+222>: pop %eax 0xc1a3748f <+223>: lea 0x0(%esi,%eiz,1),%esi <---- Doesn't look right 0xc1a37496 <+230>: sti 0xc1a37497 <+231>: sysexit --------------------------------------------- config3: 32-bit mode, pre-boot objdump entry_SYSENTER_32: ... c8e: 58 pop %eax c8f: 90 nop c90: 90 nop c91: 90 nop c92: 90 nop c93: 90 nop c94: 90 nop c95: 90 nop c96: fb sti c97: 0f 35 sysexit These tests were done with below patch: -----8<----- From: Pawan Gupta Date: Mon, 23 Oct 2023 15:04:56 -0700 Subject: [PATCH] x86/bugs: Add asm helpers for executing VERW 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 them depend on a new feature flag X86_FEATURE_CLEAR_CPU_BUF. Reported-by: Alyssa Milburn Suggested-by: Andrew Cooper Signed-off-by: Pawan Gupta --- arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/nospec-branch.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 58cb9495e40f..f21fc0f12737 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 */ /* 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 c55cc243592e..ed8218e2d9a7 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -13,6 +13,7 @@ #include #include #include +#include /* * Call depth tracking for Intel SKL CPUs to address the RSB underflow @@ -329,6 +330,29 @@ #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. + */ +.pushsection .rodata +.align 64 +mds_verw_sel: + .word __KERNEL_DS + .byte 0xcc +.align 64 +.popsection + +.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