linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg
@ 2023-12-04  7:02 Hyunwoo Kim
  2023-12-04  7:32 ` bluez.test.bot
  2023-12-06 14:48 ` [PATCH] " Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: Hyunwoo Kim @ 2023-12-04  7:02 UTC (permalink / raw)
  To: marcel; +Cc: v4bel, imv4bel, johan.hedberg, luiz.dentz, linux-bluetooth

This can cause a race with bt_sock_ioctl() because
bt_sock_recvmsg() gets the skb from sk->sk_receive_queue
and then frees it without holding lock_sock.
A use-after-free for a skb occurs with the following flow.
```
bt_sock_recvmsg() -> skb_recv_datagram() -> skb_free_datagram()
bt_sock_ioctl() -> skb_peek()
```
Add lock_sock to bt_sock_recvmsg() to fix this issue.

Signed-off-by: Hyunwoo Kim <v4bel@theori.io>
---
 net/bluetooth/af_bluetooth.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 336a76165454..9def13ac816e 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -309,11 +309,16 @@ 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)
+		if (sk->sk_shutdown & RCV_SHUTDOWN) {
+			release_sock(sk);
 			return 0;
+		}
 
+		release_sock(sk);
 		return err;
 	}
 
@@ -343,6 +348,8 @@ 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;
 
-- 
2.25.1


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

* RE: Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg
  2023-12-04  7:02 [PATCH] Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg Hyunwoo Kim
@ 2023-12-04  7:32 ` bluez.test.bot
  2023-12-06 14:48 ` [PATCH] " Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2023-12-04  7:32 UTC (permalink / raw)
  To: linux-bluetooth, v4bel

[-- Attachment #1: Type: text/plain, Size: 1668 bytes --]

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=806468

---Test result---

Test Summary:
CheckPatch                    PASS      0.48 seconds
GitLint                       PASS      0.22 seconds
SubjectPrefix                 PASS      0.07 seconds
BuildKernel                   PASS      28.01 seconds
CheckAllWarning               PASS      31.13 seconds
CheckSparse                   WARNING   36.27 seconds
CheckSmatch                   PASS      100.31 seconds
BuildKernel32                 PASS      27.37 seconds
TestRunnerSetup               PASS      423.62 seconds
TestRunner_l2cap-tester       PASS      23.57 seconds
TestRunner_iso-tester         PASS      46.96 seconds
TestRunner_bnep-tester        PASS      6.88 seconds
TestRunner_mgmt-tester        PASS      164.14 seconds
TestRunner_rfcomm-tester      PASS      11.08 seconds
TestRunner_sco-tester         PASS      14.63 seconds
TestRunner_ioctl-tester       PASS      12.31 seconds
TestRunner_mesh-tester        PASS      9.04 seconds
TestRunner_smp-tester         PASS      10.05 seconds
TestRunner_userchan-tester    PASS      7.49 seconds
IncrementalBuild              PASS      26.74 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


---
Regards,
Linux Bluetooth


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

* Re: [PATCH] Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg
  2023-12-04  7:02 [PATCH] Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg Hyunwoo Kim
  2023-12-04  7:32 ` bluez.test.bot
@ 2023-12-06 14:48 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2023-12-06 14:48 UTC (permalink / raw)
  To: Hyunwoo Kim; +Cc: marcel, imv4bel, johan.hedberg, linux-bluetooth

Hi Kim,

On Mon, Dec 4, 2023 at 2:02 AM Hyunwoo Kim <v4bel@theori.io> wrote:
>
> This can cause a race with bt_sock_ioctl() because
> bt_sock_recvmsg() gets the skb from sk->sk_receive_queue
> and then frees it without holding lock_sock.
> A use-after-free for a skb occurs with the following flow.
> ```
> bt_sock_recvmsg() -> skb_recv_datagram() -> skb_free_datagram()
> bt_sock_ioctl() -> skb_peek()
> ```
> Add lock_sock to bt_sock_recvmsg() to fix this issue.
>
> Signed-off-by: Hyunwoo Kim <v4bel@theori.io>
> ---
>  net/bluetooth/af_bluetooth.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
> index 336a76165454..9def13ac816e 100644
> --- a/net/bluetooth/af_bluetooth.c
> +++ b/net/bluetooth/af_bluetooth.c
> @@ -309,11 +309,16 @@ 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)
> +               if (sk->sk_shutdown & RCV_SHUTDOWN) {
> +                       release_sock(sk);

We could just reset the with err = 0 then we don't need to duplicate
the release_sock line.

>                         return 0;
> +               }
>
> +               release_sock(sk);
>                 return err;
>         }
>
> @@ -343,6 +348,8 @@ 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;
>
> --
> 2.25.1
>


-- 
Luiz Augusto von Dentz

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

end of thread, other threads:[~2023-12-06 14:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-04  7:02 [PATCH] Bluetooth: af_bluetooth: Fix Use-After-Free in bt_sock_recvmsg Hyunwoo Kim
2023-12-04  7:32 ` bluez.test.bot
2023-12-06 14:48 ` [PATCH] " Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).