Skip to content

Commit 1a683e4

Browse files
kext: sysfs_vfsops: fix hash table buckets allocation leak across mounts/unmounts
In sysfs_vfsops.c, sfsnode_hash_buckets is allocated on the first mount using hashinit(). During sysfs_unmount() or sysfs_structure_free(), sfsnode_hash_buckets is never deallocated (e.g., using hashdestroy() / FREE()). If the kernel module is unmounted, or if sfsnode_hash_buckets is meant to be tied to module lifespan, the memory allocated by hashinit remains leaked on unload Call hashdestroy(sfsnode_hash_buckets, M_CACHE, sfsnode_hash_to_bucket_mask) during teardown/unmount when the mounted instance count drops to zero.
1 parent 0ceff00 commit 1a683e4

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

kext/sysfs_vfsops.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,20 @@ sysfs_unmount(struct mount *mp, __unused int mntflags, __unused vfs_context_t co
244244
sysfs_mp = NULL;
245245

246246
/*
247-
* Decrement mounted instance count.
247+
* Decrement mounted instance count atomically and check
248+
* if this was the last active mount.
248249
*/
249-
OSAddAtomic(-1, &mounted_instance_count);
250+
if (OSAddAtomic(-1, &mounted_instance_count) == 1) {
251+
/*
252+
* OSAddAtomic returns the value PRIOR to the subtraction.
253+
* If the returned value was 1, count is now 0.
254+
* Safe to tear down the shared hash table!
255+
*/
256+
if (sfsnode_hash_buckets != NULL) {
257+
hashdestroy(sfsnode_hash_buckets, M_CACHE, sfsnode_hash_to_bucket_mask);
258+
sfsnode_hash_buckets = NULL;
259+
}
260+
}
250261
}
251262
sysfs_structure_free();
252263

0 commit comments

Comments
 (0)