]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
Bluetooth: Fix locking in bt_accept_enqueue() for BH context
authorMatthias Kaehlcke <mka@chromium.org>
Thu, 3 Jan 2019 00:11:20 +0000 (16:11 -0800)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 14 Aug 2019 09:18:49 +0000 (11:18 +0200)
BugLink: https://bugs.launchpad.net/bugs/1837813
commit c4f5627f7eeecde1bb6b646d8c0907b96dc2b2a6 upstream.

With commit e16337622016 ("Bluetooth: Handle bt_accept_enqueue() socket
atomically") lock_sock[_nested]() is used to acquire the socket lock
before manipulating the socket. lock_sock[_nested]() may block, which
is problematic since bt_accept_enqueue() can be called in bottom half
context (e.g. from rfcomm_connect_ind()):

[<ffffff80080d81ec>] __might_sleep+0x4c/0x80
[<ffffff800876c7b0>] lock_sock_nested+0x24/0x58
[<ffffff8000d7c27c>] bt_accept_enqueue+0x48/0xd4 [bluetooth]
[<ffffff8000e67d8c>] rfcomm_connect_ind+0x190/0x218 [rfcomm]

Add a parameter to bt_accept_enqueue() to indicate whether the
function is called from BH context, and acquire the socket lock
with bh_lock_sock_nested() if that's the case.

Also adapt all callers of bt_accept_enqueue() to pass the new
parameter:

- l2cap_sock_new_connection_cb()
  - uses lock_sock() to lock the parent socket => process context

- rfcomm_connect_ind()
  - acquires the parent socket lock with bh_lock_sock() => BH
    context

- __sco_chan_add()
  - called from sco_chan_add(), which is called from sco_connect().
    parent is NULL, hence bt_accept_enqueue() isn't called in this
    code path and we can ignore it
  - also called from sco_conn_ready(). uses bh_lock_sock() to acquire
    the parent lock => BH context

Fixes: e16337622016 ("Bluetooth: Handle bt_accept_enqueue() socket atomically")
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Cc: stable@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
include/net/bluetooth/bluetooth.h
net/bluetooth/af_bluetooth.c
net/bluetooth/l2cap_sock.c
net/bluetooth/rfcomm/sock.c
net/bluetooth/sco.c

index e89cff0c4c236aa2a755959e44280fc1b58af078..8e437bfb4435bda07ec4b001866b606fb76cbc1e 100644 (file)
@@ -276,7 +276,7 @@ int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
 int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
 int  bt_sock_wait_ready(struct sock *sk, unsigned long flags);
 
-void bt_accept_enqueue(struct sock *parent, struct sock *sk);
+void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
 void bt_accept_unlink(struct sock *sk);
 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
 
index 583951e82ceed32d5add4aa17cc950afcc0e87db..b216e697deac80ed57515b7ce9db1bf580190291 100644 (file)
@@ -154,15 +154,25 @@ void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk)
 }
 EXPORT_SYMBOL(bt_sock_unlink);
 
-void bt_accept_enqueue(struct sock *parent, struct sock *sk)
+void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh)
 {
        BT_DBG("parent %p, sk %p", parent, sk);
 
        sock_hold(sk);
-       lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
+
+       if (bh)
+               bh_lock_sock_nested(sk);
+       else
+               lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
+
        list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q);
        bt_sk(sk)->parent = parent;
-       release_sock(sk);
+
+       if (bh)
+               bh_unlock_sock(sk);
+       else
+               release_sock(sk);
+
        parent->sk_ack_backlog++;
 }
 EXPORT_SYMBOL(bt_accept_enqueue);
index 67a8642f57ea7c8ceadc1d379019ea6135877068..8c329c549ea601fe8ec622849e4722a4d371ed94 100644 (file)
@@ -1253,7 +1253,7 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
 
        l2cap_sock_init(sk, parent);
 
-       bt_accept_enqueue(parent, sk);
+       bt_accept_enqueue(parent, sk, false);
 
        release_sock(parent);
 
index 1aaccf63747937bb1f335c1490bae29c9ff898e7..8fcd9130439d4e7bfbfe7ddd3676d5268b81e10f 100644 (file)
@@ -988,7 +988,7 @@ int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc *
        rfcomm_pi(sk)->channel = channel;
 
        sk->sk_state = BT_CONFIG;
-       bt_accept_enqueue(parent, sk);
+       bt_accept_enqueue(parent, sk, true);
 
        /* Accept connection and return socket DLC */
        *d = rfcomm_pi(sk)->dlc;
index 9fe6591d5fbfed183d06970e2b45a59b35bac270..01e5b0888c889bc7d32f2da005bd547727a7b9dd 100644 (file)
@@ -193,7 +193,7 @@ static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
        conn->sk = sk;
 
        if (parent)
-               bt_accept_enqueue(parent, sk);
+               bt_accept_enqueue(parent, sk, true);
 }
 
 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,