From patchwork Fri Dec 1 13:15:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Magali Lemes X-Patchwork-Id: 1870624 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 4ShYVh2Fjlz1ySd for ; Sat, 2 Dec 2023 00:16:44 +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 1r93Nc-0008AH-Rl; Fri, 01 Dec 2023 13:16:29 +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 1r93NS-00083U-2c for kernel-team@lists.ubuntu.com; Fri, 01 Dec 2023 13:16:18 +0000 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.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 96B2D3F177 for ; Fri, 1 Dec 2023 13:16:17 +0000 (UTC) Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-5c624e68b45so540957a12.3 for ; Fri, 01 Dec 2023 05:16:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701436574; x=1702041374; 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=2Cmwt/3IjiqboAPsRrtFA25WbF4eoxce0pRaU7D7gFg=; b=lOJ0ll1Iqu4LKf7aujJhgcfE+NHTQI2QwHUtF/t6HNeawKApL4A3Lymesp+GsBOzp5 TyLQmrWusIK+77MengoaTXKU/sLv8m82bbSgJ9VmLPSNkGd83G4GY7m/fv4n2/SEU1v7 cThii+YlvVslvmx0RSVJbFiJBnFqxJVH62VJ8iYxz3EO3FAEUxLWeY3y2w8k2RHRiidR 7J/re2fK4/A5kWnlkW4gMeoFGccrhSZWc6pWM0uoszrpyb46Nlbm0qoqNEJ8vXSFMSMJ YRvYgvh1bk0YQugtMfHEl5oXj1PCDmqBS7aSyFAjVVaHGnSa37AkJRaWBdM/v63IIPqu 1WsA== X-Gm-Message-State: AOJu0Yyagwr2WULEvKtxXLqDVrl9oIDiN6nr/DItb1jm6Baiq4v9TDFO 9Dxg194wNaxhVFHduOjXhdNwgDaGjYzX/s94DkKEE4cxY99lP6KFfIS9hjEkfZTPCnkOsFCk8qs nZoHXZ8DtMbRn9rm+UwZ/qMk4Ev1rOp4yNq3DzePiihGX45wJcA== X-Received: by 2002:a05:6a20:144e:b0:153:78c1:c40f with SMTP id a14-20020a056a20144e00b0015378c1c40fmr26680193pzi.15.1701436574441; Fri, 01 Dec 2023 05:16:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IFWOphmUWaJiySpzL2jWBcriFN1EaklcNay4RQFsVuUUp54DoXh2KFdGhF8Zrwpm4rxuH8g1w== X-Received: by 2002:a05:6a20:144e:b0:153:78c1:c40f with SMTP id a14-20020a056a20144e00b0015378c1c40fmr26680176pzi.15.1701436574021; Fri, 01 Dec 2023 05:16:14 -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.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 05:16:13 -0800 (PST) From: Magali Lemes To: kernel-team@lists.ubuntu.com Subject: [SRU][Jammy][PATCH 3/3] x86/sev: Check for user-space IOIO pointing to kernel space Date: Fri, 1 Dec 2023 10:15:59 -0300 Message-Id: <20231201131601.1146971-6-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 2f6f5430316f..6728e56bf8b9 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -110,6 +110,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 #undef __pa #define __init diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 9af97213a21a..05707b7b0b44 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -213,6 +213,23 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); } +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, @@ -220,7 +237,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); @@ -241,7 +263,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);