Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp1479869pxb; Sat, 9 Jan 2021 23:14:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJxhKNcqYfGWFqJI8/9OjoGI2u87+/R9KTVi9p1TM84OP2tqID9vlrqszxWo09yoYlQlic29 X-Received: by 2002:a05:6402:22b4:: with SMTP id cx20mr10490072edb.262.1610262846543; Sat, 09 Jan 2021 23:14:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610262846; cv=none; d=google.com; s=arc-20160816; b=cRLCZCpXOIr73qQ1+n51zV1XE1eevb1eWmaTVGC2bCDS9MGVPqmB5pcj5a3R2RC5Bp I+qm9Bqrk5wOUaJ2bXOb7bw0fDcFH6kLSO5RKnE/k/LqG6U3yJvklUiY7zagqXZECiEM rZelNRArp2AuLayDJyI8vUMPY328FzI3kk24cmrjFhGS1F2BMKBOUrL0E6TGFIJ1ihZl DABS2//b/knxTTf2G/QveijXOgPm34DJbElhi9ZEXobvec+wXveEj9NTOMRvgq9z4JJ6 cFxKoCkCEdZnxC+mC93nxP2XF1NomAtsd0zCRSo4FMZ8MW8rremGe1ejZ1I0BhWCOqDu BVSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :sender:dkim-signature; bh=GX5wmdMBIxsyuKXN1f72+Mt3KLOzjKU3/Nv/Rv3H5MQ=; b=H9XRufTN6Y71xk3gUmG95jjUz51ydzvq0avnwA0TLPpZIXh2aYrsJQfovFHRhlmQSA 0Dl/bH0v/D8nk04xF1tm7cDKaTgKqWLmH4cMHGfCjoUe9Ik2OHnkJreHDVtVBCe+Wmgy ZJ9cYKhu/ADPHszPbOeU/1eNMyh6QV2Shjw0dSGTgtad4eGPAw9dPtWE8u5OxIyLna4E aVG/V4Kw63HVX19upNMX0cUAip9jUe8rwfrvRVxvKyYyeE/Cehd96SA0OlfJFTw7FYpC c7UqO8GjfPpNj536Q7pYHCbDfd7gJ8thseW0TKUyJpX8NpPjx4Ssr1eXjESbrJNFavDe iAjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MoG5wiMF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o7si5559517edq.20.2021.01.09.23.13.43; Sat, 09 Jan 2021 23:14:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=MoG5wiMF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726516AbhAJHMH (ORCPT + 99 others); Sun, 10 Jan 2021 02:12:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725783AbhAJHMH (ORCPT ); Sun, 10 Jan 2021 02:12:07 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 950F2C061786 for ; Sat, 9 Jan 2021 23:11:26 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id hg11so8991778pjb.2 for ; Sat, 09 Jan 2021 23:11:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=GX5wmdMBIxsyuKXN1f72+Mt3KLOzjKU3/Nv/Rv3H5MQ=; b=MoG5wiMFDYLMHzeuZw/016RgrckIEvkaXjUrEv25pKY/Y2WPMLHNJzR6EMIgMoErPb iX+Mm7eYc93/f7Y9VmXqH+nr6tuvD6AeI2igdK5FQH3IuWc5wYS7J+p2axNiUOUrDZZx 0/j3xwtqDcvyf2t45MKXp9T4sgmcFnZWCMA70towKZOXwRLHPAChNt48Z1dNPfjH7pCA /1iz+2gnme7tSZWZIUviNe+T5gZLEgXh5nV/p2qLt3wIpCKJJX9XRQCw+Xc0vcBzu1q+ PgvaBvRScwaMKYUFqW/+6B3o8DKD4hGEmeRpTw2H4k1sSxGcpK3Urlo9eX4M4NU6+kJS Nd5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=GX5wmdMBIxsyuKXN1f72+Mt3KLOzjKU3/Nv/Rv3H5MQ=; b=MoYma+FjiN9CIDTVQJ7aTwEaDYppHEj30BOWrnO8DB5WwsIrUmuk4x6C1p97vb0ckK GfZlk5IXHYOCuA+gfTEQCfmMH4/yceItV5lW2gw//DCcpNMTutlZEsRiOjVI80J5GLAG qJyr0oAwj2DVnj20vWA2PXxKOq0zHd6RR34eFEoXYysBoVPEiPEMCcsf6a/PBLQTg74a s2nDqnpK8HPgpYB8efIuHS9OmZoXNmCCjxo4W50wz56YkiWt/TBU5EXZosuAvyi/FIrJ 0JNyr8hBgPYwTx5FU3I964GI6cUAAfaQDSuc6JVOjmuDO3TuTuzRAm2qwz1AXjVpo519 zjRQ== X-Gm-Message-State: AOAM531+jyn7TNxbMHagegTrxhzVekskK8jxAps3PuCC8PzabtHMM/nh GjXxzczSkdWmJNy2FQYoGUJKJzDoZ3Y= Sender: "baekhw via sendgmr" X-Received: from heavenly0xe8.kir.corp.google.com ([2620:0:1008:11:f693:9fff:feea:d503]) (user=baekhw job=sendgmr) by 2002:a62:ee03:0:b029:1a9:cc29:7d1f with SMTP id e3-20020a62ee030000b02901a9cc297d1fmr11136072pfi.24.1610262685608; Sat, 09 Jan 2021 23:11:25 -0800 (PST) Date: Sat, 9 Jan 2021 23:11:02 -0800 Message-Id: <20210110071102.2576186-1-baekhw@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH V2] x86/sev-es: Fix SEV-ES #VC handler for string port IO From: "Hyunwook (Wooky) Baek" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: Joerg Roedel , Tom Lendacky , David Rientjes , Sean Christopherson , linux-kernel@vger.kernel.org, x86@kernel.org, "Hyunwook (Wooky) Baek" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Don't assume dest/source buffers are userspace addresses when manually copying data for string I/O or MOVS MMIO, as {get,put}_user() will fail if handed a kernel address and ultimately lead to a kernel panic. Signed-off-by: Hyunwook (Wooky) Baek Acked-by: David Rientjes --- This patch is tested by invoking INSB/OUTSB instructions in kernel space in a SEV-ES-enabled VM. Without the patch, the kernel crashed with the following message: "SEV-ES: Unsupported exception in #VC instruction emulation - can't continue" With the patch, the instructions successfully read/wrote the string from/to the I/O port. arch/x86/kernel/sev-es.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index 0bd1a0fc587e..ab31c34ba508 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -286,6 +286,12 @@ static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, u16 d2; u8 d1; + /* If instruction ran in kernel mode and the I/O buffer is in kernel space */ + if (!user_mode(ctxt->regs) && !access_ok(target, size)) { + memcpy(dst, buf, size); + return ES_OK; + } + switch (size) { case 1: memcpy(&d1, buf, 1); @@ -335,6 +341,12 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, u16 d2; u8 d1; + /* If instruction ran in kernel mode and the I/O buffer is in kernel space */ + if (!user_mode(ctxt->regs) && !access_ok(s, size)) { + memcpy(buf, src, size); + return ES_OK; + } + switch (size) { case 1: if (get_user(d1, s)) -- 2.30.0.284.gd98b1dd5eaa7-goog