ALSA: rawmidi: Avoid racy info ioctl via ctl device
authorTakashi Iwai <tiwai@suse.de>
Thu, 14 Dec 2017 15:44:12 +0000 (16:44 +0100)
committerBen Hutchings <ben@decadent.org.uk>
Sat, 3 Mar 2018 15:50:47 +0000 (15:50 +0000)
commit734c4d00d1525f28cc5c659fafeac43e7f0b8dec
tree70b060d8e4c3f866cc20e7cb55ad781323e6550c
parent741e32f1400a3441779f87a6bf37ce26bcf49806
ALSA: rawmidi: Avoid racy info ioctl via ctl device

commit c1cfd9025cc394fd137a01159d74335c5ac978ce upstream.

The rawmidi also allows to obtaining the information via ioctl of ctl
API.  It means that user can issue an ioctl to the rawmidi device even
when it's being removed as long as the control device is present.
Although the code has some protection via the global register_mutex,
its range is limited to the search of the corresponding rawmidi
object, and the mutex is already unlocked at accessing the rawmidi
object.  This may lead to a use-after-free.

For avoiding it, this patch widens the application of register_mutex
to the whole snd_rawmidi_info_select() function.  We have another
mutex per rawmidi object, but this operation isn't very hot path, so
it shouldn't matter from the performance POV.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
sound/core/rawmidi.c