KVM_SET_MEMORY_ATTRIBUTES validates the user-provided range only for representability (zero size, 64-bit wrap, alignment, supported attributes) and not for magnitude. kvm_vm_set_mem_attributes() then walks every GFN in the range with xa_reserve(), which materializes the full root-to-leaf path of xa_nodes (~9.3 bytes of kernel memory per GFN) even though the GFNs need no backing memory and no memslots need to exist. On ENOMEM the loop bails out without undoing prior reservations, and the only release path is kvm_destroy_vm(), so an unprivileged user with access to /dev/kvm on a VM with private memory support (sw protected, TDX or SNP) accumulates kernel memory across calls until the host runs out of memory. A single ioctl also holds kvm->slots_lock for the whole loop, blocking KVM_GET_DIRTY_LOG and memslot updates of that VM for its duration. Bound the number of attribute entries materialized per VM to KVM_MEM_ATTR_MAX_GFNS (2^25 GFNs, i.e. 128 GiB of GPA, ~300 MiB of xa_nodes), enforced under kvm->slots_lock after the idempotency early-out. Clear requests never touch the reservation path: erasing an entry cannot allocate, so a clear iterates the present entries in the range and erases them. This closes a bound bypass present in v1 (a clear of a partially populated range used to reserve every GFN in the range with no bound check) and makes clears failure-free by construction. The iteration uses xa_for_each_range(), which takes and releases the RCU lock on every step, so cond_resched() between entries is legal on all configurations, each step re-finds from the previous index (which makes erasing the current entry safe), and no RCU read-side critical section spans the MMU invalidation handlers. The per-VM counter is maintained exactly: the set path uses xa_cmpxchg() (xa_reserve() with the old entry returned) so it charges only the GFNs that were actually absent before the request, clears subtract the exact number of entries erased, and a full rescan runs only on the ENOMEM path where partial reservations are retained. Exact charging also avoids a phantom-budget exhaustion found in review of v2, where overlapping set requests were charged their full range and the overcount never returned after clearing. Build-tested warning-free on v7.2-rc5. Runtime-verified on v7.2-rc5 (KASAN build, isolated VM): growth stops with a clean -ENOSPC at 2^25 GFNs; eight consecutive clears of 2^52-GFN ranges containing a populated GFN leave Slab unchanged; 50 alternating 4 KiB set/clear conversions at a near-full array complete in 0.01 ms each; after an overlap-set cycle followed by clearing everything, the full budget is available again (four chunks re-set succeed, the fifth returns -ENOSPC). Found by an AI-assisted security audit. Fixes: 5a475554db1e ("KVM: Introduce per-page memory attributes") Assisted-by: Claude-Code:GLM-5.3-flash KASAN KCSAN Signed-off-by: David Ballesteros --- --- a/virt/kvm/kvm_main.c 2026-07-26 16:45:48.000000000 -0500 +++ b/virt/kvm/kvm_main.c 2026-09-11 16:27:45.921813177 -0500 @@ -1117,6 +1117,7 @@ xa_init(&kvm->vcpu_array); #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES xa_init(&kvm->mem_attr_array); + kvm->mem_attr_gfn_count = 0; #endif INIT_LIST_HEAD(&kvm->gpc_list); @@ -2533,6 +2534,25 @@ return kvm_arch_pre_set_memory_attributes(kvm, range); } +/* + * Return the number of GFNs with a materialized entry in mem_attr_array. + * Serialization against modifications is provided by kvm->slots_lock. + */ +static unsigned long kvm_count_mem_attr_entries(struct kvm *kvm) +{ + unsigned long count = 0; + unsigned long index; + void *entry; + + xa_for_each(&kvm->mem_attr_array, index, entry) { + count++; + if (need_resched()) + cond_resched(); + } + + return count; +} + /* Set @attributes for the gfn range [@start, @end). */ static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, unsigned long attributes) @@ -2555,7 +2575,10 @@ .may_block = true, }; unsigned long i; + unsigned long freed = 0; + unsigned long newly = 0; void *entry; + void *old; int r = 0; entry = attributes ? xa_mk_value(attributes) : NULL; @@ -2569,13 +2592,66 @@ goto out_unlock; /* + * Clearing never materializes entries: erasing cannot allocate, so + * there is no reservation phase, no bound to enforce and no way to + * fail partway through. Iterate the present entries only (absent + * spans are skipped by the xarray walk) and erase them; xa_erase() + * takes the xa_lock per entry and node frees are RCU-deferred, so + * the iterator is safe under the RCU lock while kvm->slots_lock + * excludes other writers. + */ + if (!attributes) { + unsigned long index; + + kvm_handle_gfn_range(kvm, &pre_set_range); + + /* + * xa_for_each_range() takes and releases the RCU lock on + * every iteration, so cond_resched() below is legal in all + * configurations and each step re-finds from the previous + * index, which makes erasing the current entry safe. + */ + xa_for_each_range(&kvm->mem_attr_array, index, entry, + start, end - 1) { + xa_erase(&kvm->mem_attr_array, index); + freed++; + cond_resched(); + } + + kvm_handle_gfn_range(kvm, &post_set_range); + + /* Exactly @freed entries left the array; no resync needed. */ + kvm->mem_attr_gfn_count -= freed; + mutex_unlock(&kvm->slots_lock); + + return 0; + } + + /* + * Bound the number of materialized GFNs per VM. See the comment on + * KVM_MEM_ATTR_MAX_GFNS. + */ + if (kvm->mem_attr_gfn_count + (end - start) > KVM_MEM_ATTR_MAX_GFNS) { + r = -ENOSPC; + goto out_unlock; + } + + /* * Reserve memory ahead of time to avoid having to deal with failures - * partway through setting the new attributes. + * partway through setting the new attributes. xa_cmpxchg() is + * xa_reserve() with the old entry returned: a NULL old value marks a + * GFN that was absent before this request, so @newly is the exact + * number of entries this ioctl adds to the array. */ for (i = start; i < end; i++) { - r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT); - if (r) - goto out_unlock; + old = xa_cmpxchg(&kvm->mem_attr_array, i, NULL, + XA_ZERO_ENTRY, GFP_KERNEL_ACCOUNT); + if (xa_is_err(old)) { + r = xa_err(old); + goto enomem_resync; + } + if (!old) + newly++; cond_resched(); } @@ -2591,6 +2667,20 @@ kvm_handle_gfn_range(kvm, &post_set_range); + /* Charge exactly the entries this request added. */ + kvm->mem_attr_gfn_count += newly; + mutex_unlock(&kvm->slots_lock); + + return 0; + +enomem_resync: + /* + * Reservations made so far are retained; recount to restore the + * invariant that the counter matches the array population. This is + * the only path that can leave the counter stale, and it is rare by + * construction (it requires ENOMEM under the bound). + */ + kvm->mem_attr_gfn_count = kvm_count_mem_attr_entries(kvm); out_unlock: mutex_unlock(&kvm->slots_lock); --- a/include/linux/kvm_host.h 2026-07-26 16:45:48.000000000 -0500 +++ b/include/linux/kvm_host.h 2026-09-10 14:22:15.505353725 -0500 @@ -573,6 +573,13 @@ * This number must be determined not to exceed such limits. */ #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) +/* + * Hardening bound: maximum number of GFNs with a materialized entry in + * mem_attr_array per VM (~300 MiB of xa_nodes at 2^25). Without it, + * KVM_SET_MEMORY_ATTRIBUTES grows the array without limit (~9.3 bytes of + * kernel memory per GFN) on GFNs with no backing memory. + */ +#define KVM_MEM_ATTR_MAX_GFNS (1UL << 25) /* * Since at idle each memslot belongs to two memslot sets it has to contain @@ -874,6 +881,7 @@ #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES /* Protected by slots_lock (for writes) and RCU (for reads) */ struct xarray mem_attr_array; + unsigned long mem_attr_gfn_count; #endif char stats_id[KVM_STATS_NAME_SIZE]; };