Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp4405612ioo; Tue, 31 May 2022 03:52:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx621pfKt66OYVfqDRWKHls0lBz2IAEAUh36/yEI3VRsPHgTn20WRDIM8ZYADi0jWH0z3+S X-Received: by 2002:a17:90a:2e83:b0:1da:3273:53ab with SMTP id r3-20020a17090a2e8300b001da327353abmr28194706pjd.14.1653994372377; Tue, 31 May 2022 03:52:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653994372; cv=none; d=google.com; s=arc-20160816; b=fA4lAlkc2vzffjlal0+fRwdLChbAzXZi+HSc1InHjkUlEuvC4A2kKBvwWgnyntRyLk aeG7ae/MVCkcuJIYmTuiIi3XydATcjWlC227+ksLnWbhuGO8jAjfZfVDEnnWXQv2xoCy GfE96aEMc1tMGoNynZbfchZTtvi89y+U/oX1OyISFn/38HYpiRpA1uNa/9sVeUD0u9kx 1oGQAsyO1ky/EoF1YTSKnwbIQ8bqGcD44yxNgGVv05KGKjI5rzeCALGQCS+jj8lkA1JL PIhdLrJ7+kXtoVnezDipikr5p+x31+/e9AqY9TpTrYn5YBeDUDh4gciU96YvNwAelIcg xVCw== 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=njLjhwppif66fQJqPIUMOBcO1ivCNKZtf6NBFzzpYZw=; b=Sf92mz+AN1OYg+SXbKPI3ESUfd+othCHKSpUJrOhO8RuQJxpe6y54GdOtHa8Ar7YNR nU5er6tC/0IZCYmmndxoWnvfhIheAsVSuWqaeOkTPsdGO9ADzH0Nt0BOHNJlbozVL+QE HhWzEEQheJjVoceJHQDKdBFhFESiGhb3N6QlHq+ucdivRaiE8SzsWo5rTR9e279KpOiq PU9k1N+Wq7OgP1XCcKAkUIAVo33jin+EGD3stSa/kwZzRtB24SQKeCQSyoCpnxfIL/Zl YwJ1ClB1q5zwdz69nMfg1fhCpnwlkMftQKNg3By1tGnoanRuExPy2IGxb8NJu/DWRJVh xpiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pZyd3541; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c10-20020a056a000aca00b0051b4937f176si9520617pfl.50.2022.05.31.03.52.41; Tue, 31 May 2022 03:52:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pZyd3541; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241864AbiE3Oum (ORCPT + 99 others); Mon, 30 May 2022 10:50:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242013AbiE3Oau (ORCPT ); Mon, 30 May 2022 10:30:50 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2E7F92D26; Mon, 30 May 2022 06:52:35 -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 ams.source.kernel.org (Postfix) with ESMTPS id 6D5BDB80DE8; Mon, 30 May 2022 13:52:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19DCBC3411C; Mon, 30 May 2022 13:52:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653918750; bh=tlTUH1dsjMTE2GgTuQ6cQeF+qzMwp+Na8MzUcVDio9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pZyd3541UJ1YS1fwoBrfwCy+a7SjO/EzdpEefItGjKpcZU05Vk5G31nt1ZxpHw0aZ yZLwScXwJ4Ddzdw20v5J2X5t7cWvt2qcZXrBm7EnFnJXXWVIGSiOLYbPzi6LkYmvBk iJuNH5/qbYgj3WXBGo1MoCSdEQw4eo7XXZP3UCZhiz5yZ/SG+IZukfckduF04v50FF SO8JKz4AQQxVMWBQA3ONmmbW0Z/uQPUwmqFdR7v2MwRgjDaFGRBGV9RW2l0lYFuTga YLOKwAJ+Ud7HJZGoi4gm/f5gMv2qTCBDtwIjHX3rhBcEQV1xNS/9dfonMP+XmUbKmx mOcj+gZITq3vA== 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 4.9 07/24] ALSA: jack: Access input_dev under mutex Date: Mon, 30 May 2022 09:51:54 -0400 Message-Id: <20220530135211.1937674-7-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220530135211.1937674-1-sashal@kernel.org> References: <20220530135211.1937674-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=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 1e84bfb553cf..4742f842b457 100644 --- a/include/sound/jack.h +++ b/include/sound/jack.h @@ -77,6 +77,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 36cfe1c54109..d2f9a92453f2 100644 --- a/sound/core/jack.c +++ b/sound/core/jack.c @@ -48,8 +48,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. */ @@ -58,6 +61,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; } @@ -96,8 +100,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; @@ -122,6 +129,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 */ @@ -242,9 +250,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(); @@ -262,8 +272,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) @@ -303,10 +313,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); @@ -354,6 +368,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 @@ -373,8 +389,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; @@ -393,6 +412,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