From c1b8f5f30c602fd39f0f92523485587c3c32708d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@isc.org>
Date: Thu, 14 Jul 2022 13:48:45 +0200
Subject: [PATCH] Increase the BUFSIZ-long buffers

The BUFSIZ value varies between platforms, it could be 8K on Linux and
512 bytes on mingw.  Make sure the buffers are always big enough for the
output data to prevent truncation of the output by appropriately
enlarging or sizing the buffers.

(cherry picked from commit b19d932262e84608174cb89eeed32ae0212f8a87)
---
 bin/named/server.c              |  7 ++++++-
 bin/tests/system/feature-test.c | 13 +++++--------
 lib/dns/private.c               |  3 ++-
 lib/ns/client.c                 |  7 ++++++-
 4 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/bin/named/server.c b/bin/named/server.c
index d3595c37a19..c339cfbc54b 100644
--- a/bin/named/server.c
+++ b/bin/named/server.c
@@ -14582,7 +14582,12 @@ named_server_signing(named_server_t *server, isc_lex_t *lex,
 		     result = dns_rdataset_next(&privset))
 		{
 			dns_rdata_t priv = DNS_RDATA_INIT;
-			char output[BUFSIZ];
+			/*
+			 * In theory, the output buffer could hold a full RDATA
+			 * record which is 16-bit and then some text around
+			 * it
+			 */
+			char output[UINT16_MAX + BUFSIZ];
 			isc_buffer_t buf;
 
 			dns_rdataset_current(&privset, &priv);
diff --git a/bin/tests/system/feature-test.c b/bin/tests/system/feature-test.c
index 4f422323434..99e1b80bb1d 100644
--- a/bin/tests/system/feature-test.c
+++ b/bin/tests/system/feature-test.c
@@ -11,6 +11,7 @@
  * information regarding copyright ownership.
  */
 
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,13 +27,9 @@
 #include <Winsock2.h>
 #endif /* ifdef WIN32 */
 
-#ifndef MAXHOSTNAMELEN
-#ifdef HOST_NAME_MAX
-#define MAXHOSTNAMELEN HOST_NAME_MAX
-#else /* ifdef HOST_NAME_MAX */
-#define MAXHOSTNAMELEN 256
-#endif /* ifdef HOST_NAME_MAX */
-#endif /* ifndef MAXHOSTNAMELEN */
+#ifndef _POSIX_HOST_NAME_MAX
+#define _POSIX_HOST_NAME_MAX 255
+#endif
 
 static void
 usage() {
@@ -86,7 +83,7 @@ main(int argc, char **argv) {
 	}
 
 	if (strcmp(argv[1], "--gethostname") == 0) {
-		char hostname[MAXHOSTNAMELEN];
+		char hostname[_POSIX_HOST_NAME_MAX + 1];
 		int n;
 #ifdef WIN32
 		/* From InitSocket() */
diff --git a/lib/dns/private.c b/lib/dns/private.c
index 58deda095a9..cbf947f8ccc 100644
--- a/lib/dns/private.c
+++ b/lib/dns/private.c
@@ -383,7 +383,8 @@ dns_private_totext(dns_rdata_t *private, isc_buffer_t *buf) {
 	} else if (private->length == 5) {
 		unsigned char alg = private->data[0];
 		dns_keytag_t keyid = (private->data[2] | private->data[1] << 8);
-		char keybuf[BUFSIZ], algbuf[DNS_SECALG_FORMATSIZE];
+		char keybuf[DNS_SECALG_FORMATSIZE + BUFSIZ],
+			algbuf[DNS_SECALG_FORMATSIZE];
 		bool del = private->data[3];
 		bool complete = private->data[4];
 
diff --git a/lib/ns/client.c b/lib/ns/client.c
index bf60746642d..54c35986b85 100644
--- a/lib/ns/client.c
+++ b/lib/ns/client.c
@@ -12,6 +12,7 @@
  */
 
 #include <inttypes.h>
+#include <limits.h>
 #include <stdbool.h>
 
 #include <isc/aes.h>
@@ -64,6 +65,10 @@
 #include <ns/stats.h>
 #include <ns/update.h>
 
+#ifndef _POSIX_HOST_NAME_MAX
+#define _POSIX_HOST_NAME_MAX 255
+#endif
+
 /***
  *** Client
  ***/
@@ -918,7 +923,7 @@ isc_result_t
 ns_client_addopt(ns_client_t *client, dns_message_t *message,
 		 dns_rdataset_t **opt) {
 	unsigned char ecs[ECS_SIZE];
-	char nsid[BUFSIZ], *nsidp;
+	char nsid[_POSIX_HOST_NAME_MAX + 1], *nsidp = NULL;
 	unsigned char cookie[COOKIE_SIZE];
 	isc_result_t result;
 	dns_view_t *view;
-- 
GitLab

