2015-03-26 09:10:47

by Harish Jenny K N

[permalink] [raw]
Subject: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout

There is a potential use after free in bt_sock_poll when a
socket gets killed without getting unlinked from accept_q.
Hence added code to unlink from accpept_q by calling teardown
before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.

Signed-off-by: Harish Jenny K N <[email protected]>
---
net/bluetooth/l2cap_core.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 6ba33f9..3e273e6 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -415,6 +415,10 @@ static void l2cap_chan_timeout(struct work_struct *work)

l2cap_chan_close(chan, reason);

+ if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
+ chan->chan_type == L2CAP_CHAN_CONN_ORIENTED)
+ chan->ops->teardown(chan, 0);
+
l2cap_chan_unlock(chan);

chan->ops->close(chan);
--
1.7.9.5


2015-03-19 14:59:09

by Sergei Shtylyov

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout

Hello.

On 3/19/2015 11:19 AM, Harish Jenny K N wrote:

> There is a potential use after free in bt_sock_poll when a
> socket gets killed without getting unlinked from accept_q.
> Hence added code to unlink from accpept_q by calling teardown
> before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.

> Signed-off-by: Harish Jenny K N <[email protected]>
> ---
> net/bluetooth/l2cap_core.c | 5 +++++
> 1 file changed, 5 insertions(+)

> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 6ba33f9..c6955fb 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -415,6 +415,11 @@ static void l2cap_chan_timeout(struct work_struct *work)
>
> l2cap_chan_close(chan, reason);
>
> + if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
> + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
> + chan->ops->teardown(chan, 0);
> + }
> +

{} not needed here.

[...]

WBR, Sergei

2015-03-19 08:59:42

by Harish Jenny K N

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout



On Thursday 19 March 2015 01:55 PM, Johan Hedberg wrote:
> Hi,
>
> On Thu, Mar 19, 2015, Harish Jenny K N wrote:
>> There is a potential use after free in bt_sock_poll when a
>> socket gets killed without getting unlinked from accept_q.
>> Hence added code to unlink from accpept_q by calling teardown
>> before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.
>>
>> Signed-off-by: Harish Jenny K N <[email protected]>
>> ---
>> net/bluetooth/l2cap_core.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
>> index 6ba33f9..c6955fb 100644
>> --- a/net/bluetooth/l2cap_core.c
>> +++ b/net/bluetooth/l2cap_core.c
>> @@ -415,6 +415,11 @@ static void l2cap_chan_timeout(struct work_struct *work)
>>
>> l2cap_chan_close(chan, reason);
>>
>> + if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
>> + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
>> + chan->ops->teardown(chan, 0);
>> + }
> Looks quite ok to me, except for a minor issue: we generally don't use
> {} for single-line branches.
>
> Do you have some simple way to reproduce this? It'd be nice if we could
> add a test case for it to our user space l2cap-tester tool.
>
> Johan
Unfortunately no. The crash happened only once in automated test set-up.
Have given only the proposed solution based on the code flow.
Please check from protocol perspective if this patch is correct and useful.

2015-03-19 08:57:37

by Harish Jenny K N

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout



On Thursday 19 March 2015 01:55 PM, Johan Hedberg wrote:
> Hi,
>
> On Thu, Mar 19, 2015, Harish Jenny K N wrote:
>> There is a potential use after free in bt_sock_poll when a
>> socket gets killed without getting unlinked from accept_q.
>> Hence added code to unlink from accpept_q by calling teardown
>> before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.
>>
>> Signed-off-by: Harish Jenny K N <[email protected]>
>> ---
>> net/bluetooth/l2cap_core.c | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
>> index 6ba33f9..c6955fb 100644
>> --- a/net/bluetooth/l2cap_core.c
>> +++ b/net/bluetooth/l2cap_core.c
>> @@ -415,6 +415,11 @@ static void l2cap_chan_timeout(struct work_struct *work)
>>
>> l2cap_chan_close(chan, reason);
>>
>> + if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
>> + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
>> + chan->ops->teardown(chan, 0);
>> + }
> Looks quite ok to me, except for a minor issue: we generally don't use
> {} for single-line branches.
>
> Do you have some simple way to reproduce this? It'd be nice if we could
> add a test case for it to our user space l2cap-tester tool.
>
> Johan

There is a potential use after free in bt_sock_poll when a
socket gets killed without getting unlinked from accept_q.
Hence added code to unlink from accpept_q by calling teardown
before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.

Signed-off-by: Harish Jenny K N <[email protected]>
---
net/bluetooth/l2cap_core.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 6ba33f9..3e273e6 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -415,6 +415,10 @@ static void l2cap_chan_timeout(struct work_struct *work)

l2cap_chan_close(chan, reason);

+ if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
+ chan->chan_type == L2CAP_CHAN_CONN_ORIENTED)
+ chan->ops->teardown(chan, 0);
+
l2cap_chan_unlock(chan);

chan->ops->close(chan);



2015-03-19 08:25:39

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout

Hi,

On Thu, Mar 19, 2015, Harish Jenny K N wrote:
> There is a potential use after free in bt_sock_poll when a
> socket gets killed without getting unlinked from accept_q.
> Hence added code to unlink from accpept_q by calling teardown
> before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.
>
> Signed-off-by: Harish Jenny K N <[email protected]>
> ---
> net/bluetooth/l2cap_core.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 6ba33f9..c6955fb 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -415,6 +415,11 @@ static void l2cap_chan_timeout(struct work_struct *work)
>
> l2cap_chan_close(chan, reason);
>
> + if ((chan->state == BT_CONNECTED || chan->state == BT_CONFIG) &&
> + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
> + chan->ops->teardown(chan, 0);
> + }

Looks quite ok to me, except for a minor issue: we generally don't use
{} for single-line branches.

Do you have some simple way to reproduce this? It'd be nice if we could
add a test case for it to our user space l2cap-tester tool.

Johan

2015-03-18 14:22:36

by Sergei Shtylyov

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix use after free in l2cap_chan_timeout

Hello.

On 3/18/2015 2:59 PM, Harish Jenny K N wrote:

> There is a potential use after free in bt_sock_poll when a
> socket gets killed without getting unlinked from accept_q.
> Hence added code to unlink from accpept_q by calling teardown
> before freeing the socket for channel type L2CAP_CHAN_CONN_ORIENTED.

> Signed-off-by: Harish Jenny K N <[email protected]>
> ---
> net/bluetooth/l2cap_core.c | 5 +++++
> 1 file changed, 5 insertions(+)

> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index 6ba33f9..3c3421e 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -415,6 +415,11 @@ static void l2cap_chan_timeout(struct work_struct *work)
>
> l2cap_chan_close(chan, reason);
>
> + if (((chan->state == BT_CONNECTED) || (chan->state == BT_CONFIG)) &&

Note that parens around == are unnecessary.

> + chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
> + chan->ops->teardown(chan, 0);
> + }
> +
[...]

WBR, Sergei