From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C769EB64DC for ; Mon, 3 Jul 2023 08:10:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjGCIKk (ORCPT ); Mon, 3 Jul 2023 04:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230214AbjGCIKh (ORCPT ); Mon, 3 Jul 2023 04:10:37 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 847B1C5 for ; Mon, 3 Jul 2023 01:10:36 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3AF9D1F8AC; Mon, 3 Jul 2023 08:10:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1688371835; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JF+yuK7urvWRKC3M9mc+JnxvxoWl/3dAyFeQYEcA1mc=; b=zwMAnXa8uyglsATv4kQP1InR9RqwjBStdOEcr9VqmT/2YUc9BSL7juYog1lwuT7mwSR++1 ngQjg6wCRguej3oYDMzWahB9/1uJrNd7IT+8tUBMFR8fAXhGmUYNqV3KLaQnjL6Wn2zwEV pgMHwS3qcdBmtZbvx675bvGrxMUXd5M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1688371835; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JF+yuK7urvWRKC3M9mc+JnxvxoWl/3dAyFeQYEcA1mc=; b=/QmjuhL4UqKQ7cg+4SmN4Rd1RdV/dSaZj2zD6a6fBFC2TJvoSh5Ib4ONvF4OG3pdHz9zF/ GV+uPI/FiuFQOECw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id D3FB6138FC; Mon, 3 Jul 2023 08:10:34 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id VlKSMnqComRKagAAMHmgww (envelope-from ); Mon, 03 Jul 2023 08:10:34 +0000 Date: Mon, 03 Jul 2023 10:10:34 +0200 Message-ID: <87a5wd76v9.wl-tiwai@suse.de> From: Takashi Iwai To: Tuo Li Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org, baijiaju1990@outlook.com, BassCheck Subject: Re: [PATCH] ALSA: hda: fix a possible null-pointer dereference due to data race in snd_hdac_regmap_sync() In-Reply-To: <20230703031016.1184711-1-islituo@gmail.com> References: <20230703031016.1184711-1-islituo@gmail.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > Signed-off-by: Tuo Li 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 >