[PATCH] ALSA: hda: fix a possible null-pointer dereference due to data race in snd_hdac_regmap_sync()

Tuo Li posted 1 patch 2 years, 7 months ago
sound/hda/hdac_regmap.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
[PATCH] ALSA: hda: fix a possible null-pointer dereference due to data race in snd_hdac_regmap_sync()
Posted by Tuo Li 2 years, 7 months ago
The variable codec->regmap is often protected by the lock 
codec->regmap_lock when is accessed. However, it is accessed without 
holding the lock when is accessed in snd_hdac_regmap_sync():

  if (codec->regmap)

In my opinion, this may be a harmful race, because if codec->regmap is 
set to NULL right after the condition is checked, a null-pointer 
dereference can occur in the called function regcache_sync():

  map->lock(map->lock_arg); --> Line 360 in drivers/base/regmap/regcache.c

To fix this possible null-pointer dereference caused by data race, the 
mutex_lock coverage is extended to protect the if statement as well as the 
function call to regcache_sync().

Reported-by: BassCheck <bass@buaa.edu.cn>
Signed-off-by: Tuo Li <islituo@gmail.com>
---
 sound/hda/hdac_regmap.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/sound/hda/hdac_regmap.c b/sound/hda/hdac_regmap.c
index fe3587547cfe..39610a15bcc9 100644
--- a/sound/hda/hdac_regmap.c
+++ b/sound/hda/hdac_regmap.c
@@ -597,10 +597,9 @@ EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw_once);
  */
 void snd_hdac_regmap_sync(struct hdac_device *codec)
 {
-	if (codec->regmap) {
-		mutex_lock(&codec->regmap_lock);
+	mutex_lock(&codec->regmap_lock);
+	if (codec->regmap)
 		regcache_sync(codec->regmap);
-		mutex_unlock(&codec->regmap_lock);
-	}
+	mutex_unlock(&codec->regmap_lock);
 }
 EXPORT_SYMBOL_GPL(snd_hdac_regmap_sync);
-- 
2.34.1
Re: [PATCH] ALSA: hda: fix a possible null-pointer dereference due to data race in snd_hdac_regmap_sync()
Posted by Takashi Iwai 2 years, 7 months ago
On Mon, 03 Jul 2023 05:10:16 +0200,
Tuo Li wrote:
> 
> The variable codec->regmap is often protected by the lock 
> codec->regmap_lock when is accessed. However, it is accessed without 
> holding the lock when is accessed in snd_hdac_regmap_sync():
> 
>   if (codec->regmap)
> 
> In my opinion, this may be a harmful race, because if codec->regmap is 
> set to NULL right after the condition is checked, a null-pointer 
> dereference can occur in the called function regcache_sync():
> 
>   map->lock(map->lock_arg); --> Line 360 in drivers/base/regmap/regcache.c
> 
> To fix this possible null-pointer dereference caused by data race, the 
> mutex_lock coverage is extended to protect the if statement as well as the 
> function call to regcache_sync().
> 
> Reported-by: BassCheck <bass@buaa.edu.cn>
> Signed-off-by: Tuo Li <islituo@gmail.com>

Practically seen, it's pretty harmless, as the flag is set at the very
beginning of the codec driver probe, while snd_hdac_regmap_sync() is
used only in the runtime PM resume that must not happen during the
probe.  IOW, codec->regmap_lock itself isn't really meant to
protectthe change of codec->regmap flag.

OTOH, your change makes things more consistent, so I applied the patch
now.


Thanks!

Takashi


> ---
>  sound/hda/hdac_regmap.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/sound/hda/hdac_regmap.c b/sound/hda/hdac_regmap.c
> index fe3587547cfe..39610a15bcc9 100644
> --- a/sound/hda/hdac_regmap.c
> +++ b/sound/hda/hdac_regmap.c
> @@ -597,10 +597,9 @@ EXPORT_SYMBOL_GPL(snd_hdac_regmap_update_raw_once);
>   */
>  void snd_hdac_regmap_sync(struct hdac_device *codec)
>  {
> -	if (codec->regmap) {
> -		mutex_lock(&codec->regmap_lock);
> +	mutex_lock(&codec->regmap_lock);
> +	if (codec->regmap)
>  		regcache_sync(codec->regmap);
> -		mutex_unlock(&codec->regmap_lock);
> -	}
> +	mutex_unlock(&codec->regmap_lock);
>  }
>  EXPORT_SYMBOL_GPL(snd_hdac_regmap_sync);
> -- 
> 2.34.1
>