The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Yuho Choi <oss.patchbox@gmail.com>
To: Juergen Gross <jgross@suse.com>,
	Stefano Stabellini <sstabellini@kernel.org>
Cc: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
	xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org,
	Yuho Choi <oss.patchbox@gmail.com>
Subject: [PATCH] xen/privcmd: unregister xenstore notifier on init failure
Date: Sun, 29 Nov 2026 00:41:43 +0000	[thread overview]
Message-ID: <20261129004146.923611-2-oss.patchbox@gmail.com> (raw)

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")
Signed-off-by: Yuho Choi <oss.patchbox@gmail.com>
---
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(-)

diff --git a/drivers/xen/privcmd.c b/drivers/xen/privcmd.c
index 221b31611343..5983cf65b471 100644
--- a/drivers/xen/privcmd.c
+++ b/drivers/xen/privcmd.c
@@ -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
-- 
2.56.1


                 reply	other threads:[~2026-11-29  0:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129004146.923611-2-oss.patchbox@gmail.com \
    --to=oss.patchbox@gmail.com \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox