Skip to contentSkip to conversation

ALSA: rawmidi: cleanup the get next midi device ioctl

PATCH Superseded Dan Carpenter 5 participants 12 messages Sep 8, 2010 – Sep 9, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: rawmidi: cleanup the get next midi device ioctl

I'm doing an audit to find integer overflows and my static checker complained that in the original code "device + 1" could overflow. The overflow is harmless, but it's still worth cleaning up. The other thing that I noticed is that if you pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then it doesn't return an error code but just tells you that the next device is "device + 1".

I have rewritten it to just return -EINVAL if you pass in a bogus value that's either too high or too low.

sound/core/rawmidi.c +5 -1
index eb68326..f944180 100644
@@ -829,8 +829,12 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 		
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device < 0)
+			return -EINVAL;
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;
 		mutex_lock(&register_mutex);
-		device = device < 0 ? 0 : device + 1;
+		device++;
 		while (device < SNDRV_RAWMIDI_DEVICES) {
 			if (snd_rawmidi_search(card, device))
 				break;
#1 lore raw
Quoted 9 lines · Dan Carpenter

I'm doing an audit to find integer overflows and my static checker complained that in the original code "device + 1" could overflow. The overflow is harmless, but it's still worth cleaning up. The other thing that I noticed is that if you pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then it doesn't return an error code but just tells you that the next device is "device + 1".

I have rewritten it to just return -EINVAL if you pass in a bogus value that's either too high or too low.

A negative value is a valid input.

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 835

Quoted 2 lines
sound/core/rawmidi.c +2 -0
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;

if (device >= SNDRV_RAWMIDI_DEVICES)

Regards,
Clemens

#2 lore raw

PATCH v2 1/1 ALSA: rawmidi: fix the get next midi device ioctl

In reply to Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.

---
V2: In the first version I made negative values return -EINVAL

sound/core/rawmidi.c +2 -0
index eb68326..1633bac 100644
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 		
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {
#3 lore raw

Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl

At Wed, 8 Sep 2010 21:36:41 +0200,

Quoted 17 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.

---
V2: In the first version I made negative values return -EINVAL

sound/core/rawmidi.c +0 -0
index eb68326..1633bac 100644

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 833

Quoted 5 lines
sound/core/rawmidi.c +2 -0
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;

This should be "device >= SNDRV_RAWMIDI_DEVICES".

Takashi

Quoted 4 lines
sound/core/rawmidi.c +0 -0
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {
maintainer
#4 lore raw

Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl

Quoted 20 lines · Takashi Iwai

At Wed, 8 Sep 2010 21:36:41 +0200,

Quoted 18 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.

---
V2: In the first version I made negative values return -EINVAL

sound/core/rawmidi.c +0 -0
index eb68326..1633bac 100644
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
Quoted 6 lines

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 833

Quoted 4 lines
sound/core/rawmidi.c +2 -0
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;

This should be "device >= SNDRV_RAWMIDI_DEVICES".

Also note that this check changes a bit semantics. All other NEXT_DEVICE ioctls returns -1 if the value is beyond the last device (meaning no more devices were found). So the

                if (device == SNDRV_RAWMIDI_DEVICES)
                         device = -1;

check should be

if (device >= SNDRV_RAWMIDI_DEVICES)
device = -1;

... resulting in one line patch.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.

#5 lore raw

PATCH v3 1/1 ALSA: rawmidi: fix the get next midi device ioctl

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then the "next device" should be -1. This function just returns device + 1.

But the main thing is that "device + 1" can lead to a (harmless) integer overflow and that annoys static analysis tools.

---

V2:  In the first version I made negative values return -EINVAL
V3:  We shouldn't return -EINVAL for numbers which are too large but

just set the next device to -1.

sound/core/rawmidi.c +2 -0
index eb68326..df67605 100644
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 		
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES;
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {
#9 lore raw

Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Jaroslav Kysela

At Wed, 8 Sep 2010 23:29:14 +0200 (CEST),

Quoted 37 lines · Jaroslav Kysela
Quoted 20 lines · Takashi Iwai

At Wed, 8 Sep 2010 21:36:41 +0200,

Quoted 18 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.

---
V2: In the first version I made negative values return -EINVAL

sound/core/rawmidi.c +0 -0
index eb68326..1633bac 100644
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
Quoted 6 lines

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 833

Quoted 4 lines
sound/core/rawmidi.c +2 -0
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES)
+			return -EINVAL;

This should be "device >= SNDRV_RAWMIDI_DEVICES".

Also note that this check changes a bit semantics. All other NEXT_DEVICE ioctls returns -1 if the value is beyond the last device (meaning no more devices were found). So the

if (device = SNDRV_RAWMIDI_DEVICES)
device = -1;

Quoted 6 lines

check should be

if (device >= SNDRV_RAWMIDI_DEVICES)
device = -1;

... resulting in one line patch.

But this doesn't work when you pass device = INT_MAX :)

Takashi

#6 lore raw

Re: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Dan Carpenter

At Thu, 9 Sep 2010 00:11:41 +0200,

Quoted 19 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then the "next device" should be -1. This function just returns device + 1.

But the main thing is that "device + 1" can lead to a (harmless) integer overflow and that annoys static analysis tools.

---

V2:  In the first version I made negative values return -EINVAL
V3:  We shouldn't return -EINVAL for numbers which are too large but

just set the next device to -1.

sound/core/rawmidi.c +0 -0
index eb68326..df67605 100644

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 836

Quoted 8 lines
sound/core/rawmidi.c +2 -0
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES;
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {

We still need to cover the case device = SNDRV_RAWMIDI_DEVICES. Also, device is incremented, so it has to be SNDRV_RAWMIDI_DEVICE - 1. i.e.

sound/core/rawmidi.c

Quoted 2 lines
sound/core/rawmidi.c +2 -0
+		if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES - 1;

I applied the fixed patch now.

thanks,

Takashi

#8 lore raw

Re: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Dan Carpenter
Quoted 17 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then the "next device" should be -1. This function just returns device + 1.

But the main thing is that "device + 1" can lead to a (harmless) integer overflow and that annoys static analysis tools.

---

V2:  In the first version I made negative values return -EINVAL
V3:  We shouldn't return -EINVAL for numbers which are too large but

just set the next device to -1.

sound/core/rawmidi.c +0 -0
index eb68326..df67605 100644

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 836

Quoted 8 lines
sound/core/rawmidi.c +2 -0
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES;
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {

i am not the expert here but i sound a good idea to put all device changes into one place. like:

if (device > SNDRV_RAWMIDI_DEVICES )

	device = SNDRV_RAWMIDI_DEVICES;
else if (device < 0 )
	device = 0;
else
        device++;

just my 2 cents,
re,
wh

#10 lore raw

Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless,

It would result in device==INT_MIN, which would make the while loop go through 2^31 values before finding the first MIDI device.

Regards,
Clemens

maintainer
#7 lore raw

Re: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Takashi Iwai
Quoted 34 lines · Takashi Iwai

At Thu, 9 Sep 2010 00:11:41 +0200,

Quoted 19 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then the "next device" should be -1. This function just returns device + 1.

But the main thing is that "device + 1" can lead to a (harmless) integer overflow and that annoys static analysis tools.

---

V2:  In the first version I made negative values return -EINVAL
V3:  We shouldn't return -EINVAL for numbers which are too large but

just set the next device to -1.

sound/core/rawmidi.c +0 -0
index eb68326..df67605 100644

sound/core/rawmidi.c static int snd_rawmidi_control_ioctl(struct snd_card *card, · line 836

Quoted 8 lines
sound/core/rawmidi.c +2 -0
@@ -829,6 +829,8 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
 
 		if (get_user(device, (int __user *)argp))
 			return -EFAULT;
+		if (device > SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES;
 		mutex_lock(&register_mutex);
 		device = device < 0 ? 0 : device + 1;
 		while (device < SNDRV_RAWMIDI_DEVICES) {

We still need to cover the case device = SNDRV_RAWMIDI_DEVICES. Also, device is incremented, so it has to be SNDRV_RAWMIDI_DEVICE - 1. i.e.

Quoted 5 lines

sound/core/rawmidi.c

Quoted 2 lines
sound/core/rawmidi.c +2 -0
+		if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
+			device = SNDRV_RAWMIDI_DEVICES - 1;

I applied the fixed patch now.

Maybe a goto to 'device = -1' line from the condition above might be more light (resulted instruction code size) and understandable for this case.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.

#11 lore raw

Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl

In reply to Clemens Ladisch
Quoted 11 lines · Clemens Ladisch
Quoted 6 lines · Dan Carpenter

If we pass in a device which is higher than SNDRV_RAWMIDI_DEVICES then this function just returns device + 1 which isn't helpful. I've modified it to return -EINVAL instead.

Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless,

It would result in device=INT_MIN, which would make the while loop go through 2^31 values before finding the first MIDI device.

Oh crap. You're right. For some reason I got mixed up.

regards,
dan carpenter

Quoted 3 lines

Regards,
Clemens