Skip to content

Commit

Permalink
Bluetooth: Add sockaddr length checks before accessing sa_family in b…
Browse files Browse the repository at this point in the history
…ind and connect handlers

Verify that the caller-provided sockaddr structure is large enough to
contain the sa_family field, before accessing it in bind() and connect()
handlers of the Bluetooth sockets. Since neither syscall enforces a minimum
size of the corresponding memory region, very short sockaddrs (zero or one
byte long) result in operating on uninitialized memory while referencing
sa_family.

Signed-off-by: Mateusz Jurczyk <mjurczyk@google.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
  • Loading branch information
Mateusz Jurczyk authored and Marcel Holtmann committed Jun 29, 2017
1 parent 29e2dd0 commit d2ecfa7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
5 changes: 3 additions & 2 deletions net/bluetooth/l2cap_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)

BT_DBG("sk %p", sk);

if (!addr || addr->sa_family != AF_BLUETOOTH)
if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
addr->sa_family != AF_BLUETOOTH)
return -EINVAL;

memset(&la, 0, sizeof(la));
Expand Down Expand Up @@ -181,7 +182,7 @@ static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,

BT_DBG("sk %p", sk);

if (!addr || alen < sizeof(addr->sa_family) ||
if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
addr->sa_family != AF_BLUETOOTH)
return -EINVAL;

Expand Down
3 changes: 2 additions & 1 deletion net/bluetooth/rfcomm/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
struct sock *sk = sock->sk;
int len, err = 0;

if (!addr || addr->sa_family != AF_BLUETOOTH)
if (!addr || addr_len < offsetofend(struct sockaddr, sa_family) ||
addr->sa_family != AF_BLUETOOTH)
return -EINVAL;

memset(&sa, 0, sizeof(sa));
Expand Down
6 changes: 2 additions & 4 deletions net/bluetooth/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,8 @@ static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,

BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);

if (!addr || addr->sa_family != AF_BLUETOOTH)
return -EINVAL;

if (addr_len < sizeof(struct sockaddr_sco))
if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
addr->sa_family != AF_BLUETOOTH)
return -EINVAL;

lock_sock(sk);
Expand Down

0 comments on commit d2ecfa7

Please sign in to comment.