Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

driver core: class: properly reference count class_dev_iter()

When class_dev_iter is initialized, the reference count for the subsys
private structure is incremented, but never decremented, causing a
memory leak over time. To resolve this, save off a pointer to the
internal structure into the class_dev_iter structure and then when the
iterator is finished, drop the reference count.

Reported-and-tested-by: syzbot+e7afd76ad060fa0d2605@syzkaller.appspotmail.com
Fixes: 7b884b7f24b4 ("driver core: class.c: convert to only use class_to_subsys")
Reported-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Cc: Alan Stern <stern@rowland.harvard.edu>
Acked-by: Rafael J. Wysocki <rafael@kernel.org>
Tested-by: Mirsad Goran Todorovac <mirsad.todorovac@alu.unizg.hr>
Link: https://lore.kernel.org/r/2023051610-stove-condense-9a77@gregkh
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

+3
+2
drivers/base/class.c
··· 320 320 start_knode = &start->p->knode_class; 321 321 klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); 322 322 iter->type = type; 323 + iter->sp = sp; 323 324 } 324 325 EXPORT_SYMBOL_GPL(class_dev_iter_init); 325 326 ··· 362 361 void class_dev_iter_exit(struct class_dev_iter *iter) 363 362 { 364 363 klist_iter_exit(&iter->ki); 364 + subsys_put(iter->sp); 365 365 } 366 366 EXPORT_SYMBOL_GPL(class_dev_iter_exit); 367 367
+1
include/linux/device/class.h
··· 74 74 struct class_dev_iter { 75 75 struct klist_iter ki; 76 76 const struct device_type *type; 77 + struct subsys_private *sp; 77 78 }; 78 79 79 80 int __must_check class_register(const struct class *class);