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 alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 96210EB64D7 for ; Mon, 26 Jun 2023 07:11:55 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 80A30826; Mon, 26 Jun 2023 09:11:02 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 80A30826 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1687763512; bh=JESBQ+7BRnenRvzyvCXCYXpiFjqBYYuA0oiVp9pCXSs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=aGSBv/2EFdcaac5JIozJ3wXUkRVOOEjRAj7CTVED6EM8+oHYlPiCgjKrnw92fvQzf MbSMtaCvMOocDtMb17x21ZbPPLjkDFClrGG/CA56SIVoybtkZ0fYl0Mf8/5nrrspE3 Y2ZMLfyoiDLOJuKwEkZ6sO3Z00uBSNt9vBmvpNNg= Received: by alsa1.perex.cz (Postfix, from userid 50401) id E047BF80246; Mon, 26 Jun 2023 09:11:01 +0200 (CEST) Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 78667F80246; Mon, 26 Jun 2023 09:11:01 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 73483F80246; Mon, 26 Jun 2023 09:09:06 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 65635F80093 for ; Mon, 26 Jun 2023 09:08:55 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 65635F80093 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=nicTZfve; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=y9fpu42H 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-out1.suse.de (Postfix) with ESMTPS id ACF81218B8; Mon, 26 Jun 2023 07:08:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1687763335; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RWp9iBLJZSk2uRxV2vG2dj40DxJ+Lusxw9MuXwf/nTY=; b=nicTZfvexsoypP1428uP5XWujBsWDzihiVQ/qMRMXsLqfjRByx4/Ci2eUp+BeQVenUDbjL nVajufgf/xWNyQpa0H2e3wmWdV3c4h9kG2fgJZoF0/9yqY6vwcGXArkil2Icyr+dLIM7G8 qxl3AKK0FGuStBR4kuqyRgxphOSjk0M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1687763335; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RWp9iBLJZSk2uRxV2vG2dj40DxJ+Lusxw9MuXwf/nTY=; b=y9fpu42HTeFcOVOYOuTrVliWYQVOKTGXynQ75YvERbOcyHeCDjQpHhqZEZZTg+1ji8O7pO y65rW++qYLWNlxBQ== 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 7ED4113483; Mon, 26 Jun 2023 07:08:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Z4r2HYc5mWQANAAAMHmgww (envelope-from ); Mon, 26 Jun 2023 07:08:55 +0000 Date: Mon, 26 Jun 2023 09:08:54 +0200 Message-ID: <877crqwvi1.wl-tiwai@suse.de> From: Takashi Iwai To: Tuo Li Cc: perex@perex.cz, tiwai@suse.com, alsa-devel@alsa-project.org, Linux Kernel , baijiaju1990@outlook.com Subject: Re: [BUG] ALSA: core: pcm_memory: a possible data race in do_alloc_pages() In-Reply-To: References: 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=ISO-8859-1 Content-Transfer-Encoding: 8bit Message-ID-Hash: KS6G3T2VHW2PBWC6TF35FJTOHT3Y6BPP X-Message-ID-Hash: KS6G3T2VHW2PBWC6TF35FJTOHT3Y6BPP X-MailFrom: tiwai@suse.de X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Mon, 26 Jun 2023 05:42:29 +0200, Tuo Li wrote: > > > Hello, > > Our static analysis tool finds a possible data race in ALSA in Linux 6.4.0. > > In some functions, the field snd_card.total_pcm_alloc_bytes is accessed > with holding the lock snd_card.memory_mutex. Here is an example: > >   do_free_pages() --> Line 57 >     mutex_lock(&card->memory_mutex); --> Line 61 (Lock card->memory_mutex) >     card->total_pcm_alloc_bytes -= dmab->bytes;  --> Line 63 (Access  card-> > total_pcm_alloc_bytes) > > However, in the function do_alloc_pages(): > >   if (max_alloc_per_card && >     card->total_pcm_alloc_bytes + size > max_alloc_per_card) --> Line 41 > > the variable card->total_pcm_alloc_bytes is accessed without holding > the lock card->memory_mutex, and thus a data race can occur. > > In my opinion, this data race may be harmful, because the value of > card->total_pcm_alloc_bytes may be changed by another thread after > the if check. Therefore, its value may be too large after Line 51 and can > cause memory bugs such as buffer overflow: > >   card->total_pcm_alloc_bytes += dmab->bytes;  --> Line 51 > > I am not quite sure whether this possible data race is real and how to > fix it if it is real. > > Any feedback would be appreciated, thanks! > > Reported-by: BassCheck It's a bit racy indeed, but the effect is almost negligible. The size check there is merely a sanity check, and allocating more bytes doesn't mean to conflict against anything practically. That said, it's a better-to-be-addressed bug, but nothing too serious. thanks, Takashi