diff options
| author | Miklos Szeredi <mszeredi@redhat.com> | 2026-11-03 17:19:10 +0100 |
|---|---|---|
| committer | Miklos Szeredi <mszeredi@redhat.com> | 2026-11-19 18:25:59 +0100 |
| commit | 6df984631391616dafd9b989a3505d93ca4fd235 (patch) | |
| tree | 4aac4cddaac96b6ee0ae5ea5d252dc5317d17390 /rust/kernel/alloc/allocator | |
| parent | ef925fd700ea8f64ce34225f9fea1d20ae3f3cab (diff) | |
virtiofs: fix use-after-free of fs->tag on device removal
The virtio_fs object is refcounted via its kobject, so a mount can keep it
alive well beyond the lifetime of the virtio device binding. The tag
string, however, was allocated with devm_kmalloc() and is therefore freed
by devres_release_all() as soon as the device is unbound, i.e. right after
virtio_fs_remove() returns (see device_unbind_cleanup() in
drivers/base/dd.c).
virtio_fs_get_tree() looks up the instance, takes a kobject reference and
then drops virtio_fs_mutex before doing the sleepable part of the mount
(allocations, fuse_conn_init(), sget_fc()). A concurrent device removal
can complete in that window:
CPU 0 (mount) CPU 1 (remove)
------------- --------------
virtio_fs_get_tree()
virtio_fs_find_instance()
// reference taken, mutex dropped
virtio_fs_remove()
list_del_init(&fs->list);
// driver core unbinds device
// devres frees fs->tag
virtio_fs_fill_super()
mutex_lock(&virtio_fs_mutex);
if (list_empty(&fs->list))
pr_info(... fs->tag); <-- use-after-free
The virtio_fs object itself is still valid here, only its tag has been
freed behind its back.
Fix the lifetime mismatch at the source: allocate the tag with kmalloc()
and free it from virtio_fs_ktype_release(), so that it lives exactly as
long as the object embedding it. All probe error paths already drop the
final kobject reference, so they are covered. Merely dropping the tag
from the error message would paper over this instance while leaving a
devres pointer embedded in an object that routinely outlives its device.
The other readers of fs->tag are unaffected: tag_show() is fenced off by
the kobject_del() in virtio_fs_remove(), the automatic KOBJ_REMOVE uevent
is emitted from __kobject_del() while the device is still bound, and both
virtio_fs_add_instance() and virtio_fs_find_instance() only walk instances
still on virtio_fs_instances.
Fixes: a62a8ef9d97d ("virtio-fs: add virtiofs filesystem")
Cc: <stable@vger.kernel.org>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-0328b3a1-043b-468e-8425-6a613d91b5ef
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
Diffstat (limited to 'rust/kernel/alloc/allocator')
0 files changed, 0 insertions, 0 deletions
