Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5867335ioo; Wed, 1 Jun 2022 14:29:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxvT0vlRz8+VfpfrgbLe9EZbayAxpqao8YiH2AGMOKI/uOGTL9lKbNh//LxKdoooWL8V8Wg X-Received: by 2002:a17:902:f685:b0:161:8df3:7f50 with SMTP id l5-20020a170902f68500b001618df37f50mr1423899plg.106.1654118948872; Wed, 01 Jun 2022 14:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654118948; cv=none; d=google.com; s=arc-20160816; b=T0ZlrC9n75yByh56FRnf3b0Pl/RhV91vASD7jPe/KGvBPI2CKiM8h9VSumJh1ewhLP +86wGXoITG1I6rOZ6PsFSf7maYWq/YcXKFzh9zoRMPK+4eMDVLnC1XgffXii5rUhD3mS 8PY9qk1Ry1rgOs/ywV91ENm9QuKdTxHmA1UnGdDCwyRcd8A8h6jZFM8DyzFogijNQfSS nyfyaX8esr69RyEdQjRCnY+EynNjccDgjt49Eu7fc4rwZhRtzXdaAvyLGIHkBKKM49ar diQFJ2rvZfuge0aVr7Ajp+kxKdYs5QHZOQcE+Ii8U1NL1LQG4qnE3bgL2rp1Eu21eV/o QQGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Td8JzA7NTtuMqGb8k796YXRc/rnuYMzQF6CCCUAwzTc=; b=g9ldu0BYqlRMmRheJGRUbgAmYErrzc8JKYSt4I4TgNjn0jpfjgZYfFSDNNFi4XMWun vsY0WQu9caltzfZia2WehzV6oA2TqRAW4BOtuyv+b8nqjyldg+VSCWHEF0bHnGa/Njv9 baN/kI4vQ7Rs7RbPqiC3/d1XAguaK6JH0FrWzxF/hGwgXaJfpX3U54Ug1lJmvvZI6swT 0eDdxC40D0tbjmDREi8VNvYW1cZHxEJgvZdc9v2u/JMjAWv9kzB0UQoiaV0tEqIeVYek cwAKEW5J5yBhdcZcDp3rqF1Wf/f9RowJ+in9d7JkIZi/HtCKWF7CjvXA2cSXCjwtj6dZ p1nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cFURGLVa; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id iw19-20020a170903045300b0016252715440si3618933plb.494.2022.06.01.14.29.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 14:29:08 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=cFURGLVa; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EFB9D2462D5; Wed, 1 Jun 2022 13:18:19 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240595AbiE3O3h (ORCPT + 99 others); Mon, 30 May 2022 10:29:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241486AbiE3ORh (ORCPT ); Mon, 30 May 2022 10:17:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB925532F0; Mon, 30 May 2022 06:47:30 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 294CD60F14; Mon, 30 May 2022 13:47:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65694C3411C; Mon, 30 May 2022 13:47:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653918449; bh=yYvAGK+jRj/Zget7GkGjcBBHehLh7eTbDAzE1XmmZrc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cFURGLVab6uFck4Sijs1Xf1kfl5YBpTU9ZzTyV8F7fW3PHdrFwkEUZ0qZsck/WsDA OecNNQ8rkAQrztL7NizcF1mijiR0XOWqWdglIws8UkUBarrUwslJi+Ppc+WQTTvScm AGIHyC2ac0/CBCAt+f65VXQQiL7gZFYkfyi2dXD0VrTswS9WmIdfgOJJE7MFgK6DQF 9+1REB+dPsGscZGhW53C/i2auV/kJwU6L15YLFTtpNtYWU+i6S8xrZeeO+iHsIXMwj m3tC0r53zWO60H2rHBtYqrp2CpO7NeT8dDpLNMw0FDnhws0Agio8zHlmdztaRkn8Mu M+CEh5Fg5sKqg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: =?UTF-8?q?Amadeusz=20S=C5=82awi=C5=84ski?= , Cezary Rojewski , Takashi Iwai , Sasha Levin , perex@perex.cz, tiwai@suse.com, xkernel.wang@foxmail.com, alsa-devel@alsa-project.org Subject: [PATCH AUTOSEL 5.4 10/55] ALSA: jack: Access input_dev under mutex Date: Mon, 30 May 2022 09:46:16 -0400 Message-Id: <20220530134701.1935933-10-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220530134701.1935933-1-sashal@kernel.org> References: <20220530134701.1935933-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Amadeusz Sławiński [ Upstream commit 1b6a6fc5280e97559287b61eade2d4b363e836f2 ] It is possible when using ASoC that input_dev is unregistered while calling snd_jack_report, which causes NULL pointer dereference. In order to prevent this serialize access to input_dev using mutex lock. Signed-off-by: Amadeusz Sławiński Reviewed-by: Cezary Rojewski Link: https://lore.kernel.org/r/20220412091628.3056922-1-amadeuszx.slawinski@linux.intel.com Signed-off-by: Takashi Iwai Signed-off-by: Sasha Levin --- include/sound/jack.h | 1 + sound/core/jack.c | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/sound/jack.h b/include/sound/jack.h index 9eb2b5ec1ec4..78f3619f3de9 100644 --- a/include/sound/jack.h +++ b/include/sound/jack.h @@ -62,6 +62,7 @@ struct snd_jack { const char *id; #ifdef CONFIG_SND_JACK_INPUT_DEV struct input_dev *input_dev; + struct mutex input_dev_lock; int registered; int type; char name[100]; diff --git a/sound/core/jack.c b/sound/core/jack.c index b00ae6f39f05..e7ac82d46821 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -34,8 +34,11 @@ static int snd_jack_dev_disconnect(struct snd_device *device) #ifdef CONFIG_SND_JACK_INPUT_DEV struct snd_jack *jack = device->device_data; - if (!jack->input_dev) + mutex_lock(&jack->input_dev_lock); + if (!jack->input_dev) { + mutex_unlock(&jack->input_dev_lock); return 0; + } /* If the input device is registered with the input subsystem * then we need to use a different deallocator. */ @@ -44,6 +47,7 @@ static int snd_jack_dev_disconnect(struct snd_device *device) else input_free_device(jack->input_dev); jack->input_dev = NULL; + mutex_unlock(&jack->input_dev_lock); #endif /* CONFIG_SND_JACK_INPUT_DEV */ return 0; } @@ -82,8 +86,11 @@ static int snd_jack_dev_register(struct snd_device *device) snprintf(jack->name, sizeof(jack->name), "%s %s", card->shortname, jack->id); - if (!jack->input_dev) + mutex_lock(&jack->input_dev_lock); + if (!jack->input_dev) { + mutex_unlock(&jack->input_dev_lock); return 0; + } jack->input_dev->name = jack->name; @@ -108,6 +115,7 @@ static int snd_jack_dev_register(struct snd_device *device) if (err == 0) jack->registered = 1; + mutex_unlock(&jack->input_dev_lock); return err; } #endif /* CONFIG_SND_JACK_INPUT_DEV */ @@ -228,9 +236,11 @@ int snd_jack_new(struct snd_card *card, const char *id, int type, return -ENOMEM; } - /* don't creat input device for phantom jack */ - if (!phantom_jack) { #ifdef CONFIG_SND_JACK_INPUT_DEV + mutex_init(&jack->input_dev_lock); + + /* don't create input device for phantom jack */ + if (!phantom_jack) { int i; jack->input_dev = input_allocate_device(); @@ -248,8 +258,8 @@ int snd_jack_new(struct snd_card *card, const char *id, int type, input_set_capability(jack->input_dev, EV_SW, jack_switch_types[i]); -#endif /* CONFIG_SND_JACK_INPUT_DEV */ } +#endif /* CONFIG_SND_JACK_INPUT_DEV */ err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops); if (err < 0) @@ -289,10 +299,14 @@ EXPORT_SYMBOL(snd_jack_new); void snd_jack_set_parent(struct snd_jack *jack, struct device *parent) { WARN_ON(jack->registered); - if (!jack->input_dev) + mutex_lock(&jack->input_dev_lock); + if (!jack->input_dev) { + mutex_unlock(&jack->input_dev_lock); return; + } jack->input_dev->dev.parent = parent; + mutex_unlock(&jack->input_dev_lock); } EXPORT_SYMBOL(snd_jack_set_parent); @@ -340,6 +354,8 @@ EXPORT_SYMBOL(snd_jack_set_key); /** * snd_jack_report - Report the current status of a jack + * Note: This function uses mutexes and should be called from a + * context which can sleep (such as a workqueue). * * @jack: The jack to report status for * @status: The current status of the jack @@ -359,8 +375,11 @@ void snd_jack_report(struct snd_jack *jack, int status) status & jack_kctl->mask_bits); #ifdef CONFIG_SND_JACK_INPUT_DEV - if (!jack->input_dev) + mutex_lock(&jack->input_dev_lock); + if (!jack->input_dev) { + mutex_unlock(&jack->input_dev_lock); return; + } for (i = 0; i < ARRAY_SIZE(jack->key); i++) { int testbit = SND_JACK_BTN_0 >> i; @@ -379,6 +398,7 @@ void snd_jack_report(struct snd_jack *jack, int status) } input_sync(jack->input_dev); + mutex_unlock(&jack->input_dev_lock); #endif /* CONFIG_SND_JACK_INPUT_DEV */ } EXPORT_SYMBOL(snd_jack_report); -- 2.35.1