Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1700981ybh; Tue, 14 Jul 2020 05:12:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdXu+qAfxea6JGrdxOeDovNcKrfabKIzTBqHt9NdOhDkUlkKOqzdf9AcdgJUwiR34ZSaxz X-Received: by 2002:a50:d0cc:: with SMTP id g12mr4255290edf.57.1594728735906; Tue, 14 Jul 2020 05:12:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594728735; cv=none; d=google.com; s=arc-20160816; b=YQ/LELILMHBvdpl2EmJMGjUWzmgbxSAVXhh6Z+NTpoCtZ/Vrv4INRaCju8mBVA6K6Z PTwblofRHBExDhRAWWHDsUygjzfPEl/yPCxnsSJypjUjE71HqrogIsK8rOt0wOawUnVh kpjeKXMj+AdTl3fF1jZ1VViNzyB9WU9RST/f3hEaAYQlRvAoXY8/fo5ZB1H1i/m5QqwM 3ZeAramvrJ3GMHnHh5sAOcU/xnYMWd5Zj8aO9e2cJAFX6vZScmCAQEtYEg8G91bGjT8x 2hZZbvMNlXYNVrbGrO5kS4p1pZraS8OKHsJ5IRES+ocvV8xqEyY4X8E1+ZcjFlBj7Ibr y6ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=78DSAdm5Fswm1jFviZGS9m4rjE7HzUrelacaqNJfXzg=; b=ERa47tAQaH760/vtup5HK+E2HAuuPQxWCSVVXaIb2PWR4QbcroHNe2Bfvj9OXJbmvf xfLR1jwsJ81dhSKxPyR+NKRgJwRV6VE/KQhpMp4GkqyuwQcjM69oAGz74w4/A9S/ZL78 s8r1uiwpPB3FVWtTQaWjtgB6H8nbE3dMas0LcxA0c0znDsQYynlhx9jNcRIzdUiW9yIi T+TUQ6A8ui3hGx/hyhzCSBY9ncbhz0CegWSAGMIxQOulEYaaG5b9CGHmnxR33P1V2YeI 9izjujySCVG/Z8doY8cjI5zKK7iHDLIMHam6Zip3KEnVoiIRKS2TXv/xfBS3loIWngTI vseQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v19si11729422eda.532.2020.07.14.05.11.52; Tue, 14 Jul 2020 05:12:15 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728492AbgGNMLJ (ORCPT + 99 others); Tue, 14 Jul 2020 08:11:09 -0400 Received: from 8bytes.org ([81.169.241.247]:54160 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728442AbgGNMLF (ORCPT ); Tue, 14 Jul 2020 08:11:05 -0400 Received: from cap.home.8bytes.org (p5b006776.dip0.t-ipconnect.de [91.0.103.118]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by theia.8bytes.org (Postfix) with ESMTPSA id 84AE5FBD; Tue, 14 Jul 2020 14:11:00 +0200 (CEST) From: Joerg Roedel To: x86@kernel.org Cc: Joerg Roedel , Joerg Roedel , hpa@zytor.com, Andy Lutomirski , Dave Hansen , Peter Zijlstra , Jiri Slaby , Dan Williams , Tom Lendacky , Juergen Gross , Kees Cook , David Rientjes , Cfir Cohen , Erdem Aktas , Masami Hiramatsu , Mike Stunes , Sean Christopherson , Martin Radev , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v4 50/75] x86/sev-es: Handle instruction fetches from user-space Date: Tue, 14 Jul 2020 14:08:52 +0200 Message-Id: <20200714120917.11253-51-joro@8bytes.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714120917.11253-1-joro@8bytes.org> References: <20200714120917.11253-1-joro@8bytes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel When a #VC exception is triggered by user-space the instruction decoder needs to read the instruction bytes from user addresses. Enhance vc_decode_insn() to safely fetch kernel and user instructions. Signed-off-by: Joerg Roedel --- arch/x86/kernel/sev-es.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/sev-es.c b/arch/x86/kernel/sev-es.c index e04a1868905b..08b66fd5ae9a 100644 --- a/arch/x86/kernel/sev-es.c +++ b/arch/x86/kernel/sev-es.c @@ -247,17 +247,30 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) enum es_result ret; int res; - res = vc_fetch_insn_kernel(ctxt, buffer); - if (unlikely(res == -EFAULT)) { - ctxt->fi.vector = X86_TRAP_PF; - ctxt->fi.error_code = 0; - ctxt->fi.cr2 = ctxt->regs->ip; - return ES_EXCEPTION; + if (user_mode(ctxt->regs)) { + res = insn_fetch_from_user(ctxt->regs, buffer); + if (!res) { + ctxt->fi.vector = X86_TRAP_PF; + ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER; + ctxt->fi.cr2 = ctxt->regs->ip; + return ES_EXCEPTION; + } + + if (!insn_decode(&ctxt->insn, ctxt->regs, buffer, res)) + return ES_DECODE_FAILED; + } else { + res = vc_fetch_insn_kernel(ctxt, buffer); + if (res) { + ctxt->fi.vector = X86_TRAP_PF; + ctxt->fi.error_code = X86_PF_INSTR; + ctxt->fi.cr2 = ctxt->regs->ip; + return ES_EXCEPTION; + } + + insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1); + insn_get_length(&ctxt->insn); } - insn_init(&ctxt->insn, buffer, MAX_INSN_SIZE - res, 1); - insn_get_length(&ctxt->insn); - ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED; return ret; -- 2.27.0