[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [torsocks/master] gethostbyaddr_r: Don't put garbage in data->hostname



commit 8013dfb1ebf6cb1d0a8751dcd3531e6b2e5aef39
Author: David Goulet <dgoulet@xxxxxxxxxxxxxx>
Date:   Fri Apr 20 12:04:35 2018 -0400

    gethostbyaddr_r: Don't put garbage in data->hostname
    
    Fixes #25627
    
    Signed-off-by: David Goulet <dgoulet@xxxxxxxxxxxxxx>
---
 src/lib/gethostbyname.c |  5 ++++-
 tests/test_dns.c        | 24 +++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/lib/gethostbyname.c b/src/lib/gethostbyname.c
index 296099e..df84e14 100644
--- a/src/lib/gethostbyname.c
+++ b/src/lib/gethostbyname.c
@@ -263,9 +263,12 @@ LIBC_GETHOSTBYADDR_R_RET_TYPE tsocks_gethostbyaddr_r(LIBC_GETHOSTBYADDR_R_SIG)
 	/* This call allocates hostname. On error, it's untouched. */
 	ret = tsocks_tor_resolve_ptr(addr, &data->hostname, type);
 	if (ret < 0) {
+		/* We can represent any IPv4 address in dotted quad notation in fewer than
+		 * 32 bytes (max should be 16 if we count a nul terminator). */
+		char addrbuf[32];
 		const char *ret_str;
 
-		ret_str = inet_ntop(type, addr, buf, buflen);
+		ret_str = inet_ntop(type, addr, &addrbuf[0], sizeof(addrbuf));
 		if (!ret_str) {
 			ret = HOST_NOT_FOUND;
 			if (errno == ENOSPC) {
diff --git a/tests/test_dns.c b/tests/test_dns.c
index 06fad12..7e07663 100644
--- a/tests/test_dns.c
+++ b/tests/test_dns.c
@@ -26,7 +26,7 @@
 #include <tap/tap.h>
 #include "helpers.h"
 
-#define NUM_TESTS 5
+#define NUM_TESTS 6
 
 struct test_host {
 	const char *name;
@@ -76,6 +76,27 @@ static void test_gethostbyname(const struct test_host *host)
 	return;
 }
 
+static void test_gethostbyaddr_r_failed(void)
+{
+	int result;
+	in_addr_t addr;
+	struct hostent ret;
+	char buf[1024];
+	int buflen = sizeof buf;
+	struct hostent *result_entp;
+	int h_errno;
+
+	diag("gethostbyaddr_r test");
+
+	/* RFC 6890 - An address from TEST-NET-1.  Selected in hopes that it will
+		 +   * _not_ reverse resolve to anything.
+		 +   */
+	addr = inet_addr("192.0.2.1");
+	result = gethostbyaddr_r((const void *)&addr,
+				INET_ADDRSTRLEN, AF_INET, &ret, buf, buflen, &result_entp, &h_errno);
+	ok(0 != result, "Impossible reverse resolve failed as desired.");
+}
+
 static void test_gethostbyaddr_r(const struct test_host *host)
 {
   int result;
@@ -179,6 +200,7 @@ int main(int argc, char **argv)
 	test_gethostbyname(&tor_dir_auth1);
 	test_gethostbyaddr(&tor_dir_auth2);
 	test_gethostbyaddr_r(&tor_dir_auth2);
+	test_gethostbyaddr_r_failed();
 	test_getaddrinfo(&tor_localhost);
 
 end:

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits