OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work()
@ 2026-11-30 13:19 Denis V. Lunev
  2026-11-30 13:52 ` Konstantin Khorenko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Denis V. Lunev @ 2026-11-30 13:19 UTC (permalink / raw)
  To: khorenko, ptikhomirov, vasileios.almpanis; +Cc: devel

From: Denis V. Lunev <den@openvz.org>

fs_send_uevent() queues a work item that carries plain pointers to the
superblock and to the filesystem kobject. Nothing keeps them alive
until the worker runs; each filesystem is expected to flush
fs_events_wq before it frees them. That cannot hold for ERROR and
ABORT events, which are raised from error handlers, for example by
ext4_put_super() when the journal cannot be written on unmount. The
event is queued after the only flush, the unmount completes, and the
worker walks a freed kobject:

  BUG: kernel NULL pointer dereference, address: 0000000000000000
  RIP: 0010:strlen+0x4/0x30
   fill_kobj_path
   kobject_get_path
   kobject_uevent_env
   fs_send_uevent_work

kobject_uevent_env() allocates with GFP_KERNEL between its checks and
the path walk, so the worker can sleep there and lose the race even
when it has started in time.

Take a kobject reference when the event is queued and drop it when the
worker is done. ext4 and XFS wait for the release of their registered
kobject before freeing the filesystem data (s_kobj_unregister,
xfs_sysfs_del), so the superblock stays valid for as long as a
reference is held. A kobject in zeroed memory, or one whose last
reference is gone, refuses the reference and the event is skipped.
That covers the failed mount paths too, where events used to be queued
for a superblock about to be destroyed.

The reference is taken before the one-shot ERROR and ABORT flags are
tested, so a sender that cannot take it does not consume them. An ext4
error raised during mount, before the kobject is registered, used to
burn the flag on an event that kobject_uevent_env() rejected for lack
of a kset; the first error after the mount is now reported.

An ERROR raised after ext4_unregister_sysfs() finds the kobject still
alive but already removed from sysfs. Its DEVPATH would be a bare
"/<name>", so the worker drops the event instead of sending it. The
check is best effort: it can still race with kobject_del(). ERROR and
ABORT raised while ext4 unmounts are therefore not delivered.

https://virtuozzo.atlassian.net/browse/VSTOR-157864
Feature: fs: udev events support
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
 fs/super.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/fs/super.c b/fs/super.c
index c0c067eb2d8e..99f1dcce409b 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -2326,10 +2326,8 @@ static void fs_send_uevent_work(struct work_struct *w)
 	int ret;
 
 	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
-	if (!env){
-		kfree(e);
-		return;
-	}
+	if (!env)
+		goto out_put;
 	ret = add_uevent_var(env, "FS_TYPE=%s", sb->s_type->name);
 	if (ret)
 		goto out;
@@ -2372,13 +2370,24 @@ static void fs_send_uevent_work(struct work_struct *w)
 	}
 	if (ret)
 		goto out;
+	/* best effort: a removed kobject has no valid DEVPATH */
+	if (!e->kobject->state_in_sysfs)
+		goto out;
 	ret = kobject_uevent_env(e->kobject, kaction, env->envp);
 out:
 	kfree(env);
+out_put:
+	kobject_put(e->kobject);
 	kfree(e);
 }
 
-/* fs_send_uevent - prepare and schedule event submission */
+/*
+ * fs_send_uevent - prepare and schedule event submission
+ *
+ * The kobject must sit in zeroed memory until it is initialised, so that
+ * an unregistered one has no reference to take, and the filesystem must
+ * wait for its release before it frees sb.
+ */
 void fs_send_uevent(struct super_block *sb, struct kobject *kobj,
 		    enum fs_event_type action)
 {
@@ -2386,21 +2395,25 @@ void fs_send_uevent(struct super_block *sb, struct kobject *kobj,
 
 	if (!fs_events_wq)
 		return;
-	if (action == FS_UA_ERROR && xchg(&sb->s_err_event_sent, 1))
+	if (!kobject_get_unless_zero(kobj))
 		return;
+	if (action == FS_UA_ERROR && xchg(&sb->s_err_event_sent, 1))
+		goto out_put;
 	if (action == FS_UA_ABORT && xchg(&sb->s_abrt_event_sent, 1))
-		return;
+		goto out_put;
 
 	e = kzalloc(sizeof(*e), GFP_NOIO);
 	if (!e)
-		return;
+		goto out_put;
 
-	/* Do not forget to flush fs_events_wq before you kill sb */
 	e->sb = sb;
 	e->action = action;
 	e->kobject = kobj;
 	INIT_WORK(&e->work, fs_send_uevent_work);
 	queue_work(fs_events_wq, &e->work);
+	return;
+out_put:
+	kobject_put(kobj);
 }
 EXPORT_SYMBOL(fs_send_uevent);
 
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work()
  2026-11-30 13:19 [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work() Denis V. Lunev
@ 2026-11-30 13:52 ` Konstantin Khorenko
  2026-11-30 13:52 ` Konstantin Khorenko
  2026-11-30 14:03 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Khorenko @ 2026-11-30 13:52 UTC (permalink / raw)
  To: Denis V. Lunev; +Cc: devel

Applying with these fixes on my side:
- added: Fixes: 44897dc519b2 ("fs: Introduce fs_events_wq to use
  instead ext4's rsv_conversion_wq")

On Mon, 30 Nov 2026 14:19:52 +0100, Denis V. Lunev wrote:
> fs_send_uevent() queues a work item that carries plain pointers to the
> superblock and to the filesystem kobject. Nothing keeps them alive
> until the worker runs; each filesystem is expected to flush
> fs_events_wq before it frees them.

Before 44897dc519b2 ext4 queued the event on its own
rsv_conversion_wq and dropped it once ext4_put_super() had destroyed
that queue, before the journal goes away. The check became
!fs_events_wq, which never holds after boot, so a late ERROR is
queued and outlives the sbi.

> -	/* Do not forget to flush fs_events_wq before you kill sb */

No change needed, a note for later: the flushes in ext4_put_super()
and xfs_fs_put_super() are still needed, now to deliver UMOUNT.
Without them kobject_del() clears state_in_sysfs before the worker
runs and the new check drops UMOUNT silently. Worth keeping in mind
if someone wants to remove them as obsolete.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work()
  2026-11-30 13:19 [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work() Denis V. Lunev
  2026-11-30 13:52 ` Konstantin Khorenko
@ 2026-11-30 13:52 ` Konstantin Khorenko
  2026-11-30 14:03 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Khorenko @ 2026-11-30 13:52 UTC (permalink / raw)
  To: ptikhomirov, vasileios.almpanis, Denis V. Lunev; +Cc: devel

On Mon, 30 Nov 2026 14:19:52 +0100, Denis V. Lunev wrote:
> fs: fix use-after-free in fs_send_uevent_work()

Applied to branch-rh10-6.12.0-211.75.1.26.x.vz10-ovz, will be released in rh10-6.12.0-211.75.1.26.6.vz10, thanks!

[1/1] fs: fix use-after-free in fs_send_uevent_work()
      https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/5aee661ed634

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work()
  2026-11-30 13:19 [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work() Denis V. Lunev
  2026-11-30 13:52 ` Konstantin Khorenko
  2026-11-30 13:52 ` Konstantin Khorenko
@ 2026-11-30 14:03 ` Konstantin Khorenko
  2 siblings, 0 replies; 4+ messages in thread
From: Konstantin Khorenko @ 2026-11-30 14:03 UTC (permalink / raw)
  To: ptikhomirov, vasileios.almpanis, Denis V. Lunev; +Cc: devel

On Mon, 30 Nov 2026 14:19:52 +0100, Denis V. Lunev wrote:
> fs: fix use-after-free in fs_send_uevent_work()

Applied to branch-rh10-6.12.0-211.75.1.24.x.vz10-ovz, will be released in rh10-6.12.0-211.75.1.24.5.vz10, thanks!

[1/1] fs: fix use-after-free in fs_send_uevent_work()
      https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/c34f60946881

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 14:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 13:19 [Devel] [PATCH hci-8.2] fs: fix use-after-free in fs_send_uevent_work() Denis V. Lunev
2026-11-30 13:52 ` Konstantin Khorenko
2026-11-30 13:52 ` Konstantin Khorenko
2026-11-30 14:03 ` Konstantin Khorenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox