Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp100174ybs; Tue, 26 May 2020 04:48:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuudTaEMNpt4xqGCHN4hfdA5ZjRN4kKFOAwNMf3ah1HJii3HVRQmdVy/7ONh0td7ygkJfz X-Received: by 2002:a17:906:410a:: with SMTP id j10mr726774ejk.378.1590493708532; Tue, 26 May 2020 04:48:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590493708; cv=none; d=google.com; s=arc-20160816; b=mNpSOMjxsMllHU/t1GM/vSgMTrHm0918xYptWC2wBYnY5MmNTzXFn77w4XpwxxXyeU VsVz3iweAoDOsVYZyCHyIjerv7MAd5rlqcUX7u+0b6xG26nHz41NCqaPrnXtlaTbjgS6 eQaNv7PfGfeXxUSEAIV3IhlAAVGRlEV8HgRIxjECKTrZODIGmXVkA7nBBZ9t9M9ThPhM FZmpMcJfUz0bcbgVX9qNc2dzGdWsQy5wUo+4hU6H+YnC0jPUTS0kag2fOXQ+no/IKaaU fPott2vQ75yQ7XrkDTvWimFyqgETb0zVUyDxQ/sMfwJafTzdvzUNWyqlWYHQ2fS+lIbS E5lA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=YGp3YMRKFUOtwSIg5Cu6FMpQymGuRv9t/Z4LuF1Cd4U=; b=KX9bQRrpDyfroicRQ9a974Aa5vTf5PRj6nGtJsURknapu0zeYLxVubWIwKQTQkQzzD 8qxmIOyt1QljHl+h97ZFEbWhCFFW9bwInDrpW1IT2MLRPfrOWSNxYz0y8kfE1W7VD9+J gppYvZIqreH4bzMHv1ZGKxX0FEVEOo/aD2slnKw4tt95IUkjQGJlM+PEqhZD4KNp4jmH sSlmSw9wjeZUNrdvmSeNLAwgnKks35kc7bYs7Pv3OuD47y7S/FKYpcKVrtpLuCxa1bP6 akFMTXynbQnUjvjT3urxuHl9jR1Ub83oJco3zWtB2+dME7aErygfdm5jTNSwXPGbi57s XE6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iKAFI+oj; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rl17si10989627ejb.241.2020.05.26.04.48.04; Tue, 26 May 2020 04:48:28 -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=@redhat.com header.s=mimecast20190719 header.b=iKAFI+oj; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731506AbgEZHdh (ORCPT + 99 others); Tue, 26 May 2020 03:33:37 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:22608 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726736AbgEZHdh (ORCPT ); Tue, 26 May 2020 03:33:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590478415; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YGp3YMRKFUOtwSIg5Cu6FMpQymGuRv9t/Z4LuF1Cd4U=; b=iKAFI+oj0DNUJyNWZIhigyAV94nhdGX664EsbB/zBOQMvGine3Zv/wad14hi/M0AIsYzil 3WfJUi4ZA2g9OXxkpO/Li1rNfrPJw90jovU4wF+59mtEgNINItF7bX/MRX5PdzTOaS5Zw5 07qVw8ITi2M6WGcdre3xwhHiq5Ti2Bk= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-322-zhz1EFGDMVeESFFrjTVIZg-1; Tue, 26 May 2020 03:33:31 -0400 X-MC-Unique: zhz1EFGDMVeESFFrjTVIZg-1 Received: by mail-wm1-f71.google.com with SMTP id f9so727286wml.9 for ; Tue, 26 May 2020 00:33:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=YGp3YMRKFUOtwSIg5Cu6FMpQymGuRv9t/Z4LuF1Cd4U=; b=qbAk4xCWVLuHTipGYFjHrAsTpuY2vKDS2rGMpkmXwKnQtBz7NwUl+VIKNWvr5scUR7 T2Z7Csl0+eD0Nh3mzpzcAVo5/uvJ12P0y7NU2AlZROneb4xRJje07YL3tZIi9fBhi0// 43IzhOb8d0znx/GjIy7jwXovcwbE5Du9Yrt/2y2K2uRS+uDGXQg7nfTBeu4zUasl2ILg HfpcEAoEMpqm8zRuax0u/vSefbaL7GlC/bHk2xDYA5+ezz7+93NuHlk/S+UQH+dSz6ps F5V840pZFZZIaMkCx7vU9E9DQcjzd4AzsN+qc6d2OXqWXLEiIJrKPkBeLQ3SQ0iI2KXp 82HA== X-Gm-Message-State: AOAM53033phBRkh0YoYffRTRbwu6fwtvlu5n2Jt3PU/r3g/E2VQRgCDY HI6dADA6THTsVRx8ugzptBy61EAIlmfqU7BLpSPB1JHs3lWfjED1gtMDlkia2XtHcJlTd9QN2+R I6cI5SgI5XaP15iv2AIAY4yfZ X-Received: by 2002:a5d:6986:: with SMTP id g6mr13344946wru.27.1590478410562; Tue, 26 May 2020 00:33:30 -0700 (PDT) X-Received: by 2002:a5d:6986:: with SMTP id g6mr13344925wru.27.1590478410320; Tue, 26 May 2020 00:33:30 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id l17sm6367416wmi.3.2020.05.26.00.33.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 00:33:29 -0700 (PDT) From: Vitaly Kuznetsov To: John Hubbard , LKML Cc: Souptick Joarder , John Hubbard , Ingo Molnar , Borislav Petkov , Thomas Gleixner , Paolo Bonzini , Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel , "H . Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH 1/2] KVM: SVM: fix svn_pin_memory()'s use of get_user_pages_fast() In-Reply-To: <20200526062207.1360225-2-jhubbard@nvidia.com> References: <20200526062207.1360225-1-jhubbard@nvidia.com> <20200526062207.1360225-2-jhubbard@nvidia.com> Date: Tue, 26 May 2020 09:33:27 +0200 Message-ID: <87imgj6th4.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org John Hubbard writes: > 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, unsigned long uaddr, > int write) > { > struct kvm_sev_info *sev = &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, unsigned long uaddr, > return NULL; > } > > + if (WARN_ON_ONCE(npages > INT_MAX)) > + return NULL; > + I bit unrelated to this patch, but callers of sev_pin_memory() treat NULL differently: sev_launch_secret()/svm_register_enc_region() return -ENOMEM sev_dbg_crypt() returns -EFAULT Should we switch to ERR_PTR() to preserve the error? > /* Avoid using vmalloc for smaller buffers. */ > size = npages * sizeof(struct page *); > if (size > PAGE_SIZE) Reviewed-by: Vitaly Kuznetsov -- Vitaly