All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: RFCOMM: possible deadlock in rfcomm_dlc_exists
@ 2024-05-07 11:42 Alexander Ofitserov
  2024-05-07 12:40 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Ofitserov @ 2024-05-07 11:42 UTC (permalink / raw)
  To: oficerovas, Marcel Holtmann, Johan Hedberg,
	Luiz Augusto von Dentz, Eric Dumazet, linux-bluetooth,
	linux-kernel
  Cc: lvc-project, dutyrok, kovalev

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-07 12:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-07 11:42 [PATCH] Bluetooth: RFCOMM: possible deadlock in rfcomm_dlc_exists Alexander Ofitserov
2024-05-07 12:40 ` bluez.test.bot

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.