From a847d643594b5512e958daeb2fa26ec3199118b0 Mon Sep 17 00:00:00 2001
From: jikui <jikui2@huawei.com>
Date: Tue, 19 Apr 2022 14:36:11 +0800
Subject: [PATCH] busybox: fix CVE-2022-28391

backport from upstream:
https://git.alpinelinux.org/aports/plain/main/busybox/0001-libbb-sockaddr2str-ensure-only-printable-characters-.patch
https://git.alpinelinux.org/aports/plain/main/busybox/0002-nslookup-sanitize-all-printed-strings-with-printable.patch

Signed-off-by: jikui <jikui2@huawei.com>
---
 libbb/xconnect.c      | 5 +++--
 networking/nslookup.c | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index eb2871c..b5520bb 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -501,8 +501,9 @@ static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
 	);
 	if (rc)
 		return NULL;
+	/* ensure host contains only printable characters */
 	if (flags & IGNORE_PORT)
-		return xstrdup(host);
+		return xstrdup(printable_string(host));
 #if ENABLE_FEATURE_IPV6
 	if (sa->sa_family == AF_INET6) {
 		if (strchr(host, ':')) /* heh, it's not a resolved hostname */
@@ -513,7 +514,7 @@ static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
 #endif
 	/* For now we don't support anything else, so it has to be INET */
 	/*if (sa->sa_family == AF_INET)*/
-		return xasprintf("%s:%s", host, serv);
+		return xasprintf("%s:%s", printable_string(host), serv);
 	/*return xstrdup(host);*/
 }
 
diff --git a/networking/nslookup.c b/networking/nslookup.c
index 24e09d4..89b9c8a 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -404,7 +404,7 @@ static int parse_reply(const unsigned char *msg, size_t len)
 				//printf("Unable to uncompress domain: %s\n", strerror(errno));
 				return -1;
 			}
-			printf(format, ns_rr_name(rr), dname);
+			printf(format, ns_rr_name(rr), printable_string(dname));
 			break;
 
 		case ns_t_mx:
@@ -419,7 +419,7 @@ static int parse_reply(const unsigned char *msg, size_t len)
 				//printf("Cannot uncompress MX domain: %s\n", strerror(errno));
 				return -1;
 			}
-			printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
+			printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, printable_string(dname));
 			break;
 
 		case ns_t_txt:
@@ -431,7 +431,7 @@ static int parse_reply(const unsigned char *msg, size_t len)
 			if (n > 0) {
 				memset(dname, 0, sizeof(dname));
 				memcpy(dname, ns_rr_rdata(rr) + 1, n);
-				printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), dname);
+				printf("%s\ttext = \"%s\"\n", ns_rr_name(rr), printable_string(dname));
 			}
 			break;
 
@@ -461,7 +461,7 @@ static int parse_reply(const unsigned char *msg, size_t len)
 				return -1;
 			}
 
-			printf("\tmail addr = %s\n", dname);
+			printf("\tmail addr = %s\n", printable_string(dname));
 			cp += n;
 
 			printf("\tserial = %lu\n", ns_get32(cp));
-- 
2.17.1

