]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
KVM: s390: Fix incorrect usage of mmu_notifier_register()
authorClaudio Imbrenda <imbrenda@linux.ibm.com>
Mon, 25 Aug 2025 15:18:30 +0000 (17:18 +0200)
committerJanosch Frank <frankja@linux.ibm.com>
Tue, 9 Sep 2025 08:17:29 +0000 (08:17 +0000)
If mmu_notifier_register() fails, for example because a signal was
pending, the mmu_notifier will not be registered. But when the VM gets
destroyed, it will get unregistered anyway and that will cause one
extra mmdrop(), which will eventually cause the mm of the process to
be freed too early, and cause a use-after free.

This bug happens rarely, and only when secure guests are involved.

The solution is to check the return value of mmu_notifier_register()
and return it to the caller (ultimately it will be propagated all the
way to userspace). In case of -EINTR, userspace will try again.

Fixes: ca2fd0609b5d ("KVM: s390: pv: add mmu_notifier")
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Reviewed-by: Christian Borntraeger <borntraeger@linux.ibm.com>
Reviewed-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Christoph Schlameuss <schlameuss@linux.ibm.com>
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
arch/s390/kvm/pv.c

index 25ede8354514f20cd078089d9b73d0d25fdea26a..6ba5a0305e25bc751ef193f649d0a20213b32637 100644 (file)
@@ -624,6 +624,17 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
        int cc, ret;
        u16 dummy;
 
+       /* Add the notifier only once. No races because we hold kvm->lock */
+       if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
+               /* The notifier will be unregistered when the VM is destroyed */
+               kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
+               ret = mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
+               if (ret) {
+                       kvm->arch.pv.mmu_notifier.ops = NULL;
+                       return ret;
+               }
+       }
+
        ret = kvm_s390_pv_alloc_vm(kvm);
        if (ret)
                return ret;
@@ -659,11 +670,6 @@ int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc)
                return -EIO;
        }
        kvm->arch.gmap->guest_handle = uvcb.guest_handle;
-       /* Add the notifier only once. No races because we hold kvm->lock */
-       if (kvm->arch.pv.mmu_notifier.ops != &kvm_s390_pv_mmu_notifier_ops) {
-               kvm->arch.pv.mmu_notifier.ops = &kvm_s390_pv_mmu_notifier_ops;
-               mmu_notifier_register(&kvm->arch.pv.mmu_notifier, kvm->mm);
-       }
        return 0;
 }