rfcomm: fix information leak in rfcomm_sock_bind
Subsystems (4)
PATCH 1/1 rfcomm: fix information leak in rfcomm_sock_bind
if addr_len < sizeof(sa), sa.rc_bdaddr(4bytes) can be leaked by using rfcomm_sock_getname()
--- net/bluetooth/rfcomm/sock.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
net/bluetooth/rfcomm/sock.c +5 -4
@@ -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);
Signature · 1 line
1.9.1
Marcel Holtmann
maintainerHi Insu,
Quoted 15 lines
if
addr_len< sizeof(sa),sa.rc_bdaddr(4bytes)can be leaked by usingrfcomm_sock_getname()Signed-off-by Insu Yun <[email protected]>---
net/bluetooth/rfcomm/sock.c| 9 +++++----1 file changed, 5 insertions(+), 4 deletions(-)net/bluetooth/rfcomm/sock.c +0
-0@@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr {struct sockaddr_rc sa;
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 346
Quoted 10 lines
net/bluetooth/rfcomm/sock.c +1
-4struct 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.
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 349
Quoted 6 lines
net/bluetooth/rfcomm/sock.c +4
-0+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
Quoted 29 lines · Marcel Holtmann
Hi Insu,
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock,
Quoted 13 lines
if
addr_len< sizeof(sa),sa.rc_bdaddr(4bytes)can be leaked by usingrfcomm_sock_getname()Signed-off-by Insu Yun <[email protected]>---
net/bluetooth/rfcomm/sock.c| 9 +++++----1 file changed, 5 insertions(+), 4 deletions(-)net/bluetooth/rfcomm/sock.c +0
-0@@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock,struct sockaddr *addr, int addr
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 346
Quoted 12 lines
net/bluetooth/rfcomm/sock.c +1
-4{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);
Quoted 7 lines
what are we leaking here?
If you make
addr_len(controlled by userspace) smaller than structsockaddr_rc, the kernel will only copyaddr_lenofsockaddr_rcto userspace. What is the kernel leaking again? We are happy to hand out the fullsockaddr_rcin 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.
Quoted 6 lines
Quoted 6 lines
net/bluetooth/rfcomm/sock.c +4
-0+if (addr_len !=sizeof (sa)) +return -EINVAL ; + +memcpy (&sa, addr, addr_len);
Quoted 7 lines
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 349
Quoted 1 line
net/bluetooth/rfcomm/sock.c +0
-0BT_DBG ("sk %p %pMR" , sk, &sa.rc_bdaddr);While I give you the fact that we should require at minimum to provide
bdaddr_tand 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 callingconnect().
bind will not be failed.
Quoted 6 lines
So the
sockaddr_rcsa will be memset to zero. Which means if you provide less thansockaddr_rcyou get a truncatedrc_bdaddrandrc_channelset to zero. So what isgetname()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.
Quoted 4 lines
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.
Quoted 9 lines
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
Signature · 2 lines
Regards Insu Yun
Quoted 135 lines · Marcel Holtmann
Hi Insu,
Quoted 27 lines
if
addr_len< sizeof(sa),sa.rc_bdaddr(4bytes)can be leakednet/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock,
Quoted 12 lines
by using
rfcomm_sock_getname()Signed-off-by Insu Yun <[email protected]>---
net/bluetooth/rfcomm/sock.c| 9 +++++----1 file changed, 5 insertions(+), 4 deletions(-)net/bluetooth/rfcomm/sock.c +0
-0@@ -336,14 +336,15 @@ static int rfcomm_sock_bind(struct socket *sock,struct sockaddr *addr, int addr
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 346
Quoted 12 lines
net/bluetooth/rfcomm/sock.c +1
-4{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);Quoted 7 lines
what are we leaking here?
If you make
addr_len(controlled by userspace) smaller than structsockaddr_rc, the kernel will only copyaddr_lenofsockaddr_rcto userspace. What is the kernel leaking again? We are happy to hand out the fullsockaddr_rcin the first place.Yes.
sockaddr_rcis in the kernel stack initialized only amount ofaddr_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.Quoted 6 lines
Quoted 6 lines
net/bluetooth/rfcomm/sock.c +4
-0+if (addr_len !=sizeof (sa)) +return -EINVAL ; + +memcpy (&sa, addr, addr_len);Quoted 7 lines
net/bluetooth/rfcomm/sock.c static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr · line 349
Quoted 1 line
net/bluetooth/rfcomm/sock.c +0
-0BT_DBG ("sk %p %pMR" , sk, &sa.rc_bdaddr);While I give you the fact that we should require at minimum to provide
bdaddr_tand 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 callingconnect().bind will not be failed.
Quoted 6 lines
So the
sockaddr_rcsa will be memset to zero. Which means if you provide less thansockaddr_rcyou get a truncatedrc_bdaddrandrc_channelset to zero. So what isgetname()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.
Quoted 4 lines
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.
Quoted 9 lines
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 currentrfcomm_sock_bind, it copies uninitialized memory value to src variable.
After that, it can be accessed in user space using getsockname.
Quoted 3 lines
You did run that PoC? It gives you 00000000 since as mentioned above the
sockaddr_rcsa 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.
Quoted 6 lines
Regards
Marcel
Signature · 2 lines
Regards Insu Yun