2024-03-01 18:53:07

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH v1] Bluetooth: af_bluetooth: Fix deadlock

From: Luiz Augusto von Dentz <[email protected]>

Attemting to do sock_lock on .recvmsg may cause a deadlock as shown
bellow, so instead of using sock_sock this uses sk_receive_queue.lock
on bt_sock_ioctl to avoid the UAF:

INFO: task kworker/u9:1:121 blocked for more than 30 seconds.
Not tainted 6.7.6-lemon #183
Workqueue: hci0 hci_rx_work
Call Trace:
<TASK>
__schedule+0x37d/0xa00
schedule+0x32/0xe0
__lock_sock+0x68/0xa0
? __pfx_autoremove_wake_function+0x10/0x10
lock_sock_nested+0x43/0x50
l2cap_sock_recv_cb+0x21/0xa0
l2cap_recv_frame+0x55b/0x30a0
? psi_task_switch+0xeb/0x270
? finish_task_switch.isra.0+0x93/0x2a0
hci_rx_work+0x33a/0x3f0
process_one_work+0x13a/0x2f0
worker_thread+0x2f0/0x410
? __pfx_worker_thread+0x10/0x10
kthread+0xe0/0x110
? __pfx_kthread+0x10/0x10
ret_from_fork+0x2c/0x50
? __pfx_kthread+0x10/0x10
ret_from_fork_asm+0x1b/0x30
</TASK>

Fixes: 2e07e8348ea4 ("Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg")
Signed-off-by: Luiz Augusto von Dentz <[email protected]>
---
net/bluetooth/af_bluetooth.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index b93464ac3517..67604ccec2f4 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -309,14 +309,11 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
if (flags & MSG_OOB)
return -EOPNOTSUPP;

- lock_sock(sk);
-
skb = skb_recv_datagram(sk, flags, &err);
if (!skb) {
if (sk->sk_shutdown & RCV_SHUTDOWN)
err = 0;

- release_sock(sk);
return err;
}

@@ -346,8 +343,6 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,

skb_free_datagram(sk, skb);

- release_sock(sk);
-
if (flags & MSG_TRUNC)
copied = skblen;

@@ -570,10 +565,11 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
if (sk->sk_state == BT_LISTEN)
return -EINVAL;

- lock_sock(sk);
+ spin_lock(&sk->sk_receive_queue.lock);
skb = skb_peek(&sk->sk_receive_queue);
amount = skb ? skb->len : 0;
- release_sock(sk);
+ spin_unlock(&sk->sk_receive_queue.lock);
+
err = put_user(amount, (int __user *)arg);
break;

--
2.43.0



2024-03-01 19:03:45

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH v1] Bluetooth: af_bluetooth: Fix deadlock

On Fri, Mar 1, 2024 at 1:50 PM Luiz Augusto von Dentz
<[email protected]> wrote:
>
> From: Luiz Augusto von Dentz <[email protected]>
>
> Attemting to do sock_lock on .recvmsg may cause a deadlock as shown
> bellow, so instead of using sock_sock this uses sk_receive_queue.lock
> on bt_sock_ioctl to avoid the UAF:
>
> INFO: task kworker/u9:1:121 blocked for more than 30 seconds.
> Not tainted 6.7.6-lemon #183
> Workqueue: hci0 hci_rx_work
> Call Trace:
> <TASK>
> __schedule+0x37d/0xa00
> schedule+0x32/0xe0
> __lock_sock+0x68/0xa0
> ? __pfx_autoremove_wake_function+0x10/0x10
> lock_sock_nested+0x43/0x50
> l2cap_sock_recv_cb+0x21/0xa0
> l2cap_recv_frame+0x55b/0x30a0
> ? psi_task_switch+0xeb/0x270
> ? finish_task_switch.isra.0+0x93/0x2a0
> hci_rx_work+0x33a/0x3f0
> process_one_work+0x13a/0x2f0
> worker_thread+0x2f0/0x410
> ? __pfx_worker_thread+0x10/0x10
> kthread+0xe0/0x110
> ? __pfx_kthread+0x10/0x10
> ret_from_fork+0x2c/0x50
> ? __pfx_kthread+0x10/0x10
> ret_from_fork_asm+0x1b/0x30
> </TASK>
>
> Fixes: 2e07e8348ea4 ("Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg")
> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
> ---
> net/bluetooth/af_bluetooth.c | 10 +++-------
> 1 file changed, 3 insertions(+), 7 deletions(-)
>
> diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
> index b93464ac3517..67604ccec2f4 100644
> --- a/net/bluetooth/af_bluetooth.c
> +++ b/net/bluetooth/af_bluetooth.c
> @@ -309,14 +309,11 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
> if (flags & MSG_OOB)
> return -EOPNOTSUPP;
>
> - lock_sock(sk);
> -
> skb = skb_recv_datagram(sk, flags, &err);
> if (!skb) {
> if (sk->sk_shutdown & RCV_SHUTDOWN)
> err = 0;
>
> - release_sock(sk);
> return err;
> }
>
> @@ -346,8 +343,6 @@ int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
>
> skb_free_datagram(sk, skb);
>
> - release_sock(sk);
> -
> if (flags & MSG_TRUNC)
> copied = skblen;
>
> @@ -570,10 +565,11 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
> if (sk->sk_state == BT_LISTEN)
> return -EINVAL;
>
> - lock_sock(sk);
> + spin_lock(&sk->sk_receive_queue.lock);
> skb = skb_peek(&sk->sk_receive_queue);
> amount = skb ? skb->len : 0;
> - release_sock(sk);
> + spin_unlock(&sk->sk_receive_queue.lock);
> +
> err = put_user(amount, (int __user *)arg);
> break;
>
> --
> 2.43.0
>


--
Luiz Augusto von Dentz

2024-03-01 20:23:59

by bluez.test.bot

[permalink] [raw]
Subject: RE: [v1] Bluetooth: af_bluetooth: Fix deadlock

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=831666

---Test result---

Test Summary:
CheckPatch PASS 0.66 seconds
GitLint PASS 0.32 seconds
SubjectPrefix PASS 0.12 seconds
BuildKernel PASS 28.28 seconds
CheckAllWarning PASS 30.85 seconds
CheckSparse WARNING 36.83 seconds
CheckSmatch PASS 98.99 seconds
BuildKernel32 PASS 27.51 seconds
TestRunnerSetup PASS 505.14 seconds
TestRunner_l2cap-tester PASS 20.25 seconds
TestRunner_iso-tester PASS 32.70 seconds
TestRunner_bnep-tester PASS 4.81 seconds
TestRunner_mgmt-tester FAIL 114.53 seconds
TestRunner_rfcomm-tester PASS 7.45 seconds
TestRunner_sco-tester PASS 15.01 seconds
TestRunner_ioctl-tester PASS 8.04 seconds
TestRunner_mesh-tester PASS 6.02 seconds
TestRunner_smp-tester PASS 6.93 seconds
TestRunner_userchan-tester PASS 5.01 seconds
IncrementalBuild PASS 26.39 seconds

Details
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:
net/bluetooth/af_bluetooth.c:223:25: warning: context imbalance in 'bt_accept_enqueue' - different lock contexts for basic block
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 492, Passed: 489 (99.4%), Failed: 1, Not Run: 2

Failed Test Cases
LL Privacy - Add Device 6 (RL is full) Failed 0.194 seconds


---
Regards,
Linux Bluetooth

2024-03-04 16:31:00

by patchwork-bot+bluetooth

[permalink] [raw]
Subject: Re: [PATCH v1] Bluetooth: af_bluetooth: Fix deadlock

Hello:

This patch was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <[email protected]>:

On Fri, 1 Mar 2024 13:50:34 -0500 you wrote:
> From: Luiz Augusto von Dentz <[email protected]>
>
> Attemting to do sock_lock on .recvmsg may cause a deadlock as shown
> bellow, so instead of using sock_sock this uses sk_receive_queue.lock
> on bt_sock_ioctl to avoid the UAF:
>
> INFO: task kworker/u9:1:121 blocked for more than 30 seconds.
> Not tainted 6.7.6-lemon #183
> Workqueue: hci0 hci_rx_work
> Call Trace:
> <TASK>
> __schedule+0x37d/0xa00
> schedule+0x32/0xe0
> __lock_sock+0x68/0xa0
> ? __pfx_autoremove_wake_function+0x10/0x10
> lock_sock_nested+0x43/0x50
> l2cap_sock_recv_cb+0x21/0xa0
> l2cap_recv_frame+0x55b/0x30a0
> ? psi_task_switch+0xeb/0x270
> ? finish_task_switch.isra.0+0x93/0x2a0
> hci_rx_work+0x33a/0x3f0
> process_one_work+0x13a/0x2f0
> worker_thread+0x2f0/0x410
> ? __pfx_worker_thread+0x10/0x10
> kthread+0xe0/0x110
> ? __pfx_kthread+0x10/0x10
> ret_from_fork+0x2c/0x50
> ? __pfx_kthread+0x10/0x10
> ret_from_fork_asm+0x1b/0x30
> </TASK>
>
> [...]

Here is the summary with links:
- [v1] Bluetooth: af_bluetooth: Fix deadlock
https://git.kernel.org/bluetooth/bluetooth-next/c/40245851528a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html