Skip to content

Commit e39ce12

Browse files
authored
Merge pull request #104 from purecloudlabs/TELECOM-11312
TELECOM-11312: Fix permission module memory access and error handling bugs
2 parents ab82322 + 45eee7d commit e39ce12

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

modules/permissions/address.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "../../parser/parse_from.h"
3838
#include "../../mod_fix.h"
3939
#include "../../resolve.h"
40+
#include "../../redact_pii.h"
4041

4142
#include "permissions.h"
4243
#include "hash.h"
@@ -292,25 +293,27 @@ int reload_address_table(struct pm_part_struct *part_struct)
292293
mask = (unsigned int) VAL_INT(val + 2);
293294

294295
subnet = mk_net_bitlen(ip_addr, mask);
295-
if (pm_hash_insert(new_hash_table, subnet, group, port, proto,
296-
&str_pattern, &str_info, mask) == -1) {
297-
LM_ERR("hash table insert error\n");
298-
if (subnet) {
299-
pkg_free(subnet);
300-
}
301-
goto error;
302-
}
303-
if ((mask == 32 && ip_addr->af==AF_INET) || (mask == 128 && ip_addr->af==AF_INET6)) {
304-
address_count += 1;
305-
} else {
306-
subnet_count += 1;
307-
}
308-
LM_DBG("Tuple <%.*s, %u, %u, %u, %.*s, %.*s> inserted into "
309-
"address hash table\n", str_src_ip.len, str_src_ip.s,
310-
group, port, proto, str_pattern.len, str_pattern.s,
311-
str_info.len,str_info.s);
312296
if (subnet) {
297+
if (pm_hash_insert(new_hash_table, subnet, group, port, proto,
298+
&str_pattern, &str_info, mask) == -1) {
299+
LM_ERR("hash table insert error\n");
300+
if (subnet) {
301+
pkg_free(subnet);
302+
}
303+
goto error;
304+
}
305+
if ((mask == 32 && ip_addr->af==AF_INET) || (mask == 128 && ip_addr->af==AF_INET6)) {
306+
address_count += 1;
307+
} else {
308+
subnet_count += 1;
309+
}
310+
LM_DBG("Tuple <%.*s, %u, %u, %u, %.*s, %.*s> inserted into "
311+
"address hash table\n", str_src_ip.len, str_src_ip.s,
312+
group, port, proto, str_pattern.len, str_pattern.s,
313+
str_info.len,str_info.s);
313314
pkg_free(subnet);
315+
} else {
316+
LM_ERR("invalid address: %.*s/%d\n", str_src_ip.len, redact_pii(str_src_ip.s), mask);
314317
}
315318
}
316319

modules/permissions/hash.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,16 @@ p_address_node_t *new_address_node(struct net *subnet, unsigned int port, int pr
7676
str *info) {
7777
p_address_node_t *node;
7878

79+
if (subnet == NULL) {
80+
LM_ERR("subnet is empty\n");
81+
return NULL;
82+
}
83+
7984
node = alloc_address_node(pattern, info);
80-
if (!node) return NULL;
85+
if (!node) {
86+
LM_ERR("no shm memory left for new address node\n");
87+
return NULL;
88+
}
8189

8290
node->v.port = port;
8391
node->v.proto = proto;
@@ -99,13 +107,13 @@ p_address_node_t *new_address_node(struct net *subnet, unsigned int port, int pr
99107

100108
void delete_group_node(p_group_node_t *group) {
101109
int i;
102-
p_address_node_t *address;
110+
p_address_node_t *address, *next;
103111

104112
if (!group) return;
105113

106114
for (i = 0; i < group->v.address.bucket_count; ++i) {
107-
for (address = (p_address_node_t *)group->v.address.bucket[i]; address;
108-
address = address->next) {
115+
for (address = (p_address_node_t *)group->v.address.bucket[i]; address; address = next) {
116+
next = address->next;
109117
delete_address_node(address);
110118
}
111119
}
@@ -132,13 +140,15 @@ p_group_node_t *new_group_node(unsigned int group_id, unsigned int bucket_count)
132140
if (!node->v.ipv4_subnet) {
133141
LM_ERR("no shm memory left for IPv4 subnet prefix tree\n");
134142
shm_free(node);
143+
return NULL;
135144
}
136145

137146
node->v.ipv6_subnet = ppt_create_node();
138147
if (!node->v.ipv6_subnet) {
139148
LM_ERR("no shm memory left for IPv6 subnet prefix tree\n");
140149
ppt_free_trie(node->v.ipv4_subnet);
141150
shm_free(node);
151+
return NULL;
142152
}
143153

144154
return node;
@@ -197,10 +207,7 @@ int pm_hash_insert(p_address_table_t *table, struct net *subnet, unsigned int gr
197207
p_address_node_t *address;
198208

199209
address = new_address_node(subnet, port, proto, pattern, info);
200-
if (!address) {
201-
LM_ERR("no shm memory left for new address node\n");
202-
return -1;
203-
}
210+
if (!address) return -1;
204211

205212
group = find_group_node(table, group_id);
206213

0 commit comments

Comments
 (0)