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 7d4ee5972338..60569d59cd53 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); -- 2.25.1