* [PATCH] ALSA: core: Validate card indices in lookup helpers
@ 2026-11-17 10:41 Sreeraj S Kurup
2026-11-17 15:03 ` Takashi Iwai
0 siblings, 1 reply; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-11-17 10:41 UTC (permalink / raw)
To: perex, tiwai; +Cc: linux-kernel, linux-sound, Sreeraj S Kurup
Ensure card indices are within the valid range before accessing
the card array or lock bitmap in snd_card_ref() and
snd_card_locked().
This prevents out-of-range indices from causing invalid memory
accesses in these lookup helpers. It also protects against
invalid indices passed by existing or future callers.
Return NULL from snd_card_ref() and 0 from snd_card_locked()
when an invalid card index is supplied.
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
sound/core/init.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/sound/core/init.c b/sound/core/init.c
index d518cb838777..f79b63c69a49 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -384,6 +384,9 @@ struct snd_card *snd_card_ref(int idx)
{
struct snd_card *card;
+ if (idx < 0 || idx >= SNDRV_CARDS)
+ return NULL;
+
guard(mutex)(&snd_card_mutex);
card = snd_cards[idx];
if (card)
@@ -395,6 +398,9 @@ EXPORT_SYMBOL_GPL(snd_card_ref);
/* return non-zero if a card is already locked */
int snd_card_locked(int card)
{
+ if (card < 0 || card >= SNDRV_CARDS)
+ return 0;
+
guard(mutex)(&snd_card_mutex);
return test_bit(card, snd_cards_lock);
}
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] ALSA: core: Validate card indices in lookup helpers
2026-11-17 10:41 [PATCH] ALSA: core: Validate card indices in lookup helpers Sreeraj S Kurup
@ 2026-11-17 15:03 ` Takashi Iwai
2026-11-17 15:38 ` Sreeraj S Kurup
2026-11-17 15:39 ` [PATCH v2] " Sreeraj S Kurup
0 siblings, 2 replies; 4+ messages in thread
From: Takashi Iwai @ 2026-11-17 15:03 UTC (permalink / raw)
To: Sreeraj S Kurup; +Cc: perex, tiwai, linux-kernel, linux-sound
On Tue, 17 Nov 2026 11:41:54 +0100,
Sreeraj S Kurup wrote:
>
> Ensure card indices are within the valid range before accessing
> the card array or lock bitmap in snd_card_ref() and
> snd_card_locked().
>
> This prevents out-of-range indices from causing invalid memory
> accesses in these lookup helpers. It also protects against
> invalid indices passed by existing or future callers.
>
> Return NULL from snd_card_ref() and 0 from snd_card_locked()
> when an invalid card index is supplied.
>
> Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
If the value is out of range, it must be a bug in the driver, so this
is worth for putting either WARN_ON() or snd_BUG_ON().
thanks,
Takashi
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] ALSA: core: Validate card indices in lookup helpers
2026-11-17 15:03 ` Takashi Iwai
@ 2026-11-17 15:38 ` Sreeraj S Kurup
2026-11-17 15:39 ` [PATCH v2] " Sreeraj S Kurup
1 sibling, 0 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-11-17 15:38 UTC (permalink / raw)
To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239
Hi Takashi,
Thanks for the suggestion. I've updated the patch to use WARN_ON()
and included the safe return values.
I'll send the updated v2 patch shortly.
Best regards,
Sreeraj
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] ALSA: core: Validate card indices in lookup helpers
2026-11-17 15:03 ` Takashi Iwai
2026-11-17 15:38 ` Sreeraj S Kurup
@ 2026-11-17 15:39 ` Sreeraj S Kurup
1 sibling, 0 replies; 4+ messages in thread
From: Sreeraj S Kurup @ 2026-11-17 15:39 UTC (permalink / raw)
To: tiwai; +Cc: linux-kernel, linux-sound, perex, sreekuttan2156239
Ensure card indices are within the valid range before accessing
the card array or lock bitmap in snd_card_ref() and
snd_card_locked().
This prevents out-of-range indices from causing invalid memory
accesses in these lookup helpers. It also protects against
invalid indices passed by existing or future callers.
Warn about invalid card indices and return NULL from
snd_card_ref() and 0 from snd_card_locked() when an
invalid card index is supplied.
Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
v2:
- Add WARN_ON() to report invalid card indices, as suggested by Takashi Iwai.
sound/core/init.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/sound/core/init.c b/sound/core/init.c
index d518cb838777..343c06d714c2 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -384,6 +384,9 @@ struct snd_card *snd_card_ref(int idx)
{
struct snd_card *card;
+ if (WARN_ON(idx < 0 || idx >= SNDRV_CARDS))
+ return NULL;
+
guard(mutex)(&snd_card_mutex);
card = snd_cards[idx];
if (card)
@@ -395,6 +398,9 @@ EXPORT_SYMBOL_GPL(snd_card_ref);
/* return non-zero if a card is already locked */
int snd_card_locked(int card)
{
+ if (WARN_ON(card < 0 || card >= SNDRV_CARDS))
+ return 0;
+
guard(mutex)(&snd_card_mutex);
return test_bit(card, snd_cards_lock);
}
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-17 15:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 10:41 [PATCH] ALSA: core: Validate card indices in lookup helpers Sreeraj S Kurup
2026-11-17 15:03 ` Takashi Iwai
2026-11-17 15:38 ` Sreeraj S Kurup
2026-11-17 15:39 ` [PATCH v2] " Sreeraj S Kurup
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®