All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>,
	syzbot <syzbot+355f8edb2ff45d5f95fa@syzkaller.appspotmail.com>,
	syzbot <syzbot+0f1827363a305f74996f@syzkaller.appspotmail.com>,
	Kirill Tkhai <ktkhai@virtuozzo.com>,
	Oliver Hartkopp <socketcan@hartkopp.net>,
	Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
	Marc Kleine-Budde <mkl@pengutronix.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 4.4 38/57] can: bcm/raw/isotp: use per module netdevice notifier
Date: Mon, 28 Jun 2021 10:42:37 -0400	[thread overview]
Message-ID: <20210628144256.34524-39-sashal@kernel.org> (raw)
In-Reply-To: <20210628144256.34524-1-sashal@kernel.org>

From: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>

commit 8d0caedb759683041d9db82069937525999ada53 upstream.

syzbot is reporting hung task at register_netdevice_notifier() [1] and
unregister_netdevice_notifier() [2], for cleanup_net() might perform
time consuming operations while CAN driver's raw/bcm/isotp modules are
calling {register,unregister}_netdevice_notifier() on each socket.

Change raw/bcm/isotp modules to call register_netdevice_notifier() from
module's __init function and call unregister_netdevice_notifier() from
module's __exit function, as with gw/j1939 modules are doing.

Link: https://syzkaller.appspot.com/bug?id=391b9498827788b3cc6830226d4ff5be87107c30 [1]
Link: https://syzkaller.appspot.com/bug?id=1724d278c83ca6e6df100a2e320c10d991cf2bce [2]
Link: https://lore.kernel.org/r/54a5f451-05ed-f977-8534-79e7aa2bcc8f@i-love.sakura.ne.jp
Cc: linux-stable <stable@vger.kernel.org>
Reported-by: syzbot <syzbot+355f8edb2ff45d5f95fa@syzkaller.appspotmail.com>
Reported-by: syzbot <syzbot+0f1827363a305f74996f@syzkaller.appspotmail.com>
Reviewed-by: Kirill Tkhai <ktkhai@virtuozzo.com>
Tested-by: syzbot <syzbot+355f8edb2ff45d5f95fa@syzkaller.appspotmail.com>
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 net/can/bcm.c | 61 +++++++++++++++++++++++++++++++++++++++-----------
 net/can/raw.c | 62 +++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 96 insertions(+), 27 deletions(-)

diff --git a/net/can/bcm.c b/net/can/bcm.c
index bd9e10db6cd7..c6fee58baac4 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -121,7 +121,7 @@ struct bcm_sock {
 	struct sock sk;
 	int bound;
 	int ifindex;
-	struct notifier_block notifier;
+	struct list_head notifier;
 	struct list_head rx_ops;
 	struct list_head tx_ops;
 	unsigned long dropped_usr_msgs;
@@ -129,6 +129,10 @@ struct bcm_sock {
 	char procname [32]; /* inode number in decimal with \0 */
 };
 
+static LIST_HEAD(bcm_notifier_list);
+static DEFINE_SPINLOCK(bcm_notifier_lock);
+static struct bcm_sock *bcm_busy_notifier;
+
 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
 {
 	return (struct bcm_sock *)sk;
@@ -1388,20 +1392,15 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 /*
  * notification handler for netdevice status changes
  */
-static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
-			void *ptr)
+static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
+		       struct net_device *dev)
 {
-	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
-	struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
 	struct sock *sk = &bo->sk;
 	struct bcm_op *op;
 	int notify_enodev = 0;
 
 	if (!net_eq(dev_net(dev), &init_net))
-		return NOTIFY_DONE;
-
-	if (dev->type != ARPHRD_CAN)
-		return NOTIFY_DONE;
+		return;
 
 	switch (msg) {
 
@@ -1436,7 +1435,28 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
 				sk->sk_error_report(sk);
 		}
 	}
+}
 
+static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
+			void *ptr)
+{
+	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+
+	if (dev->type != ARPHRD_CAN)
+		return NOTIFY_DONE;
+	if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
+		return NOTIFY_DONE;
+	if (unlikely(bcm_busy_notifier)) /* Check for reentrant bug. */
+		return NOTIFY_DONE;
+
+	spin_lock(&bcm_notifier_lock);
+	list_for_each_entry(bcm_busy_notifier, &bcm_notifier_list, notifier) {
+		spin_unlock(&bcm_notifier_lock);
+		bcm_notify(bcm_busy_notifier, msg, dev);
+		spin_lock(&bcm_notifier_lock);
+	}
+	bcm_busy_notifier = NULL;
+	spin_unlock(&bcm_notifier_lock);
 	return NOTIFY_DONE;
 }
 
@@ -1456,9 +1476,9 @@ static int bcm_init(struct sock *sk)
 	INIT_LIST_HEAD(&bo->rx_ops);
 
 	/* set notifier */
-	bo->notifier.notifier_call = bcm_notifier;
-
-	register_netdevice_notifier(&bo->notifier);
+	spin_lock(&bcm_notifier_lock);
+	list_add_tail(&bo->notifier, &bcm_notifier_list);
+	spin_unlock(&bcm_notifier_lock);
 
 	return 0;
 }
@@ -1479,7 +1499,14 @@ static int bcm_release(struct socket *sock)
 
 	/* remove bcm_ops, timer, rx_unregister(), etc. */
 
-	unregister_netdevice_notifier(&bo->notifier);
+	spin_lock(&bcm_notifier_lock);
+	while (bcm_busy_notifier == bo) {
+		spin_unlock(&bcm_notifier_lock);
+		schedule_timeout_uninterruptible(1);
+		spin_lock(&bcm_notifier_lock);
+	}
+	list_del(&bo->notifier);
+	spin_unlock(&bcm_notifier_lock);
 
 	lock_sock(sk);
 
@@ -1665,6 +1692,10 @@ static const struct can_proto bcm_can_proto = {
 	.prot       = &bcm_proto,
 };
 
+static struct notifier_block canbcm_notifier = {
+	.notifier_call = bcm_notifier
+};
+
 static int __init bcm_module_init(void)
 {
 	int err;
@@ -1679,6 +1710,8 @@ static int __init bcm_module_init(void)
 
 	/* create /proc/net/can-bcm directory */
 	proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
+	register_netdevice_notifier(&canbcm_notifier);
+
 	return 0;
 }
 
@@ -1688,6 +1721,8 @@ static void __exit bcm_module_exit(void)
 
 	if (proc_dir)
 		remove_proc_entry("can-bcm", init_net.proc_net);
+
+	unregister_netdevice_notifier(&canbcm_notifier);
 }
 
 module_init(bcm_module_init);
diff --git a/net/can/raw.c b/net/can/raw.c
index e9403a26a1d5..2e1d850a7f2a 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -84,7 +84,7 @@ struct raw_sock {
 	struct sock sk;
 	int bound;
 	int ifindex;
-	struct notifier_block notifier;
+	struct list_head notifier;
 	int loopback;
 	int recv_own_msgs;
 	int fd_frames;
@@ -96,6 +96,10 @@ struct raw_sock {
 	struct uniqframe __percpu *uniq;
 };
 
+static LIST_HEAD(raw_notifier_list);
+static DEFINE_SPINLOCK(raw_notifier_lock);
+static struct raw_sock *raw_busy_notifier;
+
 /*
  * Return pointer to store the extra msg flags for raw_recvmsg().
  * We use the space of one unsigned int beyond the 'struct sockaddr_can'
@@ -260,21 +264,16 @@ static int raw_enable_allfilters(struct net_device *dev, struct sock *sk)
 	return err;
 }
 
-static int raw_notifier(struct notifier_block *nb,
-			unsigned long msg, void *ptr)
+static void raw_notify(struct raw_sock *ro, unsigned long msg,
+		       struct net_device *dev)
 {
-	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
-	struct raw_sock *ro = container_of(nb, struct raw_sock, notifier);
 	struct sock *sk = &ro->sk;
 
 	if (!net_eq(dev_net(dev), &init_net))
-		return NOTIFY_DONE;
-
-	if (dev->type != ARPHRD_CAN)
-		return NOTIFY_DONE;
+		return;
 
 	if (ro->ifindex != dev->ifindex)
-		return NOTIFY_DONE;
+		return;
 
 	switch (msg) {
 
@@ -303,7 +302,28 @@ static int raw_notifier(struct notifier_block *nb,
 			sk->sk_error_report(sk);
 		break;
 	}
+}
+
+static int raw_notifier(struct notifier_block *nb, unsigned long msg,
+			void *ptr)
+{
+	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+
+	if (dev->type != ARPHRD_CAN)
+		return NOTIFY_DONE;
+	if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
+		return NOTIFY_DONE;
+	if (unlikely(raw_busy_notifier)) /* Check for reentrant bug. */
+		return NOTIFY_DONE;
 
+	spin_lock(&raw_notifier_lock);
+	list_for_each_entry(raw_busy_notifier, &raw_notifier_list, notifier) {
+		spin_unlock(&raw_notifier_lock);
+		raw_notify(raw_busy_notifier, msg, dev);
+		spin_lock(&raw_notifier_lock);
+	}
+	raw_busy_notifier = NULL;
+	spin_unlock(&raw_notifier_lock);
 	return NOTIFY_DONE;
 }
 
@@ -332,9 +352,9 @@ static int raw_init(struct sock *sk)
 		return -ENOMEM;
 
 	/* set notifier */
-	ro->notifier.notifier_call = raw_notifier;
-
-	register_netdevice_notifier(&ro->notifier);
+	spin_lock(&raw_notifier_lock);
+	list_add_tail(&ro->notifier, &raw_notifier_list);
+	spin_unlock(&raw_notifier_lock);
 
 	return 0;
 }
@@ -349,7 +369,14 @@ static int raw_release(struct socket *sock)
 
 	ro = raw_sk(sk);
 
-	unregister_netdevice_notifier(&ro->notifier);
+	spin_lock(&raw_notifier_lock);
+	while (raw_busy_notifier == ro) {
+		spin_unlock(&raw_notifier_lock);
+		schedule_timeout_uninterruptible(1);
+		spin_lock(&raw_notifier_lock);
+	}
+	list_del(&ro->notifier);
+	spin_unlock(&raw_notifier_lock);
 
 	lock_sock(sk);
 
@@ -857,6 +884,10 @@ static const struct can_proto raw_can_proto = {
 	.prot       = &raw_proto,
 };
 
+static struct notifier_block canraw_notifier = {
+	.notifier_call = raw_notifier
+};
+
 static __init int raw_module_init(void)
 {
 	int err;
@@ -866,6 +897,8 @@ static __init int raw_module_init(void)
 	err = can_proto_register(&raw_can_proto);
 	if (err < 0)
 		printk(KERN_ERR "can: registration of raw protocol failed\n");
+	else
+		register_netdevice_notifier(&canraw_notifier);
 
 	return err;
 }
@@ -873,6 +906,7 @@ static __init int raw_module_init(void)
 static __exit void raw_module_exit(void)
 {
 	can_proto_unregister(&raw_can_proto);
+	unregister_netdevice_notifier(&canraw_notifier);
 }
 
 module_init(raw_module_init);
-- 
2.30.2


  parent reply	other threads:[~2021-06-28 15:28 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-28 14:41 [PATCH 4.4 00/57] 4.4.274-rc1 review Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 01/57] HID: hid-sensor-hub: Return error for hid_set_field() failure Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 02/57] HID: Add BUS_VIRTUAL to hid_connect logging Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 03/57] HID: usbhid: fix info leak in hid_submit_ctrl Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 04/57] ARM: OMAP2+: Fix build warning when mmc_omap is not built Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 05/57] HID: gt683r: add missing MODULE_DEVICE_TABLE Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 06/57] gfs2: Fix use-after-free in gfs2_glock_shrink_scan Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 07/57] scsi: target: core: Fix warning on realtime kernels Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 08/57] ethernet: myri10ge: Fix missing error code in myri10ge_probe() Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 09/57] net: ipconfig: Don't override command-line hostnames or domains Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 10/57] rtnetlink: Fix missing error code in rtnl_bridge_notify() Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 11/57] net/x25: Return the correct errno code Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 12/57] net: " Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 13/57] fib: " Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 14/57] dmaengine: stedma40: add missing iounmap() on error in d40_probe() Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 15/57] net: ipv4: fix memory leak in netlbl_cipsov4_add_std Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 16/57] net: rds: fix memory leak in rds_recvmsg Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 17/57] rtnetlink: Fix regression in bridge VLAN configuration Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 18/57] netfilter: synproxy: Fix out of bounds when parsing TCP options Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 19/57] net: stmmac: dwmac1000: Fix extended MAC address registers definition Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 20/57] qlcnic: Fix an error handling path in 'qlcnic_probe()' Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 21/57] netxen_nic: Fix an error handling path in 'netxen_nic_probe()' Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 22/57] net: cdc_ncm: switch to eth%d interface naming Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 23/57] net: usb: fix possible use-after-free in smsc75xx_bind Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 24/57] net/af_unix: fix a data-race in unix_dgram_sendmsg / unix_release_sock Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 25/57] be2net: Fix an error handling path in 'be_probe()' Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 26/57] net: hamradio: fix memory leak in mkiss_close Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 27/57] net: cdc_eem: fix tx fixup skb leak Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 28/57] net: ethernet: fix potential use-after-free in ec_bhf_remove Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 29/57] scsi: core: Put .shost_dev in failure path if host state changes to RUNNING Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 30/57] radeon: use memcpy_to/fromio for UVD fw upload Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 31/57] can: bcm: fix infoleak in struct bcm_msg_head Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 32/57] tracing: Do no increment trace_clock_global() by one Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 33/57] PCI: Mark TI C667X to avoid bus reset Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 34/57] PCI: Mark some NVIDIA GPUs " Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 35/57] ARCv2: save ABI registers across signal handling Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 36/57] dmaengine: pl330: fix wrong usage of spinlock flags in dma_cyclc Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 37/57] net: fec_ptp: add clock rate zero check Sasha Levin
2021-06-28 14:42 ` Sasha Levin [this message]
2021-06-28 14:42 ` [PATCH 4.4 39/57] tracing: Do not stop recording cmdlines when tracing is off Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 40/57] tracing: Do not stop recording comms if the trace file is being read Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 41/57] x86/fpu: Reset state for all signal restore failures Sasha Levin
2021-07-03 15:22   ` Pavel Machek
2021-06-28 14:42 ` [PATCH 4.4 42/57] inet: use bigger hash table for IP ID generation Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 43/57] ARM: 9081/1: fix gcc-10 thumb2-kernel regression Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 44/57] Makefile: Move -Wno-unused-but-set-variable out of GCC only block Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 45/57] Revert "PCI: PM: Do not read power state in pci_enable_device_flags()" Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 46/57] cfg80211: call cfg80211_leave_ocb when switching away from OCB Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 47/57] mac80211: drop multicast fragments Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 48/57] ping: Check return value of function 'ping_queue_rcv_skb' Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 49/57] inet: annotate date races around sk->sk_txhash Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 50/57] net: caif: fix memory leak in ldisc_open Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 51/57] r8152: Avoid memcpy() over-reading of ETH_SS_STATS Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 52/57] sh_eth: " Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 53/57] r8169: " Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 54/57] net: ll_temac: Avoid ndo_start_xmit returning NETDEV_TX_BUSY Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 55/57] nilfs2: fix memory leak in nilfs_sysfs_delete_device_group Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 56/57] i2c: robotfuzz-osif: fix control-request directions Sasha Levin
2021-06-28 14:42 ` [PATCH 4.4 57/57] Linux 4.4.274-rc1 Sasha Levin
2021-06-29 10:09 ` [PATCH 4.4 00/57] 4.4.274-rc1 review Jon Hunter
2021-06-29 14:09 ` Guenter Roeck
2021-06-29 14:52 ` Naresh Kamboju
2021-06-29 18:18 ` Guenter Roeck
2021-06-29 23:42 ` Guenter Roeck
2021-07-01 10:21 ` Pavel Machek
2021-07-10 14:59 ` Naresh Kamboju

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210628144256.34524-39-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=socketcan@hartkopp.net \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+0f1827363a305f74996f@syzkaller.appspotmail.com \
    --cc=syzbot+355f8edb2ff45d5f95fa@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.