Skip to content

Commit

Permalink
net: dns: Check that dispatcher table is not overflowing
Browse files Browse the repository at this point in the history
Add CHECKIF() checks that verify that dispatcher table is
not overflowing.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
  • Loading branch information
jukkar committed Sep 26, 2024
1 parent 8e4337c commit cc1e96d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion subsys/net/lib/dns/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)
entry->pair = ctx;

for (int i = 0; i < ctx->fds_len; i++) {
CHECKIF(ctx->fds[i].fd >= ARRAY_SIZE(dispatch_table)) {
ret = -ERANGE;
goto out;
}

if (dispatch_table[ctx->fds[i].fd].ctx == NULL) {
dispatch_table[ctx->fds[i].fd].ctx = ctx;
}
Expand Down Expand Up @@ -287,6 +292,11 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)
ctx->pair = NULL;

for (int i = 0; i < ctx->fds_len; i++) {
CHECKIF(ctx->fds[i].fd >= ARRAY_SIZE(dispatch_table)) {
ret = -ERANGE;
goto out;
}

if (dispatch_table[ctx->fds[i].fd].ctx == NULL) {
dispatch_table[ctx->fds[i].fd].ctx = ctx;
}
Expand All @@ -308,17 +318,25 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)

int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx)
{
int ret = 0;

k_mutex_lock(&lock, K_FOREVER);

(void)sys_slist_find_and_remove(&sockets, &ctx->node);

for (int i = 0; i < ctx->fds_len; i++) {
CHECKIF(ctx->fds[i].fd >= ARRAY_SIZE(dispatch_table)) {
ret = -ERANGE;
goto out;
}

dispatch_table[ctx->fds[i].fd].ctx = NULL;
}

out:
k_mutex_unlock(&lock);

return 0;
return ret;
}

void dns_dispatcher_init(void)
Expand Down

0 comments on commit cc1e96d

Please sign in to comment.