mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
To: tiwai@suse.de
Cc: linux-kernel@vger.kernel.org, linux-sound@vger.kernel.org,
	perex@perex.cz, sreekuttan2156239@gmail.com
Subject: [PATCH v2] ALSA: core: Validate card indices in lookup helpers
Date: Tue, 17 Nov 2026 15:39:31 +0000	[thread overview]
Message-ID: <20261117153931.2784-1-sreekuttan2156239@gmail.com> (raw)
In-Reply-To: <87se0zxzyt.wl-tiwai@suse.de>

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


      parent reply	other threads:[~2026-11-17 15:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:41 [PATCH] " Sreeraj S Kurup
2026-11-17 15:03 ` Takashi Iwai
2026-11-17 15:38   ` Sreeraj S Kurup
2026-11-17 15:39   ` Sreeraj S Kurup [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117153931.2784-1-sreekuttan2156239@gmail.com \
    --to=sreekuttan2156239@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®