Skip to contentSkip to conversation

Bluetooth: prevent double l2cap_chan_destroy

RFC/PATCH Minho Ban 3 participants 5 messages May 21, 2012 – May 23, 2012

Subsystems (4)
lore
Share & tools
lore

Minho Ban

author
#0 lore raw

l2cap_sock_kill can be called in l2cap_sock_release and l2cap_sock_close_cb either. This lead l2cap_chan_destroy to be called twice for same channel. To prevent double list_del and double chan_put, chan_destroy should be protected with chan->refcnt and chan_list_lock so that reentrance could be forbidden.

---
 net/bluetooth/l2cap_core.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
net/bluetooth/l2cap_core.c +6 -2
index 24f144b..156ca14 100644
@@ -400,10 +400,14 @@ struct l2cap_chan *l2cap_chan_create(void)
 void l2cap_chan_destroy(struct l2cap_chan *chan)
 {
 	write_lock(&chan_list_lock);
+	/* Check if channel is valid */
+	if (!atomic_read(&chan->refcnt)) {
+		write_unlock(&chan_list_lock);
+		return;
+	}
 	list_del(&chan->global_l);
-	write_unlock(&chan_list_lock);
-
 	l2cap_chan_put(chan);
+	write_unlock(&chan_list_lock);
 }
 
 void l2cap_chan_set_defaults(struct l2cap_chan *chan)
Signature · 1 line
1.7.5.4
#1 lore raw

Hi Minho,

Quoted 4 lines · Minho Ban

l2cap_sock_kill can be called in l2cap_sock_release and l2cap_sock_close_cb either. This lead l2cap_chan_destroy to be called twice for same channel. To prevent double list_del and double chan_put, chan_destroy should be protected with chan->refcnt and chan_list_lock so that reentrance could be forbidden.

Even if l2cap_sock_kill() is called twice it will call l2cap_chan_destroy() only once. If this is not happening we just have a broken piece of code somewhere else and not here.

Gustavo

author
#2 lore raw
Quoted 9 lines · Gustavo Padovan

Hi Minho,

Quoted 4 lines · Minho Ban

l2cap_sock_kill can be called in l2cap_sock_release and l2cap_sock_close_cb either. This lead l2cap_chan_destroy to be called twice for same channel. To prevent double list_del and double chan_put, chan_destroy should be protected with chan->refcnt and chan_list_lock so that reentrance could be forbidden.

Quoted 6 lines

Even if l2cap_sock_kill() is called twice it will call l2cap_chan_destroy() only once. If this is not happening we just have a broken piece of code somewhere else and not here.

Gustavo

Thanks for comment but I could not found any suitable code in l2cap_sock_kill that can make l2cap_chan_destroy to be called just once. sock flag test is not enough to do it.

I agree this path should not be the fix. Testing chan->refcnt is nonsense because chan might have been freed already. So I looked for another point,

@@ -1343,10 +1343,10 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)

                l2cap_chan_lock(chan);
 
                l2cap_chan_del(chan, err);

+ chan->ops->close(chan->data);

                l2cap_chan_unlock(chan);
-               chan->ops->close(chan->data);
                l2cap_chan_put(chan);
        }

close callback must locate within chan_lock unless it can be scheduled to other thread which may wait for chan_lock in l2cap_sock_shutdown and this lead to duplicate sock_kill.

static void l2cap_sock_kill(struct sock *sk)
{

-       if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
+       if (!sock_flag(sk, SOCK_ZAPPED) || sock_flag(sk, SOCK_DEAD) ||
+                       sk->sk_socket)
                return;
 
        BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));

Duplicate sock_kill may happen anyway, need test SOCK_DEAD if chan_destroy is already called.

Regards,
Minho Ban

#3 lore raw

Hi

Review static void l2cap_conn_del(struct hci_conn *hcon, int err) · line 1351

Quoted 11 lines · Minho Ban
(quoted diff) +1 -1
@@ -1343,10 +1343,10 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)
                 l2cap_chan_lock(chan);
 
                 l2cap_chan_del(chan, err);
+               chan->ops->close(chan->data);
 
                 l2cap_chan_unlock(chan);
 
-               chan->ops->close(chan->data);
                 l2cap_chan_put(chan);
         }

I think this patch does not make sense Because inside chan->ops->close() "chan" could be freed in the l2cap_chan_destroy().

Quoted 12 lines

close callback must locate within chan_lock unless it can be scheduled to other thread which may wait for chan_lock in l2cap_sock_shutdown and this lead to duplicate sock_kill.

  static void l2cap_sock_kill(struct sock *sk)
  {
(quoted diff) +2 -1
-       if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
+       if (!sock_flag(sk, SOCK_ZAPPED) || sock_flag(sk, SOCK_DEAD) ||
+                       sk->sk_socket)
                 return;
 
         BT_DBG("sk %p state %s", sk, state_to_string(sk->sk_state));
Quoted 1 line

Duplicate sock_kill may happen anyway, need test SOCK_DEAD if chan_destroy is already called.

Even l2cap_sock_kill() is called twice, " if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)" can't filter it. I tested Mr.ban case. it works fine with me.

BR
Chanyeol Park.

author
#4 lore raw
Quoted 9 lines · Chanyeol Park

Hi

Quoted 6 lines · Minho Ban
(quoted diff) +1 -0
@@ -1343,10 +1343,10 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)
                 l2cap_chan_lock(chan);
 
                 l2cap_chan_del(chan, err);
+               chan->ops->close(chan->data);
 
Quoted 7 lines

Review static void l2cap_conn_del(struct hci_conn *hcon, int err) · line 1351

Quoted 5 lines
(quoted diff) +0 -1
                 l2cap_chan_unlock(chan);
 
-               chan->ops->close(chan->data);
                 l2cap_chan_put(chan);
         }

I think this patch does not make sense Because inside chan->ops->close() "chan" could be freed in the l2cap_chan_destroy().

I agree, thanks for pointing out.