Skip to contentSkip to conversation

nfc: close a race condition in llcp_sock_getname()

PATCH Applied Cong Wang 3 participants 3 messages Jan 29, 2016 – Feb 25, 2016

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH v2 1/1 nfc: close a race condition in llcp_sock_getname()

llcp_sock_getname() checks llcp_sock->dev to make sure llcp_sock is already connected or bound, however, we could be in the middle of llcp_sock_bind() where llcp_sock->dev is bound and llcp_sock->service_name_len is set, but llcp_sock->service_name is not, in this case we would lead to copy some bytes from a NULL pointer.

Just lock the sock since this is not a hot path anyway.

Cc Lauro Ramos Venancio <[email protected]>
Cc Aloisio Almeida Jr <[email protected]>
Cc Samuel Ortiz <sameo-VuQAYsv1563Yd54FQh9/[email protected]>
---
 net/nfc/llcp_sock.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/nfc/llcp_sock.c +6 -0
index ecf0a01..b9edf5f 100644
@@ -509,6 +509,11 @@ static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
 	memset(llcp_addr, 0, sizeof(*llcp_addr));
 	*len = sizeof(struct sockaddr_nfc_llcp);
 
+	lock_sock(sk);
+	if (!llcp_sock->dev) {
+		release_sock(sk);
+		return -EBADFD;
+	}
 	llcp_addr->sa_family = AF_NFC;
 	llcp_addr->dev_idx = llcp_sock->dev->idx;
 	llcp_addr->target_idx = llcp_sock->target_idx;
@@ -518,6 +523,7 @@ static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
 	llcp_addr->service_name_len = llcp_sock->service_name_len;
 	memcpy(llcp_addr->service_name, llcp_sock->service_name,
 	       llcp_addr->service_name_len);
+	release_sock(sk);
 
 	return 0;
 }
Signature · 6 lines
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-wireless" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 7 lines · Cong Wang

llcp_sock_getname() checks llcp_sock->dev to make sure llcp_sock is already connected or bound, however, we could be in the middle of llcp_sock_bind() where llcp_sock->dev is bound and llcp_sock->service_name_len is set, but llcp_sock->service_name is not, in this case we would lead to copy some bytes from a NULL pointer.

Quoted 7 lines

Just lock the sock since this is not a hot path anyway.

Cc Lauro Ramos Venancio <[email protected]>
Cc Aloisio Almeida Jr <[email protected]>
Cc Samuel Ortiz <[email protected]>

Samuel, please take a look at this one too.

Quoted 31 lines
---
 net/nfc/llcp_sock.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/nfc/llcp_sock.c +6 -1
index ecf0a01..b9edf5f 100644
@@ -509,6 +509,11 @@ static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
        memset(llcp_addr, 0, sizeof(*llcp_addr));
        *len = sizeof(struct sockaddr_nfc_llcp);
 
+       lock_sock(sk);
+       if (!llcp_sock->dev) {
+               release_sock(sk);
+               return -EBADFD;
+       }
        llcp_addr->sa_family = AF_NFC;
        llcp_addr->dev_idx = llcp_sock->dev->idx;
        llcp_addr->target_idx = llcp_sock->target_idx;
@@ -518,6 +523,7 @@ static int llcp_sock_getname(struct socket *sock, struct sockaddr *uaddr,
        llcp_addr->service_name_len = llcp_sock->service_name_len;
        memcpy(llcp_addr->service_name, llcp_sock->service_name,
               llcp_addr->service_name_len);
+       release_sock(sk);
 
        return 0;
 }
--

1.8.3.1

Applied
#2 lore raw
In reply to Cong Wang

Hi Cong,

Quoted 9 lines · Cong Wang

llcp_sock_getname() checks llcp_sock->dev to make sure llcp_sock is already connected or bound, however, we could be in the middle of llcp_sock_bind() where llcp_sock->dev is bound and llcp_sock->service_name_len is set, but llcp_sock->service_name is not, in this case we would lead to copy some bytes from a NULL pointer.

Just lock the sock since this is not a hot path anyway.

Quoted 8 lines
Cc Lauro Ramos Venancio <[email protected]>
Cc Aloisio Almeida Jr <[email protected]>
Cc Samuel Ortiz <[email protected]>
---
 net/nfc/llcp_sock.c | 6 ++++++
 1 file changed, 6 insertions(+)

Applied as well, thanks.

Cheers,
Samuel.