Skip to contentSkip to conversation

bluetooth: 6lowpan: fix use after free in chan_suspend/resume

PATCH In review Michael Scott 4 participants 4 messages Mar 29, 2017 – Mar 31, 2017

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bluetooth: 6lowpan: fix use after free in chan_suspend/resume

A status field in the skb_cb struct was storing a channel status based on channel suspend/resume events. This stored status was then used to return EAGAIN if there were packet sending issues in snd_pkt().

The issue is that the skb has been freed by the time the callback to 6lowpan's suspend/resume was called. So, this generates a "use after free" issue that was noticed while running kernel tests with KASAN debug enabled.

Let's eliminate the status field entirely as we can use the channel tx_credits to indicate whether we should return EAGAIN when handling packets.

---
 net/bluetooth/6lowpan.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
net/bluetooth/6lowpan.c +3 -18
index d491529332f4..e27be3ca0a0c 100644
@@ -38,7 +38,6 @@ struct skb_cb {
 	struct in6_addr addr;
 	struct in6_addr gw;
 	struct l2cap_chan *chan;
-	int status;
 };
 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
 
@@ -528,7 +527,7 @@ static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
 	}
 
 	if (!err)
-		err = lowpan_cb(skb)->status;
+		err = (!chan->tx_credits ? -EAGAIN : 0);
 
 	if (err < 0) {
 		if (err == -EAGAIN)
@@ -964,26 +963,12 @@ static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
 
 static void chan_suspend_cb(struct l2cap_chan *chan)
 {
-	struct sk_buff *skb = chan->data;
-
-	BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-	if (!skb)
-		return;
-
-	lowpan_cb(skb)->status = -EAGAIN;
+	BT_DBG("chan %p suspend", chan);
 }
 
 static void chan_resume_cb(struct l2cap_chan *chan)
 {
-	struct sk_buff *skb = chan->data;
-
-	BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-	if (!skb)
-		return;
-
-	lowpan_cb(skb)->status = 0;
+	BT_DBG("chan %p resume", chan);
 }
 
 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
Signature · 1 line
2.11.0
#1 lore raw

Hi Michael,

Quoted 70 lines · Michael Scott

A status field in the skb_cb struct was storing a channel status based on channel suspend/resume events. This stored status was then used to return EAGAIN if there were packet sending issues in snd_pkt().

The issue is that the skb has been freed by the time the callback to 6lowpan's suspend/resume was called. So, this generates a "use after free" issue that was noticed while running kernel tests with KASAN debug enabled.

Let's eliminate the status field entirely as we can use the channel tx_credits to indicate whether we should return EAGAIN when handling packets.

---
 net/bluetooth/6lowpan.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
net/bluetooth/6lowpan.c +3 -19
index d491529332f4..e27be3ca0a0c 100644
@@ -38,7 +38,6 @@ struct skb_cb {
        struct in6_addr addr;
        struct in6_addr gw;
        struct l2cap_chan *chan;
-       int status;
 };
 #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
 
@@ -528,7 +527,7 @@ static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
        }
 
        if (!err)
-               err = lowpan_cb(skb)->status;
+               err = (!chan->tx_credits ? -EAGAIN : 0);
 
        if (err < 0) {
                if (err == -EAGAIN)
@@ -964,26 +963,12 @@ static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
 
 static void chan_suspend_cb(struct l2cap_chan *chan)
 {
-       struct sk_buff *skb = chan->data;
-
-       BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-       if (!skb)
-               return;
-
-       lowpan_cb(skb)->status = -EAGAIN;
+       BT_DBG("chan %p suspend", chan);
 }
 
 static void chan_resume_cb(struct l2cap_chan *chan)
 {
-       struct sk_buff *skb = chan->data;
-
-       BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-       if (!skb)
-               return;
-
-       lowpan_cb(skb)->status = 0;
+       BT_DBG("chan %p resume", chan);
 }
 
 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
--
Quoted 1 line

2.11.0

It should be possible to queue the packets on l2cap_chan_send, Im not sure why we have this suspend logic in the first place so perhaps Jukka can shed some light here.

Signature · 1 line
Luiz Augusto von Dentz
Acked-by on v1 1/1
#2 lore raw
In reply to Michael Scott

Hi Michael,

Quoted 68 lines · Michael Scott

A status field in the skb_cb struct was storing a channel status based on channel suspend/resume events.  This stored status was then used to return EAGAIN if there were packet sending issues in snd_pkt().

The issue is that the skb has been freed by the time the callback to 6lowpan's suspend/resume was called.  So, this generates a "use after free" issue that was noticed while running kernel tests with KASAN debug enabled.

Let's eliminate the status field entirely as we can use the channel tx_credits to indicate whether we should return EAGAIN when handling packets.

---
 net/bluetooth/6lowpan.c | 21 +++------------------

 1 file changed, 3 insertions(+), 18 deletions(-)
net/bluetooth/6lowpan.c +0 -1
index d491529332f4..e27be3ca0a0c 100644
@@ -38,7 +38,6 @@ struct skb_cb {
  	struct in6_addr addr;
  	struct in6_addr gw;
  	struct l2cap_chan *chan;
-	int status;
  };
  #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))

net/bluetooth/6lowpan.c +0 -0
@@ -528,7 +527,7 @@ static int send_pkt(struct l2cap_chan *chan,
 struct sk_buff *skb,
  	}


  if (!err)

net/bluetooth/6lowpan.c +1 -1
-		err = lowpan_cb(skb)->status;
+		err = (!chan->tx_credits ? -EAGAIN : 0);


  if (err < 0) {
  if (err == -EAGAIN)

net/bluetooth/6lowpan.c +0 -0
@@ -964,26 +963,12 @@ static struct sk_buff *chan_alloc_skb_cb(struct
 l2cap_chan *chan,


 static void chan_suspend_cb(struct l2cap_chan *chan) {

net/bluetooth/6lowpan.c +1 -8
-	struct sk_buff *skb = chan->data;
-
-	BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-	if (!skb)
-		return;
-
-	lowpan_cb(skb)->status = -EAGAIN;
+	BT_DBG("chan %p suspend", chan);

 }

 static void chan_resume_cb(struct l2cap_chan *chan) {

net/bluetooth/6lowpan.c +1 -8
-	struct sk_buff *skb = chan->data;
-
-	BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
-
-	if (!skb)
-		return;
-
-	lowpan_cb(skb)->status = 0;
+	BT_DBG("chan %p resume", chan);
Quoted 3 lines

 }

 static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)

Good catch! If we can avoid using the status variable, that is very good. We could probably also remove the resume and suspend callbacks as they are now empty functions (unless we need the debug info for something).

Cheers,
Jukka

maintainer
#3 lore raw
In reply to Michael Scott

Hi Michael,

Quoted 10 lines

A status field in the skb_cb struct was storing a channel status based on channel suspend/resume events. This stored status was then used to return EAGAIN if there were packet sending issues in snd_pkt().

The issue is that the skb has been freed by the time the callback to 6lowpan's suspend/resume was called. So, this generates a "use after free" issue that was noticed while running kernel tests with KASAN debug enabled.

Quoted 8 lines

Let's eliminate the status field entirely as we can use the channel tx_credits to indicate whether we should return EAGAIN when handling packets.

---
net/bluetooth/6lowpan.c | 21 +++------------------

1 file changed, 3 insertions(+), 18 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel