From patchwork Fri Dec 1 13:16:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magali Lemes X-Patchwork-Id: 1870622 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 4ShYVf3GpNz1ySd for ; Sat, 2 Dec 2023 00:16:42 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1r93Ne-0008Bb-Oy; Fri, 01 Dec 2023 13:16:30 +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 1r93NW-000865-Nt for kernel-team@lists.ubuntu.com; Fri, 01 Dec 2023 13:16:22 +0000 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.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-1.canonical.com (Postfix) with ESMTPS id 7C95D3F18B for ; Fri, 1 Dec 2023 13:16:22 +0000 (UTC) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-6cdeb817ea6so2031250b3a.1 for ; Fri, 01 Dec 2023 05:16:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701436580; x=1702041380; 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=j/IppmZ/3efSjH1Xoa0+ejQgV3tOjRcSILPbLF3Bs9U=; b=cTHv0r/KnoigggTg2/N+UXOIMbxZJy6cyfzOCVKs9R2MNVF+dneAFdMeLLGTj6YxN0 nOYwSidpT/G/x1RmlvVrCO75yDGMBlu1pkLKXefJ5pDfET6eunumErOVBuKLin12UwMc /ZMkYmq4PwkndaJttvzNhtqCW9dSf4rnrT/gCbVmtSafIe1FQvU2hHDOAjKO2bB9GLAV fDoQxgzSVSUnWA+NWVu3ulPx45sATaj0CvNr3prFxqOVEv2LgXRqvUWr6NqdWLM2dfhZ XIaOwzLiNVyjBoV7AcMmj8YQLkEKMHiYJCFfogLkMhYF7VKA2GP+5T4hFFzbrx1sFXD1 GuvQ== X-Gm-Message-State: AOJu0Yzk4az4WBC57+0pqxDk2x4d461xVrCVJrVkqR35RLDChbURMYY/ pUjJgQX9B9jFzVwJmZemJrcPOI4N8mAqboT4FX4MHZw9igg4sr4811wwRuJ/l0UpzsfPnd5mWPP G44iLh4lJPU2yWo4wYGZJD4vsjgRjEdHFVYJz2Z7ex9uRRQmpPw== X-Received: by 2002:a05:6a00:c90:b0:6cd:f75b:a0dd with SMTP id a16-20020a056a000c9000b006cdf75ba0ddmr4197133pfv.21.1701436580528; Fri, 01 Dec 2023 05:16:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnBZBEw9AzzSdFhN9pbQZ+JqAY1Wm/0zpClHl5qZy7VAZyICjuOrcxeKwdBovqANwoHaI7/A== X-Received: by 2002:a05:6a00:c90:b0:6cd:f75b:a0dd with SMTP id a16-20020a056a000c9000b006cdf75ba0ddmr4197089pfv.21.1701436580073; Fri, 01 Dec 2023 05:16:20 -0800 (PST) Received: from magali.. ([2804:7f0:b442:2377:dd30:3fac:53f2:e6fd]) by smtp.gmail.com with ESMTPSA id fb3-20020a056a002d8300b006bde2480806sm2978028pfb.47.2023.12.01.05.16.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 05:16:17 -0800 (PST) From: Magali Lemes To: kernel-team@lists.ubuntu.com Subject: [SRU][Mantic/OEM-6.5][PATCH 3/3] x86/sev: Check for user-space IOIO pointing to kernel space Date: Fri, 1 Dec 2023 10:16:01 -0300 Message-Id: <20231201131601.1146971-8-magali.lemes@canonical.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231201131601.1146971-1-magali.lemes@canonical.com> References: <20231201131601.1146971-1-magali.lemes@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: Joerg Roedel Check the memory operand of INS/OUTS before emulating the instruction. The #VC exception can get raised from user-space, but the memory operand can be manipulated to access kernel memory before the emulation actually begins and after the exception handler has run. [ bp: Massage commit message. ] Fixes: 597cfe48212a ("x86/boot/compressed/64: Setup a GHCB-based VC Exception handler") Reported-by: Tom Dohrmann Signed-off-by: Joerg Roedel Signed-off-by: Borislav Petkov (AMD) Cc: (cherry picked from commit 63e44bc52047f182601e7817da969a105aa1f721) CVE-2023-46813 Signed-off-by: Magali Lemes --- arch/x86/boot/compressed/sev.c | 5 +++++ arch/x86/kernel/sev-shared.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index f9e1e865b4e4..45dd3ca2c737 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -108,6 +108,11 @@ static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t si return ES_OK; } +static bool fault_in_kernel_space(unsigned long address) +{ + return false; +} + #undef __init #define __init diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index bcd77c48be0a..4d729686f992 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -592,6 +592,23 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); } +static enum es_result vc_insn_string_check(struct es_em_ctxt *ctxt, + unsigned long address, + bool write) +{ + if (user_mode(ctxt->regs) && fault_in_kernel_space(address)) { + ctxt->fi.vector = X86_TRAP_PF; + ctxt->fi.error_code = X86_PF_USER; + ctxt->fi.cr2 = address; + if (write) + ctxt->fi.error_code |= X86_PF_WRITE; + + return ES_EXCEPTION; + } + + return ES_OK; +} + static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt, void *src, char *buf, unsigned int data_size, @@ -599,7 +616,12 @@ static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt, bool backwards) { int i, b = backwards ? -1 : 1; - enum es_result ret = ES_OK; + unsigned long address = (unsigned long)src; + enum es_result ret; + + ret = vc_insn_string_check(ctxt, address, false); + if (ret != ES_OK) + return ret; for (i = 0; i < count; i++) { void *s = src + (i * data_size * b); @@ -620,7 +642,12 @@ static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, bool backwards) { int i, s = backwards ? -1 : 1; - enum es_result ret = ES_OK; + unsigned long address = (unsigned long)dst; + enum es_result ret; + + ret = vc_insn_string_check(ctxt, address, true); + if (ret != ES_OK) + return ret; for (i = 0; i < count; i++) { void *d = dst + (i * data_size * s);