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:40:26 +0000 (10:40 +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 023a36a4922b290b32034ca5fc7b3972d65f928e..6c5be4e3cb9df177e3cc646240689c4ebab4b0a4 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 6b9e6b57d58dffda815c55e3acda54221d2005c5..02b4d0638e008b60dc633c3bfdb34d6d33367c61 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;
@@ -399,8 +403,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...) \