ALSA: pcm: Move rwsem lock inside snd_ctl_elem_read to prevent UAF
authorClement Lecigne <clecigne@google.com>
Fri, 13 Jan 2023 12:07:45 +0000 (13:07 +0100)
committerSalvatore Bonaccorso <carnil@debian.org>
Sat, 21 Jan 2023 14:35:48 +0000 (14:35 +0000)
Origin: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=df02234e6b87d2a9a82acd3198e44bdeff8488c6
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2023-0266

[ Note: this is a fix that works around the bug equivalently as the
  two upstream commits:
   1fa4445f9adf ("ALSA: control - introduce snd_ctl_notify_one() helper")
   56b88b50565c ("ALSA: pcm: Move rwsem lock inside snd_ctl_elem_read to prevent UAF")
  but in a simpler way to fit with older stable trees -- tiwai ]

Add missing locking in ctl_elem_read_user/ctl_elem_write_user which can be
easily triggered and turned into an use-after-free.

Example code paths with SNDRV_CTL_IOCTL_ELEM_READ:

64-bits:
snd_ctl_ioctl
  snd_ctl_elem_read_user
    [takes controls_rwsem]
    snd_ctl_elem_read [lock properly held, all good]
    [drops controls_rwsem]

32-bits (compat):
snd_ctl_ioctl_compat
  snd_ctl_elem_write_read_compat
    ctl_elem_write_read
      snd_ctl_elem_read [missing lock, not good]

CVE-2023-0266 was assigned for this issue.

Signed-off-by: Clement Lecigne <clecigne@google.com>
Cc: stable@kernel.org # 5.12 and older
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Reviewed-by: Jaroslav Kysela <perex@perex.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Gbp-Pq: Topic bugfix/all
Gbp-Pq: Name ALSA-pcm-Move-rwsem-lock-inside-snd_ctl_elem_read-to.patch

sound/core/control_compat.c

index 97467f6a32a135790d62dd036c0d55aa9f53828f..980ab3580f1b7cb35c659ca8d180d17ad06029df 100644 (file)
@@ -304,7 +304,9 @@ static int ctl_elem_read_user(struct snd_card *card,
        err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
        if (err < 0)
                goto error;
+       down_read(&card->controls_rwsem);
        err = snd_ctl_elem_read(card, data);
+       up_read(&card->controls_rwsem);
        if (err < 0)
                goto error;
        err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
@@ -332,7 +334,9 @@ static int ctl_elem_write_user(struct snd_ctl_file *file,
        err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
        if (err < 0)
                goto error;
+       down_write(&card->controls_rwsem);
        err = snd_ctl_elem_write(card, file, data);
+       up_write(&card->controls_rwsem);
        if (err < 0)
                goto error;
        err = copy_ctl_value_to_user(userdata, valuep, data, type, count);