Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp311065ybb; Tue, 7 Apr 2020 23:41:58 -0700 (PDT) X-Google-Smtp-Source: APiQypKc5TdOal0c/RCKOyolnPnfzncIRgpNKJI7LJCOuyZJ2Qk7bKlOL9BlNn2YQWN572xU7d6A X-Received: by 2002:a9d:6a51:: with SMTP id h17mr4391356otn.247.1586328117934; Tue, 07 Apr 2020 23:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586328117; cv=none; d=google.com; s=arc-20160816; b=zNC91KymXOf9av6uhYugLenNsseusjjCaN4omPnSJz9C7fFlo3X4u2hgwqJQZLLhXL 3Nn5WC78oD1z8z02CZkb0/AWwJg9Jx0tYVTCi/q9JJf8mVem0spyAH9o/xnFpdlWSSRs pOqPnb63LbWDDyFx82KfCXceTa7hDxgw2eN5PM2Tao1IBJ9q2tKN57BiT6g6qcR4ON6O JLHPTazy+Y9iyBxycYEnYRpbSQKnwOAbhl7h6BLrdwMcNEpRy0luyS5i9+jcKVLD77Yb 2wpmIUNRmrPrdLa5nKpXWTYsZ683r6Z3Tb359YhJCCx68aqZyg7g9bouZErhJk74Ki5Z SEbw== 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 :ironport-sdr:ironport-sdr; bh=QFzaTjw3lqSijrjuqBkGhAHeKI1axrV6gg3QxJoW3LI=; b=qVxQcCvmSXT7ORd9ECupEFj+/WJE7E4lxSYf8AomoWR0IpnNbE8s3emnJhONsvuLPm Gme9KYPaeENZDgevxfnzKWqNgO6gQQC4gj6re046VAbNMSN4Fj77YA9hxppdedxdsZWz FLu8/wVsy9wwTFkOkEKc/dCJ290HbKzP47RrP3N+akOIFbSpylr43h2nRoOAJrBdPTlY 5in4QpzSxHw+jc6wjJzrh0L0WEf0GpUCuzyJS/KY2Z26FjjJIEo6QogQ9mD4qRRJuWFY iIsbActHnINk5UYaH94JJHaBKXPiKVI8nBP2mrRJVackV3h9xwhJJ+3nImZo9Phol96l hr7g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m1si1616161oim.76.2020.04.07.23.41.42; Tue, 07 Apr 2020 23:41:57 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726609AbgDHGlB (ORCPT + 99 others); Wed, 8 Apr 2020 02:41:01 -0400 Received: from mga11.intel.com ([192.55.52.93]:58629 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHGlA (ORCPT ); Wed, 8 Apr 2020 02:41:00 -0400 IronPort-SDR: hsczhdo64+ePuI3P9IvcPEUFAHM8AdfFwQcNPeyKNXKD96mw9Fo2SRih1kHhhuGxdU/+cuM9/o H5NatOmDDERA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 23:41:00 -0700 IronPort-SDR: kXTd4oDxUKWyEoclSVSNSkdaL7KhEVHfzJ6FUtRw0GL2tAO7pgYelG74re22M+/MIRlGlIw1Ha U+zWgrp/r1OQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="240207984" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.202]) by orsmga007.jf.intel.com with ESMTP; 07 Apr 2020 23:40:59 -0700 From: Sean Christopherson To: Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: David Hildenbrand , Cornelia Huck , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+d889b59b2bb87d4047a2@syzkaller.appspotmail.com Subject: [PATCH 1/2] KVM: Check validity of resolved slot when searching memslots Date: Tue, 7 Apr 2020 23:40:58 -0700 Message-Id: <20200408064059.8957-2-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200408064059.8957-1-sean.j.christopherson@intel.com> References: <20200408064059.8957-1-sean.j.christopherson@intel.com> 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 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]; -- 2.24.1