misc: ocxl: fix possible name leak in ocxl_file_register_afu()
Subsystems (2)
PATCH 1/1 misc: ocxl: fix possible name leak in ocxl_file_register_afu()
If device_register() returns error in ocxl_file_register_afu(), the name allocated by dev_set_name() need be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup(), and info is freed in info_release().
--- drivers/misc/ocxl/file.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/misc/ocxl/file.c +5 -2
@@ -541,8 +541,11 @@ int ocxl_file_register_afu(struct ocxl_afu *afu) goto err_put; rc =device_register (&info->dev); -if (rc) - goto err_put; +if (rc) { +free_minor (info); +put_device (&info->dev); +return rc; + } rc =ocxl_sysfs_register_afu (info);if (rc)
Signature · 1 line
2.25.1
Quoted 18 lines · Yang Yingliang
If
device_register()returns error inocxl_file_register_afu(), the name allocated bydev_set_name()need be freed. As comment ofdevice_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup(), and info is freed ininfo_release().Fixes 75ca758adbaf ("ocxl: Create a clear delineation between ocxl backend & frontend")Signed-off-by Yang Yingliang <[email protected]>--- drivers/misc/ocxl/file.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/misc/ocxl/file.c +0
-0
drivers/misc/ocxl/file.c int ocxl_file_register_afu(struct ocxl_afu *afu) · line 548
Quoted 10 lines
drivers/misc/ocxl/file.c +5
-2@@ -541,8 +541,11 @@ int ocxl_file_register_afu(struct ocxl_afu *afu) goto err_put; rc =device_register (&info->dev); -if (rc) - goto err_put; +if (rc) { +free_minor (info); +put_device (&info->dev); +return rc; + }
While I agree that a put_device() is needed on that error path, the fix above is not correct as it forgets to release the afu reference and the memory allocated in info. That was taken care of by the jump to the err_put label, so it should be kept. Something like:
- if (rc)
+ if (rc) {
+ put_device((&info->dev);
goto err_put;
+ }
Fred
Hi,
Quoted 34 lines · Frederic Barrat
Quoted 18 lines · Yang Yingliang
If
device_register()returns error inocxl_file_register_afu(), the name allocated bydev_set_name()need be freed. As comment ofdevice_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup(), and info is freed ininfo_release().Fixes 75ca758adbaf ("ocxl: Create a clear delineation between ocxl backend & frontend")Signed-off-by Yang Yingliang <[email protected]>---
drivers/misc/ocxl/file.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)drivers/misc/ocxl/file.c +0
-0drivers/misc/ocxl/file.c int ocxl_file_register_afu(struct ocxl_afu *afu) · line 547
Quoted 9 lines
drivers/misc/ocxl/file.c +5
-2@@ -541,8 +541,11 @@ int ocxl_file_register_afu(struct ocxl_afu *afu) goto err_put; rc = device_register(&info->dev); - if (rc) - goto err_put; + if (rc) { + free_minor(info); + put_device(&info->dev); + return rc; + }While I agree that a
put_device()is needed on that error path, the fix above is not correct as it forgets to release the afu reference and the memory allocated in info. That was taken care of by the jump to theerr_putlabel, so it should be kept. Something like:
Quoted 5 lines
- if (rc)
+ if (rc) {
+ put_device((&info->dev);
gotoerr_put;
+ }
The 'info' and the reference is released in info_release().
Here is call chain:put_device()
kobject_release()
kobject_cleanup()
device_release()
info_release()
static void info_release(struct device *dev)
{
struct ocxl_file_info *info = container_of(dev, struct ocxl_file_info, dev);
ocxl_afu_put(info->afu);
kfree(info);
}
So it don't need jump to the error label in this case.
Thanks,
Yang
Quoted 5 lines
Fred
.
Frederic Barrat
Acked-byQuoted 56 lines · Yang Yingliang
Hi,
Quoted 34 lines · Frederic Barrat
Quoted 18 lines · Yang Yingliang
If
device_register()returns error inocxl_file_register_afu(), the name allocated bydev_set_name()need be freed. As comment ofdevice_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup(), and info is freed ininfo_release().Fixes 75ca758adbaf ("ocxl: Create a clear delineation between ocxl backend & frontend")Signed-off-by Yang Yingliang <[email protected]>---
drivers/misc/ocxl/file.c| 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)drivers/misc/ocxl/file.c +0
-0drivers/misc/ocxl/file.c int ocxl_file_register_afu(struct ocxl_afu *afu) · line 547
Quoted 9 lines
drivers/misc/ocxl/file.c +5
-2@@ -541,8 +541,11 @@ int ocxl_file_register_afu(struct ocxl_afu *afu) goto err_put; rc = device_register(&info->dev); - if (rc) - goto err_put; + if (rc) { + free_minor(info); + put_device(&info->dev); + return rc; + }While I agree that a
put_device()is needed on that error path, the fix above is not correct as it forgets to release the afu reference and the memory allocated in info. That was taken care of by the jump to theerr_putlabel, so it should be kept. Something like:Quoted 5 lines
- if (rc)
+ if (rc) {
+ put_device((&info->dev);
gotoerr_put;
+ }The 'info' and the reference is released in
info_release().Here is call chain:
put_device()
kobject_release()
kobject_cleanup()
device_release()
info_release()static void
info_release(struct device *dev)
{
structocxl_file_info*info = container_of(dev, structocxl_file_info, dev);
Quoted 4 lines
ocxl_afu_put(info->afu); kfree(info);
}
So it don't need jump to the error label in this case.
You're right, I went too fast and the patch is correct. So:
Fred
Quoted 10 lines · Yang Yingliang
If
device_register()returns error inocxl_file_register_afu(), the name allocated bydev_set_name()need be freed. As comment ofdevice_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup(), and info is freed ininfo_release().Fixes 75ca758adbaf ("ocxl: Create a clear delineation between ocxlbackend & frontend")
Signed-off-by Yang Yingliang <[email protected]>
Thanks for the fix - as you point out, put_device() should clean everything up that needs cleaning up.
Quoted 22 lines
---
drivers/misc/ocxl/file.c | 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)drivers/misc/ocxl/file.c +0
-0@@ -541,8 +541,11 @@ int ocxl_file_register_afu(struct ocxl_afu *afu) goto err_put;
rc =device_register(&info->dev);
- if (rc)
- gotoerr_put;
+ if (rc) {
+ free_minor(info);
+ put_device(&info->dev);
+ return rc;
+ }
rc =ocxl_sysfs_register_afu(info);
if (rc)
Signature · 2 lines
Andrew Donnellan OzLabs, ADL Canberra [email protected] IBM Australia Limited
Michael Ellerman
AppliedQuoted 8 lines · Yang Yingliang
If
device_register()returns error inocxl_file_register_afu(), the name allocated bydev_set_name()need be freed. As comment ofdevice_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup(), and info is freed ininfo_release().[...]
Applied to powerpc/next.
[1/1] misc: ocxl: fix possible name leak in ocxl_file_register_afu()
https://git.kernel.org/powerpc/c/295faa17722a11cac8dbf51e4c9f9405a5e07ef1
cheers