Skip to contentSkip to conversation

net/uip: Avoid deadlock in uip_tcp_socket_free()

PATCH Superseded Steven Price 2 participants 2 messages Sep 12, 2025 – Sep 17, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/uip: Avoid deadlock in uip_tcp_socket_free()

The function uip_tcp_socket_free() is called with the sk lock held, but then goes on to call uip_tcp_socket_close() which attempts to aquire the lock a second time, triggering a deadlock if there are outstanding TCP connections.

Rather than call uip_tcp_socket_close(), just do the cleanup directly in uip_tcp_socket_free().

Fixes d87b503f4d6e ("net/uip: Add exit function")
---
 net/uip/tcp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/uip/tcp.c +6 -2
index 8e0ad5235240..2a6a8f5265d9 100644
@@ -109,8 +109,12 @@ static void uip_tcp_socket_free(struct uip_tcp_socket *sk)
 		pthread_join(sk->thread, NULL);
 	}
 
-	sk->write_done = sk->read_done = 1;
-	uip_tcp_socket_close(sk, SHUT_RDWR);
+	shutdown(sk->fd, SHUT_RDWR);
+	close(sk->fd);
+	list_del(&sk->list);
+
+	free(sk->buf);
+	free(sk);
 }
 
 static int uip_tcp_payload_send(struct uip_tcp_socket *sk, u8 flag, u16 payload_len)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 22 lines · Steven Price

The function uip_tcp_socket_free() is called with the sk lock held, but then goes on to call uip_tcp_socket_close() which attempts to aquire the lock a second time, triggering a deadlock if there are outstanding TCP connections.

Rather than call uip_tcp_socket_close(), just do the cleanup directly in uip_tcp_socket_free().

Fixes d87b503f4d6e ("net/uip: Add exit function")
---
 net/uip/tcp.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/uip/tcp.c +0 -0
index 8e0ad5235240..2a6a8f5265d9 100644
@@ -109,8 +109,12 @@ static void uip_tcp_socket_free(struct uip_tcp_socket *sk)
 		pthread_join(sk->thread, NULL);
 	}
 

net/uip/tcp.c static void uip_tcp_socket_free(struct uip_tcp_socket *sk) · line 119

Quoted 10 lines
net/uip/tcp.c +6 -2
-	sk->write_done = sk->read_done = 1;
-	uip_tcp_socket_close(sk, SHUT_RDWR);
+	shutdown(sk->fd, SHUT_RDWR);
+	close(sk->fd);
+	list_del(&sk->list);
+
+	free(sk->buf);
+	free(sk);
 }
 

Cheers, Steven.

Rather than duplicate the guts of uip_tcp_socket_close() here, how about introducing uip_tcp_socket_close_locked() which doesn't take the mutex and then making uip_tcp_socket_close() a wrapper around that:

static int uip_tcp_socket_close(struct uip_tcp_socket *sk, int how)

{
	int ret;

	mutex_lock(sk->lock);
	ret = uip_tcp_socket_close_locked(sk, how);
	mutex_unlock(sk->lock);

	return ret;
}

then uip_tcp_socket_free() can just call the _locked() flavour directly.

Will