]> git.baikalelectronics.ru Git - kernel.git/commit
ALSA: control: Fix racy management of user ctl memory size account
authorTakashi Iwai <tiwai@suse.de>
Thu, 15 Apr 2021 13:18:56 +0000 (15:18 +0200)
committerTakashi Iwai <tiwai@suse.de>
Fri, 16 Apr 2021 07:57:49 +0000 (09:57 +0200)
commit77c31991aeae66d1eeef06d7acb3afc3e35bf10e
tree8c7720ee8e97ab081bcf3b5c38f85d7b0362c358
parent5fe7dfbacc8504ca09d05208b884c7fbc6b4c3aa
ALSA: control: Fix racy management of user ctl memory size account

We've got a report about the possible race in the user control element
counts (card->user_ctl_count), and it was confirmed that the race
wasn't serious in the old code up to 5.12.  There, the value
modification itself was exclusive and protected via a write semaphore,
hence it's at most concurrent reads and evaluations before the
increment.  Since it's only about the soft-limit to avoid the
exhausting memory usage, one-off isn't a big problem at all.

Meanwhile, the relevant code has been largely modified recently, and
now card->user_ctl_count was replaced with card->user_ctl_alloc_size,
and a few more places were added to access this field.  And, in this
new code, it turned out to be more serious: the modifications are
scattered in various places, and a few of them are without protection.
It implies that it may lead to an inconsistent value by racy
accesses.

For addressing it, this patch extends the range covered by the
card->controls_rwsem write lock at snd_ctl_elem_add() so that the all
code paths that modify and refer to card->user_ctl_alloc_size are
protected by the rwsem properly.

The patch adds also comments in a couple of functions to indicate that
they are under the rwsem lock.

Fixes: f14388ba4802 ("ALSA: control: Add memory consumption limit to user controls")
Link: https://lore.kernel.org/r/FEEBF384-44BE-42CF-8FB3-93470933F64F@purdue.edu
Link: https://lore.kernel.org/r/20210415131856.13113-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/control.c