media: dvb-core: Fix double free in dvb_register_device()
authorKeita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
Tue, 26 Apr 2022 05:29:19 +0000 (06:29 +0100)
committerMauro Carvalho Chehab <mchehab@kernel.org>
Fri, 25 Nov 2022 10:02:57 +0000 (10:02 +0000)
In function dvb_register_device() -> dvb_register_media_device() ->
dvb_create_media_entity(), dvb->entity is allocated and initialized. If
the initialization fails, it frees the dvb->entity, and return an error
code. The caller takes the error code and handles the error by calling
dvb_media_device_free(), which unregisters the entity and frees the
field again if it is not NULL. As dvb->entity may not NULLed in
dvb_create_media_entity() when the allocation of dvbdev->pad fails, a
double free may occur. This may also cause an Use After free in
media_device_unregister_entity().

Fix this by storing NULL to dvb->entity when it is freed.

Link: https://lore.kernel.org/linux-media/20220426052921.2088416-1-keitasuzuki.park@sslab.ics.keio.ac.jp
Fixes: fcd5ce4b3936 ("media: dvb-core: fix a memory leak bug")
Cc: stable@vger.kernel.org
Cc: Wenwen Wang <wenwen@cs.uga.edu>
Signed-off-by: Keita Suzuki <keitasuzuki.park@sslab.ics.keio.ac.jp>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
drivers/media/dvb-core/dvbdev.c

index d5a142e..5b275a9 100644 (file)
@@ -333,6 +333,7 @@ static int dvb_create_media_entity(struct dvb_device *dvbdev,
                                       GFP_KERNEL);
                if (!dvbdev->pads) {
                        kfree(dvbdev->entity);
+                       dvbdev->entity = NULL;
                        return -ENOMEM;
                }
        }