KVM: arm64: Don't inherit exec permission across page-table levels
Subsystems (3)
Will Deacon
maintainerPATCH 1/1 KVM: arm64: Don't inherit exec permission across page-table levels
If a stage-2 page-table contains an executable, read-only mapping at the pte level (e.g. due to dirty logging being enabled), a subsequent write fault to the same page which tries to install a larger block mapping (e.g. due to dirty logging having been disabled) will erroneously inherit the exec permission and consequently skip I-cache invalidation for the rest of the block.
Ensure that exec permission is only inherited by write faults when the new mapping is of the same size as the existing one. A subsequent instruction abort will result in I-cache invalidation for the entire block mapping.
---
Found by code inspection, rather than something actually going wrong.
arch/arm64/kvm/mmu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/arm64/kvm/mmu.c +5 -4
@@ -1326,7 +1326,7 @@ static bool stage2_get_leaf_entry(struct kvm *kvm, phys_addr_t addr,return true; } -static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr) +static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr,unsigned long sz) {pud_t *pudp;pmd_t *pmdp;
@@ -1338,9 +1338,9 @@ static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)return false;if (pudp) -return kvm_s2pud_exec (pudp); +return sz ==PUD_SIZE &&kvm_s2pud_exec (pudp);else if (pmdp) -return kvm_s2pmd_exec (pmdp); +return sz ==PMD_SIZE &&kvm_s2pmd_exec (pmdp);else return kvm_s2pte_exec (ptep); }
@@ -1958,7 +1958,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,* execute permissions, and we preserve whatever we have. */ needs_exec = exec_fault || - (fault_status ==FSC_PERM &&stage2_is_exec (kvm, fault_ipa)); + (fault_status ==FSC_PERM && +stage2_is_exec (kvm, fault_ipa, vma_pagesize));if (vma_pagesize ==PUD_SIZE ) {pud_t new_pud =kvm_pfn_pud (pfn, mem_type);
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
Hey Will,
Quoted 14 lines · Will Deacon
arch/arm64/kvm/mmu.c +1
-1@@ -1326,7 +1326,7 @@ static bool stage2_get_leaf_entry(struct kvm *kvm, phys_addr_t addr,return true; } -static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr) +static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr,unsigned long sz) {pud_t *pudp;pmd_t *pmdp;
arch/arm64/kvm/mmu.c static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr) · line 1345
Quoted 10 lines
arch/arm64/kvm/mmu.c +2
-2@@ -1338,9 +1338,9 @@ static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)return false;if (pudp) -return kvm_s2pud_exec (pudp); +return sz ==PUD_SIZE &&kvm_s2pud_exec (pudp);else if (pmdp) -return kvm_s2pmd_exec (pmdp); +return sz ==PMD_SIZE &&kvm_s2pmd_exec (pmdp);else return kvm_s2pte_exec (ptep);
This wants a 'sz == PAGE_SIZE' check, otherwise you'll happily inherit the exec flag when a PTE has exec rights while you create a block mapping on top.
Also, I think it should be safe to make the PMD and PUD case more permissive, as 'sz <= PMD_SIZE' for instance, as the icache invalidation shouldn't be an issue there? That probably doesn't matter all that much though.
Quoted 14 lines
arch/arm64/kvm/mmu.c +2
-1}@@ -1958,7 +1958,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,* execute permissions, and we preserve whatever we have. */ needs_exec = exec_fault || - (fault_status ==FSC_PERM &&stage2_is_exec (kvm, fault_ipa)); + (fault_status ==FSC_PERM && +stage2_is_exec (kvm, fault_ipa, vma_pagesize));if (vma_pagesize ==PUD_SIZE ) {pud_t new_pud =kvm_pfn_pud (pfn, mem_type);Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
FWIW, I reproduced the issue with a dummy guest accessing memory just the wrong way, and toggling dirty logging at the right moment. And this patch + my suggestion above seems to cure things. So, with the above applied:
Thanks,
Quentin
Will Deacon
maintainerHi Quentin,
Quoted 17 lines · Quentin Perret
Quoted 16 lines · Will Deacon
arch/arm64/kvm/mmu.c +1
-1@@ -1326,7 +1326,7 @@ static bool stage2_get_leaf_entry(struct kvm *kvm, phys_addr_t addr,return true; } -static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr) +static bool stage2_is_exec (struct kvm *kvm,phys_addr_t addr,unsigned long sz) {pud_t *pudp;pmd_t *pmdp;@@ -1338,9 +1338,9 @@ static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)return false;
Quoted 12 lines
arch/arm64/kvm/mmu.c static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr) · line 1345
Quoted 8 lines
arch/arm64/kvm/mmu.c +2
-2if (pudp) -return kvm_s2pud_exec (pudp); +return sz ==PUD_SIZE &&kvm_s2pud_exec (pudp);else if (pmdp) -return kvm_s2pmd_exec (pmdp); +return sz ==PMD_SIZE &&kvm_s2pmd_exec (pmdp);else return kvm_s2pte_exec (ptep);This wants a 'sz ==
PAGE_SIZE' check, otherwise you'll happily inherit the exec flag when a PTE has exec rights while you create a block mapping on top.
Nice catch! Somehow I thought we always had PAGE_SIZE in the 'else' case, but that's obviously not true now that you've pointed it out.
Quoted 4 lines
Also, I think it should be safe to make the PMD and PUD case more permissive, as 'sz <=
PMD_SIZE' for instance, as the icache invalidation shouldn't be an issue there? That probably doesn't matter all that much though.
I'll make that change anyway.
Quoted 8 lines
Quoted 8 lines
arch/arm64/kvm/mmu.c +2
-1@@ -1958,7 +1958,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,* execute permissions, and we preserve whatever we have. */ needs_exec = exec_fault || - (fault_status ==FSC_PERM &&stage2_is_exec (kvm, fault_ipa)); + (fault_status ==FSC_PERM && +stage2_is_exec (kvm, fault_ipa, vma_pagesize));
Quoted 9 lines
Quoted 5 lines
arch/arm64/kvm/mmu.c +0
-0if (vma_pagesize ==PUD_SIZE ) {pud_t new_pud =kvm_pfn_pud (pfn, mem_type);Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-googFWIW, I reproduced the issue with a dummy guest accessing memory just the wrong way, and toggling dirty logging at the right moment. And this patch + my suggestion above seems to cure things.
Testing?! It'll never catch on...
Quoted 4 lines
So, with the above applied:
Reviewed-by Quentin Perret <[email protected]>Tested-by Quentin Perret <[email protected]>
Cheers. v2 coming up.
Will