ALSA: pcm: Fix potential data race at PCM memory allocation helpers
authorTakashi Iwai <tiwai@suse.de>
Mon, 3 Jul 2023 11:24:30 +0000 (13:24 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 3 Jul 2023 12:52:27 +0000 (14:52 +0200)
commitbd55842ed998a622ba6611fe59b3358c9f76773d
tree10854a48c3a5c0a0eec4320a4cdec33d8f98a7ef
parent1f4a08fed450db87fbb5ff5105354158bdbe1a22
ALSA: pcm: Fix potential data race at PCM memory allocation helpers

The PCM memory allocation helpers have a sanity check against too many
buffer allocations.  However, the check is performed without a proper
lock and the allocation isn't serialized; this allows user to allocate
more memories than predefined max size.

Practically seen, this isn't really a big problem, as it's more or
less some "soft limit" as a sanity check, and it's not possible to
allocate unlimitedly.  But it's still better to address this for more
consistent behavior.

The patch covers the size check in do_alloc_pages() with the
card->memory_mutex, and increases the allocated size there for
preventing the further overflow.  When the actual allocation fails,
the size is decreased accordingly.

Reported-by: BassCheck <bass@buaa.edu.cn>
Reported-by: Tuo Li <islituo@gmail.com>
Link: https://lore.kernel.org/r/CADm8Tek6t0WedK+3Y6rbE5YEt19tML8BUL45N2ji4ZAz1KcN_A@mail.gmail.com
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Cc: <stable@vger.kernel.org>
Link: https://lore.kernel.org/r/20230703112430.30634-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/pcm_memory.c