From cce9a80aee6fed285a1d5a7cfe2feb221d929d6b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 7 Nov 2024 10:38:48 +0100 Subject: [PATCH] fix(sta2eth): Check for null `netif` before starting/stopping DHCP server Fixes a potential null pointer dereference in `esp_netif` when PPP mode is enabled. In the Ethernet event handler, `esp_netif_dhcps_start()` and `esp_netif_dhcps_stop()` are now only called if `netif` is non-null (in provisioning mode when the actual TCP/IP stack from IDF is used, in work mode the `netif` is null, since the trafic is simply forwarded between wireless and wired networks without TCP/IP stack involved) Closes https://github.com/espressif/esp-idf/issues/14816 --- examples/network/sta2eth/main/ethernet_iface.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/network/sta2eth/main/ethernet_iface.c b/examples/network/sta2eth/main/ethernet_iface.c index e67fd70097..c856a05537 100644 --- a/examples/network/sta2eth/main/ethernet_iface.c +++ b/examples/network/sta2eth/main/ethernet_iface.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -49,7 +49,11 @@ void eth_event_handler(void *arg, esp_event_base_t event_base, switch (event_id) { case ETHERNET_EVENT_CONNECTED: ESP_LOGI(TAG, "Ethernet Link Up"); - esp_netif_dhcps_start(netif); + if (netif) { + // Start DHCP server only if we "have" the actual netif (provisioning mode) + // (if netif==NULL we are only forwarding frames, no lwip involved) + esp_netif_dhcps_start(netif); + } esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); @@ -57,7 +61,9 @@ void eth_event_handler(void *arg, esp_event_base_t event_base, break; case ETHERNET_EVENT_DISCONNECTED: ESP_LOGI(TAG, "Ethernet Link Down"); - esp_netif_dhcps_stop(netif); + if (netif) { + esp_netif_dhcps_stop(netif); + } s_ethernet_is_connected = false; break; case ETHERNET_EVENT_START: