Skip to content

Commit 16f801f

Browse files
redraincatchingggarberCopilot
authored
addressed null pointer deref warnings (coturn#1712)
addressing issues raised by code scanning, specifically null pointer dereferences in server ns_turn_server.c - [33](https://github.com/redraincatching/coturn/security/code-scanning/33) ignored, the `is_rfc5780()` function exits early if the server is null - this also catches [36](https://github.com/redraincatching/coturn/security/code-scanning/36) - [34](https://github.com/redraincatching/coturn/security/code-scanning/34) addressed - [174](https://github.com/redraincatching/coturn/security/code-scanning/174) addressed ns_turn_maps.c - [27](https://github.com/redraincatching/coturn/security/code-scanning/27), [160](https://github.com/redraincatching/coturn/security/code-scanning/160), [161](https://github.com/redraincatching/coturn/security/code-scanning/161), [162](https://github.com/redraincatching/coturn/security/code-scanning/162), [163](https://github.com/redraincatching/coturn/security/code-scanning/163), [164](https://github.com/redraincatching/coturn/security/code-scanning/164), [165](https://github.com/redraincatching/coturn/security/code-scanning/165) false positives, suppressed with assert() ns_turn_allocations.c - [9](https://github.com/redraincatching/coturn/security/code-scanning/9) addressed --------- Co-authored-by: Gustavo Garcia <gustavogb@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent cb74638 commit 16f801f

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/server/ns_turn_allocation.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ void add_unsent_buffer(unsent_buffer *ub, ioa_network_buffer_handle nbh) {
725725
ioa_network_buffer_delete(NULL, nbh);
726726
} else {
727727
ub->bufs = (ioa_network_buffer_handle *)realloc(ub->bufs, sizeof(ioa_network_buffer_handle) * (ub->sz + 1));
728+
if (!ub->bufs) {
729+
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Memory allocation failed in add_unsent_buffer\n");
730+
ioa_network_buffer_delete(NULL, nbh);
731+
return;
732+
}
728733
ub->bufs[ub->sz] = nbh;
729734
ub->sz += 1;
730735
}

src/server/ns_turn_maps.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "ns_turn_khash.h"
4040

41+
#include <assert.h> // for assert
4142
#include <stdlib.h> // for size_t, free, malloc, NULL, realloc
4243
#include <string.h> // for memset, strcmp, memcpy, strlen
4344

@@ -261,6 +262,8 @@ bool lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value) {
261262
a->extra_values[i] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type));
262263
valuep = a->extra_values[i];
263264
}
265+
assert(keyp);
266+
assert(valuep);
264267
*keyp = key;
265268
*valuep = value;
266269
return false;
@@ -271,11 +274,13 @@ bool lm_map_put(lm_map *map, ur_map_key_type key, ur_map_value_type value) {
271274
size_t old_sz = esz;
272275
size_t old_sz_mem = esz * sizeof(ur_map_key_type *);
273276
a->extra_keys = (ur_map_key_type **)realloc(a->extra_keys, old_sz_mem + sizeof(ur_map_key_type *));
277+
assert(a->extra_keys);
274278
a->extra_keys[old_sz] = (ur_map_key_type *)malloc(sizeof(ur_map_key_type));
275279
*(a->extra_keys[old_sz]) = key;
276280

277281
old_sz_mem = esz * sizeof(ur_map_value_type *);
278282
a->extra_values = (ur_map_value_type **)realloc(a->extra_values, old_sz_mem + sizeof(ur_map_value_type *));
283+
assert(a->extra_values);
279284
a->extra_values[old_sz] = (ur_map_value_type *)malloc(sizeof(ur_map_value_type));
280285
*(a->extra_values[old_sz]) = value;
281286

@@ -528,6 +533,7 @@ static void addr_list_add(addr_list_header *slh, const ioa_addr *key, ur_addr_ma
528533
size_t old_sz = slh->extra_sz;
529534
size_t old_sz_mem = old_sz * sizeof(addr_elem);
530535
slh->extra_list = (addr_elem *)realloc(slh->extra_list, old_sz_mem + sizeof(addr_elem));
536+
assert(slh->extra_list);
531537
elem = &(slh->extra_list[old_sz]);
532538
slh->extra_sz += 1;
533539
}
@@ -947,6 +953,7 @@ ur_string_map *ur_string_map_create(ur_string_map_func del_value_func) {
947953
free(map);
948954
return NULL;
949955
}
956+
assert(map);
950957
map->del_value_func = del_value_func;
951958
return map;
952959
}

src/server/ns_turn_server.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,6 +2938,10 @@ static int handle_turn_send(turn_turnserver *server, ts_ur_super_session *ss, in
29382938
addr_set_any(&peer_addr);
29392939
allocation *a = get_allocation_ss(ss);
29402940

2941+
if (!server) {
2942+
return -1;
2943+
}
2944+
29412945
if (ss->is_tcp_relay) {
29422946
*err_code = 403;
29432947
*reason = (const uint8_t *)"Send cannot be used with TCP relay";
@@ -4102,6 +4106,10 @@ int shutdown_client_connection(turn_turnserver *server, ts_ur_super_session *ss,
41024106
return -1;
41034107
}
41044108

4109+
if (!server) {
4110+
return -1;
4111+
}
4112+
41054113
SOCKET_TYPE socket_type = get_ioa_socket_type(ss->client_socket);
41064114

41074115
turn_report_session_usage(ss, 1);
@@ -4228,6 +4236,10 @@ static int write_client_connection(turn_turnserver *server, ts_ur_super_session
42284236

42294237
FUNCSTART;
42304238

4239+
if (!server) {
4240+
return -1;
4241+
}
4242+
42314243
if (!(ss->client_socket)) {
42324244
ioa_network_buffer_delete(server->e, nbh);
42334245
FUNCEND;

0 commit comments

Comments
 (0)