Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2605707lqp; Mon, 25 Mar 2024 04:19:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX7A+a4pcQCw8aRR+GNjH/ZiTv3INEyIyMK460VDPTNDCzuuMxwC5DH375QydJsNIBY8OBAf8tM06SzOC7UPPeY1C6rW5DytVWyveKJMA== X-Google-Smtp-Source: AGHT+IHSZy1/OGoWDKXK/JCVRVlvZN0SipQdi3UaDSOaL+oaIGKQHuK/lWvkRJAvtPJ19oCpNR9C X-Received: by 2002:a17:902:ec87:b0:1dc:adad:f54b with SMTP id x7-20020a170902ec8700b001dcadadf54bmr7209526plg.44.1711365547743; Mon, 25 Mar 2024 04:19:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711365547; cv=pass; d=google.com; s=arc-20160816; b=NJ0P6aza7Klhgaah/keMhFUFYgBeR75ja75XdB0uu882l4dRsXURr1hOtgG/HwkBXA r7kCDZOJbqMCmb2str1moql7lvTiqIljgJfcx+U5sqdVqT27kOthZsMbrTGufH2rj7DZ WpW6rdwZcjZMrs2tUKzZBmhfGl1NZdg+lSYTInuzB85lPXU3pn2xH4nkSMOZTDZVCIR4 qUXRSxXQxv+cVTFHDdnoPQZURtmMGwRACkyPwzs3ggameGNz5nbO1e+AgDI2JDiOlZU3 /EjnZZTZQ93c/Dcff7r5mmHf6/TcBNq4SJ4ALt6TGqIi04kIzX8QDM2VKNXHNi7bnb9V 7lUw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dDLEb9hELSzJS1abKiqQPMzo5xIfbgrGSFnBQJPDB48=; fh=QjpbeGVwognWl3PIZE4AWsCuylyHvllwVo1MB9wPxTE=; b=KqcqHDocku+xtMNeww6i7MDmCtCzzN1Sj27MzgTRoNydE+tWrtXpoZ0xR3gKIAKJ5k Zvas9awffDhPncTeaggQs+Kwg6xBaly2hi24ZDPQrmMeM60GfZCkBslQii01FW4BPLGD n/bdLZHFpg38HnuH1HNV8EkK+m44UdHTlriHoQXT1vPXF0CBrU+++/AeRef1av+yubNR H4+SMVu8OVQeNBervx43dfHzYlWsCBu5omlZqRTvDTBxyzcL94688Ks24lz8IflMIQMY uOPVsZbwD/r3yrUDK/b7ISl4qH9Ii6b0bAhXuXNQ33qWZRq15WHxj5j6QOuKM7K8ujnE 4KfA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ciyyJTap; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s11-20020a170902a50b00b001dd779ed4c4si4863921plq.218.2024.03.25.04.19.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 04:19:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-115886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ciyyJTap; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 1568FB35A07 for ; Mon, 25 Mar 2024 09:39:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 81EAA22751F; Mon, 25 Mar 2024 03:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ciyyJTap" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A34A14601D; Sun, 24 Mar 2024 23:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322079; cv=none; b=nZ9Iq78QYuxDt5BQg1baR4FYDnm9NAm3ncdmwY2Z7UMQcRD32TtXLbbk68M123Ji4D/iEHs6QNoAEgsxSN3BL5xQ+X9Za9qquPThK0bS8fWx5Tvy+7auiqhlHf/qwJP5mSlSpQ6xDxJSI4f6rx26HDUDW0LJh90Da2v/hsJdz/A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322079; c=relaxed/simple; bh=XPaa/mrroDUfMDpkjAsvH2WF2Yb7bzR0M63O/5YR8l4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=T1v5L1nipqwGYM+sMsIBVuc0ryLCT7zb246xLGfXWtpSZcu+f+w5diCllTiuFBZaZOOzi45nIpUwbN+KZLnXI9sse+XfUY01T4EZ6Kz5u10BvKST7bJnMpigeoFilGVo3vKqsodMfnLy4ADNN3mhWKk/0D4Ybx3tY1ogHLvPVMw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ciyyJTap; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6559FC433C7; Sun, 24 Mar 2024 23:14:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711322079; bh=XPaa/mrroDUfMDpkjAsvH2WF2Yb7bzR0M63O/5YR8l4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ciyyJTapkRaPTmE3Yo5ZJku523Dk2dkQVaCNiyBlY2n89Ar4Hu6+SkhbpTK0tKXH8 QPK9Ij+lxbz5WpojeTiYXA8uMCyXrVmMP1BCefMMa2eUGR4rn2YsVK3g0Rs3MY1ZSg wAGoG0fJN0Xg43wTYVOkhfX0vqNJchyl/2YsOJRvNTECxOFDWVNTTyx9ss8tD+/nwC F/XEh617VcyEHQ3FbWFuMp9OqYZlUtg/V+SKTUJ0TmD4R3gqOafm/I0fApNEn9sT00 KxfLS+5RYgzlTrzgLJaHs+iKkbtKZ8prKxUTP6nB6wIg5ht2e40OIP2qnwloaZysfb HO8fkdpORyjmA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: =?UTF-8?q?Alexis=20Lothor=C3=A9?= , Kalle Valo , Sasha Levin Subject: [PATCH 6.1 152/451] wifi: wilc1000: prevent use-after-free on vif when cleaning up all interfaces Date: Sun, 24 Mar 2024 19:07:08 -0400 Message-ID: <20240324231207.1351418-153-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Alexis Lothoré [ Upstream commit cb5942b77c05d54310a0420cac12935e9b6aa21c ] wilc_netdev_cleanup currently triggers a KASAN warning, which can be observed on interface registration error path, or simply by removing the module/unbinding device from driver: echo spi0.1 > /sys/bus/spi/drivers/wilc1000_spi/unbind ================================================================== BUG: KASAN: slab-use-after-free in wilc_netdev_cleanup+0x508/0x5cc Read of size 4 at addr c54d1ce8 by task sh/86 CPU: 0 PID: 86 Comm: sh Not tainted 6.8.0-rc1+ #117 Hardware name: Atmel SAMA5 unwind_backtrace from show_stack+0x18/0x1c show_stack from dump_stack_lvl+0x34/0x58 dump_stack_lvl from print_report+0x154/0x500 print_report from kasan_report+0xac/0xd8 kasan_report from wilc_netdev_cleanup+0x508/0x5cc wilc_netdev_cleanup from wilc_bus_remove+0xc8/0xec wilc_bus_remove from spi_remove+0x8c/0xac spi_remove from device_release_driver_internal+0x434/0x5f8 device_release_driver_internal from unbind_store+0xbc/0x108 unbind_store from kernfs_fop_write_iter+0x398/0x584 kernfs_fop_write_iter from vfs_write+0x728/0xf88 vfs_write from ksys_write+0x110/0x1e4 ksys_write from ret_fast_syscall+0x0/0x1c [...] Allocated by task 1: kasan_save_track+0x30/0x5c __kasan_kmalloc+0x8c/0x94 __kmalloc_node+0x1cc/0x3e4 kvmalloc_node+0x48/0x180 alloc_netdev_mqs+0x68/0x11dc alloc_etherdev_mqs+0x28/0x34 wilc_netdev_ifc_init+0x34/0x8ec wilc_cfg80211_init+0x690/0x910 wilc_bus_probe+0xe0/0x4a0 spi_probe+0x158/0x1b0 really_probe+0x270/0xdf4 __driver_probe_device+0x1dc/0x580 driver_probe_device+0x60/0x140 __driver_attach+0x228/0x5d4 bus_for_each_dev+0x13c/0x1a8 bus_add_driver+0x2a0/0x608 driver_register+0x24c/0x578 do_one_initcall+0x180/0x310 kernel_init_freeable+0x424/0x484 kernel_init+0x20/0x148 ret_from_fork+0x14/0x28 Freed by task 86: kasan_save_track+0x30/0x5c kasan_save_free_info+0x38/0x58 __kasan_slab_free+0xe4/0x140 kfree+0xb0/0x238 device_release+0xc0/0x2a8 kobject_put+0x1d4/0x46c netdev_run_todo+0x8fc/0x11d0 wilc_netdev_cleanup+0x1e4/0x5cc wilc_bus_remove+0xc8/0xec spi_remove+0x8c/0xac device_release_driver_internal+0x434/0x5f8 unbind_store+0xbc/0x108 kernfs_fop_write_iter+0x398/0x584 vfs_write+0x728/0xf88 ksys_write+0x110/0x1e4 ret_fast_syscall+0x0/0x1c [...] David Mosberger-Tan initial investigation [1] showed that this use-after-free is due to netdevice unregistration during vif list traversal. When unregistering a net device, since the needs_free_netdev has been set to true during registration, the netdevice object is also freed, and as a consequence, the corresponding vif object too, since it is attached to it as private netdevice data. The next occurrence of the loop then tries to access freed vif pointer to the list to move forward in the list. Fix this use-after-free thanks to two mechanisms: - navigate in the list with list_for_each_entry_safe, which allows to safely modify the list as we go through each element. For each element, remove it from the list with list_del_rcu - make sure to wait for RCU grace period end after each vif removal to make sure it is safe to free the corresponding vif too (through unregister_netdev) Since we are in a RCU "modifier" path (not a "reader" path), and because such path is expected not to be concurrent to any other modifier (we are using the vif_mutex lock), we do not need to use RCU list API, that's why we can benefit from list_for_each_entry_safe. [1] https://lore.kernel.org/linux-wireless/ab077dbe58b1ea5de0a3b2ca21f275a07af967d2.camel@egauge.net/ Fixes: 8399918f3056 ("staging: wilc1000: use RCU list to maintain vif interfaces list") Signed-off-by: Alexis Lothoré Signed-off-by: Kalle Valo Link: https://msgid.link/20240212-wilc_rework_deinit-v1-1-9203ae56c27f@bootlin.com Signed-off-by: Sasha Levin --- .../net/wireless/microchip/wilc1000/netdev.c | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/drivers/net/wireless/microchip/wilc1000/netdev.c b/drivers/net/wireless/microchip/wilc1000/netdev.c index 0e6eeeed2e086..b714da48eaa17 100644 --- a/drivers/net/wireless/microchip/wilc1000/netdev.c +++ b/drivers/net/wireless/microchip/wilc1000/netdev.c @@ -878,8 +878,7 @@ static const struct net_device_ops wilc_netdev_ops = { void wilc_netdev_cleanup(struct wilc *wilc) { - struct wilc_vif *vif; - int srcu_idx, ifc_cnt = 0; + struct wilc_vif *vif, *vif_tmp; if (!wilc) return; @@ -889,32 +888,19 @@ void wilc_netdev_cleanup(struct wilc *wilc) wilc->firmware = NULL; } - srcu_idx = srcu_read_lock(&wilc->srcu); - list_for_each_entry_rcu(vif, &wilc->vif_list, list) { + list_for_each_entry_safe(vif, vif_tmp, &wilc->vif_list, list) { + mutex_lock(&wilc->vif_mutex); + list_del_rcu(&vif->list); + wilc->vif_num--; + mutex_unlock(&wilc->vif_mutex); + synchronize_srcu(&wilc->srcu); if (vif->ndev) unregister_netdev(vif->ndev); } - srcu_read_unlock(&wilc->srcu, srcu_idx); wilc_wfi_deinit_mon_interface(wilc, false); destroy_workqueue(wilc->hif_workqueue); - while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) { - mutex_lock(&wilc->vif_mutex); - if (wilc->vif_num <= 0) { - mutex_unlock(&wilc->vif_mutex); - break; - } - vif = wilc_get_wl_to_vif(wilc); - if (!IS_ERR(vif)) - list_del_rcu(&vif->list); - - wilc->vif_num--; - mutex_unlock(&wilc->vif_mutex); - synchronize_srcu(&wilc->srcu); - ifc_cnt++; - } - wilc_wlan_cfg_deinit(wilc); wlan_deinit_locks(wilc); wiphy_unregister(wilc->wiphy); -- 2.43.0