From patchwork Wed Jul 3 22:11:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuxuan Luo X-Patchwork-Id: 1956460 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WDvCQ04JWz1xql for ; Thu, 4 Jul 2024 08:12:17 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sP8Cu-0005I3-N8; Wed, 03 Jul 2024 22:12:08 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sP8Cs-0005HI-HN for kernel-team@lists.ubuntu.com; Wed, 03 Jul 2024 22:12:06 +0000 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 2FF573F6BC for ; Wed, 3 Jul 2024 22:12:06 +0000 (UTC) Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4465f627934so58841601cf.1 for ; Wed, 03 Jul 2024 15:12:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720044724; x=1720649524; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U6Sh7NN4HRJpp1ZsDFIDpKlPtPm02ypIs1cjiJ1iqRg=; b=lb2mYJLpCB6+Lmi1II+hWzwgpTR1DV31M1WkwWcG2VOayNGxhQkH5Di2JXLeROLft8 lPIo2XV4udlH3GAlyArGyOtCa3SGoAxzrDlFLyqBDpUOnxrqff7hVOnrYCZI3QNmqht9 qky5ufaK5A6xPY+pyjej4Rw7PXAAKC31MPzyqmvgA+Kcu6/fRXgvDPAjKhcR1jUkkPhu pebbXAk4LHdCBknd7ZNSJBkyMIWPPYwG1KtZbdC/JaK+hKfaXGYfPJVBQyRYLPXedvEU aZZuc+hpYdMnUpVhBwjlALAyVSRErf4AdCplJ+mucnvPrDZlRW8sAXNLtfqczr8mp0LV ZmpQ== X-Gm-Message-State: AOJu0YwrjcOakYDKai3csZ46f5pUkyxxAg3N18qM8Eu3VFc6FjToUplU lw9In1HTrABCHPAMhQ/uWlgKeLcA4i9E+7GzukvpGGgib6eCQttmyvoaCdlO8+ooulYi1FTDpx8 hzolvPRzdVNGqSfS+Zzvk0+EFaH46k+PC/+53qUXOxIOIh9ayX5O0yM0uGr8bTwQmBv7OEsM5Ug vOU9bVDcqNJA== X-Received: by 2002:ac8:59c8:0:b0:446:59e9:a9ec with SMTP id d75a77b69052e-44662dd4a97mr152853391cf.22.1720044723762; Wed, 03 Jul 2024 15:12:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGUAxkVMXxHU7L/PR+gzJBqMr1Z/0IH4MQCIgr79upeIvJPZ+8vaJt0JmKysK/ST/5JFOgvbQ== X-Received: by 2002:ac8:59c8:0:b0:446:59e9:a9ec with SMTP id d75a77b69052e-44662dd4a97mr152853211cf.22.1720044723338; Wed, 03 Jul 2024 15:12:03 -0700 (PDT) Received: from cache-ubuntu.hsd1.nj.comcast.net ([2001:67c:1562:8007::aac:4795]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4465143c93csm54430991cf.52.2024.07.03.15.12.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 15:12:01 -0700 (PDT) From: Yuxuan Luo To: kernel-team@lists.ubuntu.com Subject: [SRU][Jammy][PATCH 1/2] x86/sev: Harden #VC instruction emulation somewhat Date: Wed, 3 Jul 2024 18:11:55 -0400 Message-Id: <20240703221157.1675604-2-yuxuan.luo@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240703221157.1675604-1-yuxuan.luo@canonical.com> References: <20240703221157.1675604-1-yuxuan.luo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: "Borislav Petkov (AMD)" 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 (backported from commit e3ef461af35a8c74f2f4ce6616491ddb355a208f) [yuxuan.luo: manually applied three chunks for sev-shared.c.] CVE-2024-25742 Signed-off-by: Yuxuan Luo --- arch/x86/boot/compressed/sev.c | 4 ++ arch/x86/kernel/sev-shared.c | 103 ++++++++++++++++++++++++++++++++- arch/x86/kernel/sev.c | 5 +- 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 6728e56bf8b9b..a326d1a1e4765 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -192,6 +192,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 05707b7b0b443..263ee1f400b8d 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -10,8 +10,13 @@ */ #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 +#define sev_printk(fmt, ...) +#define sev_printk_rtl(fmt, ...) #endif static bool __init sev_es_check_cpu_features(void) @@ -148,11 +153,16 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) { unsigned int fn = lower_bits(regs->ax, 32); unsigned long val; + u16 opcode = *(unsigned short *)regs->ip; /* Only CPUID is supported via MSR protocol */ if (exit_code != SVM_EXIT_CPUID) goto fail; + /* Is it really a CPUID insn? */ + if (opcode != 0xa20f) + goto fail; + sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EAX)); VMGEXIT(); val = sev_es_rd_ghcb_msr(); @@ -561,3 +571,92 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, return ES_OK; } + +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 9cafe9931a30a..cbc80ede54abe 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1254,7 +1254,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: From patchwork Wed Jul 3 22:11:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuxuan Luo X-Patchwork-Id: 1956462 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WDvCQ06sHz1xqt for ; Thu, 4 Jul 2024 08:12:17 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1sP8Cx-0005JJ-1W; Wed, 03 Jul 2024 22:12:11 +0000 Received: from smtp-relay-internal-1.internal ([10.131.114.114] helo=smtp-relay-internal-1.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1sP8Cu-0005IA-QL for kernel-team@lists.ubuntu.com; Wed, 03 Jul 2024 22:12:08 +0000 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id A37183F0D2 for ; Wed, 3 Jul 2024 22:12:08 +0000 (UTC) Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-44502b26836so76424171cf.2 for ; Wed, 03 Jul 2024 15:12:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720044727; x=1720649527; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0IVU73u/slTKh8r0Oh3gNCgmDPAslhsliGwpZOQd1gk=; b=GdOVFU64l+l+o2uDmVYKHVNyrSSFjhYVAWCpd0VgriRo6aTx9queDKL6mTna8TFgyf kQQZT7U7BsxypuwXjXB9EpmzD93nZqZftOsY97TMtpLL6ZtXmYVT0m0HDJQb0jZ49aus vhHfqZcpcs71BlKnDHGF+X8nt7G9ON4GV7cDMy2vtEdr9fcHwEukLs2G6orXuJwRIzfq 2xBZIbZtqFXu1YbgtJppORntDL+wClSkwjjqPevad1HaE1ucvoLqSzYiSKV0zZGn50H2 WqnS7rWaA9BGLes4KTijSNmTgW4A3yoE5uS8if2d/vmlM2iXucEplMuIIM8ykCBK7cPn WNGA== X-Gm-Message-State: AOJu0YyM2mMKb4T4a3Ttf0d0p4Xw/tZefbaZn9hFLW/Kc0D9h7eJvNGR zHQXxYHnk7UNOEQwdQsTo2B/n45hKEBEdFbKlrSq9kR9y1f/ThR8uCsvuh8cb8/h5a4H8xZXS43 wsH1gVPvzimdjjA3nk3OAFncOECk9ylJQAWgcIY/AsczZ2xb0bTPJzd+cYoXXz8W8FLTRhc7d/j vbjoeUrSy9Bg== X-Received: by 2002:a05:622a:14a:b0:444:9d95:1b9e with SMTP id d75a77b69052e-44662e3d1d6mr166913041cf.35.1720044726851; Wed, 03 Jul 2024 15:12:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTWdH6qotNJkhwh/XdztU3iqVG6CQCFEJTCt/RBBXQKjC5aRapJcopRaBOmOHwvtG83b1xAA== X-Received: by 2002:a05:622a:14a:b0:444:9d95:1b9e with SMTP id d75a77b69052e-44662e3d1d6mr166912871cf.35.1720044726488; Wed, 03 Jul 2024 15:12:06 -0700 (PDT) Received: from cache-ubuntu.hsd1.nj.comcast.net ([2001:67c:1562:8007::aac:4795]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4465143c93csm54430991cf.52.2024.07.03.15.12.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jul 2024 15:12:04 -0700 (PDT) From: Yuxuan Luo To: kernel-team@lists.ubuntu.com Subject: [SRU][J/N][PATCH 2/2] x86/sev: Check for MWAITX and MONITORX opcodes in the #VC handler Date: Wed, 3 Jul 2024 18:11:57 -0400 Message-Id: <20240703221157.1675604-4-yuxuan.luo@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240703221157.1675604-1-yuxuan.luo@canonical.com> References: <20240703221157.1675604-1-yuxuan.luo@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Tom Lendacky The MWAITX and MONITORX instructions generate the same #VC error code as the MWAIT and MONITOR instructions, respectively. Update the #VC handler opcode checking to also support the MWAITX and MONITORX opcodes. Fixes: e3ef461af35a ("x86/sev: Harden #VC instruction emulation somewhat") Signed-off-by: Tom Lendacky Signed-off-by: Borislav Petkov (AMD) Link: https://lore.kernel.org/r/453d5a7cfb4b9fe818b6fb67f93ae25468bc9e23.1713793161.git.thomas.lendacky@amd.com (cherry picked from commit e70316d17f6ab49a6038ffd115397fd68f8c7be8) CVE-2024-25742 Signed-off-by: Yuxuan Luo --- arch/x86/kernel/sev-shared.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 263ee1f400b8d..d474197e33d4a 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -596,12 +596,14 @@ static enum es_result vc_check_opcode_bytes(struct es_em_ctxt *ctxt, break; case SVM_EXIT_MONITOR: - if (opcode == 0x010f && modrm == 0xc8) + /* MONITOR and MONITORX instructions generate the same error code */ + if (opcode == 0x010f && (modrm == 0xc8 || modrm == 0xfa)) return ES_OK; break; case SVM_EXIT_MWAIT: - if (opcode == 0x010f && modrm == 0xc9) + /* MWAIT and MWAITX instructions generate the same error code */ + if (opcode == 0x010f && (modrm == 0xc9 || modrm == 0xfb)) return ES_OK; break;