ALSA: usb-audio: Add error checks for usb_driver_claim_interface() calls
authorTakashi Iwai <tiwai@suse.de>
Tue, 6 Apr 2021 11:35:34 +0000 (13:35 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 22 May 2021 08:57:32 +0000 (10:57 +0200)
[ Upstream commit 5fb45414ae03421255593fd5556aa2d1d82303aa ]

There are a few calls of usb_driver_claim_interface() but all of those
miss the proper error checks, as reported by Coverity.  This patch
adds those missing checks.

Along with it, replace the magic pointer with -1 with a constant
USB_AUDIO_IFACE_UNUSED for better readability.

Reported-by: coverity-bot <keescook+coverity-bot@chromium.org>
Addresses-Coverity-ID: 1475943 ("Error handling issues")
Addresses-Coverity-ID: 1475944 ("Error handling issues")
Addresses-Coverity-ID: 1475945 ("Error handling issues")
Fixes: b1ce7ba619d9 ("ALSA: usb-audio: claim autodetected PCM interfaces all at once")
Fixes: e5779998bf8b ("ALSA: usb-audio: refactor code")
Link: https://lore.kernel.org/r/202104051059.FB7F3016@keescook
Link: https://lore.kernel.org/r/20210406113534.30455-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
sound/usb/card.c
sound/usb/quirks.c
sound/usb/usbaudio.h

index 721f91f5766dff6d6ff26693b3262dce8daec39c..1a1cb73360a46394db86ca942598b489d52e88d4 100644 (file)
@@ -183,9 +183,8 @@ static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int int
                                ctrlif, interface);
                        return -EINVAL;
                }
-               usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
-
-               return 0;
+               return usb_driver_claim_interface(&usb_audio_driver, iface,
+                                                 USB_AUDIO_IFACE_UNUSED);
        }
 
        if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
@@ -205,7 +204,8 @@ static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int int
 
        if (! snd_usb_parse_audio_interface(chip, interface)) {
                usb_set_interface(dev, interface, 0); /* reset the current interface */
-               usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
+               return usb_driver_claim_interface(&usb_audio_driver, iface,
+                                                 USB_AUDIO_IFACE_UNUSED);
        }
 
        return 0;
@@ -665,7 +665,7 @@ static void usb_audio_disconnect(struct usb_interface *intf)
        struct snd_card *card;
        struct list_head *p;
 
-       if (chip == (void *)-1L)
+       if (chip == USB_AUDIO_IFACE_UNUSED)
                return;
 
        card = chip->card;
@@ -765,7 +765,7 @@ static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
        struct usb_mixer_interface *mixer;
        struct list_head *p;
 
-       if (chip == (void *)-1L)
+       if (chip == USB_AUDIO_IFACE_UNUSED)
                return 0;
 
        if (!chip->num_suspended_intf++) {
@@ -795,7 +795,7 @@ static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
        struct list_head *p;
        int err = 0;
 
-       if (chip == (void *)-1L)
+       if (chip == USB_AUDIO_IFACE_UNUSED)
                return 0;
 
        atomic_inc(&chip->active); /* avoid autopm */
index 6fc9ad067d822016dc71d710c157f8fd01c5d283..182c9de4f255c4da9a4d9d201ad2a143663746a2 100644 (file)
@@ -66,8 +66,12 @@ static int create_composite_quirk(struct snd_usb_audio *chip,
                if (!iface)
                        continue;
                if (quirk->ifnum != probed_ifnum &&
-                   !usb_interface_claimed(iface))
-                       usb_driver_claim_interface(driver, iface, (void *)-1L);
+                   !usb_interface_claimed(iface)) {
+                       err = usb_driver_claim_interface(driver, iface,
+                                                        USB_AUDIO_IFACE_UNUSED);
+                       if (err < 0)
+                               return err;
+               }
        }
 
        return 0;
@@ -398,8 +402,12 @@ static int create_autodetect_quirks(struct snd_usb_audio *chip,
                        continue;
 
                err = create_autodetect_quirk(chip, iface, driver);
-               if (err >= 0)
-                       usb_driver_claim_interface(driver, iface, (void *)-1L);
+               if (err >= 0) {
+                       err = usb_driver_claim_interface(driver, iface,
+                                                        USB_AUDIO_IFACE_UNUSED);
+                       if (err < 0)
+                               return err;
+               }
        }
 
        return 0;
index f4ee83c8e0b2bb29ac78c3e5933d1b4566d8df04..62456a806bb4d9b7c6bdb5e6c8715e9d13f68e74 100644 (file)
@@ -63,6 +63,8 @@ struct snd_usb_audio {
        struct usb_host_interface *ctrl_intf;   /* the audio control interface */
 };
 
+#define USB_AUDIO_IFACE_UNUSED ((void *)-1L)
+
 #define usb_audio_err(chip, fmt, args...) \
        dev_err(&(chip)->dev->dev, fmt, ##args)
 #define usb_audio_warn(chip, fmt, args...) \