Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/lwip: fix race in async sock API with event #21093

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/lwip/contrib/sock/tcp/lwip_sock_tcp.c
Original file line number Diff line number Diff line change
@@ -85,6 +85,9 @@ int sock_tcp_listen(sock_tcp_queue_t *queue, const sock_tcp_ep_t *local,
queue->array = queue_array;
queue->len = queue_len;
queue->used = 0;
/* This wipe is important: We cancel pending events in async event API when
* reusing the socket. If the memory would be uninitialized, bad things
* would happen */
memset(queue->array, 0, sizeof(sock_tcp_t) * queue_len);
mutex_unlock(&queue->mutex);

@@ -125,6 +128,14 @@ void sock_tcp_disconnect(sock_tcp_t *sock)
}
}

#ifdef SOCK_HAS_ASYNC_CTX
/* Cancel any pending event in the event queue */
if (sock->base.async_ctx.queue) {
event_cancel(sock->base.async_ctx.queue,
&sock->base.async_ctx.event.super);
}
#endif

mutex_unlock(&sock->mutex);
memset(&sock->mutex, 0, sizeof(mutex_t));
}
@@ -240,6 +251,16 @@ int sock_tcp_accept(sock_tcp_queue_t *queue, sock_tcp_t **sock,
for (unsigned short i = 0; i < queue->len; i++) {
sock_tcp_t *s = &queue->array[i];
if (s->base.conn == NULL) {
#ifdef SOCK_HAS_ASYNC_CTX
/* If there still is an event pending, we cannot just wipe
* its memory but have to remove the event from the list
* first. We rely here sock_tcp_listen to zero-initialize
* the sockets. */
if (s->base.async_ctx.queue) {
event_cancel(s->base.async_ctx.queue,
&s->base.async_ctx.event.super);
}
#endif
_tcp_sock_init(s, tmp, queue);
queue->used++;
assert(queue->used > 0);