Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3640308ybz; Mon, 20 Apr 2020 06:49:09 -0700 (PDT) X-Google-Smtp-Source: APiQypJw/xP7wG0cpGDrvLk8nppGssrKsvwMbSO3bjv+tYeTw8HFaYK4WxQuKHeHuWSyl8LUF8SK X-Received: by 2002:a17:906:310e:: with SMTP id 14mr16812332ejx.177.1587390549677; Mon, 20 Apr 2020 06:49:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587390549; cv=none; d=google.com; s=arc-20160816; b=jnQhP01ORsHwJ2EQYYg3i0+ahNzyp93KVzDQpU1i1ZuN/eeM4nkI8HH/XQ7/BpG7/c 99USqfMUl3gYI5HfUt0/1IXQf3yPXltR2a593t+Vg+ugWffoLmlHtzkOQLv4ZWtMQF5t iyOdiWcR1ox7r4qTzO2k06r9kfWvEfEh0b7+shVXw5cKGEjOTC5m+M0QGNMmmyhTRKYH sW/nFooLFk90qeb20bR73hsf97+25TpmfVSarLUMnVr1Xk9/noF7pOobgknIBcwacTjC kfy9Cm25EXtvjACyieXqNfB/MJvgn54oVbMK9Dq7XyKruKZyMYFo5WflOj4/xcL5lU+c u+Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SC2rD/a7Yvc6crUlWadwZgrAaWE8DTfWtr7xGJ3Xzjs=; b=X/2ekq7zrknkOH/o61FzyxVsdWqiFJNox4XQLUoJaB7fkkkH+AfUWYZLLjlgo+RokB HpMTv1W4ZZv85y8AHSvKke9z0dpg/4oc5tcXt7O5SdjjYiYCV4wp/0+RM9xU4feJKwhZ DoDF+sT+7PIjFChcfXPiPJyAKSO1WcgpNzBbtzb+JNu1ZuMzJ42YjMZ+yFVHeasKJmjY Az4jCAKgkF5B2DR9+fAOJuIbvZTkQLoR6qz3MLkGrzq0j6le4hv0xwS2iVsjVoVeVfr+ 3u8puXgxOkcWAE9QXrjvistCXGXgotICBd3RjALmoX++BcIqoMrq6aueBLh5E1iJEvth wXGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=A0en1bzV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v3si586250ede.381.2020.04.20.06.48.45; Mon, 20 Apr 2020 06:49:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=A0en1bzV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728089AbgDTMls (ORCPT + 99 others); Mon, 20 Apr 2020 08:41:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:34488 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728073AbgDTMlo (ORCPT ); Mon, 20 Apr 2020 08:41:44 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7FE2E20735; Mon, 20 Apr 2020 12:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587386504; bh=3w05jCYjIhoDyDncLmt8aY0aq/NFMnH9Bfe0njl5I1E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A0en1bzV10ZcECkn4SuSifbIaOH0DiatyCgRFik3kmHFN8vX9tB0lqYZcYyDezp/Z wjI86Fmf5lQdFvWj6QMJznif9gHJI+Q4V3FCz21GsA7JoSxkphOvKXU8bWl6RviNxt am4rnaVxxeLk4JO08p96c4zq6skgwQIUEupPoK8w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sumit Garg , Johannes Berg Subject: [PATCH 5.5 50/65] mac80211: fix race in ieee80211_register_hw() Date: Mon, 20 Apr 2020 14:38:54 +0200 Message-Id: <20200420121517.737681332@linuxfoundation.org> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200420121505.909671922@linuxfoundation.org> References: <20200420121505.909671922@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sumit Garg commit 52e04b4ce5d03775b6a78f3ed1097480faacc9fd upstream. A race condition leading to a kernel crash is observed during invocation of ieee80211_register_hw() on a dragonboard410c device having wcn36xx driver built as a loadable module along with a wifi manager in user-space waiting for a wifi device (wlanX) to be active. Sequence diagram for a particular kernel crash scenario: user-space ieee80211_register_hw() ieee80211_tasklet_handler() ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | | | |<---phy0----wiphy_register() | |-----iwd if_add---->| | | |<---IRQ----(RX packet) | Kernel crash | | due to unallocated | | workqueue. | | | | | alloc_ordered_workqueue() | | | | | Misc wiphy init. | | | | | ieee80211_if_add() | | | | As evident from above sequence diagram, this race condition isn't specific to a particular wifi driver but rather the initialization sequence in ieee80211_register_hw() needs to be fixed. So re-order the initialization sequence and the updated sequence diagram would look like: user-space ieee80211_register_hw() ieee80211_tasklet_handler() ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | | | | alloc_ordered_workqueue() | | | | | Misc wiphy init. | | | | |<---phy0----wiphy_register() | |-----iwd if_add---->| | | |<---IRQ----(RX packet) | | | | ieee80211_if_add() | | | | Cc: stable@vger.kernel.org Signed-off-by: Sumit Garg Link: https://lore.kernel.org/r/1586254255-28713-1-git-send-email-sumit.garg@linaro.org [Johannes: fix rtnl imbalances] Signed-off-by: Johannes Berg Signed-off-by: Greg Kroah-Hartman --- net/mac80211/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) --- a/net/mac80211/main.c +++ b/net/mac80211/main.c @@ -1051,7 +1051,7 @@ int ieee80211_register_hw(struct ieee802 local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC; if (hw->max_signal <= 0) { result = -EINVAL; - goto fail_wiphy_register; + goto fail_workqueue; } } @@ -1113,7 +1113,7 @@ int ieee80211_register_hw(struct ieee802 result = ieee80211_init_cipher_suites(local); if (result < 0) - goto fail_wiphy_register; + goto fail_workqueue; if (!local->ops->remain_on_channel) local->hw.wiphy->max_remain_on_channel_duration = 5000; @@ -1139,10 +1139,6 @@ int ieee80211_register_hw(struct ieee802 local->hw.wiphy->max_num_csa_counters = IEEE80211_MAX_CSA_COUNTERS_NUM; - result = wiphy_register(local->hw.wiphy); - if (result < 0) - goto fail_wiphy_register; - /* * We use the number of queues for feature tests (QoS, HT) internally * so restrict them appropriately. @@ -1198,9 +1194,9 @@ int ieee80211_register_hw(struct ieee802 goto fail_flows; rtnl_lock(); - result = ieee80211_init_rate_ctrl_alg(local, hw->rate_control_algorithm); + rtnl_unlock(); if (result < 0) { wiphy_debug(local->hw.wiphy, "Failed to initialize rate control algorithm\n"); @@ -1254,6 +1250,12 @@ int ieee80211_register_hw(struct ieee802 local->sband_allocated |= BIT(band); } + result = wiphy_register(local->hw.wiphy); + if (result < 0) + goto fail_wiphy_register; + + rtnl_lock(); + /* add one default STA interface if supported */ if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) && !ieee80211_hw_check(hw, NO_AUTO_VIF)) { @@ -1293,17 +1295,17 @@ int ieee80211_register_hw(struct ieee802 #if defined(CONFIG_INET) || defined(CONFIG_IPV6) fail_ifa: #endif + wiphy_unregister(local->hw.wiphy); + fail_wiphy_register: rtnl_lock(); rate_control_deinitialize(local); ieee80211_remove_interfaces(local); - fail_rate: rtnl_unlock(); + fail_rate: fail_flows: ieee80211_led_exit(local); destroy_workqueue(local->workqueue); fail_workqueue: - wiphy_unregister(local->hw.wiphy); - fail_wiphy_register: if (local->wiphy_ciphers_allocated) kfree(local->hw.wiphy->cipher_suites); kfree(local->int_scan_req); @@ -1353,8 +1355,8 @@ void ieee80211_unregister_hw(struct ieee skb_queue_purge(&local->skb_queue_unreliable); skb_queue_purge(&local->skb_queue_tdls_chsw); - destroy_workqueue(local->workqueue); wiphy_unregister(local->hw.wiphy); + destroy_workqueue(local->workqueue); ieee80211_led_exit(local); kfree(local->int_scan_req); }