From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 016AD221F1C for ; Tue, 2 Dec 2025 02:03:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764641022; cv=none; b=tPOWJHJcXFU3JKbfgpbFjU4u7QRAbInluI4RpmWpJt2kRrtcztQicFqwsJfBQgLPcy4vdAlO8R6zEa4iY6bDQc/IieZbwVBlAZVsti8u9g9i/YUEBjSjXcuFOsuZVdlY6kLdUwbfg3O+cP5pWS10msnJAQesrT+VBOtm58HGZ6g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764641022; c=relaxed/simple; bh=kb+WfV9Q7rFZLXc2r0KPnkY0aTdLtzVPJWe+u5PyP1U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=P/AYCOW7EaQl6roarfvCSIxiIWhxzmYjnhWomq9FzFJ3QDdSyzQF68YRWU6sON1cCpmbxqkQgG1w/vPYPwNkpqnT6yxOMOWB4r+KHEpAqaHGwDt+NNnyNnK6/riAE/GaTTITov91LOSAO78p0JH7o+cGUZgp5egvL5bR2bsoyCg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cw8FeHeb; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cw8FeHeb" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-29557f43d56so53786735ad.3 for ; Mon, 01 Dec 2025 18:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764641020; x=1765245820; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=QRLg7QRwKtVVIRI8XsoliW/N9ravr7XFCmLVC3DKNm8=; b=cw8FeHebOiHuHlTK6wiANg3yvped4bwP5YcyCs8rHDN/aMFZKau9rVjRAdI2Qz3fTt EX/do5aJlTVYDJ7/NNKH3gdZKOVsIHWEGVqSRl1x7IW2iLGJCaaU4IuWcsTmrilISlwY 73hJ1TL1Gul6Hw/ADqyZ78OS00TQFXyF+gZqKH2DA33RNYtT5IfnQjVqEwIIY3VChUKw A3saWh0JSBylDbjUVu9AQFOGty/mC273gw6RVS7PKRxy/tCqm1yQbqlvwObBNx4QnB8g O0oShSlx7osKPUMBLJFJX0kuWQHWxqnX6PlbmO4Rh989OpcWqSxSu+tvNgLKlqP+dhki 1w7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764641020; x=1765245820; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QRLg7QRwKtVVIRI8XsoliW/N9ravr7XFCmLVC3DKNm8=; b=ok45VdnnDvCKEO8zuimsftFKMn0jhYZ4cvnte78/GucVgGFPoeRR3D6q0zyXUOWMg7 zhBP1sehKYU+uHUkXsTAVtpO8x9ZEjgyxthxYq+HS80WUTrtyuP0eJ2L17ivy4ZIu0MW SUHnG3a676d48WdGS0CjHbIywLasJwme8wwn5rNRk8yZ09s6OVPVychSD4hZGiBAJida 5WJNbC40WWpzQ62u7avoHUE7mmmRhFUe0zlazg/WqIkrsSe5vZYcDkKyVKkzzBDrpGYx r3p76hFJBPYNaVxd22BZD4QuayCoCo13ZsfB2cCDRX1e1FekeIe7ksYEv4cB7gXpjfdh 74vA== X-Forwarded-Encrypted: i=1; AJvYcCVMuyOgSVs+0RHqaxXRKBjvAhM3ROoeRDiUPHJU2eJoVWQ2bEM8CNseyMpytPAxiYc7ku765edUA9A3dqk=@vger.kernel.org X-Gm-Message-State: AOJu0YyuIvmpcrP5VppsZ7hHgLdpERsQq4W70YaTrTUTbIBsGnYCqSRO QHRPNB+US9SrTlQfGx2kilACGN24SPQV8TFJVvXS8BVjnNtcXkUv7ZOMwcdcMOpdFfeu2Tp5NBF Gq7gO2w== X-Google-Smtp-Source: AGHT+IHDbeeIz8Rdl49NrVuNa4lLNFb3bX31rGFPJ0FFXz94HdMk5/TUzcG5pTPETwwLtkwl+b1ivaovCZQ= X-Received: from plrs22.prod.google.com ([2002:a17:902:b196:b0:292:a8c8:e414]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:da82:b0:269:82a5:f9e9 with SMTP id d9443c01a7336-29bab148972mr313593735ad.29.1764641019996; Mon, 01 Dec 2025 18:03:39 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 1 Dec 2025 18:03:34 -0800 In-Reply-To: <20251202020334.1171351-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251202020334.1171351-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.107.ga0afd4fd5b-goog Message-ID: <20251202020334.1171351-3-seanjc@google.com> Subject: [PATCH 2/2] KVM: Harden and prepare for modifying existing guest_memfd memslots From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Potapenko , Sean Christopherson Content-Type: text/plain; charset="UTF-8" Unbind guest_memfd memslots if KVM commits a MOVE or FLAGS_ONLY memslot change to harden against use-after-free, and to prepare for eventually supporting dirty logging on guest_memfd memslots, at which point FLAGS_ONLY changes will be expected/supported. Add two separate WARNs, once to yell if a guest_memfd memslot is moved (which KVM is never expected to allow/support), and again if the unbind() is triggered, to help detect uAPI goofs prior to deliberately allowing FLAGS_ONLY changes. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8891df136416..f822d3e389b0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1748,6 +1748,12 @@ static void kvm_commit_memory_region(struct kvm *kvm, kvm_free_memslot(kvm, old); break; case KVM_MR_MOVE: + /* + * Moving a guest_memfd memslot isn't supported, and will never + * be supported. + */ + WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD); + fallthrough; case KVM_MR_FLAGS_ONLY: /* * Free the dirty bitmap as needed; the below check encompasses @@ -1756,6 +1762,15 @@ static void kvm_commit_memory_region(struct kvm *kvm, if (old->dirty_bitmap && !new->dirty_bitmap) kvm_destroy_dirty_bitmap(old); + /* + * Unbind the guest_memfd instance as needed; the @new slot has + * already created its own binding. TODO: Drop the WARN when + * dirty logging guest_memfd memslots is supported. Until then, + * flags-only changes on guest_memfd slots should be impossible. + */ + if (WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD)) + kvm_gmem_unbind(old); + /* * The final quirk. Free the detached, old slot, but only its * memory, not any metadata. Metadata, including arch specific -- 2.52.0.107.ga0afd4fd5b-goog