Skip to content

Commit 405f893

Browse files
authored
Merge pull request #3191 from tung7970/fix-mbedos
Fix lwip_mac_address buffer overflow and set_ip_bytes out of bound access
2 parents 805af00 + 70ad0f5 commit 405f893

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void mbed_lwip_socket_callback(struct netconn *nc, enum netconn_evt eh, u
104104
/* TCP/IP and Network Interface Initialisation */
105105
static struct netif lwip_netif;
106106
static bool lwip_dhcp = false;
107-
static char lwip_mac_address[NSAPI_MAC_SIZE] = "\0";
107+
static char lwip_mac_address[NSAPI_MAC_SIZE];
108108

109109
#if !LWIP_IPV4 || !LWIP_IPV6
110110
static bool all_zeros(const uint8_t *p, int len)
@@ -309,13 +309,13 @@ static void mbed_lwip_netif_status_irq(struct netif *lwip_netif)
309309
static void mbed_lwip_set_mac_address(void)
310310
{
311311
#if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
312-
snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
312+
snprintf(lwip_mac_address, NSAPI_MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
313313
MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
314314
MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
315315
#else
316316
char mac[6];
317317
mbed_mac_address(mac);
318-
snprintf(lwip_mac_address, 19, "%02x:%02x:%02x:%02x:%02x:%02x",
318+
snprintf(lwip_mac_address, NSAPI_MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
319319
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
320320
#endif
321321
}

features/netsocket/SocketAddress.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,14 @@ bool SocketAddress::set_ip_address(const char *addr)
203203
void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
204204
{
205205
nsapi_addr_t addr;
206+
207+
addr = nsapi_addr_t();
206208
addr.version = version;
207-
memcpy(addr.bytes, bytes, NSAPI_IP_BYTES);
209+
if (version == NSAPI_IPv6) {
210+
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
211+
} else if (version == NSAPI_IPv4) {
212+
memcpy(addr.bytes, bytes, NSAPI_IPv4_BYTES);
213+
}
208214
set_addr(addr);
209215
}
210216

0 commit comments

Comments
 (0)