Skip to content

Commit 0a44efa

Browse files
committed
napt: Fixed NULL pointer dereference in ip_napt_enable
Prevent potential NULL pointer dereference when calling ip_napt_enable() with invalid addresses. Add validation to ensure NAPT can only be enabled/disabled for existing network interfaces.
1 parent 865d7d0 commit 0a44efa

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/core/ipv4/ip4_napt.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,15 @@ ip_napt_deinit(void)
182182
#if IP_NAPT_PORTMAP
183183
ip_portmap_max = 0;
184184
#endif
185-
mem_free(ip_napt_table);
186-
ip_napt_table = NULL;
185+
if (ip_napt_table != NULL) {
186+
mem_free(ip_napt_table);
187+
ip_napt_table = NULL;
188+
}
187189
#if IP_NAPT_PORTMAP
188-
mem_free(ip_portmap_table);
189-
ip_portmap_table = NULL;
190+
if (ip_portmap_table != NULL) {
191+
mem_free(ip_portmap_table);
192+
ip_portmap_table = NULL;
193+
}
190194
#endif
191195
sys_untimeout(ip_napt_tmr, NULL);
192196
}
@@ -234,15 +238,22 @@ void
234238
ip_napt_enable(u32_t addr, int enable)
235239
{
236240
struct netif *netif;
241+
struct netif *matching_netif = NULL;
237242
int napt_in_any_netif = 0;
238243
for (netif = netif_list; netif; netif = netif->next) {
239244
if (netif_is_up(netif) && !ip_addr_isany(&netif->ip_addr) && (ip_2_ip4(&netif->ip_addr)->addr) == addr) {
240245
netif->napt = enable;
246+
matching_netif = netif;
241247
}
242248
if (netif->napt) {
243249
napt_in_any_netif = 1;
244250
}
245251
}
252+
253+
if (matching_netif == NULL && napt_in_any_netif == 0 && enable) {
254+
return;
255+
}
256+
246257
if (napt_in_any_netif) {
247258
#if IP_NAPT_PORTMAP
248259
ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);

0 commit comments

Comments
 (0)