[PATCH 5.4 52/71] cdrom: gdrom: deallocate struct gdrom_unit fields in remove_gdrom

From: Greg Kroah-Hartman
Date: Mon May 24 2021 - 11:55:28 EST

From: Atul Gopinathan <atulgopinathan@xxxxxxxxx>

commit d03d1021da6fe7f46efe9f2a7335564e7c9db5ab upstream.

The fields, "toc" and "cd_info", of "struct gdrom_unit gd" are allocated
in "probe_gdrom()". Prevent a memory leak by making sure "gd.cd_info" is
deallocated in the "remove_gdrom()" function.

Also prevent double free of the field "gd.toc" by moving it from the
module's exit function to "remove_gdrom()". This is because, in
"probe_gdrom()", the function makes sure to deallocate "gd.toc" in case
of any errors, so the exit function invoked later would again free

The patch also maintains consistency by deallocating the above mentioned
fields in "remove_gdrom()" along with another memory allocated field

Suggested-by: Jens Axboe <axboe@xxxxxxxxx>
Cc: Peter Rosin <peda@xxxxxxxxxx>
Cc: stable <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Atul Gopinathan <atulgopinathan@xxxxxxxxx>
Link: https://lore.kernel.org/r/20210503115736.2104747-28-gregkh@xxxxxxxxxxxxxxxxxxx
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
drivers/cdrom/gdrom.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

--- a/drivers/cdrom/gdrom.c
+++ b/drivers/cdrom/gdrom.c
@@ -828,6 +828,8 @@ static int remove_gdrom(struct platform_
if (gdrom_major)
unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
+ kfree(gd.cd_info);
+ kfree(gd.toc);

return 0;
@@ -859,7 +861,6 @@ static void __exit exit_gdrom(void)
- kfree(gd.toc);