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

drivers: dio: fix possible memory leak in dio_init()

If device_register() returns error, the 'dev' and name needs be
freed. Add a release function, and then call put_device() in the
error path, so the name is freed in kobject_cleanup() and to the
'dev' is freed in release function.

Fixes: 2e4c77bea3d8 ("m68k: dio - Kill warn_unused_result warnings")
Fixes: 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Link: https://lore.kernel.org/r/20221109064036.1835346-1-yangyingliang@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Yang Yingliang and committed by
Greg Kroah-Hartman
e63e9939 30a0b95b

+8
+8
drivers/dio/dio.c
··· 109 109 110 110 #endif /* CONFIG_DIO_CONSTANTS */ 111 111 112 + static void dio_dev_release(struct device *dev) 113 + { 114 + struct dio_dev *ddev = container_of(dev, typeof(struct dio_dev), dev); 115 + kfree(ddev); 116 + } 117 + 112 118 int __init dio_find(int deviceid) 113 119 { 114 120 /* Called to find a DIO device before the full bus scan has run. ··· 231 225 dev->bus = &dio_bus; 232 226 dev->dev.parent = &dio_bus.dev; 233 227 dev->dev.bus = &dio_bus_type; 228 + dev->dev.release = dio_dev_release; 234 229 dev->scode = scode; 235 230 dev->resource.start = pa; 236 231 dev->resource.end = pa + DIO_SIZE(scode, va); ··· 259 252 if (error) { 260 253 pr_err("DIO: Error registering device %s\n", 261 254 dev->name); 255 + put_device(&dev->dev); 262 256 continue; 263 257 } 264 258 error = dio_create_sysfs_dev_files(dev);