Skip to content

Commit d2ecfa7

Browse files
j00ruholtmann
authored andcommitted
Bluetooth: Add sockaddr length checks before accessing sa_family in bind 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>
1 parent 29e2dd0 commit d2ecfa7

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

‎net/bluetooth/l2cap_sock.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
8787

8888
BT_DBG("sk %p", sk);
8989

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

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

182183
BT_DBG("sk %p", sk);
183184

184-
if (!addr || alen < sizeof(addr->sa_family) ||
185+
if (!addr || alen < offsetofend(struct sockaddr, sa_family) ||
185186
addr->sa_family != AF_BLUETOOTH)
186187
return -EINVAL;
187188

‎net/bluetooth/rfcomm/sock.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
339339
struct sock *sk = sock->sk;
340340
int len, err = 0;
341341

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

345346
memset(&sa, 0, sizeof(sa));

‎net/bluetooth/sco.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,8 @@ static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
524524

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

527-
if (!addr || addr->sa_family != AF_BLUETOOTH)
528-
return -EINVAL;
529-
530-
if (addr_len < sizeof(struct sockaddr_sco))
527+
if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
528+
addr->sa_family != AF_BLUETOOTH)
531529
return -EINVAL;
532530

533531
lock_sock(sk);

0 commit comments

Comments
 (0)