Skip to content

Commit 2fcfd51

Browse files
Vudentzgregkh
authored andcommitted
Bluetooth: SCO: Fix possible circular locking dependency sco_sock_getsockopt
[ Upstream commit 975abc0 ] This attempts to fix the following trace: ====================================================== WARNING: possible circular locking dependency detected 6.3.0-rc2-g68fcb3a7bf97 #4706 Not tainted ------------------------------------------------------ sco-tester/31 is trying to acquire lock: ffff8880025b8070 (&hdev->lock){+.+.}-{3:3}, at: sco_sock_getsockopt+0x1fc/0xa90 but task is already holding lock: ffff888001eeb130 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO){+.+.}-{0:0}, at: sco_sock_getsockopt+0x104/0xa90 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #2 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO){+.+.}-{0:0}: lock_sock_nested+0x32/0x80 sco_connect_cfm+0x118/0x4a0 hci_sync_conn_complete_evt+0x1e6/0x3d0 hci_event_packet+0x55c/0x7c0 hci_rx_work+0x34c/0xa00 process_one_work+0x575/0x910 worker_thread+0x89/0x6f0 kthread+0x14e/0x180 ret_from_fork+0x2b/0x50 -> #1 (hci_cb_list_lock){+.+.}-{3:3}: __mutex_lock+0x13b/0xcc0 hci_sync_conn_complete_evt+0x1ad/0x3d0 hci_event_packet+0x55c/0x7c0 hci_rx_work+0x34c/0xa00 process_one_work+0x575/0x910 worker_thread+0x89/0x6f0 kthread+0x14e/0x180 ret_from_fork+0x2b/0x50 -> #0 (&hdev->lock){+.+.}-{3:3}: __lock_acquire+0x18cc/0x3740 lock_acquire+0x151/0x3a0 __mutex_lock+0x13b/0xcc0 sco_sock_getsockopt+0x1fc/0xa90 __sys_getsockopt+0xe9/0x190 __x64_sys_getsockopt+0x5b/0x70 do_syscall_64+0x42/0x90 entry_SYSCALL_64_after_hwframe+0x70/0xda other info that might help us debug this: Chain exists of: &hdev->lock --> hci_cb_list_lock --> sk_lock-AF_BLUETOOTH-BTPROTO_SCO Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(sk_lock-AF_BLUETOOTH-BTPROTO_SCO); lock(hci_cb_list_lock); lock(sk_lock-AF_BLUETOOTH-BTPROTO_SCO); lock(&hdev->lock); *** DEADLOCK *** 1 lock held by sco-tester/31: #0: ffff888001eeb130 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO){+.+.}-{0:0}, at: sco_sock_getsockopt+0x104/0xa90 Fixes: 248733e ("Bluetooth: Allow querying of supported offload codecs over SCO socket") Signed-off-by: Luiz Augusto von Dentz <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 99f1bc3 commit 2fcfd51

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

net/bluetooth/sco.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,8 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
11291129
break;
11301130
}
11311131

1132+
release_sock(sk);
1133+
11321134
/* find total buffer size required to copy codec + caps */
11331135
hci_dev_lock(hdev);
11341136
list_for_each_entry(c, &hdev->local_codecs, list) {
@@ -1146,15 +1148,13 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
11461148
buf_len += sizeof(struct bt_codecs);
11471149
if (buf_len > len) {
11481150
hci_dev_put(hdev);
1149-
err = -ENOBUFS;
1150-
break;
1151+
return -ENOBUFS;
11511152
}
11521153
ptr = optval;
11531154

11541155
if (put_user(num_codecs, ptr)) {
11551156
hci_dev_put(hdev);
1156-
err = -EFAULT;
1157-
break;
1157+
return -EFAULT;
11581158
}
11591159
ptr += sizeof(num_codecs);
11601160

@@ -1194,12 +1194,14 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
11941194
ptr += len;
11951195
}
11961196

1197-
if (!err && put_user(buf_len, optlen))
1198-
err = -EFAULT;
1199-
12001197
hci_dev_unlock(hdev);
12011198
hci_dev_put(hdev);
12021199

1200+
lock_sock(sk);
1201+
1202+
if (!err && put_user(buf_len, optlen))
1203+
err = -EFAULT;
1204+
12031205
break;
12041206

12051207
default:

0 commit comments

Comments
 (0)