Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp1645887ybs; Mon, 25 May 2020 23:26:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwotZa4f2cqzI/pOhzJpU/WuPSVYO+0kI+EyDxkhoc/jJv8DuKA5bZYQJSxOVVd2h4+4M2o X-Received: by 2002:a50:ed0e:: with SMTP id j14mr11544090eds.52.1590474394950; Mon, 25 May 2020 23:26:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590474394; cv=none; d=google.com; s=arc-20160816; b=OFhprX5Sq4z5kqrjISS3o6JNOWkl+p4lceqkuY70YUAfpewKcr2SMAPp5EvoSn4L3k pfokLhIW4Y9xVPDJnZcSP+dZYUGb971LK5bbAGE3MDuRrupH9Kw8pm1Ci9nmWPlOals6 3zNhSFm5DjXK3B+4XcYv/1VI2kOvJq+SEC2hRx+sqH3W4kRyHxMoSbhbBFC3M0lApnvi C1CnZLwO+3BwnvZ/ZPJJ4x+SjnENQAMwDMK0beuhi8y7tkMXzylxlxkAsvioIzm07dnM cHlFRn5DYxdmmBuRqtUYEy8GxN0ImJFYvZG7znGqqAZwOtzQOQ5RAslZED9JiofaNtnX aXNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=AzbBhbmfNLGU9QQd3KrM41vIMJiGiViVOVN10y9DMkc=; b=STdGMUI2oNfB33YioBt2Amyfz2Q9lU3bFksuOwZ01YndqlAL5E3Q45tDmZ5EE8jr2i CMPCxNK89h8FgHWiUBYP/VHlhjzVa5Ae4sveoOt/4Gfetg4t5BrwaGjuxe88vNkV+kk8 9fY6sOfhHkSR3Z7quoY+9zuYOTktJk6FBXcxtlFeCJWv34Ra/T3t0u14SyKUFpK6gp77 4/PjTRU1jn1ncl8cRDUjFLu/UylkjvdzUCEyA8u3uH4v7FfjRzfSwmNBpHlUwQs1P+/E 8/J0hs0rU5cdwqoRsfTPmMfts/r7vEHxf1nnzpAUYTZu5+ReziXrY7lQtnc7ksN6SSSd lZJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=dpvKsqJh; 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=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v28si209437edd.233.2020.05.25.23.26.11; Mon, 25 May 2020 23:26:34 -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; dkim=pass header.i=@nvidia.com header.s=n1 header.b=dpvKsqJh; 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=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729342AbgEZGWO (ORCPT + 99 others); Tue, 26 May 2020 02:22:14 -0400 Received: from hqnvemgate24.nvidia.com ([216.228.121.143]:13893 "EHLO hqnvemgate24.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725271AbgEZGWN (ORCPT ); Tue, 26 May 2020 02:22:13 -0400 Received: from hqpgpgate102.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Mon, 25 May 2020 23:20:42 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate102.nvidia.com (PGP Universal service); Mon, 25 May 2020 23:22:10 -0700 X-PGP-Universal: processed; by hqpgpgate102.nvidia.com on Mon, 25 May 2020 23:22:10 -0700 Received: from HQMAIL101.nvidia.com (172.20.187.10) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 26 May 2020 06:22:10 +0000 Received: from hqnvemgw03.nvidia.com (10.124.88.68) by HQMAIL101.nvidia.com (172.20.187.10) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 26 May 2020 06:22:10 +0000 Received: from sandstorm.nvidia.com (Not Verified[10.2.58.199]) by hqnvemgw03.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Mon, 25 May 2020 23:22:10 -0700 From: John Hubbard To: LKML CC: Souptick Joarder , John Hubbard , Ingo Molnar , Borislav Petkov , Thomas Gleixner , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , "H . Peter Anvin" , , Subject: [PATCH 1/2] KVM: SVM: fix svn_pin_memory()'s use of get_user_pages_fast() Date: Mon, 25 May 2020 23:22:06 -0700 Message-ID: <20200526062207.1360225-2-jhubbard@nvidia.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200526062207.1360225-1-jhubbard@nvidia.com> References: <20200526062207.1360225-1-jhubbard@nvidia.com> MIME-Version: 1.0 X-NVConfidentiality: public Content-Transfer-Encoding: quoted-printable Content-Type: text/plain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1590474042; bh=AzbBhbmfNLGU9QQd3KrM41vIMJiGiViVOVN10y9DMkc=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:X-NVConfidentiality: Content-Transfer-Encoding:Content-Type; b=dpvKsqJh4h5Z2BhpqEVc3v6UcgU5o/vCA95fTXvUOxYoScXTrr9Lg4O3ueM29978W 4BLIV+gymq+4pv+u32F3pE98YnnwMlbCBi8nUAfIBb5BQ2srnsx2E/De5o3rUbTN0r yqiQDfmNk//62pRKdG76kAB2cKscT0ZjPabQkHn2lxzGmQ8J9912mcvZ8Ou9ENo/j7 X1129T1qjZClMHyuBgxwQpv9HMJ2SDURPlXWVxKX4QEsNb6f4HZRk3folnOwMTaKDY ncopQzo4p78OypaWdCb9t4C+hrm3gwJj9tRxpZTI5yQBMI0ECuIsdQDwuSEsJCw/BF sXOuL3NvR/fGg== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are two problems in svn_pin_memory(): 1) The return value of get_user_pages_fast() is stored in an unsigned long, although the declared return value is of type int. This will not cause any symptoms, but it is misleading. Fix this by changing the type of npinned to "int". 2) The number of pages passed into get_user_pages_fast() is stored in an unsigned long, even though get_user_pages_fast() accepts an int. This means that it is possible to silently overflow the number of pages. Fix this by adding a WARN_ON_ONCE() and an early error return. The npages variable is left as an unsigned long for convenience in checking for overflow. Fixes: 89c505809052 ("KVM: SVM: Add support for KVM_SEV_LAUNCH_UPDATE_DATA = command") Cc: Ingo Molnar Cc: Borislav Petkov Cc: Thomas Gleixner Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: Joerg Roedel Cc: H. Peter Anvin Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: John Hubbard --- arch/x86/kvm/svm/sev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 89f7f3aebd31..9693db1af57c 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -313,7 +313,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, un= signed long uaddr, int write) { struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; - unsigned long npages, npinned, size; + unsigned long npages, size; + int npinned; unsigned long locked, lock_limit; struct page **pages; unsigned long first, last; @@ -333,6 +334,9 @@ static struct page **sev_pin_memory(struct kvm *kvm, un= signed long uaddr, return NULL; } =20 + if (WARN_ON_ONCE(npages > INT_MAX)) + return NULL; + /* Avoid using vmalloc for smaller buffers. */ size =3D npages * sizeof(struct page *); if (size > PAGE_SIZE) --=20 2.26.2