Skip to content

Commit 0311d57

Browse files
jukkarbjarki-andreasen
authored andcommitted
[nrf fromtree] net: dns: Check that dispatcher table is not overflowing
Add CHECKIF() checks that verify that dispatcher table is not overflowing. (cherry picked from commit 2ff2667) Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 47aac5f commit 0311d57

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

subsys/net/lib/dns/dispatcher.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)
235235
entry->pair = ctx;
236236

237237
for (int i = 0; i < ctx->fds_len; i++) {
238+
CHECKIF((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) {
239+
ret = -ERANGE;
240+
goto out;
241+
}
242+
238243
if (dispatch_table[ctx->fds[i].fd].ctx == NULL) {
239244
dispatch_table[ctx->fds[i].fd].ctx = ctx;
240245
}
@@ -267,6 +272,11 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)
267272
ctx->pair = NULL;
268273

269274
for (int i = 0; i < ctx->fds_len; i++) {
275+
if ((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) {
276+
ret = -ERANGE;
277+
goto out;
278+
}
279+
270280
if (dispatch_table[ctx->fds[i].fd].ctx == NULL) {
271281
dispatch_table[ctx->fds[i].fd].ctx = ctx;
272282
}
@@ -288,17 +298,25 @@ int dns_dispatcher_register(struct dns_socket_dispatcher *ctx)
288298

289299
int dns_dispatcher_unregister(struct dns_socket_dispatcher *ctx)
290300
{
301+
int ret = 0;
302+
291303
k_mutex_lock(&lock, K_FOREVER);
292304

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

295307
for (int i = 0; i < ctx->fds_len; i++) {
308+
CHECKIF((int)ctx->fds[i].fd >= (int)ARRAY_SIZE(dispatch_table)) {
309+
ret = -ERANGE;
310+
goto out;
311+
}
312+
296313
dispatch_table[ctx->fds[i].fd].ctx = NULL;
297314
}
298315

316+
out:
299317
k_mutex_unlock(&lock);
300318

301-
return 0;
319+
return ret;
302320
}
303321

304322
void dns_dispatcher_init(void)

0 commit comments

Comments
 (0)