Skip to contentSkip to conversation

ALSA: pcmtest: fix reference leak on failed device registration

PATCH Applied Guangshuo Li 3 participants 6 messages Apr 15 – Apr 23

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

PATCH 1/1 ALSA: pcmtest: fix reference leak on failed device registration

When platform_device_register() fails in mod_init(), the embedded struct device in pcmtst_pdev has already been initialized by device_initialize(), but the failure path returns the error without dropping the device reference for the current platform device:

mod_init()

    -> platform_device_register(&pcmtst_pdev)
       -> device_initialize(&pcmtst_pdev.dev)
       -> setup_pdev_dma_masks(&pcmtst_pdev)
       -> platform_device_add(&pcmtst_pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() before returning the error.

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")
---
 sound/drivers/pcmtest.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
sound/drivers/pcmtest.c +3 -1
index 768bb698adfb..20ceb9082fa9 100644
@@ -756,8 +756,10 @@ static int __init mod_init(void)
 	if (err)
 		return err;
 	err = platform_device_register(&pcmtst_pdev);
-	if (err)
+	if (err) {
+		platform_device_put(&pcmtst_pdev);
 		return err;
+	}
 	err = platform_driver_register(&pcmtst_pdrv);
 	if (err)
 		platform_device_unregister(&pcmtst_pdev);
Signature · 1 line
2.43.0
Applied
#1 lore raw
Quoted 15 lines · Guangshuo Li

When platform_device_register() fails in mod_init(), the embedded struct device in pcmtst_pdev has already been initialized by device_initialize(), but the failure path returns the error without dropping the device reference for the current platform device:

mod_init()

    -> platform_device_register(&pcmtst_pdev)
       -> device_initialize(&pcmtst_pdev.dev)
       -> setup_pdev_dma_masks(&pcmtst_pdev)
       -> platform_device_add(&pcmtst_pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() before returning the error.

Quoted 6 lines

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")

Thanks, applied now.

Takashi

#2 lore raw
Quoted 18 lines · Takashi Iwai
Quoted 15 lines · Guangshuo Li

When platform_device_register() fails in mod_init(), the embedded struct device in pcmtst_pdev has already been initialized by device_initialize(), but the failure path returns the error without dropping the device reference for the current platform device:

mod_init()

    -> platform_device_register(&pcmtst_pdev)
       -> device_initialize(&pcmtst_pdev.dev)
       -> setup_pdev_dma_masks(&pcmtst_pdev)
       -> platform_device_add(&pcmtst_pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() before returning the error.

Quoted 8 lines
Quoted 6 lines

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")

Thanks, applied now.

... and now I looked through the whole tree, and noticed that the majority of callers of platform_device_register() don't care the error cases without calling platform_device_put(). There are over a hundred callers of platform_device_register() while only 5 or so are doing the proper cleanup at the error.

Judging from the numbers above, it might be better to change the behavior of platform_device_register() itself to call *_put() at the error case internally. Or, if we keep the current behavior, at least we should properly document it.

Greg, Rafael, Danilo, what do you think?

thanks,

Takashi

#3 lore raw

Hi Takashi,

Thanks for reviewing.

Quoted 13 lines · Takashi Iwai

... and now I looked through the whole tree, and noticed that the majority of callers of platform_device_register() don't care the error cases without calling platform_device_put(). There are over a hundred callers of platform_device_register() while only 5 or so are doing the proper cleanup at the error.

Judging from the numbers above, it might be better to change the behavior of platform_device_register() itself to call *_put() at the error case internally. Or, if we keep the current behavior, at least we should properly document it.

Quoted 6 lines

Greg, Rafael, Danilo, what do you think?

thanks,

Takashi

I agree with your observation. Given that many callers of platform_device_register() do not handle the error path with a matching platform_device_put(), this does seem more like an issue related to the core/API behavior, or at least something that should be documented more clearly.

Also, the underlying issue here appears to be related to the platform_device_register() core/API behavior. We are currently discussing in another similar case whether the better fix, if any, should be made in the core/API code rather than in individual callers:

https://patchew.org/linux/[email protected]/

Once that discussion reaches a conclusion, we will revisit this and make the appropriate fix if needed.

Thanks,
Guangshuo

#4 lore raw
In reply to Takashi Iwai
Quoted 27 lines · Takashi Iwai
Quoted 25 lines · Takashi Iwai
Quoted 21 lines · Guangshuo Li

When platform_device_register() fails in mod_init(), the embedded struct device in pcmtst_pdev has already been initialized by device_initialize(), but the failure path returns the error without dropping the device reference for the current platform device:

   mod_init()
     -> platform_device_register(&pcmtst_pdev)
        -> device_initialize(&pcmtst_pdev.dev)
        -> setup_pdev_dma_masks(&pcmtst_pdev)
        -> platform_device_add(&pcmtst_pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() before returning the error.

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")
Quoted 7 lines
Quoted 1 line

Thanks, applied now.

... and now I looked through the whole tree, and noticed that the majority of callers of platform_device_register() don't care the error cases without calling platform_device_put(). There are over a hundred callers of platform_device_register() while only 5 or so are doing the proper cleanup at the error.

Moreover, unless the static pcmtst_pdev has ->type->release or ->release set, the patch triggers a warning upon put().

Quoted 3 lines

Judging from the numbers above, it might be better to change the behavior of platform_device_register() itself to call *_put() at the error case internally.

Yes, that's what should be done instead.

thanks,

Signature · 2 lines
js
suse labs
#5 lore raw
Quoted 31 lines · Jiri Slaby
Quoted 29 lines · Takashi Iwai
Quoted 26 lines · Takashi Iwai
Quoted 15 lines · Guangshuo Li

When platform_device_register() fails in mod_init(), the embedded struct device in pcmtst_pdev has already been initialized by device_initialize(), but the failure path returns the error without dropping the device reference for the current platform device:

   mod_init()
     -> platform_device_register(&pcmtst_pdev)
        -> device_initialize(&pcmtst_pdev.dev)
        -> setup_pdev_dma_masks(&pcmtst_pdev)
        -> platform_device_add(&pcmtst_pdev)

This leads to a reference leak when platform_device_register() fails. Fix this by calling platform_device_put() before returning the error.

Quoted 6 lines

The issue was identified by a static analysis tool I developed and confirmed by manual review.

Fixes 315a3d57c64c5 ("ALSA: Implement the new Virtual PCM Test Driver")

Thanks, applied now.

Quoted 8 lines
Quoted 5 lines

... and now I looked through the whole tree, and noticed that the majority of callers of platform_device_register() don't care the error cases without calling platform_device_put(). There are over a hundred callers of platform_device_register() while only 5 or so are doing the proper cleanup at the error.

Moreover, unless the static pcmtst_pdev has ->type->release or ->release set, the patch triggers a warning upon put().

Ah a good point, the call should be conditional.

Quoted 5 lines
Quoted 3 lines

Judging from the numbers above, it might be better to change the behavior of platform_device_register() itself to call *_put() at the error case internally.

Yes, that's what should be done instead.

OK, I'm going to revert this patch for pcmtest.c for now (so back to the upstream state again), so that we can work on the platform_device_register() side.

thanks,

Takashi