Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp327166ybb; Wed, 8 Apr 2020 00:06:25 -0700 (PDT) X-Google-Smtp-Source: APiQypII3B44XXk1g5aRtBWJT4KRJWD/Wes1EVhSXhzDOYA/0J4pF6Kh1i9vbszlsuXKr0s6TE5z X-Received: by 2002:a54:4716:: with SMTP id k22mr1365341oik.131.1586329584989; Wed, 08 Apr 2020 00:06:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586329584; cv=none; d=google.com; s=arc-20160816; b=0CM7OJoLw5wvjnJwRL9rD+j1sLXhaGS65oPasCOfPnnVN6wZPFejkZ6yFPaE+kPiJX Gm5B96T4/q4IRuybMtqDXBESC441wQfNCJHRtvYt58PYE+ZLkF/gt8kx+XX8+gGSYWxo ns0yPHLk337v4E33tk2t8uTa17DMuj2slX+tGPl816+6WufHsyRCS+iwYw3MfxKPawYQ qdA076UhAN+NRGQa1CZhmL6J5JAOk4qK/09VxL4IVr5JAv7CPa3oUIuo1Rw1ka1EwhYg BIIDnyZScAx3dhsRIsMShJxqVlfAx0wfXgpvkRByo0fDaLVcymZYEKmaYZiRJkomJGVf h1xQ== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=PL5NL12CLrgF4s0JIXllKLCGUD/J5TZTVhR34cXuYOE=; b=ODi9QtzwGUrwkeM16aXBqkZ07XrWQiH9AqLFXJ6Csa3pEkZ+Mbn4yXQNCiuaoZoPXi rJflnKyzBph8ano7xi2hPERsrAxvk4V02BFj02jCW6g73oTm+e7J33JKGHr0ePh8h+uY dNx5E8jiv/59xYYVdCGAVFsTjO8Mfxc8depqUYFuTkEZt7VVcwgvYmOIjAcyEkeE+roC MwthwegNPqm7RN+jglLzzCwLB507LJPXKJjIFTiQuQB1TtZYEhoHM1DVklyrz7oGbp0t +3x5hqVCc/RgU5hcH/4Pgx+jSscOtOgVwzAFJqoC8Pcq1n1fENwM5gwjLmzHwo2HQIGr VyjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WWqAWRvC; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u7si2296462oou.82.2020.04.08.00.06.08; Wed, 08 Apr 2020 00:06:24 -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=@redhat.com header.s=mimecast20190719 header.b=WWqAWRvC; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726597AbgDHHEr (ORCPT + 99 others); Wed, 8 Apr 2020 03:04:47 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:25982 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725932AbgDHHEr (ORCPT ); Wed, 8 Apr 2020 03:04:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586329486; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PL5NL12CLrgF4s0JIXllKLCGUD/J5TZTVhR34cXuYOE=; b=WWqAWRvCJ+2+mc5i275Asd/0iOpLGzYGUcXiehptXU55UKe1dsX1yBvl8y2wINmtB228Um YESj1y8XMAebWxf/E8xYPu5TXdb5RbmDSsVkYLnBE1cMRKGO4l+1QdxUvLY34nKaIWjuie 4Q/1XjSfXRqh2T1nKRelpbMvb9dOzKs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-309-xXQiYdcaPGOk9HzBCfDvfQ-1; Wed, 08 Apr 2020 03:04:44 -0400 X-MC-Unique: xXQiYdcaPGOk9HzBCfDvfQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 40E1E7A28B; Wed, 8 Apr 2020 07:04:43 +0000 (UTC) Received: from gondolin (ovpn-113-103.ams2.redhat.com [10.36.113.103]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4F1B5DA60; Wed, 8 Apr 2020 07:04:38 +0000 (UTC) Date: Wed, 8 Apr 2020 09:04:36 +0200 From: Cornelia Huck To: Sean Christopherson Cc: Christian Borntraeger , Janosch Frank , Paolo Bonzini , David Hildenbrand , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+d889b59b2bb87d4047a2@syzkaller.appspotmail.com Subject: Re: [PATCH 1/2] KVM: Check validity of resolved slot when searching memslots Message-ID: <20200408090436.2bd1f303.cohuck@redhat.com> In-Reply-To: <20200408064059.8957-2-sean.j.christopherson@intel.com> References: <20200408064059.8957-1-sean.j.christopherson@intel.com> <20200408064059.8957-2-sean.j.christopherson@intel.com> Organization: Red Hat GmbH MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 7 Apr 2020 23:40:58 -0700 Sean Christopherson wrote: > Check that the resolved slot (somewhat confusingly named 'start') is a > valid/allocated slot before doing the final comparison to see if the > specified gfn resides in the associated slot. The resolved slot can be > invalid if the binary search loop terminated because the search index > was incremented beyond the number of used slots. > > This bug has existed since the binary search algorithm was introduced, > but went unnoticed because KVM statically allocated memory for the max > number of slots, i.e. the access would only be truly out-of-bounds if > all possible slots were allocated and the specified gfn was less than > the base of the lowest memslot. Commit 36947254e5f98 ("KVM: Dynamically > size memslot array based on number of used slots") eliminated the "all > possible slots allocated" condition and made the bug embarrasingly easy > to hit. > > Fixes: 9c1a5d38780e6 ("kvm: optimize GFN to memslot lookup with large slots amount") > Reported-by: syzbot+d889b59b2bb87d4047a2@syzkaller.appspotmail.com > Cc: stable@vger.kernel.org > Signed-off-by: Sean Christopherson > --- > include/linux/kvm_host.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 6d58beb65454..01276e3d01b9 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -1048,7 +1048,7 @@ search_memslots(struct kvm_memslots *slots, gfn_t gfn) > start = slot + 1; > } > > - if (gfn >= memslots[start].base_gfn && > + if (start < slots->used_slots && gfn >= memslots[start].base_gfn && > gfn < memslots[start].base_gfn + memslots[start].npages) { > atomic_set(&slots->lru_slot, start); > return &memslots[start]; Reviewed-by: Cornelia Huck