Skip to contentSkip to conversation

ALSA: core: Fix possible null-pointer dereferences in snd_timer_proc_read()

PATCH In review Jia-Ju Bai 2 participants 2 messages Jul 24, 2019 Sound

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 ALSA: core: Fix possible null-pointer dereferences in snd_timer_proc_read()

In snd_timer_proc_read(), there is an if statement on line 1204 to check whether timer->card is NULL:

    if (timer->card && timer->card->shutdown)

When timer->card is NULL, it is used on lines 1212 and 1215:

    timer->card->number

Thus, possible null-pointer dereferences may occur.

To fix these bugs, timer->card is checked before being used.

These bugs are found by a static analysis tool STCheck written by us.

---
 sound/core/timer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/timer.c +5 -3
index 5c9fbf3f4340..967d06a3cdec 100644
@@ -1208,11 +1208,13 @@ static void snd_timer_proc_read(struct snd_info_entry *entry,
 			snd_iprintf(buffer, "G%i: ", timer->tmr_device);
 			break;
 		case SNDRV_TIMER_CLASS_CARD:
-			snd_iprintf(buffer, "C%i-%i: ",
-				    timer->card->number, timer->tmr_device);
+			snd_iprintf(buffer, "C%i-%i: ", 
+					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:
Signature · 1 line
2.17.0
#1 lore raw
Quoted 11 lines · Jia-Ju Bai

In snd_timer_proc_read(), there is an if statement on line 1204 to check whether timer->card is NULL:

    if (timer->card && timer->card->shutdown)

When timer->card is NULL, it is used on lines 1212 and 1215:

    timer->card->number

Thus, possible null-pointer dereferences may occur.

To fix these bugs, timer->card is checked before being used.

Both cases can (should) never happen. The SNDRV_TIMER_CLASS_CARD is always associated with a timer with a card object. Ditto for SNDRV_TIMER_CLASS_PCM, where a PCM is always tied with a card.

So, if any, this should be a WARN_ON(). But I doubt it being any useful.

thanks,

Takashi

Quoted 33 lines

These bugs are found by a static analysis tool STCheck written by us.

---
 sound/core/timer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/core/timer.c +5 -3
index 5c9fbf3f4340..967d06a3cdec 100644
@@ -1208,11 +1208,13 @@ static void snd_timer_proc_read(struct snd_info_entry *entry,
 			snd_iprintf(buffer, "G%i: ", timer->tmr_device);
 			break;
 		case SNDRV_TIMER_CLASS_CARD:
-			snd_iprintf(buffer, "C%i-%i: ",
-				    timer->card->number, timer->tmr_device);
+			snd_iprintf(buffer, "C%i-%i: ", 
+					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:
Signature · 1 line
2.17.0