Skip to content

Commit 0a43f16

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 0a43f16

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
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

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)