Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp1128503ybb; Fri, 20 Mar 2020 13:57:35 -0700 (PDT) X-Google-Smtp-Source: ADFU+vs6+sbEJDQadC7ROXzuAbaefbgPAIZNzKxxR2dSy7dHhtkz9MVrxdh7iy6GbFvdcvvu/KEP X-Received: by 2002:a05:6830:2415:: with SMTP id j21mr8533464ots.93.1584737855389; Fri, 20 Mar 2020 13:57:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584737855; cv=none; d=google.com; s=arc-20160816; b=BxfPupVoytQ6dmCnS/xP52byXttW77e+te/kk346pIaqzlOkqFQiw5jHbZUxFJF9km FwmUdNpOjJLejCFpb2jU8xfSYgtJ3tzXaUiPS5ep7SeDhHz09GD17OQ8jQBBYOh5NEXG hj/QEkp1Y2GLtA0thElilfCVV52sBljkXR+V9RKkux3zZ+N6BfHxxqsnpL3701oBt53R RXumatQPwXsYpSRZsEjJfCn7PkFel6PnA3ri4JqnGEdMBocx/a2FIh1uuSKbYaNYS/++ 1BLkuADBzNfoLJRf/e8SmiX3B4aY4fF4J1XxDIx0i223wb9tSroxdEM8+4jbH6lB+/9e ONXw== 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=g/QtakzI+Jay8MN6D5KMbeDJsvSta5Kyt9yMXeUo93E=; b=Lf7sn5ouNkh3jblPiMu2jcdcYJyP+yTbABFmx4++EOsZ8OA41q0euUTTtyWJlYdzOH cL1Uv5NRaR/3fBmmKmUNeyuzqWys3bBupAe7BNkdG5tg6qKqSKAslQaOm7VFax5Saaz5 98p2LZbXc8ciAaJcVJnmd8ilZ1dzxsTcIOr0X5ksiOO0Nmv8fkCwASGClPNPXDZh2wUf EhR9qxMtj/hTehzz+HCK2BsbUJvl6KNaDTVtBOz7rZ2zniH+G+T0K0O8Q9164IXPO+jQ lPvD5ruW4JBghYVTTkuTfGGcaulZUK+5ksc8dtsC7cFuDJ8T7zUL8DptpRxYstrCsKz6 fcwA== 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 x26si3438337otp.61.2020.03.20.13.57.23; Fri, 20 Mar 2020 13:57:35 -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 S1727457AbgCTU4c (ORCPT + 99 others); Fri, 20 Mar 2020 16:56:32 -0400 Received: from mga11.intel.com ([192.55.52.93]:32133 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726880AbgCTUzs (ORCPT ); Fri, 20 Mar 2020 16:55:48 -0400 IronPort-SDR: zBOXt9AIK2pA/NfnUCLlmB88OZU4UxuqvreV8/at98T3h62uRy2txAGSjOODIIlCzO9UroCUrg UeqCv10w1bkA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Mar 2020 13:55:47 -0700 IronPort-SDR: OUj5t6iz8d0eZtivRlx4QNpH4zqb8RymZ0YbZYk/OPYGii3ES4xsc5w3DGMWvQqAlBRFaZvkyg 9q4P3wc+4UTw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,285,1580803200"; d="scan'208";a="280543317" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.202]) by fmsmga002.fm.intel.com with ESMTP; 20 Mar 2020 13:55:47 -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, Qian Cai , Peter Xu Subject: [PATCH 1/7] KVM: Fix out of range accesses to memslots Date: Fri, 20 Mar 2020 13:55:40 -0700 Message-Id: <20200320205546.2396-2-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200320205546.2396-1-sean.j.christopherson@intel.com> References: <20200320205546.2396-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 Reset the LRU slot if it becomes invalid when deleting a memslot to fix an out-of-bounds/use-after-free access when searching through memslots. Explicitly check for there being no used slots in search_memslots(), and in the caller of s390's approximation variant. Fixes: 36947254e5f9 ("KVM: Dynamically size memslot array based on number of used slots") Reported-by: Qian Cai Cc: Peter Xu Signed-off-by: Sean Christopherson --- arch/s390/kvm/kvm-s390.c | 3 +++ include/linux/kvm_host.h | 3 +++ virt/kvm/kvm_main.c | 3 +++ 3 files changed, 9 insertions(+) diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c index 807ed6d722dd..cb15fdda1fee 100644 --- a/arch/s390/kvm/kvm-s390.c +++ b/arch/s390/kvm/kvm-s390.c @@ -2002,6 +2002,9 @@ static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, struct kvm_memslots *slots = kvm_memslots(kvm); struct kvm_memory_slot *ms; + if (unlikely(!slots->used_slots)) + return 0; + cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); ms = gfn_to_memslot(kvm, cur_gfn); args->count = 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 35bc52e187a2..b19dee4ed7d9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1032,6 +1032,9 @@ search_memslots(struct kvm_memslots *slots, gfn_t gfn) int slot = atomic_read(&slots->lru_slot); struct kvm_memory_slot *memslots = slots->memslots; + if (unlikely(!slots->used_slots)) + return NULL; + if (gfn >= memslots[slot].base_gfn && gfn < memslots[slot].base_gfn + memslots[slot].npages) return &memslots[slot]; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 28eae681859f..f744bc603c53 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -882,6 +882,9 @@ static inline void kvm_memslot_delete(struct kvm_memslots *slots, slots->used_slots--; + if (atomic_read(&slots->lru_slot) >= slots->used_slots) + atomic_set(&slots->lru_slot, 0); + for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) { mslots[i] = mslots[i + 1]; slots->id_to_index[mslots[i].id] = i; -- 2.24.1