net/uip: Avoid deadlock in uip_tcp_socket_free()
Subsystems (2)
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().
--- net/uip/tcp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
net/uip/tcp.c +6 -2
@@ -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
Quoted 22 lines · Steven Price
The function
uip_tcp_socket_free()is called with the sk lock held, but then goes on to calluip_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 inuip_tcp_socket_free().Fixes d87b503f4d6e ("net/uip: Add exit function")Signed-off-by Steven Price <[email protected]>--- net/uip/tcp.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)net/uip/tcp.c +0
-0@@ -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