Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] rfcomm: fix information leak in rfcomm_sock_bind
@ 2016-01-06  4:36 Insu Yun
  2016-01-06  6:48 ` Marcel Holtmann
  0 siblings, 1 reply; 4+ messages in thread
From: Insu Yun @ 2016-01-06  4:36 UTC (permalink / raw)
  To: marcel, gustavo, johan.hedberg, davem, peter, jaganath.k,
	ying.xue, ebiederm, linux-bluetooth, netdev, linux-kernel
  Cc: taesoo, yeongjin.jang, insu, changwoo, Insu Yun

if addr_len < sizeof(sa), sa.rc_bdaddr(4bytes) can be leaked 
by using rfcomm_sock_getname()

Signed-off-by: Insu Yun <wuninsu@gmail.com>
---
 net/bluetooth/rfcomm/sock.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 7511df7..d61dfdc 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
 {
 	struct sockaddr_rc sa;
 	struct sock *sk = sock->sk;
-	int len, err = 0;
+	int err = 0;
 
 	if (!addr || addr->sa_family != AF_BLUETOOTH)
 		return -EINVAL;
 
-	memset(&sa, 0, sizeof(sa));
-	len = min_t(unsigned int, sizeof(sa), addr_len);
-	memcpy(&sa, addr, len);
+	if (addr_len != sizeof(sa))
+		return -EINVAL;
+
+	memcpy(&sa, addr, addr_len);
 
 	BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rfcomm: fix information leak in rfcomm_sock_bind
  2016-01-06  4:36 [PATCH] rfcomm: fix information leak in rfcomm_sock_bind Insu Yun
@ 2016-01-06  6:48 ` Marcel Holtmann
  2016-01-06 13:50   ` Insu Yun
  0 siblings, 1 reply; 4+ messages in thread
From: Marcel Holtmann @ 2016-01-06  6:48 UTC (permalink / raw)
  To: Insu Yun
  Cc: Gustavo F. Padovan, Johan Hedberg, David S. Miller, Peter Hurley,
	JAGANATH KANAKKASSERY, ying.xue, Eric W. Biederman,
	BlueZ development, Network Development, LKML, taesoo,
	yeongjin.jang, insu, changwoo

Hi Insu,

> if addr_len < sizeof(sa), sa.rc_bdaddr(4bytes) can be leaked 
> by using rfcomm_sock_getname()
> 
> Signed-off-by: Insu Yun <wuninsu@gmail.com>
> ---
> net/bluetooth/rfcomm/sock.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
> index 7511df7..d61dfdc 100644
> --- a/net/bluetooth/rfcomm/sock.c
> +++ b/net/bluetooth/rfcomm/sock.c
> @@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
> {
> 	struct sockaddr_rc sa;
> 	struct sock *sk = sock->sk;
> -	int len, err = 0;
> +	int err = 0;
> 
> 	if (!addr || addr->sa_family != AF_BLUETOOTH)
> 		return -EINVAL;
> 
> -	memset(&sa, 0, sizeof(sa));
> -	len = min_t(unsigned int, sizeof(sa), addr_len);
> -	memcpy(&sa, addr, len);

what are we leaking here?

If you make addr_len (controlled by userspace) smaller than struct sockaddr_rc, the kernel will only copy addr_len of sockaddr_rc to userspace. What is the kernel leaking again? We are happy to hand out the full sockaddr_rc in the first place.

> +	if (addr_len != sizeof(sa))
> +		return -EINVAL;
> +
> +	memcpy(&sa, addr, addr_len);
> 
> 	BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);

While I give you the fact that we should require at minimum to provide bdaddr_t and channel just as a good safe guard, but even if you don't, then it just means you try to bind to a truncated address, which will fail when calling connect().

So the sockaddr_rc sa will be memset to zero. Which means if you provide less than sockaddr_rc you get a truncated rc_bdaddr and rc_channel set to zero. So what is getname() leaking now. It will leak zeros. It will not leak random kernel memory.

Please help me to understand where this is an information leak. Since leaking memory that is explicitly set to zero is not an information leak.

And just FYI you can bind a RFCOMM socket to a truncated address and channel 0 in a valid way with the full size of sockaddr_rc. That is still valid. We do not discriminate any addresses. If some company manages to get the IEEE to assign that address to them, than that is a valid address.

Regards

Marcel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rfcomm: fix information leak in rfcomm_sock_bind
  2016-01-06  6:48 ` Marcel Holtmann
@ 2016-01-06 13:50   ` Insu Yun
       [not found]     ` <2C9BF94E-E383-4931-9BE8-9FB65F693242@holtmann.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Insu Yun @ 2016-01-06 13:50 UTC (permalink / raw)
  To: Marcel Holtmann
  Cc: Gustavo F. Padovan, Johan Hedberg, David S. Miller, Peter Hurley,
	JAGANATH KANAKKASSERY, ying.xue, Eric W. Biederman,
	BlueZ development, Network Development, LKML, Taesoo Kim,
	Yeongjin Jang, Yun, Insu, Changwoo Min

[-- Attachment #1: Type: text/plain, Size: 3663 bytes --]

On Wed, Jan 6, 2016 at 1:48 AM, Marcel Holtmann <marcel@holtmann.org> wrote:

> Hi Insu,
>
> > if addr_len < sizeof(sa), sa.rc_bdaddr(4bytes) can be leaked
> > by using rfcomm_sock_getname()
> >
> > Signed-off-by: Insu Yun <wuninsu@gmail.com>
> > ---
> > net/bluetooth/rfcomm/sock.c | 9 +++++----
> > 1 file changed, 5 insertions(+), 4 deletions(-)
> >
> > diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
> > index 7511df7..d61dfdc 100644
> > --- a/net/bluetooth/rfcomm/sock.c
> > +++ b/net/bluetooth/rfcomm/sock.c
> > @@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock,
> struct sockaddr *addr, int addr
> > {
> >       struct sockaddr_rc sa;
> >       struct sock *sk = sock->sk;
> > -     int len, err = 0;
> > +     int err = 0;
> >
> >       if (!addr || addr->sa_family != AF_BLUETOOTH)
> >               return -EINVAL;
> >
> > -     memset(&sa, 0, sizeof(sa));
> > -     len = min_t(unsigned int, sizeof(sa), addr_len);
> > -     memcpy(&sa, addr, len);
>
> what are we leaking here?
>
> If you make addr_len (controlled by userspace) smaller than struct
> sockaddr_rc, the kernel will only copy addr_len of sockaddr_rc to
> userspace. What is the kernel leaking again? We are happy to hand out the
> full sockaddr_rc in the first place.
>

Yes. sockaddr_rc is in the kernel stack initialized only amount of
addr_len. Remaining part is uninitialized kernel stack which may contain
some address.


>
> > +     if (addr_len != sizeof(sa))
> > +             return -EINVAL;
> > +
> > +     memcpy(&sa, addr, addr_len);
> >
> >       BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);
>
> While I give you the fact that we should require at minimum to provide
> bdaddr_t and channel just as a good safe guard, but even if you don't, then
> it just means you try to bind to a truncated address, which will fail when
> calling connect().
>

bind will not be failed.


>
> So the sockaddr_rc sa will be memset to zero. Which means if you provide
> less than sockaddr_rc you get a truncated rc_bdaddr and rc_channel set to
> zero. So what is getname() leaking now. It will leak zeros. It will not
> leak random kernel memory.
>

Yes not random. Sorry for confusing, it only leaks unitiailized memory in
stack.


>
> Please help me to understand where this is an information leak. Since
> leaking memory that is explicitly set to zero is not an information leak.
>
> And just FYI you can bind a RFCOMM socket to a truncated address and
> channel 0 in a valid way with the full size of sockaddr_rc. That is still
> valid. We do not discriminate any addresses. If some company manages to get
> the IEEE to assign that address to them, than that is a valid address.
>
> Regards
>
> Marcel
>


#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdint.h>
#include <stdlib.h>

#define BUG

int main() {
  int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  struct sockaddr addr;
  int len = sizeof(addr);
  int i;

  if (s == -1) {
    perror("socket");
    exit(-1);
  }

  memset(&addr, 0, sizeof(addr));
  addr.sa_family = AF_BLUETOOTH;
#ifdef BUG
  if (bind(s, &addr, sizeof(addr.sa_family)) == -1) {
#else
  if (bind(s, &addr, sizeof(addr)) == -1) {
#endif
    perror("bind");
    exit(-1);
  }
  getsockname(s, &addr, &len);
  printf("%08X\n", ((struct sockaddr_rc*)&addr)->rc_bdaddr);
}

This is PoC code. When bind, it passes kernel stack address initialized by
addr_len.
But in current rfcomm_sock_bind, it copies uninitialized memory value to
src variable.
After that, it can be accessed in user space using getsockname.
Thanks
-- 
Regards
Insu Yun

[-- Attachment #2: Type: text/html, Size: 6587 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rfcomm: fix information leak in rfcomm_sock_bind
       [not found]     ` <2C9BF94E-E383-4931-9BE8-9FB65F693242@holtmann.org>
@ 2016-01-06 15:37       ` Insu Yun
  0 siblings, 0 replies; 4+ messages in thread
From: Insu Yun @ 2016-01-06 15:37 UTC (permalink / raw)
  To: Marcel Holtmann
  Cc: Gustavo F. Padovan, Johan Hedberg, David S. Miller, Peter Hurley,
	JAGANATH KANAKKASSERY, ying.xue, Eric W. Biederman,
	BlueZ development, Network Development, LKML, Taesoo Kim,
	Yeongjin Jang, Yun, Insu, Changwoo Min

[-- Attachment #1: Type: text/plain, Size: 4498 bytes --]

On Wed, Jan 6, 2016 at 10:20 AM, Marcel Holtmann <marcel@holtmann.org>
wrote:

> Hi Insu,
>
> > if addr_len < sizeof(sa), sa.rc_bdaddr(4bytes) can be leaked
>> > by using rfcomm_sock_getname()
>> >
>> > Signed-off-by: Insu Yun <wuninsu@gmail.com>
>> > ---
>> > net/bluetooth/rfcomm/sock.c | 9 +++++----
>> > 1 file changed, 5 insertions(+), 4 deletions(-)
>> >
>> > diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
>> > index 7511df7..d61dfdc 100644
>> > --- a/net/bluetooth/rfcomm/sock.c
>> > +++ b/net/bluetooth/rfcomm/sock.c
>> > @@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock,
>> struct sockaddr *addr, int addr
>> > {
>> >       struct sockaddr_rc sa;
>> >       struct sock *sk = sock->sk;
>> > -     int len, err = 0;
>> > +     int err = 0;
>> >
>> >       if (!addr || addr->sa_family != AF_BLUETOOTH)
>> >               return -EINVAL;
>> >
>> > -     memset(&sa, 0, sizeof(sa));
>> > -     len = min_t(unsigned int, sizeof(sa), addr_len);
>> > -     memcpy(&sa, addr, len);
>>
>> what are we leaking here?
>>
>> If you make addr_len (controlled by userspace) smaller than struct
>> sockaddr_rc, the kernel will only copy addr_len of sockaddr_rc to
>> userspace. What is the kernel leaking again? We are happy to hand out the
>> full sockaddr_rc in the first place.
>>
>
> Yes. sockaddr_rc is in the kernel stack initialized only amount of
> addr_len. Remaining part is uninitialized kernel stack which may contain
> some address.
>
>
> it is initialized to 0 via memset(&sa, 0, sizeof(sa)). It is not
> uninitialized kernel stack memory. It is initialized to zero.
>
>
>
>>
>> > +     if (addr_len != sizeof(sa))
>> > +             return -EINVAL;
>> > +
>> > +     memcpy(&sa, addr, addr_len);
>> >
>> >       BT_DBG("sk %p %pMR", sk, &sa.rc_bdaddr);
>>
>> While I give you the fact that we should require at minimum to provide
>> bdaddr_t and channel just as a good safe guard, but even if you don't, then
>> it just means you try to bind to a truncated address, which will fail when
>> calling connect().
>>
>
> bind will not be failed.
>
>
>>
>> So the sockaddr_rc sa will be memset to zero. Which means if you provide
>> less than sockaddr_rc you get a truncated rc_bdaddr and rc_channel set to
>> zero. So what is getname() leaking now. It will leak zeros. It will not
>> leak random kernel memory.
>>
>
> Yes not random. Sorry for confusing, it only leaks unitiailized memory in
> stack.
>
>
> Again, it is initialized to zero memory.
>
>
>
>>
>> Please help me to understand where this is an information leak. Since
>> leaking memory that is explicitly set to zero is not an information leak.
>>
>> And just FYI you can bind a RFCOMM socket to a truncated address and
>> channel 0 in a valid way with the full size of sockaddr_rc. That is still
>> valid. We do not discriminate any addresses. If some company manages to get
>> the IEEE to assign that address to them, than that is a valid address.
>>
>> Regards
>>
>> Marcel
>>
>
>
> #include <bluetooth/bluetooth.h>
> #include <bluetooth/rfcomm.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <stdint.h>
> #include <stdlib.h>
>
> #define BUG
>
> int main() {
>   int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
>   struct sockaddr addr;
>   int len = sizeof(addr);
>   int i;
>
>   if (s == -1) {
>     perror("socket");
>     exit(-1);
>   }
>
>   memset(&addr, 0, sizeof(addr));
>   addr.sa_family = AF_BLUETOOTH;
> #ifdef BUG
>   if (bind(s, &addr, sizeof(addr.sa_family)) == -1) {
> #else
>   if (bind(s, &addr, sizeof(addr)) == -1) {
> #endif
>     perror("bind");
>     exit(-1);
>   }
>   getsockname(s, &addr, &len);
>   printf("%08X\n", ((struct sockaddr_rc*)&addr)->rc_bdaddr);
> }
>
> This is PoC code. When bind, it passes kernel stack address initialized by
> addr_len.
> But in current rfcomm_sock_bind, it copies uninitialized memory value to
> src variable.
> After that, it can be accessed in user space using getsockname.
>
>
> You did run that PoC? It gives you 00000000 since as mentioned above the
> sockaddr_rc sa is actually set to zero.
>

Sorry. I got it now. I misread the code. You are right, it only leaks NULL
bytes.
I read code and I tested in older version of Linux which does not have that
memset code.
insu@insu:~$ ./a
880C93BE
In that version, it has memory leak.
But after this commit, 951b6a0717db97ce420547222647bcc40bf1eacd, it does
not produce such issue.
Sorry.



>
> Regards
>
> Marcel
>
>


-- 
Regards
Insu Yun

[-- Attachment #2: Type: text/html, Size: 9158 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-01-06 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-06  4:36 [PATCH] rfcomm: fix information leak in rfcomm_sock_bind Insu Yun
2016-01-06  6:48 ` Marcel Holtmann
2016-01-06 13:50   ` Insu Yun
     [not found]     ` <2C9BF94E-E383-4931-9BE8-9FB65F693242@holtmann.org>
2016-01-06 15:37       ` Insu Yun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox