Skip to content

Commit e128f82

Browse files
zijun-hugregkh
authored andcommitted
driver core: class: Fix wild pointer dereferences in API class_dev_iter_next()
There are a potential wild pointer dereferences issue regarding APIs class_dev_iter_(init|next|exit)(), as explained by below typical usage: // All members of @iter are wild pointers. struct class_dev_iter iter; // class_dev_iter_init(@iter, @Class, ...) checks parameter @Class for // potential class_to_subsys() error, and it returns void type and does // not initialize its output parameter @iter, so caller can not detect // the error and continues to invoke class_dev_iter_next(@iter) even if // @iter still contains wild pointers. class_dev_iter_init(&iter, ...); // Dereference these wild pointers in @iter here once suffer the error. while (dev = class_dev_iter_next(&iter)) { ... }; // Also dereference these wild pointers here. class_dev_iter_exit(&iter); Actually, all callers of these APIs have such usage pattern in kernel tree. Fix by: - Initialize output parameter @iter by memset() in class_dev_iter_init() and give callers prompt by pr_crit() for the error. - Check if @iter is valid in class_dev_iter_next(). Fixes: 7b884b7 ("driver core: class.c: convert to only use class_to_subsys") Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com> Link: https://lore.kernel.org/r/20250105-class_fix-v6-1-3a2f1768d4d4@quicinc.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 896be78 commit e128f82

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

drivers/base/class.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,12 @@ void class_dev_iter_init(struct class_dev_iter *iter, const struct class *class,
323323
struct subsys_private *sp = class_to_subsys(class);
324324
struct klist_node *start_knode = NULL;
325325

326-
if (!sp)
326+
memset(iter, 0, sizeof(*iter));
327+
if (!sp) {
328+
pr_crit("%s: class %p was not registered yet\n",
329+
__func__, class);
327330
return;
331+
}
328332

329333
if (start)
330334
start_knode = &start->p->knode_class;
@@ -351,6 +355,9 @@ struct device *class_dev_iter_next(struct class_dev_iter *iter)
351355
struct klist_node *knode;
352356
struct device *dev;
353357

358+
if (!iter->sp)
359+
return NULL;
360+
354361
while (1) {
355362
knode = klist_next(&iter->ki);
356363
if (!knode)

0 commit comments

Comments
 (0)