public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: David Stevens <stevensd@chromium.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Yu Zhang <yu.c.zhang@linux.intel.com>,
	Isaku Yamahata <isaku.yamahata@gmail.com>,
	Zhi Wang <zhi.wang.linux@gmail.com>,
	kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org
Subject: [PATCH v9 1/6] KVM: Assert that a page's refcount is elevated when marking accessed/dirty
Date: Mon, 11 Sep 2023 11:16:31 +0900	[thread overview]
Message-ID: <20230911021637.1941096-2-stevensd@google.com> (raw)
In-Reply-To: <20230911021637.1941096-1-stevensd@google.com>

From: Sean Christopherson <seanjc@google.com>

Assert that a page's refcount is elevated, i.e. that _something_ holds a
reference to the page, when KVM marks a page as accessed and/or dirty.
KVM typically doesn't hold a reference to pages that are mapped into the
guest, e.g. to allow page migration, compaction, swap, etc., and instead
relies on mmu_notifiers to react to changes in the primary MMU.

Incorrect handling of mmu_notifier events (or similar mechanisms) can
result in KVM keeping a mapping beyond the lifetime of the backing page,
i.e. can (and often does) result in use-after-free.  Yelling if KVM marks
a freed page as accessed/dirty doesn't prevent badness as KVM usually
only does A/D updates when unmapping memory from the guest, i.e. the
assertion fires well after an underlying bug has occurred, but yelling
does help detect, triage, and debug use-after-free bugs.

Note, the assertion must use page_count(), NOT page_ref_count()!  For
hugepages, the returned struct page may be a tailpage and thus not have
its own refcount.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 virt/kvm/kvm_main.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index d63cf1c4f5a7..ee6090ecb1fe 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2914,6 +2914,19 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_unmap);
 
 static bool kvm_is_ad_tracked_page(struct page *page)
 {
+	/*
+	 * Assert that KVM isn't attempting to mark a freed page as Accessed or
+	 * Dirty, i.e. that KVM's MMU doesn't have a use-after-free bug.  KVM
+	 * (typically) doesn't pin pages that are mapped in KVM's MMU, and
+	 * instead relies on mmu_notifiers to know when a mapping needs to be
+	 * zapped/invalidated.  Unmapping from KVM's MMU must happen _before_
+	 * KVM returns from its mmu_notifier, i.e. the page should have an
+	 * elevated refcount at this point even though KVM doesn't hold a
+	 * reference of its own.
+	 */
+	if (WARN_ON_ONCE(!page_count(page)))
+		return false;
+
 	/*
 	 * Per page-flags.h, pages tagged PG_reserved "should in general not be
 	 * touched (e.g. set dirty) except by its owner".
-- 
2.42.0.283.g2d96d420d3-goog


  reply	other threads:[~2023-09-11  2:16 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-11  2:16 [PATCH v9 0/6] KVM: allow mapping non-refcounted pages David Stevens
2023-09-11  2:16 ` David Stevens [this message]
2023-09-11  2:16 ` [PATCH v9 2/6] KVM: mmu: Introduce __kvm_follow_pfn function David Stevens
2023-10-03 16:54   ` Maxim Levitsky
2024-02-06  1:25     ` Sean Christopherson
2024-02-06  3:16   ` Sean Christopherson
2024-02-13  3:27   ` Sean Christopherson
2024-02-13  3:44   ` Sean Christopherson
2023-09-11  2:16 ` [PATCH v9 3/6] KVM: mmu: Improve handling of non-refcounted pfns David Stevens
2023-10-03 16:54   ` Maxim Levitsky
2024-02-06  2:54     ` Sean Christopherson
2024-02-13  3:44   ` Sean Christopherson
2023-09-11  2:16 ` [PATCH v9 4/6] KVM: Migrate kvm_vcpu_map to __kvm_follow_pfn David Stevens
2023-10-03 16:54   ` Maxim Levitsky
2023-09-11  2:16 ` [PATCH v9 5/6] KVM: x86: Migrate " David Stevens
2023-10-03 16:54   ` Maxim Levitsky
2023-10-03 20:58     ` Sean Christopherson
2023-09-11  2:16 ` [PATCH v9 6/6] KVM: x86/mmu: Handle non-refcounted pages David Stevens
2023-09-18  9:53   ` Dmitry Osipenko
2023-09-19  2:25     ` David Stevens
2023-09-30 13:34       ` Dmitry Osipenko
2023-09-18  9:58   ` Dmitry Osipenko
2023-09-18 11:19     ` Dmitry Osipenko
2023-09-19  2:59       ` David Stevens
2023-09-21 20:06         ` Dmitry Osipenko
2023-09-30 13:34           ` Dmitry Osipenko
2023-09-19  2:31     ` David Stevens
2023-09-21 20:04       ` Dmitry Osipenko
2024-02-06  3:02       ` Sean Christopherson
2023-10-03 16:54   ` Maxim Levitsky
2024-02-06  3:23   ` Sean Christopherson
2023-09-29  5:19 ` [PATCH v9 0/6] KVM: allow mapping " Christoph Hellwig
2023-09-29 16:06   ` Sean Christopherson
2023-10-02  6:25     ` Christoph Hellwig
2024-02-06  3:29       ` Sean Christopherson
2023-10-31  4:30 ` David Stevens
2023-10-31 14:30   ` Sean Christopherson
2023-12-12  1:59     ` David Stevens
2023-12-20  1:37       ` Sean Christopherson
2024-02-06  3:30         ` Sean Christopherson
2024-02-13  3:39           ` Sean Christopherson
2024-02-21  6:05             ` David Stevens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230911021637.1941096-2-stevensd@google.com \
    --to=stevensd@chromium.org \
    --cc=isaku.yamahata@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=yu.c.zhang@linux.intel.com \
    --cc=zhi.wang.linux@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox