Skip to contentSkip to conversation

KVM: arm64: Check range args for pKVM mem transitions

PATCH Superseded Vincent Donnefort 3 participants 7 messages Sep 19, 2025 – Oct 3, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: arm64: Check range args for pKVM mem transitions

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +27 -4
index 8957734d6183..65fcd2148f59 100644
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);
+}
+
 int __pkvm_host_share_hyp(u64 pfn)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
@@ -772,10 +780,13 @@ int __pkvm_host_unshare_hyp(u64 pfn)
 int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
-	u64 size = PAGE_SIZE * nr_pages;
 	void *virt = __hyp_va(phys);
+	u64 size;
 	int ret;
 
+	if (!check_range_args(phys, nr_pages, &size))
+		return -EINVAL;
+
 	host_lock_component();
 	hyp_lock_component();
 
@@ -800,10 +811,13 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)
 int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
-	u64 size = PAGE_SIZE * nr_pages;
 	u64 virt = (u64)__hyp_va(phys);
+	u64 size;
 	int ret;
 
+	if (!check_range_args(phys, nr_pages, &size))
+		return -EINVAL;
+
 	host_lock_component();
 	hyp_lock_component();
 
@@ -884,9 +898,12 @@ void hyp_unpin_shared_mem(void *from, void *to)
 int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
-	u64 size = PAGE_SIZE * nr_pages;
+	u64 size;
 	int ret;
 
+	if (!check_range_args(phys, nr_pages, &size))
+		return -EINVAL;
+
 	host_lock_component();
 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
 	if (!ret)
@@ -899,9 +916,12 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)
 int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)
 {
 	u64 phys = hyp_pfn_to_phys(pfn);
-	u64 size = PAGE_SIZE * nr_pages;
+	u64 size;
 	int ret;
 
+	if (!check_range_args(phys, nr_pages, &size))
+		return -EINVAL;
+
 	host_lock_component();
 	ret = __host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_OWNED);
 	if (!ret)
@@ -945,6 +965,9 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu
 	if (prot & ~KVM_PGTABLE_PROT_RWX)
 		return -EINVAL;
 
+	if (!check_range_args(phys, nr_pages, &size))
+		return -EINVAL;
+
 	ret = __guest_check_transition_size(phys, ipa, nr_pages, &size);
 	if (ret)
 		return ret;

base-commit: 8b789f2b7602a818e7c7488c74414fae21392b63

Signature · 1 line
2.51.0.470.ga7dc726c21-goog
maintainer
#1 lore raw
Quoted 25 lines · Vincent Donnefort

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 8957734d6183..65fcd2148f59 100644

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

That's not a problem for PAs, as we will be stuck with at most 56bit PAs for quite a while, but VAs are a different story, and this sort of range check should be valid for VAs as well.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#2 lore raw
Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Vincent Donnefort

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 8957734d6183..65fcd2148f59 100644
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

Do you mean allowing something like start == 0xfffffffffffff000 and size == 4096?

But I guess that would still put all the following checks using "addr + size" at risk. Also, I believe even the code in pgtable.c wouldn't support a such range as it is also using a u64 end boundary.

Quoted 11 lines

That's not a problem for PAs, as we will be stuck with at most 56bit PAs for quite a while, but VAs are a different story, and this sort of range check should be valid for VAs as well.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#3 lore raw
Quoted 43 lines · Vincent Donnefort
Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Vincent Donnefort

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 8957734d6183..65fcd2148f59 100644
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

Quoted 2 lines

Do you mean allowing something like start == 0xfffffffffffff000 and size == 4096?

Yes, this is what I was alluding to on v1.

Quoted 3 lines

But I guess that would still put all the following checks using "addr + size" at risk. Also, I believe even the code in pgtable.c wouldn't support a such range as it is also using a u64 end boundary.

I'm not sure I follow. Ranges are pretty commonly expressed as a range terminated by an exclusive value. This just hasn't been an issue yet as the page table code is only ever dealing with TTBR0 or VTTBR translations.

Anyway, I'd rather these range checks have as few assumptions of the applied address space as possible.

Thanks,
Oliver

#4 lore raw
Quoted 49 lines · Oliver Upton
Quoted 43 lines · Vincent Donnefort
Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Vincent Donnefort

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 8957734d6183..65fcd2148f59 100644
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

Quoted 2 lines

Do you mean allowing something like start == 0xfffffffffffff000 and size == 4096?

Yes, this is what I was alluding to on v1.

Quoted 8 lines
Quoted 3 lines

But I guess that would still put all the following checks using "addr + size" at risk. Also, I believe even the code in pgtable.c wouldn't support a such range as it is also using a u64 end boundary.

I'm not sure I follow. Ranges are pretty commonly expressed as a range terminated by an exclusive value. This just hasn't been an issue yet as the page table code is only ever dealing with TTBR0 or VTTBR translations.

If I do exclude the end boundary, evading checks would be as simple as making sure we overflow the end boundary?

e.g. __pkvm_host_share_guest(phys = 0xfffffffffffff000, size = 4096)

        check_range_allowed_memory(phys, phys + size) /* nop */
	....
	for_each_hyp_page(page, phys, size) {  /* nop */
               ...
	} 
	...
	/* Install a valid mapping to phys */
	kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys, ...) 
Quoted 9 lines

Anyway, I'd rather these range checks have as few assumptions of the applied address space as possible.

Thanks,
Oliver

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

maintainer
#5 lore raw
Quoted 65 lines · Vincent Donnefort
Quoted 49 lines · Oliver Upton
Quoted 43 lines · Vincent Donnefort
Quoted 30 lines · Marc Zyngier
Quoted 28 lines · Vincent Donnefort

There's currently no verification for host issued ranges in most of the pKVM memory transitions. The subsequent end boundary might therefore be subject to overflow and could evade the later checks.

Close this loophole with an additional check_range_args() check on a per public function basis.

host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.

---

v1 -> v2:
- Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
- Rename to check_range_args().

arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 8957734d6183..65fcd2148f59 100644
@@ -712,6 +712,14 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
 	return check_page_state_range(&vm->pgt, addr, size, &d);
 }
 
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

Quoted 2 lines

Do you mean allowing something like start == 0xfffffffffffff000 and size == 4096?

Yes, this is what I was alluding to on v1.

Quoted 8 lines
Quoted 3 lines

But I guess that would still put all the following checks using "addr + size" at risk. Also, I believe even the code in pgtable.c wouldn't support a such range as it is also using a u64 end boundary.

I'm not sure I follow. Ranges are pretty commonly expressed as a range terminated by an exclusive value. This just hasn't been an issue yet as the page table code is only ever dealing with TTBR0 or VTTBR translations.

If I do exclude the end boundary, evading checks would be as simple as making sure we overflow the end boundary?

e.g. __pkvm_host_share_guest(phys = 0xfffffffffffff000, size = 4096)

Quoted 8 lines
        check_range_allowed_memory(phys, phys + size) /* nop */
	....
	for_each_hyp_page(page, phys, size) {  /* nop */
               ...
	} 
	...
	/* Install a valid mapping to phys */
	kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys, ...) 

Why shouldn't this be as simple as this:

static bool check_range_args(u64 start, u64 nr_pages, u64 *size)

{
	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
		return false;

	return start < (start + *size - 1);
}

which correctly deals with the boundary issue?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw

[...]

Quoted 43 lines
Quoted 31 lines
Quoted 17 lines
Quoted 12 lines
Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr, · line 720

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +6 -0
+static bool check_range_args(u64 start, u64 nr_pages, u64 *size)
+{
+	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
+		return false;
+
+	return start < (start + *size);

I will echo Oliver's concern on v1: you probably want to convert the boundary check to be inclusive of the end of the range. Otherwise, a range that ends at the top of the 64bit range will be represented as 0, and fail the check despite being perfectly valid.

Quoted 2 lines

Do you mean allowing something like start == 0xfffffffffffff000 and size == 4096?

Yes, this is what I was alluding to on v1.

Quoted 8 lines
Quoted 3 lines

But I guess that would still put all the following checks using "addr + size" at risk. Also, I believe even the code in pgtable.c wouldn't support a such range as it is also using a u64 end boundary.

I'm not sure I follow. Ranges are pretty commonly expressed as a range terminated by an exclusive value. This just hasn't been an issue yet as the page table code is only ever dealing with TTBR0 or VTTBR translations.

If I do exclude the end boundary, evading checks would be as simple as making sure we overflow the end boundary?

e.g. __pkvm_host_share_guest(phys = 0xfffffffffffff000, size = 4096)

Quoted 8 lines
        check_range_allowed_memory(phys, phys + size) /* nop */
	....
	for_each_hyp_page(page, phys, size) {  /* nop */
               ...
	} 
	...
	/* Install a valid mapping to phys */
	kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys, ...) 

Why shouldn't this be as simple as this:

static bool check_range_args(u64 start, u64 nr_pages, u64 *size)

Quoted 8 lines
{
	if (check_mul_overflow(nr_pages, PAGE_SIZE, size))
		return false;

	return start < (start + *size - 1);
}

which correctly deals with the boundary issue?

I am concerned about allowing ranges that will still overflow "phys + size".

e.g. phys=0xfffffffffffff000 and size=4096 would pass check_range_args().

But in __pkvm_host_share_guest() that would mean:

bypassing check_range_allowed_memory()
bypassing for_each_hyp_page()

but installing a valid mapping to phys with:
kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys, ...)

Quoted 5 lines
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.