Skip to content

Commit 3eff2d5

Browse files
wyf-777Rbb666
authored andcommitted
[net][at] Reject overlong numeric hostnames
Numeric hostnames were copied into a fixed stack buffer without a length bound. Cache the length, reject values that cannot fit, and terminate the copy explicitly. Fixes #11332 Assisted-by: OpenAI Codex:GPT-5
1 parent 5d1aee9 commit 3eff2d5

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎components/net/at/at_socket/at_socket.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,7 @@ static int _gethostbyname_by_device(const char *name, ip_addr_t *addr)
14091409
struct at_device *device = RT_NULL;
14101410
char ipstr[16] = { 0 };
14111411
size_t idx = 0;
1412+
size_t name_len = 0;
14121413

14131414
device = at_device_get_first_initialized();
14141415
if (device == RT_NULL)
@@ -1421,9 +1422,10 @@ static int _gethostbyname_by_device(const char *name, ip_addr_t *addr)
14211422
return -1;
14221423
}
14231424

1424-
for (idx = 0; idx < strlen(name) && !isalpha(name[idx]); idx++);
1425+
name_len = strlen(name);
1426+
for (idx = 0; idx < name_len && !isalpha((unsigned char)name[idx]); idx++);
14251427

1426-
if (idx < strlen(name))
1428+
if (idx < name_len)
14271429
{
14281430
if (at_dlock == RT_NULL)
14291431
{
@@ -1444,7 +1446,13 @@ static int _gethostbyname_by_device(const char *name, ip_addr_t *addr)
14441446
}
14451447
else
14461448
{
1447-
strncpy(ipstr, name, strlen(name));
1449+
if (name_len >= sizeof(ipstr))
1450+
{
1451+
return -1;
1452+
}
1453+
1454+
rt_memcpy(ipstr, name, name_len);
1455+
ipstr[name_len] = '\0';
14481456
}
14491457

14501458
#if NETDEV_IPV4 && NETDEV_IPV6

0 commit comments

Comments
 (0)