From patchwork Mon Jan 22 13:52:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 190171 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2581001dyb; Mon, 22 Jan 2024 05:52:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IHRCfwaJaUWAj0OoeB7EuYUfERgnNV35b7Zf1Fi9GgEvvtzixTwiHDiJkWghhqXD1gJuVvE X-Received: by 2002:a81:a552:0:b0:5ff:56b4:263e with SMTP id v18-20020a81a552000000b005ff56b4263emr3025839ywg.101.1705931550701; Mon, 22 Jan 2024 05:52:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705931550; cv=pass; d=google.com; s=arc-20160816; b=xZTIRPjYwU52q+U4I/Jtz2u9KMUY3HNcc88SBv21KBcXHIfR+hrX5swV9v3QtBLAsT vQVOtyzfHXHQrVXi0SjF3tqaE/JEhQtazx3mBYAUwLyLuh8VVo6knWfSr9mr+Heo5Zu9 432Dkge07LFc/vX6IOLKKxV1tpuJY/x5sl+qGc31wg/gRrEmYIIy71E0xQM9uwMFUV6k wT0iFwN/7x85YNS6MBxee7PNmtQs4LL5Z3LZ0hhnwELxsgZ2CjDQPg+Hyq0nzrSE5h8d g8sAzMm+O/8/RbcXK65F/VLdSzvmgUyLRHxQcxyMx67mrgedbeIgYbyQc9G2d1W4ghGw rW9A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=zkICZpLK8fX7JPvw7cdwjuDNI2tT7NrN7jBRSSl3G+0=; fh=PHDPJwf1JiuHpUmCPT6qo//dmmqyBAKEilUlZrUacLU=; b=baWyU0l0ASfWkJglLPkU79gPPiOl5hxEVxKzE6vdtHzuVbApJ2O4mkBf2O84LtQ3gY /TOZl5BdGzKGDkKuPohoWU4MZgVMY/BH1JQioPZBnbFL0qQY6woFctsaogXd9IdrmplR txy071zF43iIuEB0xSmpxU+GRN0Y7+GZ0U11POWYdry+PTl+7oNFmRLy3Edbvh/9HfrD C89BDOdi67nmNYlOAlVccrnZWhBUomy5PJK1Us1Wap4e0k+oNIwcmug07yNaMfKqVSfF im56uE8oJSrlcT8Iuh9giZzZohSsAcIT0Ef83FCDpbl3UUQs8EqFjSXXo843Il5gIMAP 0oMQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="W4zoo/o9"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-33113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33113-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id x19-20020ae9f813000000b007834b7bf527si5618930qkh.351.2024.01.22.05.52.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 05:52:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="W4zoo/o9"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-33113-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33113-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de 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 6FB011C211C6 for ; Mon, 22 Jan 2024 13:52:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0DF13D0DF; Mon, 22 Jan 2024 13:52:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W4zoo/o9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nQuzdFPC" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 22860DF6E; Mon, 22 Jan 2024 13:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705931535; cv=none; b=odzDaG/2cPTfV7X6olp4tvUlIPX//mbY42dOGfqLQbrFPeJT1Qd8rZUIT72RTqA/gGCA0eGNBe5vcMRrCskVa7HwQyyXNICN9yvs59+l9Z0Owk6vWaUohvGf2UDgQCVDw2cZEmEYku/UYD73T29CuXmCiS8yCkUoVuhtnKd5tF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705931535; c=relaxed/simple; bh=lyYjyW0PG3GmvvfnJJ7aIuOt9frvldH3vJ4YoyfkjHE=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=VArAcM4vKNqSiCOeoUY5naADWAkG6bl2+s4+IGevjzs5yc9OrKPi97lVuiJpthFvTVzkS0apv/TGZypRgE5D0fb9zS3SZfbWhPW1V08Nokkmq/VUmXqmS49mAOAgSilF7nMVuvwLWkJJa64NdRZC581RdKfOJlsr+pefWQGAOQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W4zoo/o9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nQuzdFPC; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Mon, 22 Jan 2024 13:52:10 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1705931532; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zkICZpLK8fX7JPvw7cdwjuDNI2tT7NrN7jBRSSl3G+0=; b=W4zoo/o95cZ/e8CUSPgHI1Mn5YO7jdANLUvCx6A6A1rAJXSZ/Y7TeIALLd/yMs+F9aiUTJ 6puMbS3+f3qo7fRyVW0IsxFNbRL8aGXTC4re7j19H31n3SfEEM2Z5bt3Yk0umXCJX3fo/k Z47+dVxfd9GFPN9D9kk3MIkMW2sjTGxAYV/V0i16Cjp52CrQAvgNlBF/y40TvvyeeJOjBp yl+HxJLqpHROafaHsJYDlaPxeALG3yeSeMoaBWYFnooqqp0Jc6JCxblDM3TQr1hmMWRyx7 ZONmfRI/gx4YGtN4nBukk8PFY0/lRUJe//HT7Y/aEZel1KAKMstql77Y0dPn1w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1705931532; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zkICZpLK8fX7JPvw7cdwjuDNI2tT7NrN7jBRSSl3G+0=; b=nQuzdFPChKVrvrU72W8r+r6k+ydwOk73DojdQ+F4Xc2UhiBscG6aK4ecMxsQ/M6lTafeY6 wPLl4GQz7obFiWDw== From: "tip-bot2 for Borislav Petkov (AMD)" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sev] x86/sev: Harden #VC instruction emulation somewhat Cc: "Borislav Petkov (AMD)" , Tom Lendacky , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240105101407.11694-1-bp@alien8.de> References: <20240105101407.11694-1-bp@alien8.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <170593153094.398.7791095482489457964.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788798881405986360 X-GMAIL-MSGID: 1788798881405986360 The following commit has been merged into the x86/sev branch of tip: Commit-ID: df38347f1934b29d4b3075a53a6404b0d58dcb2f Gitweb: https://git.kernel.org/tip/df38347f1934b29d4b3075a53a6404b0d58dcb2f Author: Borislav Petkov (AMD) AuthorDate: Fri, 05 Jan 2024 11:14:07 +01:00 Committer: Borislav Petkov (AMD) CommitterDate: Mon, 22 Jan 2024 14:46:36 +01:00 x86/sev: Harden #VC instruction emulation somewhat Compare the opcode bytes at rIP for each #VC exit reason to verify the instruction which raised the #VC exception is actually the right one. Signed-off-by: Borislav Petkov (AMD) Acked-by: Tom Lendacky Link: https://lore.kernel.org/r/20240105101407.11694-1-bp@alien8.de --- arch/x86/boot/compressed/sev.c | 4 +- arch/x86/kernel/sev-shared.c | 102 +++++++++++++++++++++++++++++++- arch/x86/kernel/sev.c | 5 +- 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 454acd7..0732918 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -304,6 +304,10 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) if (result != ES_OK) goto finish; + result = vc_check_opcode_bytes(&ctxt, exit_code); + if (result != ES_OK) + goto finish; + switch (exit_code) { case SVM_EXIT_RDTSC: case SVM_EXIT_RDTSCP: diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 1d24ec6..5db24d0 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -10,11 +10,15 @@ */ #ifndef __BOOT_COMPRESSED -#define error(v) pr_err(v) -#define has_cpuflag(f) boot_cpu_has(f) +#define error(v) pr_err(v) +#define has_cpuflag(f) boot_cpu_has(f) +#define sev_printk(fmt, ...) printk(fmt, ##__VA_ARGS__) +#define sev_printk_rtl(fmt, ...) printk_ratelimited(fmt, ##__VA_ARGS__) #else #undef WARN #define WARN(condition, format...) (!!(condition)) +#define sev_printk(fmt, ...) +#define sev_printk_rtl(fmt, ...) #endif /* I/O parameters for CPUID-related helpers */ @@ -574,6 +578,7 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) { unsigned int subfn = lower_bits(regs->cx, 32); unsigned int fn = lower_bits(regs->ax, 32); + u16 opcode = *(unsigned short *)regs->ip; struct cpuid_leaf leaf; int ret; @@ -581,6 +586,10 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) if (exit_code != SVM_EXIT_CPUID) goto fail; + /* Is it really a CPUID insn? */ + if (opcode != 0xa20f) + goto fail; + leaf.fn = fn; leaf.subfn = subfn; @@ -1170,3 +1179,92 @@ static int vmgexit_psc(struct ghcb *ghcb, struct snp_psc_desc *desc) out: return ret; } + +static enum es_result vc_check_opcode_bytes(struct es_em_ctxt *ctxt, + unsigned long exit_code) +{ + unsigned int opcode = (unsigned int)ctxt->insn.opcode.value; + u8 modrm = ctxt->insn.modrm.value; + + switch (exit_code) { + + case SVM_EXIT_IOIO: + case SVM_EXIT_NPF: + /* handled separately */ + return ES_OK; + + case SVM_EXIT_CPUID: + if (opcode == 0xa20f) + return ES_OK; + break; + + case SVM_EXIT_INVD: + if (opcode == 0x080f) + return ES_OK; + break; + + case SVM_EXIT_MONITOR: + if (opcode == 0x010f && modrm == 0xc8) + return ES_OK; + break; + + case SVM_EXIT_MWAIT: + if (opcode == 0x010f && modrm == 0xc9) + return ES_OK; + break; + + case SVM_EXIT_MSR: + /* RDMSR */ + if (opcode == 0x320f || + /* WRMSR */ + opcode == 0x300f) + return ES_OK; + break; + + case SVM_EXIT_RDPMC: + if (opcode == 0x330f) + return ES_OK; + break; + + case SVM_EXIT_RDTSC: + if (opcode == 0x310f) + return ES_OK; + break; + + case SVM_EXIT_RDTSCP: + if (opcode == 0x010f && modrm == 0xf9) + return ES_OK; + break; + + case SVM_EXIT_READ_DR7: + if (opcode == 0x210f && + X86_MODRM_REG(ctxt->insn.modrm.value) == 7) + return ES_OK; + break; + + case SVM_EXIT_VMMCALL: + if (opcode == 0x010f && modrm == 0xd9) + return ES_OK; + + break; + + case SVM_EXIT_WRITE_DR7: + if (opcode == 0x230f && + X86_MODRM_REG(ctxt->insn.modrm.value) == 7) + return ES_OK; + break; + + case SVM_EXIT_WBINVD: + if (opcode == 0x90f) + return ES_OK; + break; + + default: + break; + } + + sev_printk(KERN_ERR "Wrong/unhandled opcode bytes: 0x%x, exit_code: 0x%lx, rIP: 0x%lx\n", + opcode, exit_code, ctxt->regs->ip); + + return ES_UNSUPPORTED; +} diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index c672858..1ec7533 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1752,7 +1752,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt, struct ghcb *ghcb, unsigned long exit_code) { - enum es_result result; + enum es_result result = vc_check_opcode_bytes(ctxt, exit_code); + + if (result != ES_OK) + return result; switch (exit_code) { case SVM_EXIT_READ_DR7: