Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3512697img; Mon, 25 Mar 2019 11:48:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqwsPJJ/XA8ELh9/8IxKK7wwJJpTuMjvg6GV7hIVMdjxC+8w6zuxOr5OCdB2M3PF5byPpAuG X-Received: by 2002:a17:902:28ea:: with SMTP id f97mr27258771plb.203.1553539709382; Mon, 25 Mar 2019 11:48:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553539709; cv=none; d=google.com; s=arc-20160816; b=ov9xSmvz56+S+eFPnE4PFnPkiETUtli/sqlYRHJ2Xpu8c0BqBf2oR2m1FpTRzvT+H/ LryN70CKP0VWjQTWF6RBCYDukVEUjxfCUw8ryi7embiGjBZ3PUSxCskFlhYUn3gzMNB+ z1y3pLIostC5m5Tu5F3BdpEkWzAXSyF5jPWIk5Nv9davR0vvjOpMlP4yfoTHs1oU+PkM u4wRgIK9EbJrG1N8B1YqB4i5depLE6aUAsqSr7L2wAbPr0hZLS5RD/+znnvlMvcEyuqT B2LZVSOzRktgMcZG2tXYTTP+kyS8ieYPeRbq4X+oSWofpXQFvSjYyaLR7u1ntMPilr68 5Zxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:message-id :subject:cc:to:from:date:dkim-signature; bh=fRdq63OhSxm5cgv8tRdBJK1Sff/Wou+7tzmBvJBIrrU=; b=oYkdTkd4t0OBCVJiypgv8tNQNRS/Kq3VVFUdFEoJRbL6tgtHuV5N/d5lDybBUHHpx0 cNR5lBk0NbNIDtsiBARYE4ZKA+Asvkck8hsGZzQfgXsobFHKzvVDIbbU+Xjf+8vYfg5A b0gdGEkiOPavP1D3GBoKtmPn454vGZtmn+zN+P36DcDtYqlUitLw9luMvCkmrnTNcrcT kPgusJglWvqQpi9lnmB8vmBpEIutZwhz1en5urJsomFdVuLRHQJz8Qb8Et3P3Upi7pEI w0E8tSOBHI/b1Q+BeULbhYlfu6/8DSLxxDrTyQkBuYQgVAWI2A4UEcFchHg4i9JMIWFV H8rA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ApsvyUbV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id d5si15085490pla.312.2019.03.25.11.48.14; Mon, 25 Mar 2019 11:48:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ApsvyUbV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1730311AbfCYSrf (ORCPT + 99 others); Mon, 25 Mar 2019 14:47:35 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:40956 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730212AbfCYSre (ORCPT ); Mon, 25 Mar 2019 14:47:34 -0400 Received: by mail-pg1-f195.google.com with SMTP id u9so6999593pgo.7 for ; Mon, 25 Mar 2019 11:47:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:user-agent:mime-version; bh=fRdq63OhSxm5cgv8tRdBJK1Sff/Wou+7tzmBvJBIrrU=; b=ApsvyUbVLS4sN8mLCNZ0dSvTIL37bi8ZAKmmhCRHCqF/YOYbXxBHnlpBmimIgdrjPZ D8eX4iQR/iV7vwrlu/a1q9PTDnqrdazDIhqfIeyA1Zg47+toNQdIqMFLg3UBE4mpqyVF 2FrrfdiEIWJ8keELyAaOmLJMLXl1PeU+CSezjU1LsmJGKP1IhUR0aL3UrSCyNM2OiUkD 72all7OweA4O7qWPv6WmgeSzrIlLAFy/A0TZ8kcFnQiEZfThjLFe/ff9lcIiU5VFwxI2 04F7YUp1RyoQOaijNWFANSlS+h/QH0+i/ufdYHwY/Pt1O3BitMDRMKBzwwfE7OnBN6FX k1dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:user-agent :mime-version; bh=fRdq63OhSxm5cgv8tRdBJK1Sff/Wou+7tzmBvJBIrrU=; b=ejBjOGCcnw0IzO2ag753AEdXx2CD1Gd0fwQ/BJ/gP8ZPuIVAroo6OboRmeAtwNIsQr 99P3ynWODEbc7bYQwDDb1QENDZN1/ZqPp0AaXodQD2A252ydeRn7+Qsk9W4Z9ey+bB77 XZ2E85sDujDNYsjfUetQXWLcKFxZG53PyB60S309om9HI5FWaRqqZ6yQGHDiquioaNPz dMIWI3py3bO7fIp5L2G+ZJiC2YZ7bVJ8gESeahC4RsikuVoIbWqLdA4/QSTEm4gHRZD9 IkaIbo7wRQpXWVdA7zS85pk+/25++b3cudhfdjHUVvQgsd2HJeYnJjI2JjOvg5MagALI y5yg== X-Gm-Message-State: APjAAAVJ9OLIHjib/Vd4J2oPwzroMnDyY41ElgvkWAYCiJcdmxRqLmbV 5ypPprY+yD3bPbxg1mxvb2vuDg== X-Received: by 2002:a63:c145:: with SMTP id p5mr25246258pgi.248.1553539652886; Mon, 25 Mar 2019 11:47:32 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id f14sm20714146pfn.3.2019.03.25.11.47.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 Mar 2019 11:47:32 -0700 (PDT) Date: Mon, 25 Mar 2019 11:47:31 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Brijesh Singh , Joerg Roedel , Paolo Bonzini , =?UTF-8?Q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= cc: Borislav Petkov , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [patch] KVM: SVM: prevent DBG_DECRYPT and DBG_ENCRYPT overflow Message-ID: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This ensures that the address and length provided to DBG_DECRYPT and DBG_ENCRYPT do not cause an overflow. At the same time, pass the actual number of pages pinned in memory to sev_unpin_memory() as a cleanup. Reported-by: Cfir Cohen Signed-off-by: David Rientjes --- arch/x86/kvm/svm.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -6799,7 +6799,8 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) struct page **src_p, **dst_p; struct kvm_sev_dbg debug; unsigned long n; - int ret, size; + unsigned int size; + int ret; if (!sev_guest(kvm)) return -ENOTTY; @@ -6807,6 +6808,11 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug))) return -EFAULT; + if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr) + return -EINVAL; + if (!debug.dst_uaddr) + return -EINVAL; + vaddr = debug.src_uaddr; size = debug.len; vaddr_end = vaddr + size; @@ -6857,8 +6863,8 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) dst_vaddr, len, &argp->error); - sev_unpin_memory(kvm, src_p, 1); - sev_unpin_memory(kvm, dst_p, 1); + sev_unpin_memory(kvm, src_p, n); + sev_unpin_memory(kvm, dst_p, n); if (ret) goto err;