KVM: arm64: Check range args for pKVM mem transitions
PATCH v3 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 end boundary might therefore be subject to overflow and later checks could be evaded.
Close this loophole with an additional pfn_range_is_valid() check on a per public function basis. Once this check has passed, it is safe to convert pfn and nr_pages into a phys_addr_t and a size.
host_unshare_guest transition is already protected via __check_host_shared_guest(), while assert_host_shared_guest() callers are already ignoring host checks.
---
v2 -> v3:
* Test range against PA-range and make the func phys specific.
v1 -> v2:
* Also check for (nr_pages * PAGE_SIZE) overflow. (Quentin)
* Rename to check_range_args().
arch/arm64/kvm/hyp/nvhe/mem_protect.c +28 -0
@@ -367,6 +367,19 @@ static int host_stage2_unmap_dev_all(void)return kvm_pgtable_stage2_unmap (pgt, addr,BIT (pgt->ia_bits) - addr); } +/* +* Ensure the PFN range is contained within PA-range. +* +* This check is also robust to overflows and is therefore a requirement before +* using a pfn/nr_pages pair from an untrusted source. +*/ +static bool pfn_range_is_valid (u64 pfn,u64 nr_pages) +{ +u64 limit =BIT (kvm_phys_shift (&host_mmu.arch.mmu) -PAGE_SHIFT ); + +return pfn < limit && ((limit - pfn) >= nr_pages); +} +struct kvm_mem_range {u64 start;u64 end;
@@ -776,6 +789,9 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)void *virt =__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();
@@ -804,6 +820,9 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)u64 virt = (u64 )__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();
@@ -887,6 +906,9 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_OWNED );if (!ret)
@@ -902,6 +924,9 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_SHARED_OWNED );if (!ret)
@@ -945,6 +970,9 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpuif (prot & ~KVM_PGTABLE_PROT_RWX )return -EINVAL ; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; + ret =__guest_check_transition_size (phys, ipa, nr_pages, &size);if (ret)return ret;
base-commit: 7ea30958b3054f5e488fa0b33c352723f7ab3a2a
Signature · 1 line
2.51.0.869.ge66316f041-goog
Quoted 28 lines · Vincent Donnefort
There's currently no verification for host issued ranges in most of the pKVM memory transitions. The end boundary might therefore be subject to overflow and later checks could be evaded.
Close this loophole with an additional
pfn_range_is_valid()check on a per public function basis. Once this check has passed, it is safe to convert pfn andnr_pagesinto aphys_addr_tand a size.
host_unshare_guesttransition is already protected via__check_host_shared_guest(), whileassert_host_shared_guest()callers are already ignoring host checks.Signed-off-by Vincent Donnefort <[email protected]>---
v2 -> v3:
* Test range against PA-range and make the func phys specific.v1 -> v2:
* Also check for (nr_pages*PAGE_SIZE) overflow. (Quentin)
* Rename tocheck_range_args().arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_unmap_dev_all(void) · line 368
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -367,6 +367,19 @@ static int host_stage2_unmap_dev_all(void)return kvm_pgtable_stage2_unmap (pgt, addr,BIT (pgt->ia_bits) - addr); }
Hello Vincent,
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0+/* +* Ensure the PFN range is contained within PA-range. +*
arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_unmap_dev_all(void) · line 382
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+* This check is also robust to overflows and is therefore a requirement before +* using a pfn/nr_pages pair from an untrusted source. +*/ +static bool pfn_range_is_valid (u64 pfn,u64 nr_pages) +{ +u64 limit =BIT (kvm_phys_shift (&host_mmu.arch.mmu) -PAGE_SHIFT ); + +return pfn < limit && ((limit - pfn) >= nr_pages); +} +
This newly introduced function is probably fine to be called without the host lock held as long as no one modifies the vtcr field from the host.mmu structure. While
searching I couldn't find a place where this is directly modified so
this is probably fine.
Quoted 44 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +12
-0struct kvm_mem_range {u64 start;u64 end;@@ -776,6 +789,9 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)void *virt =__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();@@ -804,6 +820,9 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)u64 virt = (u64 )__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();@@ -887,6 +906,9 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_OWNED );if (!ret)@@ -902,6 +924,9 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_SHARED_OWNED );if (!ret)
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu · line 975
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0@@ -945,6 +970,9 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpuif (prot & ~KVM_PGTABLE_PROT_RWX )return -EINVAL ; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +
I think we don't need it here because __pkvm_host_share_guest has the __guest_check_transition_size verification in place which limits nr_pages.
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0ret =__guest_check_transition_size (phys, ipa, nr_pages, &size);if (ret)return ret;base-commit:
7ea30958b3054f5e488fa0b33c352723f7ab3a2aSignature · 1 line
2.51.0.869.ge66316f041-goog
Other than that this looks good, thanks
Sebastian
Quoted 101 lines · Sebastian Ene
Quoted 28 lines · Vincent Donnefort
There's currently no verification for host issued ranges in most of the pKVM memory transitions. The end boundary might therefore be subject to overflow and later checks could be evaded.
Close this loophole with an additional
pfn_range_is_valid()check on a per public function basis. Once this check has passed, it is safe to convert pfn andnr_pagesinto aphys_addr_tand a size.
host_unshare_guesttransition is already protected via__check_host_shared_guest(), whileassert_host_shared_guest()callers are already ignoring host checks.Signed-off-by Vincent Donnefort <[email protected]>---
v2 -> v3:
* Test range against PA-range and make the func phys specific.v1 -> v2:
* Also check for (nr_pages*PAGE_SIZE) overflow. (Quentin)
* Rename tocheck_range_args().arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_unmap_dev_all(void) · line 368
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0@@ -367,6 +367,19 @@ static int host_stage2_unmap_dev_all(void)return kvm_pgtable_stage2_unmap (pgt, addr,BIT (pgt->ia_bits) - addr); }Hello Vincent,
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0+/* +* Ensure the PFN range is contained within PA-range. +* arch/arm64/kvm/hyp/nvhe/mem_protect.c static int host_stage2_unmap_dev_all(void) · line 382
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +10
-0+* This check is also robust to overflows and is therefore a requirement before +* using a pfn/nr_pages pair from an untrusted source. +*/ +static bool pfn_range_is_valid (u64 pfn,u64 nr_pages) +{ +u64 limit =BIT (kvm_phys_shift (&host_mmu.arch.mmu) -PAGE_SHIFT ); + +return pfn < limit && ((limit - pfn) >= nr_pages); +} +This newly introduced function is probably fine to be called without the host lock held as long as no one modifies the vtcr field from the host.mmu structure. While
searching I couldn't find a place where this is directly modified so
this is probably fine.Quoted 47 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +12
-0struct kvm_mem_range {u64 start;u64 end;@@ -776,6 +789,9 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages)void *virt =__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();@@ -804,6 +820,9 @@ int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages)u64 virt = (u64 )__hyp_va (phys);int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component ();hyp_lock_component ();@@ -887,6 +906,9 @@ int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_OWNED );if (!ret)@@ -902,6 +924,9 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages)u64 size =PAGE_SIZE * nr_pages;int ret; +if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +host_lock_component (); ret =__host_check_page_state_range (phys, size,PKVM_PAGE_SHARED_OWNED );if (!ret)@@ -945,6 +970,9 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpuif (prot & ~KVM_PGTABLE_PROT_RWX )return -EINVAL ;
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) · line 794
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +3
-0+if (!pfn_range_is_valid (pfn, nr_pages)) +return -EINVAL ; +I think we don't need it here because
__pkvm_host_share_guesthas the__guest_check_transition_sizeverification in place which limitsnr_pages.
__guest_check_transition size will only limit to PMD_SIZE, which can be quite a big number if you consider > 4KiB pages systems. So I believe this is still a loophole worth fixing.
Quoted 5 lines
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0
-0ret =__guest_check_transition_size (phys, ipa, nr_pages, &size);if (ret)return ret;
Quoted 7 lines
Quoted 4 lines
base-commit:
7ea30958b3054f5e488fa0b33c352723f7ab3a2aSignature · 1 line
2.51.0.869.ge66316f041-googOther than that this looks good, thanks
Sebastian
Thanks for having a look at the patch.
Quoted 9 lines · Vincent Donnefort
There's currently no verification for host issued ranges in most of the pKVM memory transitions. The end boundary might therefore be subject to overflow and later checks could be evaded.
Close this loophole with an additional
pfn_range_is_valid()check on a per public function basis. Once this check has passed, it is safe to convert pfn andnr_pagesinto aphys_addr_tand a size.[...]
Applied to fixes, thanks!
[1/1] KVM: arm64: Check range args for pKVM mem transitions
commit: f71f7afd0a0cd3f044cd2f8aba71a1a7229df762
Cheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.