Skip to contentSkip to conversation

net: zero terminate string with headers in wget_fill_info()

PATCH New Adriano Cordova 1 participant 2 messages Nov 26, 2024 Networking

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 net: zero terminate string with headers in wget_fill_info()

From: Heinrich Schuchardt <[email protected]>

Commit 2dd076a9c1b4 ("net: wget: integrate struct wget_info into legacy wget code") introduced function wget_fill_info() which retrieves the headers from the HTTP server response. As we want to parse the string in later patches we need to ensure that it is NUL terminated.

We must further check that wget_info->headers in not NULL. Otherwise a crash occurs.

Fixes 2dd076a9c1b4 ("net: wget: integrate struct wget_info into legacy wget code")

---

Changes in v2:
- removed double check of wget_info->headers
- removed an unnecessary "- 1"

 net/wget.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/wget.c +7 -2
index 3bc2522cde..5d70b7a82e 100644
@@ -208,8 +208,13 @@ static void wget_fill_info(const uchar *pkt, int hlen)
 	const char *second_space;
 	char *pos, *end;
 
-	if (wget_info->headers && hlen < MAX_HTTP_HEADERS_SIZE)
-		strncpy(wget_info->headers, pkt, hlen);
+	if (wget_info->headers) {
+		if (hlen < MAX_HTTP_HEADERS_SIZE)
+			strncpy(wget_info->headers, pkt, hlen);
+		else
+			hlen = 0;
+		wget_info->headers[hlen] = 0;
+	}
 
 	//Get status code
 	first_space = strchr(pkt, ' ');
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 2/2 net-lwip: zero terminate string with headers in wget_lwip_fill_info()

This patch comes as a companion to the same patch but for the legacy net stack. Commit 1327c2a8d6 ("net/lwip: wget: integrate struct wget_info into wget code") introduced function wget_fill_info() which retrieves the headers from the HTTP server response. As we want to parse the string in later patches we need to ensure that it is NUL terminated.

We must further check that wget_info->headers in not NULL. Otherwise a crash occurs.

---

Changes in v2:
- removed double check of wget_info->headers

 net/lwip/wget.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/lwip/wget.c +7 -2
index 53c3b169e0..9f8664b722 100644
@@ -36,8 +36,13 @@ struct wget_ctx {
 
 static void wget_lwip_fill_info(struct pbuf *hdr, u16_t hdr_len, u32_t hdr_cont_len)
 {
-	if (wget_info->headers && hdr_len < MAX_HTTP_HEADERS_SIZE)
-		pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0);
+	if (wget_info->headers) {
+		if (hdr_len < MAX_HTTP_HEADERS_SIZE)
+			pbuf_copy_partial(hdr, (void *)wget_info->headers, hdr_len, 0);
+		else
+			hdr_len = 0;
+		wget_info->headers[hdr_len] = 0;
+	}
 	wget_info->hdr_cont_len = (u32)hdr_cont_len;
 }
 
Signature · 1 line
2.43.0