Skip to content

Commit ad4fbc7

Browse files
committed
net: ip: account for the size in the inet_ntop code path
The code was writing to the dst without a verification check on size which is not appropriate. The guard on the arguements should be enforced and so just ensure the size is larger then the definition of the strings from POSIX and return an error in those cases. Signed-off-by: Charles Hardin <[email protected]>
1 parent 6133ba4 commit ad4fbc7

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

drivers/modem/hl7800.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ struct xmodem_packet {
286286
#define MAX_PROFILE_LINE_LENGTH \
287287
MAX(sizeof(PROFILE_LINE_1), sizeof(PROFILE_LINE_2))
288288

289-
#define IPV6_ADDR_FORMAT "####:####:####:####:####:####:####:####"
289+
#define IPV6_ADDR_FORMAT "####:####:####:####:####:####:xxx.xxx.xxx.xxx"
290290
#define HL7800_IPV6_ADDR_LEN \
291291
sizeof("a01.a02.a03.a04.a05.a06.a07.a08.a09.a10.a11.a12.a13.a14.a15.a16")
292292

drivers/modem/quectel-bg9x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ static int offload_connect(void *obj, const struct sockaddr *addr,
692692
char *protocol = "TCP";
693693
struct modem_cmd cmd[] = { MODEM_CMD("+QIOPEN: ", on_cmd_atcmdinfo_sockopen, 2U, ",") };
694694
char buf[sizeof("AT+QIOPEN=#,#,'###','###',"
695-
"####.####.####.####.####.####.####.####,######,"
695+
"####:####:####:####:####:####:xxx.xxx.xxx.xxx,######,"
696696
"0,0")] = {0};
697697
int ret;
698698
char ip_str[NET_IPV6_ADDR_LEN];

drivers/modem/simcom-sim7080.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ static int offload_connect(void *obj, const struct sockaddr *addr, socklen_t add
135135
uint16_t dst_port = 0;
136136
char *protocol;
137137
struct modem_cmd cmd[] = { MODEM_CMD("+CAOPEN: ", on_cmd_caopen, 2U, ",") };
138-
char buf[sizeof("AT+CAOPEN: #,#,#####,#xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx#,####")];
138+
char buf[sizeof("AT+CAOPEN: #,#,#####,"
139+
"#xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxx.xxx.xxx.xxx#,####")];
139140
char ip_str[NET_IPV6_ADDR_LEN];
140141
int ret;
141142

drivers/modem/ublox-sara-r4.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static ssize_t send_socket_data(void *obj,
292292
{
293293
int ret;
294294
char send_buf[sizeof("AT+USO**=###,"
295-
"!####.####.####.####.####.####.####.####!,"
295+
"!####:####:####:####:####:####:xxx.xxx.xxx.xxx!,"
296296
"#####,#########\r\n")];
297297
uint16_t dst_port = 0U;
298298
struct modem_socket *sock = (struct modem_socket *)obj;

include/zephyr/net/net_ip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ extern const struct in6_addr in6addr_loopback;
479479
/** @cond INTERNAL_HIDDEN */
480480

481481
/* These are for internal usage of the stack */
482-
#define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx")
482+
#define NET_IPV6_ADDR_LEN sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxx.xxx.xxx.xxx")
483483
#define NET_IPV4_ADDR_LEN sizeof("xxx.xxx.xxx.xxx")
484484

485485
/** @endcond */

subsys/net/ip/utils.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ char *z_impl_net_addr_ntop(sa_family_t family, const void *src,
174174
bool needcolon = false;
175175
bool mapped = false;
176176

177-
if (family == AF_INET6) {
177+
switch (family) {
178+
case AF_INET6:
179+
if (size < INET6_ADDRSTRLEN) {
180+
/* POSIX definition is the size - includes nil */
181+
return NULL;
182+
}
183+
178184
net_ipv6_addr_copy_raw(addr6.s6_addr, src);
179185
w = (uint16_t *)addr6.s6_addr16;
180186
len = 8;
@@ -203,12 +209,20 @@ char *z_impl_net_addr_ntop(sa_family_t family, const void *src,
203209
if (longest == 1U) {
204210
pos = -1;
205211
}
212+
break;
213+
214+
case AF_INET:
215+
if (size < INET_ADDRSTRLEN) {
216+
/* POSIX definition is the size - includes nil */
217+
return NULL;
218+
}
206219

207-
} else if (family == AF_INET) {
208220
net_ipv4_addr_copy_raw(addr.s4_addr, src);
209221
len = 4;
210222
delim = '.';
211-
} else {
223+
break;
224+
225+
default:
212226
return NULL;
213227
}
214228

@@ -281,10 +295,6 @@ char *z_impl_net_addr_ntop(sa_family_t family, const void *src,
281295
needcolon = true;
282296
}
283297

284-
if (!(ptr - dst)) {
285-
return NULL;
286-
}
287-
288298
if (family == AF_INET) {
289299
*(ptr - 1) = '\0';
290300
} else {

0 commit comments

Comments
 (0)