[PATCH] Bluetooth: RFCOMM: possible deadlock in rfcomm_dlc_exists

Alexander Ofitserov posted 1 patch 3 weeks ago
net/bluetooth/rfcomm/core.c |  2 +-
net/bluetooth/rfcomm/sock.c |  2 --
net/bluetooth/rfcomm/tty.c  | 11 +++++++----
3 files changed, 8 insertions(+), 7 deletions(-)
[PATCH] Bluetooth: RFCOMM: possible deadlock in rfcomm_dlc_exists
Posted by Alexander Ofitserov 3 weeks ago
This bug was found by syzkaller.
This bug is also very similar to another RFCOMM possible deadlock
in rfcomm_dlc_exists. Both of these bugs are present in syzbot.
Patch fixes two of these possible dedlock errors.
Also this particular patch was already written in march of 2023
by Hillf Danton, but seems like it just got lost or forgotten.
Links to these bugs and original patch on syzbot:
Link: https://syzkaller.appspot.com/bug?extid=b69a625d06e8ece26415
Link: https://syzkaller.appspot.com/bug?extid=d7ce59b06b3eb14fd218
Link: https://syzkaller.appspot.com/text?tag=Patch&x=133a6835c80000

Signed-off-by: Alexander Ofitserov <oficerovas@altlinux.org>
---
 net/bluetooth/rfcomm/core.c |  2 +-
 net/bluetooth/rfcomm/sock.c |  2 --
 net/bluetooth/rfcomm/tty.c  | 11 +++++++----
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index 1d34d849703329..d8502914e3602c 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -490,8 +490,8 @@ static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
 
 		rfcomm_dlc_lock(d);
 		d->state = BT_CLOSED;
-		d->state_change(d, err);
 		rfcomm_dlc_unlock(d);
+		d->state_change(d, err);
 
 		skb_queue_purge(&d->tx_queue);
 		rfcomm_dlc_unlink(d);
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 29aa07e9db9d71..cbff37b3273407 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -865,9 +865,7 @@ static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned lon
 
 	if (err == -ENOIOCTLCMD) {
 #ifdef CONFIG_BT_RFCOMM_TTY
-		lock_sock(sk);
 		err = rfcomm_dev_ioctl(sk, cmd, (void __user *) arg);
-		release_sock(sk);
 #else
 		err = -EOPNOTSUPP;
 #endif
diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 69c75c041fe10a..5be21b987141fc 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -403,9 +403,12 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
 		return -EPERM;
 
 	if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
+		lock_sock(sk);
 		/* Socket must be connected */
-		if (sk->sk_state != BT_CONNECTED)
+		if (sk->sk_state != BT_CONNECTED) {
+			release_sock(sk);
 			return -EBADFD;
+		}
 
 		dlc = rfcomm_pi(sk)->dlc;
 		rfcomm_dlc_hold(dlc);
@@ -422,13 +425,13 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
 	}
 
 	id = rfcomm_dev_add(&req, dlc);
-	if (id < 0)
-		return id;
 
 	if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
 		/* DLC is now used by device.
 		 * Socket must be disconnected */
-		sk->sk_state = BT_CLOSED;
+		if (!(id < 0))
+			sk->sk_state = BT_CLOSED;
+		release_sock(sk);
 	}
 
 	return id;
-- 
2.42.1