Skip to contentSkip to conversation

Fix potential NULL pointer dereference in echoaudio midi.

PATCH Applied Jesper Juhl 4 participants 6 messages Oct 31, 2006 – Nov 6, 2006 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix potential NULL pointer dereference in echoaudio midi.

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

---

sound/pci/echoaudio/midi.c +2 -1
index e31f0f1..69ef9f0 100644
@@ -236,7 +236,8 @@ static void snd_echo_midi_output_write(u
 	}
 
 	/* We restart the timer only if there is some data left to send */
-	if (!snd_rawmidi_transmit_empty(chip->midi_out) && chip->tinuse) {
+	if (chip->midi_out && !snd_rawmidi_transmit_empty(chip->midi_out) &&
+			chip->tinuse) {
 		/* The timer will expire slightly after the data has been
 		   sent */
 		time = (sent << 3) / 25 + 1;	/* 8/25=0.32ms to send a byte */
#1 lore raw
Quoted 5 lines · Jesper Juhl

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

How about just adding an early test:

	if (!chip->midi_out)
		goto out;

and adding a label for out before the chip->lock unlock? We still need to clear chip->midi_full so we still require the spinlock, but there's no reason we should be testing chip->midi_out multiple times since the remaining code path in its entirety depends on it.

David

#2 lore raw
Quoted 8 lines · David Rientjes
Quoted 5 lines · Jesper Juhl

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

Quoted 9 lines

How about just adding an early test:

	if (!chip->midi_out)
		goto out;

and adding a label for out before the chip->lock unlock? We still need to clear chip->midi_full so we still require the spinlock, but there's no reason we should be testing chip->midi_out multiple times since the remaining code path in its entirety depends on it.

Sure, that's an alternative solution. Probably a superiour one since, as you say, we'll then only be testing 'chip->midi_out' once.

Here's a patch that makes that change instead.
I'll leave it up to the powers-that-be to pick the one they like best :)

---

sound/pci/echoaudio/midi.c +4 -1
index e31f0f1..0385b4e 100644
@@ -213,7 +213,9 @@ static void snd_echo_midi_output_write(u
 	sent = bytes = 0;
 	spin_lock_irqsave(&chip->lock, flags);
 	chip->midi_full = 0;
-	if (chip->midi_out && !snd_rawmidi_transmit_empty(chip->midi_out)) {
+	if (!chip->midi_out)
+		goto out;
+	if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
 		bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
 						  MIDI_OUT_BUFFER_SIZE - 1);
 		DE_MID(("Try to send %d bytes...\n", bytes));
@@ -243,6 +245,7 @@ static void snd_echo_midi_output_write(u
 		mod_timer(&chip->timer, jiffies + (time * HZ + 999) / 1000);
 		DE_MID(("Timer armed(%d)\n", ((time * HZ + 999) / 1000)));
 	}
+ out:
 	spin_unlock_irqrestore(&chip->lock, flags);
 }
 
#3 lore raw
Quoted 9 lines · On Tue, 31 Oct 2006 23:26:31 +0100 Jesper Juhl <[email protected]> wrote:
Quoted 8 lines · David Rientjes
Quoted 5 lines · Jesper Juhl

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

Quoted 3 lines
Quoted 3 lines

How about just adding an early test:

	if (!chip->midi_out)
		goto out;

The point of that check is to make sure is doesn't access chip->midi_out when (surprise!) it is NULL. This can only happen in the rare (possible?) case snd_echo_midi_output_close() is called while the timer handler is running. I have another proposal which IMHO is smp-safer that just moving the check. In that case we should also put a spinlock around the chip->midi_out=0 in the snd_echo_midi_output_close() callback.

pci/echoaudio/midi.c renamed from pci/echoaudio/midi.c__orig +4 -2
@@ -213,7 +213,7 @@ static void snd_echo_midi_output_write(u
 	sent = bytes = 0;
 	spin_lock_irqsave(&chip->lock, flags);
 	chip->midi_full = 0;
-	if (chip->midi_out && !snd_rawmidi_transmit_empty(chip->midi_out)) {
+	if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
 		bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
 						  MIDI_OUT_BUFFER_SIZE - 1);
 		DE_MID(("Try to send %d bytes...\n", bytes));
@@ -264,9 +264,11 @@ static void snd_echo_midi_output_trigger
 		}
 	} else {
 		if (chip->tinuse) {
-			del_timer(&chip->timer);
 			chip->tinuse = 0;
+			spin_unlock_irq(&chip->lock);
+			del_timer_sync(&chip->timer);
 			DE_MID(("Timer removed\n"));
+			return;
 		}
 	}
 	spin_unlock_irq(&chip->lock);

--
Giuliano.

#4 lore raw
Quoted 42 lines · Giuliano Pochini
Quoted 9 lines · On Tue, 31 Oct 2006 23:26:31 +0100 Jesper Juhl <[email protected]> wrote:
Quoted 8 lines · David Rientjes
Quoted 5 lines · Jesper Juhl

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

Quoted 3 lines
Quoted 3 lines

How about just adding an early test:

    if (!chip->midi_out)
            goto out;

The point of that check is to make sure is doesn't access chip->midi_out when (surprise!) it is NULL. This can only happen in the rare (possible?) case snd_echo_midi_output_close() is called while the timer handler is running. I have another proposal which IMHO is smp-safer that just moving the check. In that case we should also put a spinlock around the chip->midi_out=0 in the snd_echo_midi_output_close() callback.

pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 renamed from pci/echoaudio/midi.c__orig 2006-11-02 20:39:45.000000000 +0100 +1 -1
@@ -213,7 +213,7 @@ static void snd_echo_midi_output_write(u
        sent = bytes = 0;
        spin_lock_irqsave(&chip->lock, flags);
        chip->midi_full = 0;
-       if (chip->midi_out && !snd_rawmidi_transmit_empty(chip->midi_out)) {
+       if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
                bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
                                                  MIDI_OUT_BUFFER_SIZE - 1);
                DE_MID(("Try to send %d bytes...\n", bytes));
@@ -264,9 +264,11 @@ static void snd_echo_midi_output_trigger
                }
        } else {

pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 static void snd_echo_midi_output_trigger · line 274

Quoted 10 lines
pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 renamed from pci/echoaudio/midi.c__orig 2006-11-02 20:39:45.000000000 +0100 +3 -1
                if (chip->tinuse) {
-                       del_timer(&chip->timer);
                        chip->tinuse = 0;
+                       spin_unlock_irq(&chip->lock);
+                       del_timer_sync(&chip->timer);
                        DE_MID(("Timer removed\n"));
+                       return;
                }
        }
        spin_unlock_irq(&chip->lock);

Fine by me.
Let's just get one of the versions pushed into -mm or mainline :)

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
Applied
#5 lore raw

At Thu, 2 Nov 2006 21:22:43 +0100,

Quoted 49 lines · Jesper Juhl
Quoted 47 lines · Giuliano Pochini
Quoted 9 lines · On Tue, 31 Oct 2006 23:26:31 +0100 Jesper Juhl <[email protected]> wrote:
Quoted 8 lines · David Rientjes
Quoted 5 lines · Jesper Juhl

In sound/pci/echoaudio/midi.c::snd_echo_midi_output_write(), there's a risk of dereferencing a NULL 'chip->midi_out'.
This patch contains the obvious fix as also used a bit higher up in the same function.

Quoted 3 lines
Quoted 3 lines

How about just adding an early test:

    if (!chip->midi_out)
            goto out;

The point of that check is to make sure is doesn't access chip->midi_out when (surprise!) it is NULL. This can only happen in the rare (possible?) case snd_echo_midi_output_close() is called while the timer handler is running. I have another proposal which IMHO is smp-safer that just moving the check. In that case we should also put a spinlock around the chip->midi_out=0 in the snd_echo_midi_output_close() callback.

pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 renamed from pci/echoaudio/midi.c__orig 2006-11-02 20:39:45.000000000 +0100 +3 -2
@@ -213,7 +213,7 @@ static void snd_echo_midi_output_write(u
        sent = bytes = 0;
        spin_lock_irqsave(&chip->lock, flags);
        chip->midi_full = 0;
-       if (chip->midi_out && !snd_rawmidi_transmit_empty(chip->midi_out)) {
+       if (!snd_rawmidi_transmit_empty(chip->midi_out)) {
                bytes = snd_rawmidi_transmit_peek(chip->midi_out, buf,
                                                  MIDI_OUT_BUFFER_SIZE - 1);
                DE_MID(("Try to send %d bytes...\n", bytes));
@@ -264,9 +264,11 @@ static void snd_echo_midi_output_trigger
                }
        } else {
                if (chip->tinuse) {
-                       del_timer(&chip->timer);
                        chip->tinuse = 0;
+                       spin_unlock_irq(&chip->lock);
+                       del_timer_sync(&chip->timer);
                        DE_MID(("Timer removed\n"));
Quoted 8 lines

pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 static void snd_echo_midi_output_trigger · line 274

Quoted 5 lines
pci/echoaudio/midi.c 2006-11-02 20:44:22.000000000 +0100 renamed from pci/echoaudio/midi.c__orig 2006-11-02 20:39:45.000000000 +0100 +1 -0
+                       return;
                }
        }
        spin_unlock_irq(&chip->lock);

Fine by me.
Let's just get one of the versions pushed into -mm or mainline :)

I applied it to ALSA tree mm branch.
Thanks.

Takashi