2015-02-03 08:01:13

by Johan Hedberg

[permalink] [raw]
Subject: [PATCH] Bluetooth: Fix potential NULL dereference

From: Johan Hedberg <[email protected]>

The bnep_get_device function may be triggered by an ioctl just after a
connection has gone down. In such a case the respective L2CAP chan->conn
pointer will get set to NULL (by l2cap_chan_del). This patch adds a
missing NULL check for this case in the bnep_get_device() function.

Reported-by: Patrik Flykt <[email protected]>
Signed-off-by: Johan Hedberg <[email protected]>
---
net/bluetooth/bnep/core.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/bnep/core.c b/net/bluetooth/bnep/core.c
index ce82722d049b..05f57e491ccb 100644
--- a/net/bluetooth/bnep/core.c
+++ b/net/bluetooth/bnep/core.c
@@ -511,13 +511,12 @@ static int bnep_session(void *arg)

static struct device *bnep_get_device(struct bnep_session *session)
{
- struct hci_conn *conn;
+ struct l2cap_conn *conn = l2cap_pi(session->sock->sk)->chan->conn;

- conn = l2cap_pi(session->sock->sk)->chan->conn->hcon;
- if (!conn)
+ if (!conn || !conn->hcon)
return NULL;

- return &conn->dev;
+ return &conn->hcon->dev;
}

static struct device_type bnep_type = {
--
2.1.0



2015-02-03 08:03:43

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix potential NULL dereference

Hi Johan,

> The bnep_get_device function may be triggered by an ioctl just after a
> connection has gone down. In such a case the respective L2CAP chan->conn
> pointer will get set to NULL (by l2cap_chan_del). This patch adds a
> missing NULL check for this case in the bnep_get_device() function.
>
> Reported-by: Patrik Flykt <[email protected]>
> Signed-off-by: Johan Hedberg <[email protected]>
> ---
> net/bluetooth/bnep/core.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel


2015-05-15 05:37:47

by Jaganath Kanakkassery

[permalink] [raw]
Subject: Re: Re: [PATCH] Bluetooth: Fix potential NULL dereference

SGkgTWFyY2VsLA0KDQo+Pj4+IGFkZHIgY2FuIGJlIE5V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GNhbiBiZSBjYWxsZWQgZnJvbSBrZXJuZWxfYmluZCgpIGFsc28gd2hpY2ggSSB0aGluayB3aWxs
IG5ldmVyIGJlIGNhbGxlZCBmb3IgUkZDT01NLg0KPj4gU28gdGhpcyBwYXRjaCBpcyBub3QgcmVx
dWlyZWQ/IA0KPg0KPnRoYXQgaXMgd2hhdCBJIHRob3VnaHQuIEhvd2V2ZXIgY29udmVydGluZyBp
dCB0byB0aGUgc2FtZSBoYW5kbGluZyB1c2luZyBtaW4gYW5kIGNvcHlpbmcgaW50byBsb2NhbCBz
dG9yYWdlIG1pZ2h0IGJlIGEgZ29vZCBpZGVhLiBUaGUgbW9yZSBwaWVjZXMgaW4gSENJLCBMMkNB
UCwgU0NPIGFuZCBSRkNPTU0gc29ja2V0cyB0aGF0IGFyZSBzaW1pbGFyLCB0aGUgYmV0dGVyLg0K
DQpJIGhhdmUgcmFpc2VkIHYxIHdpdGggdGhlIGNoYW5nZXMgeW91IHN1Z2dlc3RlZCwgUGx6IGNo
ZWNrIGl0Lg0KDQpUaGFua3MsDQpKYWdhbmF0aA==



2015-05-14 06:34:23

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix potential NULL dereference

Hi Jaganath,

>>> addr can be NULL and it should not be dereferenced before NULL checking.
>>>
>>> Signed-off-by: Jaganath Kanakkassery <[email protected]>
>>> ---
>>
>> if we start changing things here, then we better change the code into something that all the other socket handling code is doing anyway>y. So do the min comparison and copy the data into a local copy of the sockaddr_rc.
>>
>> And on a side note, I wonder if addr can actually be NULL. It might be interesting to check the generic socket code if this really can happe>n if you provide no address structure to the bind() system call or if this gets filtered out by the core socket code.
>
> I checked generic socket code and it looks like addr will never be NULL when user space calls bind.
> But this can be called from kernel_bind() also which I think will never be called for RFCOMM.
> So this patch is not required?

that is what I thought. However converting it to the same handling using min and copying into local storage might be a good idea. The more pieces in HCI, L2CAP, SCO and RFCOMM sockets that are similar, the better.

Regards

Marcel


2015-05-14 06:13:45

by J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W4gdXNl
ciBzcGFjZSBjYWxscyBiaW5kLg0KQnV0IHRoaXMgY2FuIGJlIGNhbGxlZCBmcm9tIGtlcm5lbF9i
aW5kKCkgYWxzbyB3aGljaCBJIHRoaW5rIHdpbGwgbmV2ZXIgYmUgY2FsbGVkIGZvciBSRkNPTU0u
DQpTbyB0aGlzIHBhdGNoIGlzIG5vdCByZXF1aXJlZD8gDQoNClRoYW5rcywNCkphZ2FuYXRo

2015-05-13 20:34:34

by Marcel Holtmann

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix potential NULL dereference

Hi Jaganath,

> addr can be NULL and it should not be dereferenced before NULL checking.
>
> Signed-off-by: Jaganath Kanakkassery <[email protected]>
> ---
> net/bluetooth/rfcomm/sock.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
> index 825e8fb..e576374 100644
> --- a/net/bluetooth/rfcomm/sock.c
> +++ b/net/bluetooth/rfcomm/sock.c
> @@ -336,14 +336,16 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
> {
> struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
> struct sock *sk = sock->sk;
> - int chan = sa->rc_channel;
> + int chan;
> int err = 0;
>
> - BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr);
> -
> if (!addr || addr->sa_family != AF_BLUETOOTH)
> return -EINVAL;
>
> + BT_DBG("sk %p %pMR", sk, &sa->rc_bdaddr);
> +
> + chan = sa->rc_channel;
> +

if we start changing things here, then we better change the code into something that all the other socket handling code is doing anyway. So do the min comparison and copy the data into a local copy of the sockaddr_rc.

And on a side note, I wonder if addr can actually be NULL. It might be interesting to check the generic socket code if this really can happen if you provide no address structure to the bind() system call or if this gets filtered out by the core socket code.

Regards

Marcel


2015-06-06 15:35:13

by chanyeol

[permalink] [raw]
Subject: Re: [PATCH] Bluetooth: Fix potential NULL dereference

Hi Marcel,

On Thu, 2015-05-14 at 08:34 +0200, Marcel Holtmann wrote:
> Hi Jaganath,
>
> > > > addr can be NULL and it should not be dereferenced before NULL
> > > > checking.
> > > >
> > > > Signed-off-by: Jaganath Kanakkassery <[email protected]>
> > > > ---
> > >
> > > if we start changing things here, then we better change the code
> > > into something that all the other socket handling code is doing
> > > anyway>y. So do the min comparison and copy the data into a local
> > > copy of the sockaddr_rc.
> > >
> > > And on a side note, I wonder if addr can actually be NULL. It
> > > might be interesting to check the generic socket code if this
> > > really can happe>n if you provide no address structure to the
> > > bind() system call or if this gets filtered out by the core
> > > socket code.
> >
> > I checked generic socket code and it looks like addr will never be
> > NULL when user space calls bind.
> > But this can be called from kernel_bind() also which I think will
> > never be called for RFCOMM.
> > So this patch is not required?
>
> that is what I thought. However converting it to the same handling
> using min and copying into local storage might be a good idea.
Could you tell us why this is good idea? I failed to find it in git
history/mailing list.

In addition to RFCOMM connect that you mentioned, I found out SCO
connect/bind still use the old style in Bluetooth unlikely HCI,L2CAP.

Regards
Chanyeol

> The more pieces in HCI, L2CAP, SCO and RFCOMM sockets that are
> similar, the better.
>
> Regards
>
> Marcel
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux
> -bluetooth" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html

2015-06-02 08:28:18

by Jaganath K

[permalink] [raw]
Subject: Re: Re: [PATCH] Bluetooth: Fix potential NULL dereference

Ping.


On Fri, May 15, 2015 at 11:07 AM, JAGANATH KANAKKASSERY <
[email protected]> wrote:

> Hi Marcel,
>
> >>>> addr can be NULL and it should not be dereferenced before NULL
> checking.
> >>>>
> >>>> Signed-off-by: Jaganath Kanakkassery <[email protected]>
> >>>> ---
> >> >
> >> >if we start changing things here, then we better change the code into
> something that all the other socket handling code is doing anyway>y. So do
> the min comparison and copy the data into a local copy of the sockaddr_rc.
> >>>
> >>> And on a side note, I wonder if addr can actually be NULL. It might be
> interesting to check the generic socket code if this really can happe>n if
> you provide no address structure to the bind() system call or if this gets
> filtered out by the core socket code.
> >>
> >> I checked generic socket code and it looks like addr will never be NULL
> when user space calls bind.
> >> But this can be called from kernel_bind() also which I think will never
> be called for RFCOMM.
> >> So this patch is not required?
> >
> >that is what I thought. However converting it to the same handling using
> min and copying into local storage might be a good idea. The more pieces in
> HCI, L2CAP, SCO and RFCOMM sockets that are similar, the better.
>
> I have raised v1 with the changes you suggested, Plz check it.
>
> Thanks,
> Jaganath