From e8654c06ee7d839bdf22a7a4871db513626296b9 Mon Sep 17 00:00:00 2001 From: Li Jun Date: Mon, 15 Jun 2026 18:15:41 +0800 Subject: [PATCH] block: genhd: Add NULL check for kobject_create_and_add in genhd_device_init The kobject_create_and_add() call in genhd_device_init() may return NULL if memory allocation fails, but the return value was not being checked. This could lead to NULL pointer dereferences in subsequent calls to sysfs_create_link() and sysfs_remove_link() which use block_depr. Add proper error checking and cleanup path to handle the case when kobject_create_and_add() fails. Fixes: 721da5cee9d4 ("driver core: remove CONFIG_SYSFS_DEPRECATED and CONFIG_SYSFS_DEPRECATED_V2") Signed-off-by: Li Jun --- block/genhd.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/block/genhd.c b/block/genhd.c index 7d6854fd28e95..1e8d4889e767d 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1005,7 +1005,15 @@ static int __init genhd_device_init(void) /* create top-level block dir */ block_depr = kobject_create_and_add("block", NULL); + if (!block_depr) { + error = -ENOMEM; + goto out_class_unregister; + } return 0; + +out_class_unregister: + class_unregister(&block_class); + return error; } subsys_initcall(genhd_device_init);