Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp1508124rdd; Thu, 11 Jan 2024 00:59:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFbf3xgRzHiJQ5qcuT8AuPpgJWxPTCPqNiHs/oiNKBJTwHMxcOW6tQsY+akihGYdNiCbfVK X-Received: by 2002:a05:6402:228b:b0:557:ebac:7e4d with SMTP id cw11-20020a056402228b00b00557ebac7e4dmr371627edb.14.1704963560576; Thu, 11 Jan 2024 00:59:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704963560; cv=none; d=google.com; s=arc-20160816; b=vh8/qGvCuS0ao/6bxdLP3nalIHD3szAsuDvGJ78WH5y+eEf0SNWbs/r2qY/LkCyECt N0raqV0s9H7Ch8f3DFJiJMPJRw9TvEuEWFIMTF/AgTYthk+DWX5u0acfONjvH4BPFJLT 74QkKL0z/8txi36WbJnnReR1efdtV5NWB7q5rVFH/okjEVs7x94/w5PjnVmeEsUmaA9d VKIU93d2YZ2GIWE2MtlLqPWLXwgnNxmWVeecOtfQyvik85vT5EL1BQFBoB3oyTdo3dbs ysr02ogp6z1yNq+PtHGqX5Rt89z2IZxbuVQGw2sVxqgvuXM6VGKRy9BYkRsd71qj23VV bGNQ== ARC-Message-Signature: i=1; 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=khozrtzAj3vwjyUrXpWv9rTDxIFxnIQL0DQ40domI8A=; fh=9zb6IElBBOwPDhWsAkR8DeNi2jqlz/RyBa2WbyJnsJM=; b=zc464h0Z+6FdX31rUEeCJeC9JOZABznP6G3t8QXMztNqiOhqKbRc28tGgFeX86tacm uK/f49Xtm0haOQoMlaB5j0NPeVNjwo0Jg0rRkWuyvkuzidb/98oYwCcwn2XtDIOfPo6N h9RUWAIi+f9yOcYkZ0qrPYLkDq3QN2bMHa9Y/IkO/g9Xz4LKB9OqkgTVJGqaRrnywI2o AYokCsCBBQHfcnZeeUPLh6gGQ5s0FMj3euIM8ngvW8l+MmvP4eAjOMzhWrUMxT/KcRHg rTI437iA8Xmazb0/7lM/jWfr8wBmWsiaej+OiagVn51OAz+rEAyENM04o5Up9X7a2A2E Tnyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R5pjxTc5; spf=pass (google.com: domain of linux-kernel+bounces-23263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id i10-20020a056402054a00b0054c943a07bfsi331690edx.373.2024.01.11.00.59.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 00:59:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R5pjxTc5; spf=pass (google.com: domain of linux-kernel+bounces-23263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23263-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 542491F243D7 for ; Thu, 11 Jan 2024 08:59:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C193B15AEF; Thu, 11 Jan 2024 08:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="R5pjxTc5" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 8248B15AD6; Thu, 11 Jan 2024 08:56:32 +0000 (UTC) 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=1704963393; x=1736499393; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=u0ACNJnmCWkNPcbKiCWkDdCakrm+/qCMT4yvny0P32Q=; b=R5pjxTc5pSokC0/W59iU6/CbcmCpGUPd9Ao+MJsxTbypYBQOIIVSDGAy Q72i4lW++SjqTu091No022FCNueNC/c1TPnUYYiqV81+yrzyp4OtFav2+ 2BOr/FpX6iCmdUtDxMuKXvOd7HV4dUtpcLL/eb+Y7vt3iLU8s43p/UeJ5 /3jC+PIZ3TS3692OoOTOiGF236R3qDTcBFKrhF77Z+LAlkoG/BI8Kgvcf N16m/wVEzmP1zpFDr2ynClfYyogxe3hvHyJ6zGkUwV203IUoyohJMW90B I/sUJCV5lW9Y19VLjWX5TnvzWdpH40AzrXIC2zz3DEgGI8VoRyMEe5hB7 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="6144907" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="6144907" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jan 2024 00:56:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10949"; a="905877631" X-IronPort-AV: E=Sophos;i="6.04,185,1695711600"; d="scan'208";a="905877631" Received: from ericwong-mobl2.amr.corp.intel.com (HELO desk) ([10.209.43.169]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jan 2024 00:56:30 -0800 Date: Thu, 11 Jan 2024 00:56:30 -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 , Dave Hansen Subject: [PATCH v5 2/6] x86/entry_64: Add VERW just before userspace transition Message-ID: <20240111-delay-verw-v5-2-a3b234933ea6@linux.intel.com> X-Mailer: b4 0.12.3 References: <20240111-delay-verw-v5-0-a3b234933ea6@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: <20240111-delay-verw-v5-0-a3b234933ea6@linux.intel.com> Mitigation for MDS is to use VERW instruction to clear any secrets in CPU Buffers. Any memory accesses after VERW execution can still remain in CPU buffers. It is safer to execute VERW late in return to user path to minimize the window in which kernel data can end up in CPU buffers. There are not many kernel secrets to be had after SWITCH_TO_USER_CR3. Add support for deploying VERW mitigation after user register state is restored. This helps minimize the chances of kernel data ending up into CPU buffers after executing VERW. Note that the mitigation at the new location is not yet enabled. Corner case not handled ======================= Interrupts returning to kernel don't clear CPUs buffers since the exit-to-user path is expected to do that anyways. But, there could be a case when an NMI is generated in kernel after the exit-to-user path has cleared the buffers. This case is not handled and NMI returning to kernel don't clear CPU buffers because: 1. It is rare to get an NMI after VERW, but before returning to userspace. 2. For an unprivileged user, there is no known way to make that NMI less rare or target it. 3. It would take a large number of these precisely-timed NMIs to mount an actual attack. There's presumably not enough bandwidth. 4. The NMI in question occurs after a VERW, i.e. when user state is restored and most interesting data is already scrubbed. Whats left is only the data that NMI touches, and that may or may not be of any interest. Suggested-by: Dave Hansen Signed-off-by: Pawan Gupta --- arch/x86/entry/entry_64.S | 11 +++++++++++ arch/x86/entry/entry_64_compat.S | 1 + 2 files changed, 12 insertions(+) diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index de6469dffe3a..bdb17fad5d04 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -161,6 +161,7 @@ syscall_return_via_sysret: SYM_INNER_LABEL(entry_SYSRETQ_unsafe_stack, SYM_L_GLOBAL) ANNOTATE_NOENDBR swapgs + CLEAR_CPU_BUFFERS sysretq SYM_INNER_LABEL(entry_SYSRETQ_end, SYM_L_GLOBAL) ANNOTATE_NOENDBR @@ -601,6 +602,7 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL) /* Restore RDI. */ popq %rdi swapgs + CLEAR_CPU_BUFFERS jmp .Lnative_iret @@ -712,6 +714,8 @@ native_irq_return_ldt: */ popq %rax /* Restore user RAX */ + CLEAR_CPU_BUFFERS + /* * RSP now points to an ordinary IRET frame, except that the page * is read-only and RSP[31:16] are preloaded with the userspace @@ -1438,6 +1442,12 @@ nmi_restore: std movq $0, 5*8(%rsp) /* clear "NMI executing" */ + /* + * Skip CLEAR_CPU_BUFFERS here, since it only helps in rare cases like + * NMI in kernel after user state is restored. For an unprivileged user + * these conditions are hard to meet. + */ + /* * iretq reads the "iret" frame and exits the NMI stack in a * single instruction. We are returning to kernel mode, so this @@ -1455,6 +1465,7 @@ SYM_CODE_START(entry_SYSCALL32_ignore) UNWIND_HINT_END_OF_STACK ENDBR mov $-ENOSYS, %eax + CLEAR_CPU_BUFFERS sysretl SYM_CODE_END(entry_SYSCALL32_ignore) diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S index de94e2e84ecc..eabf48c4d4b4 100644 --- a/arch/x86/entry/entry_64_compat.S +++ b/arch/x86/entry/entry_64_compat.S @@ -270,6 +270,7 @@ SYM_INNER_LABEL(entry_SYSRETL_compat_unsafe_stack, SYM_L_GLOBAL) xorl %r9d, %r9d xorl %r10d, %r10d swapgs + CLEAR_CPU_BUFFERS sysretl SYM_INNER_LABEL(entry_SYSRETL_compat_end, SYM_L_GLOBAL) ANNOTATE_NOENDBR -- 2.34.1