ALSA: rawmidi: cleanup the get next midi device ioctl
Subsystems (2)
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
@@ -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 (®ister_mutex); - device = device <0 ?0 : device +1 ; + device++;while (device <SNDRV_RAWMIDI_DEVICES ) {if (snd_rawmidi_search (card, device))break ;
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_DEVICESthen 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
-EINVALif 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
PATCH v2 1/1 ALSA: rawmidi: fix the get next midi device ioctl
In reply to Dan CarpenterIf 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
@@ -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 (®ister_mutex); device = device <0 ?0 : device +1 ;while (device <SNDRV_RAWMIDI_DEVICES ) {
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_DEVICESthen this function just returns device + 1 which isn't helpful. I've modified it to return-EINVALinstead.Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return-EINVALsound/core/rawmidi.c +0
-0
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
-0mutex_lock (®ister_mutex); device = device <0 ?0 : device +1 ;while (device <SNDRV_RAWMIDI_DEVICES ) {
Jaroslav Kysela
maintainerRe: [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_DEVICESthen this function just returns device + 1 which isn't helpful. I've modified it to return-EINVALinstead.Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return-EINVALsound/core/rawmidi.c +0
-0@@ -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
-0if (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.
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
@@ -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 (®ister_mutex); device = device <0 ?0 : device +1 ;while (device <SNDRV_RAWMIDI_DEVICES ) {
Re: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl
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_DEVICESthen 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.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return -EINVAL V3: We shouldn't return -EINVAL for numbers which are too large butjust set the next device to -1.
sound/core/rawmidi.c +0
-0
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 (®ister_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
Jaroslav Kysela
maintainerRe: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl
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_DEVICESthen 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.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return -EINVAL V3: We shouldn't return -EINVAL for numbers which are too large butjust set the next device to -1.
sound/core/rawmidi.c +0
-0sound/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 (®ister_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 beSNDRV_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.
Re: [patch v3] ALSA: rawmidi: fix the get next midi device ioctl
In reply to Dan CarpenterQuoted 17 lines · Dan Carpenter
If we pass in a device which is higher than
SNDRV_RAWMIDI_DEVICESthen 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.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return -EINVAL V3: We shouldn't return -EINVAL for numbers which are too large butjust set the next device to -1.
sound/core/rawmidi.c +0
-0
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 (®ister_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
Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl
In reply to Jaroslav KyselaAt 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_DEVICESthen this function just returns device + 1 which isn't helpful. I've modified it to return-EINVALinstead.Also Smatch complains because the "device + 1" could be an integer overflow. It's harmless, but we may as well silence the warning.
Signed-off-by Dan Carpenter <[email protected]>---
V2: In the first version I made negative values return-EINVALsound/core/rawmidi.c +0
-0@@ -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
-0if (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_DEVICEioctls returns -1 if the value is beyond the last device (meaning no more devices were found). So theif (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
Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl
In reply to Dan CarpenterQuoted 6 lines · Dan Carpenter
If we pass in a device which is higher than
SNDRV_RAWMIDI_DEVICESthen this function just returns device + 1 which isn't helpful. I've modified it to return-EINVALinstead.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
Re: [patch v2] ALSA: rawmidi: fix the get next midi device ioctl
Quoted 11 lines · Clemens Ladisch
Quoted 6 lines · Dan Carpenter
If we pass in a device which is higher than
SNDRV_RAWMIDI_DEVICESthen this function just returns device + 1 which isn't helpful. I've modified it to return-EINVALinstead.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