Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp224455rbe; Wed, 28 Feb 2024 18:58:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXKl7H2dnXlHjMwT6VZGGVtFOud21UNCpubyYgrOdLB3OiZ13saNI8ou91zT7I+KbwCC5X7oQLmpmzsPA2sgj8eKZG/Z5QPgwmMvaLdGA== X-Google-Smtp-Source: AGHT+IE+Bi4LVbW8BKMzFqFtyuTzmXa/QPc1kd3vDbLBP33heGGrJ6VKL2vsyyujqFzF0Ceb4IKW X-Received: by 2002:a17:902:c412:b0:1dc:aea:10e1 with SMTP id k18-20020a170902c41200b001dc0aea10e1mr975631plk.54.1709175528349; Wed, 28 Feb 2024 18:58:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709175528; cv=pass; d=google.com; s=arc-20160816; b=TMCOHWpTRjJ1XYOmFCePBWVptq5gi1ztiBRJ0Y4OiAP4wFUW5nK+e9TJy/2lI5Ud+C U5EyzFVZdj3jJHF07hDH4dP3MoyTGOo3FoQFVT+JcpcrS51mAK5Gm/H/GAAoZc1a64tw PGXuDvDYdjBzEzBY9SBzgwyflwvRde/QTDG1AnH+k6xLCSOnP1e56Dh/U/n1Y8yc0Zji g/T6z4dTucYPWTlQczkXEYsMBBTZhUcmN8nF9AlyPE+iPtHLf2XjHvcYluIf40WBATA2 CFKnwmp0OjI4KuJUqbH06JyK/hFugLENkYpPZtNeLAyOJ1ZKIDfLAhdY3K1GJ4esJkhm SDqw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; fh=mK71tCayosplD7hKXdVC2WehuaAZDNTKdCAyGa1Fgwk=; b=ojiIFfyKvsRydh9VR2CoSX8XE4Jg2gzKoEXizrExMzNzJC14cL5WnO4Ee1eLQMOAm9 VhbywBR/fQfQFzAeyXpNmXaLEL5/+gdEQjmtdvV/Cy4UzKeGJL58v2dYchKO0Oe95hs8 F8OFiU/gglcx69PAhHfqvOoKKKNIQnYKht4jQKlcMp6pHCKhDMVsS37GP+gEpSjQ8lnd u4+apEN131ParjICpDNg1fYD+d8a03s1HxJmqMaF41L+6DpiyclkLZu6DDF0C3IfLdC6 h2H5prd74B2zvTzzB299xNIjvpYW9UT/NFi7ncTm9d9r/h3i7igB0kayBe4pLHtR7Vm6 m1Uw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KgjD/AlN"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86065-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86065-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id i4-20020a170902cf0400b001dcd0d28d46si413667plg.50.2024.02.28.18.58.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 18:58:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86065-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KgjD/AlN"; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86065-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86065-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E901DB22806 for ; Thu, 29 Feb 2024 02:58:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ABCC0376F1; Thu, 29 Feb 2024 02:58:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="KgjD/AlN" Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5436B374F7 for ; Thu, 29 Feb 2024 02:58:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175493; cv=none; b=svC6Cm3PM/+YX9+bqUecOnMZnoivUYRDthMHYt481JOe6Wt5LcED85HW8U6xhWDum9LuO9juhRZbalw3PE7iCk4gZ6sWtDulJrdtcB33d2WuuVjrRaBNxhiBiOv0Skn4oMWoc8jjlwn14YRAbUrSB6qzuffISFX6l/yQbzR/yzw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175493; c=relaxed/simple; bh=4TOA+r5FgQu9r7XyUG+jnEXfKZGB1J0B4LAWQX2lpPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i6/6zvs4vRX/e5gxIW+Axuq5ogz8zcI3aEmSDSioGGiodfw1gtH8sTGYLzLED6Lb4N7NctUeGyEf2yN1awhhnqc9UosW86z43smOWXC5whOKbYYDQXr0hoXd9tVf/EnR51SFKNO0Rn5OF130NvtSIav5wrZZdki2sAp6NmjNtlw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=KgjD/AlN; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-6e48153c13aso202777a34.3 for ; Wed, 28 Feb 2024 18:58:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709175491; x=1709780291; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; b=KgjD/AlNkquVx5HzOsnwQTVby5PEbzF8CT9VtOG6+9VTjGFd1/M9KSvgEKOA9xLvbb BY/KVjfOgJj9vGOhDs1ghheKsREuquRCGywg9BMWydxuhwC64noIs18BAvNfI+ewTVlY z5rKjQEsjgaMHTB+CWbvJ3e3Tnr9onosiTpvs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709175491; x=1709780291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h8gBHZBcb2YMWItHZ5v52k75LYEpNNA/oZohojyoTqA=; b=rUJVvPn+Ue1C+Mawin7TN7ag+7kOu9nPMJJ+1bwJXys/s7PnDGCQMt3finHXQXOINY YQNGtMd/OPnwrIVw9Mg2gZmR28tX67lstfS0/g3eKKeDX1/L5PY3dUJwKx7rzqHo47m9 mQ85NBgSKJOW3vE7r/jGO7rQSfRb+zEm/r4gEqaTo0jRe4KD29CuPpZAwgQNb9VMk1fZ Sz7BlGinpXC+1Aphwvbbrn9+PObD5WHthU5Bk5TVdjvEEsd3hfn3/ZJg5S+jR3yD3oO8 98S6pAr0YgOhOAkhgPsuZr6fHUEbVBCGFsLFWkjk95tCKbTdPj2RzYZJzfwCnAiLCYDT TAWQ== X-Forwarded-Encrypted: i=1; AJvYcCVc5EMq1DrLXss/WhWabAJdvbretz9pWzRrTfMpZBer3dWfQ/+QgbeKq+A6uVTk9qxNdjvWJQQmO0vMn+TbLUyQ0XECLC8VGzIhQ+hD X-Gm-Message-State: AOJu0YwXQixYshK8e+zqd4UuCJ7LfCZG2GQU9Sk6cHTrcH5rxF86MxZE gRlpXJIRElSIi4XJpxIK9woh+heJyBUHlZprpZ7OjVEbUi5UVeJXo3N1bZk8mw== X-Received: by 2002:a05:6358:6f12:b0:17b:c624:b0a1 with SMTP id r18-20020a0563586f1200b0017bc624b0a1mr1060447rwn.23.1709175491359; Wed, 28 Feb 2024 18:58:11 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:f51:e79e:9056:77ea]) by smtp.gmail.com with UTF8SMTPSA id r37-20020a632065000000b005dcc075d5edsm190825pgm.60.2024.02.28.18.58.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 18:58:10 -0800 (PST) From: David Stevens X-Google-Original-From: David Stevens To: Sean Christopherson , Paolo Bonzini Cc: Yu Zhang , Isaku Yamahata , Zhi Wang , Maxim Levitsky , kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH v11 1/8] KVM: Assert that a page's refcount is elevated when marking accessed/dirty Date: Thu, 29 Feb 2024 11:57:52 +0900 Message-ID: <20240229025759.1187910-2-stevensd@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240229025759.1187910-1-stevensd@google.com> References: <20240229025759.1187910-1-stevensd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sean Christopherson 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 --- 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 10bfc88a69f7..c5e4bf7c48f9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3204,6 +3204,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.44.0.rc1.240.g4c46232300-goog