Skip to contentSkip to conversation

media: em28xx: fix memory leak in em28xx_init_dev

PATCH In review Dongliang Mu 3 participants 6 messages Nov 1, 2021 – Nov 8, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: em28xx: fix memory leak in em28xx_init_dev

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/media/usb/em28xx/em28xx-cards.c +13 -6
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 
 	if (dev->is_audio_only) {
 		retval = em28xx_audio_setup(dev);
-		if (retval)
-			return -ENODEV;
+		if (retval) {
+			retval = -ENODEV;
+			goto err_deinit_media;
+		}
 		em28xx_init_extension(dev);
 
 		return 0;
@@ -3645,7 +3647,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 		dev_err(&dev->intf->dev,
 			"%s: em28xx_i2c_register bus 0 - error [%d]!\n",
 		       __func__, retval);
-		return retval;
+		goto err_deinit_media;
 	}
 
 	/* register i2c bus 1 */
@@ -3661,9 +3663,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 				"%s: em28xx_i2c_register bus 1 - error [%d]!\n",
 				__func__, retval);
 
-			em28xx_i2c_unregister(dev, 0);
-
-			return retval;
+			goto err_unreg_i2c;
 		}
 	}
 
@@ -3671,6 +3671,13 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 	em28xx_card_setup(dev);
 
 	return 0;
+
+
+err_unreg_i2c:
+	em28xx_i2c_unregister(dev, 0);
+err_deinit_media:
+	em28xx_unregister_media_device(dev);
+	return retval;
 }
 
 static int em28xx_duplicate_dev(struct em28xx *dev)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Dongliang Mu

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

Quoted 7 lines

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")

Is this really a syzbot warning? If so it should be in the format:

Reported-by [email protected]

Syzbot is different from syzkaller. Syzkaller is the fuzzer and syzbot is the program which reports syzkaller bugs.

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3625

Quoted 10 lines
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/media/usb/em28xx/em28xx-cards.c +0 -0
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 

There is no check to see if the em28xx_media_device_init() fails. I don't love that we call unregister() to undo the init() but it seems like it should work...

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3629

Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +2 -2
 	if (dev->is_audio_only) {
 		retval = em28xx_audio_setup(dev);
-		if (retval)
-			return -ENODEV;
+		if (retval) {
+			retval = -ENODEV;

This was in the original code, but probably we should preserve the error code.

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3631

Quoted 2 lines
drivers/media/usb/em28xx/em28xx-cards.c +2 -0
+			goto err_deinit_media;
+		}

regards,
dan carpenter

#2 lore raw
Quoted 14 lines · Dan Carpenter
Quoted 5 lines · Dongliang Mu

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

Quoted 7 lines

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")
Quoted 7 lines

Is this really a syzbot warning? If so it should be in the format:

Reported-by [email protected]

Syzbot is different from syzkaller. Syzkaller is the fuzzer and syzbot is the program which reports syzkaller bugs.

Bug report is from his local instance. He just wants to give credit to syzbot for finding it

Quoted 4 lines
Quoted 4 lines
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Quoted 8 lines

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3625

Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +0 -0
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 

There is no check to see if the em28xx_media_device_init() fails. I

I guess, it should work, since there a lot of checks to see if this pointer is valid, i.e driver can work even without this pointer, AFAIK

Quoted 2 lines

don't love that we call unregister() to undo the init() but it seems like it should work...

Same here, but it is out of scope of this patch :)

With regards,
Pavel Skripkin

#3 lore raw
Quoted 18 lines · Pavel Skripkin
Quoted 16 lines · Dan Carpenter
Quoted 5 lines · Dongliang Mu

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

Quoted 7 lines

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")

Is this really a syzbot warning? If so it should be in the format:

Quoted 8 lines
Quoted 5 lines
Reported-by [email protected]

Syzbot is different from syzkaller. Syzkaller is the fuzzer and syzbot is the program which reports syzkaller bugs.

Bug report is from his local instance. He just wants to give credit to syzbot for finding it

Hi Dan,

just as explained by Pavel, I leveraged the local syzkaller instance to find this bug.

I can modify it to "Reported-by: syzkaller <[email protected]>", this one looks better.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Quoted 8 lines

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3625

Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +0 -0
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 

There is no check to see if the em28xx_media_device_init() fails. I

Quoted 7 lines

I guess, it should work, since there a lot of checks to see if this pointer is valid, i.e driver can work even without this pointer, AFAIK

Quoted 2 lines

don't love that we call unregister() to undo the init() but it seems like it should work...

Same here, but it is out of scope of this patch :)

From the implementation, em28xx_media_device_init and em28xx_unregister_media_device should not be a pair of functions (do/undo).

Maybe I can write em28xx_free_media_device to be paired with em28xx_media_device_init, like below. And then only call it from the error handling context.

static void em28xx_free_media_device(struct em28xx *dev) {

#ifdef CONFIG_MEDIA_CONTROLLER
                kfree(dev->media_dev);
                dev->media_dev = NULL;

#endif
}

Quoted 5 lines

With regards,
Pavel Skripkin

#4 lore raw
Quoted 28 lines · Dongliang Mu
Quoted 18 lines · Pavel Skripkin
Quoted 16 lines · Dan Carpenter
Quoted 5 lines · Dongliang Mu

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

Quoted 7 lines

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")

Is this really a syzbot warning? If so it should be in the format:

Quoted 8 lines
Quoted 5 lines
Reported-by [email protected]

Syzbot is different from syzkaller. Syzkaller is the fuzzer and syzbot is the program which reports syzkaller bugs.

Bug report is from his local instance. He just wants to give credit to syzbot for finding it

Quoted 8 lines

Hi Dan,

just as explained by Pavel, I leveraged the local syzkaller instance to find this bug.

I can modify it to "Reported-by: syzkaller <[email protected]>", this one looks better.

No need for a Reported-by at all, but if you want to credit syzkaller that's an okay format.

Quoted 17 lines
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Quoted 8 lines

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3625

Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +0 -0
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 

There is no check to see if the em28xx_media_device_init() fails. I

I guess, it should work, since there a lot of checks to see if this pointer is valid, i.e driver can work even without this pointer, AFAIK

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

don't love that we call unregister() to undo the init() but it seems like it should work...

Same here, but it is out of scope of this patch :)

From the implementation, em28xx_media_device_init and

em28xx_unregister_media_device should not be a pair of functions (do/undo).

That's how it is now, but it's not necessarily how it should be. Anyway, it's unrelated to you patch. Just forget I mentioned it.

regards,
dan carpenter

#5 lore raw
Quoted 35 lines · Dan Carpenter
Quoted 33 lines · Dongliang Mu
Quoted 18 lines · Pavel Skripkin
Quoted 16 lines · Dan Carpenter
Quoted 5 lines · Dongliang Mu

In the em28xx_init_rev, if em28xx_audio_setup fails, this function fails to deallocate the media_dev allocated in the em28xx_media_device_init.

Fix this by adding em28xx_unregister_media_device to free media_dev.

Quoted 7 lines

BTW, this patch is tested in my local syzkaller instance, and it can prevent the memory leak from occurring again.

CC Pavel Skripkin <[email protected]>
Fixes 37ecc7b1278f ("[media] em28xx: add media controller support")

Is this really a syzbot warning? If so it should be in the format:

Quoted 8 lines
Quoted 5 lines
Reported-by [email protected]

Syzbot is different from syzkaller. Syzkaller is the fuzzer and syzbot is the program which reports syzkaller bugs.

Bug report is from his local instance. He just wants to give credit to syzbot for finding it

Hi Dan,

just as explained by Pavel, I leveraged the local syzkaller instance to find this bug.

Quoted 7 lines
Quoted 3 lines

I can modify it to "Reported-by: syzkaller <[email protected]>", this one looks better.

No need for a Reported-by at all, but if you want to credit syzkaller that's an okay format.

Hi all,

do I need to send a v2 patch with a new Reported-by tag?

Quoted 33 lines
Quoted 17 lines
Quoted 17 lines
Quoted 5 lines
Quoted 5 lines
---
 drivers/media/usb/em28xx/em28xx-cards.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Quoted 8 lines

drivers/media/usb/em28xx/em28xx-cards.c static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, · line 3625

Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +0 -0
index c1e0dccb7408..fca68939ca50 100644
@@ -3625,8 +3625,10 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
 

There is no check to see if the em28xx_media_device_init() fails. I

I guess, it should work, since there a lot of checks to see if this pointer is valid, i.e driver can work even without this pointer, AFAIK

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

don't love that we call unregister() to undo the init() but it seems like it should work...

Same here, but it is out of scope of this patch :)

From the implementation, em28xx_media_device_init and

em28xx_unregister_media_device should not be a pair of functions (do/undo).

That's how it is now, but it's not necessarily how it should be. Anyway, it's unrelated to you patch. Just forget I mentioned it.

regards,
dan carpenter