Skip to contentSkip to conversation

xen/privcmd: unregister xenstore notifier on init failure

PATCH New Yuho Choi 1 participant 1 message Nov 29 Xen hypervisor interface

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 xen/privcmd: unregister xenstore notifier on init failure

In an unprivileged domU, privcmd_init() calls restrict_driver(), which registers xenstore_notifier when Xenstore is not ready yet. If one of the device registrations after it fails, privcmd_init() returns without unregistering the notifier. With XEN_PRIVCMD=m, the module is then freed while its notifier block is still on the xenstore chain: the next register_xenstore_notifier() walks into freed module memory, and init_restrict() would be called from it once Xenstore becomes ready.

Unregister the notifier on the error path, as privcmd_exit() does since commit cd7e1fef5a1c ("xen/privcmd: unregister xenstore notifier on module exit").

Fixes 453b8fb68f36 ("xen/privcmd: restrict usage in unprivileged domU")

---
Tested on 8dac6df92a2e in a KASAN guest running as an HVM domU under QEMU's KVM-based Xen emulation, with XEN_PRIVCMD=m. To keep Xenstore not ready when the module loads (as when the guest waits for xenstored or a Xenstore stub domain to start), the guest booted with "xen_no_vector_callback initcall_blacklist=platform_driver_init", and a test-only module parameter made the second misc_register() fail. Loading the module with that failure and then loading it again gives, without this patch:

notifier callback init_restrict [xen_privcmd] already registered

  WARNING: kernel/notifier.c:24 at notifier_chain_register+0xd9/0x300, CPU#1: insmod/73

The module came back at the same address, so the chain walk found the stale entry instead of reading freed memory. With this patch applied, there is no warning.

 drivers/xen/privcmd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/xen/privcmd.c +4 -1
index 221b31611343..5983cf65b471 100644
@@ -1750,7 +1750,7 @@ static int __init privcmd_init(void)
 	err = misc_register(&privcmd_dev);
 	if (err != 0) {
 		pr_err("Could not register Xen privcmd device\n");
-		return err;
+		goto err_privcmd;
 	}
 
 	err = misc_register(&xen_privcmdbuf_dev);
@@ -1771,6 +1771,9 @@ static int __init privcmd_init(void)
 	misc_deregister(&xen_privcmdbuf_dev);
 err_privcmdbuf:
 	misc_deregister(&privcmd_dev);
+err_privcmd:
+	if (!xen_initial_domain())
+		unregister_xenstore_notifier(&xenstore_notifier);
 	return err;
 }
 

base-commit: 8dac6df92a2e66e2098bbc69061964cfe7e8375b

Signature · 1 line
2.56.1