linux/sound
Takashi Iwai 9661bf674d ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock
commit bc55cfd5718c7c23e5524582e9fa70b4d10f2433 upstream.

syzbot caught a potential deadlock between the PCM
runtime->buffer_mutex and the mm->mmap_lock.  It was brought by the
recent fix to cover the racy read/write and other ioctls, and in that
commit, I overlooked a (hopefully only) corner case that may take the
revert lock, namely, the OSS mmap.  The OSS mmap operation
exceptionally allows to re-configure the parameters inside the OSS
mmap syscall, where mm->mmap_mutex is already held.  Meanwhile, the
copy_from/to_user calls at read/write operations also take the
mm->mmap_lock internally, hence it may lead to a AB/BA deadlock.

A similar problem was already seen in the past and we fixed it with a
refcount (in commit b248371628).  The former fix covered only the
call paths with OSS read/write and OSS ioctls, while we need to cover
the concurrent access via both ALSA and OSS APIs now.

This patch addresses the problem above by replacing the buffer_mutex
lock in the read/write operations with a refcount similar as we've
used for OSS.  The new field, runtime->buffer_accessing, keeps the
number of concurrent read/write operations.  Unlike the former
buffer_mutex protection, this protects only around the
copy_from/to_user() calls; the other codes are basically protected by
the PCM stream lock.  The refcount can be a negative, meaning blocked
by the ioctls.  If a negative value is seen, the read/write aborts
with -EBUSY.  In the ioctl side, OTOH, they check this refcount, too,
and set to a negative value for blocking unless it's already being
accessed.

Reported-by: syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com
Fixes: dca947d4d26d ("ALSA: pcm: Fix races among concurrent read/write and buffer changes")
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/000000000000381a0d05db622a81@google.com
Link: https://lore.kernel.org/r/20220330120903.4738-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
[OP: backport to 5.4: adjusted context]
Signed-off-by: Ovidiu Panait <ovidiu.panait@windriver.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-05-12 12:23:49 +02:00
..
ac97 ALSA: ac97: fix PM reference leak in ac97_bus_remove() 2021-07-20 16:10:42 +02:00
aoa ALSA: aoa: onyx: always initialize register read value 2019-07-29 09:21:39 +02:00
arm
atmel
core ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock 2022-05-12 12:23:49 +02:00
drivers ALSA: drivers: opl3: Fix incorrect use of vp->state 2021-12-29 12:23:36 +01:00
firewire ALSA: fireworks: fix wrong return count shorter than expected by 4 bytes 2022-05-12 12:23:40 +02:00
hda ALSA: hda: hdac_stream: fix potential locking issue in snd_hdac_stream_assign() 2021-11-26 10:47:23 +01:00
i2c ALSA: i2c: ak4xxx-adda: Fix a possible null pointer dereference in build_adc_controls() 2019-07-26 14:25:37 +02:00
isa ALSA: cs4236: fix an incorrect NULL check on list iterator 2022-04-15 14:18:00 +02:00
mips
oss sound: dmasound_atari: Mark expected switch fall-through 2019-07-30 09:36:13 +02:00
parisc
pci ALSA: hda/realtek: Add quirk for Clevo PD50PNT 2022-04-20 09:19:38 +02:00
pcmcia
ppc ALSA: ppc: fix error return code in snd_pmac_probe() 2021-07-20 16:10:44 +02:00
sh ALSA: sh: Fix compile warning wrt const 2020-02-24 08:36:42 +01:00
soc ASoC: dmaengine: Restore NULL prepare_slave_config() callback 2022-05-12 12:23:44 +02:00
sparc ALSA: sparc: Mark expected switch fall-throughs 2019-07-30 09:37:01 +02:00
spi ALSA: spi: Add check for clk_enable() 2022-04-15 14:18:08 +02:00
synth ALSA: synth: missing check for possible NULL after the call to kstrdup 2021-11-17 09:48:19 +01:00
usb ALSA: usb-audio: Fix undefined behavior due to shift overflowing the constant 2022-04-27 13:50:47 +02:00
x86 ALSA: intel_hdmi: Fix reference to PCM buffer address 2022-03-08 19:07:46 +01:00
xen
Kconfig
Makefile
ac97_bus.c
last.c
sound_core.c sound: fix a memory leak bug 2019-08-08 08:18:32 +02:00