Skip to contentSkip to conversation

ALSA: timer: Fix null dereference of 'timer->card' reported by smatch

PATCH In review [email protected] 2 participants 2 messages Nov 3, 2025 – Nov 4, 2025 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: timer: Fix null dereference of 'timer->card' reported by smatch

From: HariKrishna Sagala <[email protected]>

Fix null dereference in snd_timer_proc_read().

Smatch reported that was previously assumed to be null
at line 1226, but later accessed without a check.
This could lead to a null pointer dereference under certain conditions.

Add a null check before accessing to ensure safe execution.

---
This patch addresses a null pointer dereference in snd_timer_proc_read() by adding a proper check for timer->card.The issue was flagged by Smatch, which noted that timer->card was previously assumed to be null at line 1226 but accessed without validation.To ensure safe access, a conditional check has been introduced before referencing timer->card. The fix was verified by booting the kernel and performing audio playback using ALSA commands. Additionally, the /proc/asound/timers interface was inspected to confirm that timer information is correctly populated and accessible without errors.
Note: Couldn't simulate a scenario where 'timer->card' is null and 'timer->card->shutdown' is 1 (shutdown initiated).

 sound/core/timer.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sound/core/timer.c +3 -2
index d9fff5c87613..d65e8bcb2f46 100644
@@ -1231,10 +1231,11 @@ static void snd_timer_proc_read(struct snd_info_entry *entry,
 			break;
 		case SNDRV_TIMER_CLASS_CARD:
 			snd_iprintf(buffer, "C%i-%i: ",
-				    timer->card->number, timer->tmr_device);
+				    timer->card ? timer->card->number : -1, timer->tmr_device);
 			break;
 		case SNDRV_TIMER_CLASS_PCM:
-			snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
+			snd_iprintf(buffer, "P%i-%i-%i: ",
+				    timer->card ? timer->card->number : -1,
 				    timer->tmr_device, timer->tmr_subdevice);
 			break;
 		default:

base-commit: 6093a688a07da07808f0122f9aa2a3eed250d853

Signature · 1 line
2.43.0
#1 lore raw
Quoted 12 lines · On Mon, 03 Nov 2025 12:49:03 +0100, [email protected] wrote:

From: HariKrishna Sagala <[email protected]>

Fix null dereference in snd_timer_proc_read().

Smatch reported that was previously assumed to be null
at line 1226, but later accessed without a check.
This could lead to a null pointer dereference under certain conditions.

Add a null check before accessing to ensure safe execution.

Quoted 12 lines

---
This patch addresses a null pointer dereference in snd_timer_proc_read() by adding a proper check for timer->card.The issue was flagged by Smatch, which noted that timer->card was previously assumed to be null at line 1226 but accessed without validation.To ensure safe access, a conditional check has been introduced before referencing timer->card. The fix was verified by booting the kernel and performing audio playback using ALSA commands. Additionally, the /proc/asound/timers interface was inspected to confirm that timer information is correctly populated and accessible without errors.
Note: Couldn't simulate a scenario where 'timer->card' is null and 'timer->card->shutdown' is 1 (shutdown initiated).

timer->card is guaranteed to be non-NULL for SNDRV_TIMER_CLASS_CARD timer->and SNDRV_TIMER_CLASS_PCM at the creation in snd_timer_new(). So this check is superfluous.

thanks,

Takashi

Quoted 27 lines
 sound/core/timer.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sound/core/timer.c +3 -2
index d9fff5c87613..d65e8bcb2f46 100644
@@ -1231,10 +1231,11 @@ static void snd_timer_proc_read(struct snd_info_entry *entry,
 			break;
 		case SNDRV_TIMER_CLASS_CARD:
 			snd_iprintf(buffer, "C%i-%i: ",
-				    timer->card->number, timer->tmr_device);
+				    timer->card ? timer->card->number : -1, timer->tmr_device);
 			break;
 		case SNDRV_TIMER_CLASS_PCM:
-			snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
+			snd_iprintf(buffer, "P%i-%i-%i: ",
+				    timer->card ? timer->card->number : -1,
 				    timer->tmr_device, timer->tmr_subdevice);
 			break;
 		default:

base-commit: 6093a688a07da07808f0122f9aa2a3eed250d853

Signature · 1 line
2.43.0