8202181: Correctly specify size of hostname buffer in Unix Inet*AddressImpl_getLocalHostName implementations
authorclanger
Fri, 04 May 2018 14:37:58 +0100
changeset 49985 44e581f54d08
parent 49984 aa3afd9bda87
child 49986 29b840b16a96
8202181: Correctly specify size of hostname buffer in Unix Inet*AddressImpl_getLocalHostName implementations Reviewed-by: stuefe, bpb, vtewari, chegar
src/java.base/unix/native/libnet/Inet4AddressImpl.c
src/java.base/unix/native/libnet/Inet6AddressImpl.c
src/java.base/unix/native/libnet/net_util_md.h
--- a/src/java.base/unix/native/libnet/Inet4AddressImpl.c	Thu May 03 15:17:27 2018 +0200
+++ b/src/java.base/unix/native/libnet/Inet4AddressImpl.c	Fri May 04 14:37:58 2018 +0100
@@ -41,11 +41,6 @@
 extern jobjectArray lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6);
 #endif
 
-/* the initial size of our hostent buffers */
-#ifndef NI_MAXHOST
-#define NI_MAXHOST 1025
-#endif
-
 #define SET_NONBLOCKING(fd) {       \
     int flags = fcntl(fd, F_GETFL); \
     flags |= O_NONBLOCK;            \
@@ -66,10 +61,10 @@
     char hostname[NI_MAXHOST + 1];
 
     hostname[0] = '\0';
-    if (gethostname(hostname, NI_MAXHOST) != 0) {
+    if (gethostname(hostname, sizeof(hostname)) != 0) {
         strcpy(hostname, "localhost");
+    } else {
 #if defined(__solaris__)
-    } else {
         // try to resolve hostname via nameservice
         // if it is known but getnameinfo fails, hostname will still be the
         // value from gethostname
@@ -82,17 +77,15 @@
         hints.ai_family = AF_INET;
 
         if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
-            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
+            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, sizeof(hostname),
                         NULL, 0, NI_NAMEREQD);
             freeaddrinfo(res);
         }
-    }
 #else
-    } else {
         // make sure string is null-terminated
         hostname[NI_MAXHOST] = '\0';
+#endif
     }
-#endif
     return (*env)->NewStringUTF(env, hostname);
 }
 
@@ -248,7 +241,7 @@
     sa.sin_family = AF_INET;
 
     if (getnameinfo((struct sockaddr *)&sa, sizeof(struct sockaddr_in),
-                    host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
+                    host, sizeof(host), NULL, 0, NI_NAMEREQD)) {
         JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
     } else {
         ret = (*env)->NewStringUTF(env, host);
--- a/src/java.base/unix/native/libnet/Inet6AddressImpl.c	Thu May 03 15:17:27 2018 +0200
+++ b/src/java.base/unix/native/libnet/Inet6AddressImpl.c	Fri May 04 14:37:58 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,11 +42,6 @@
 #include "java_net_Inet4AddressImpl.h"
 #include "java_net_Inet6AddressImpl.h"
 
-/* the initial size of our hostent buffers */
-#ifndef NI_MAXHOST
-#define NI_MAXHOST 1025
-#endif
-
 #define SET_NONBLOCKING(fd) {       \
     int flags = fcntl(fd, F_GETFL); \
     flags |= O_NONBLOCK;            \
@@ -67,10 +62,10 @@
     char hostname[NI_MAXHOST + 1];
 
     hostname[0] = '\0';
-    if (gethostname(hostname, NI_MAXHOST) != 0) {
+    if (gethostname(hostname, sizeof(hostname)) != 0) {
         strcpy(hostname, "localhost");
+    } else {
 #if defined(__solaris__)
-    } else {
         // try to resolve hostname via nameservice
         // if it is known but getnameinfo fails, hostname will still be the
         // value from gethostname
@@ -83,17 +78,15 @@
         hints.ai_family = AF_UNSPEC;
 
         if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
-            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST,
+            getnameinfo(res->ai_addr, res->ai_addrlen, hostname, sizeof(hostname),
                         NULL, 0, NI_NAMEREQD);
             freeaddrinfo(res);
         }
-    }
 #else
-    } else {
         // make sure string is null-terminated
         hostname[NI_MAXHOST] = '\0';
+#endif
     }
-#endif
     return (*env)->NewStringUTF(env, hostname);
 }
 
@@ -103,7 +96,7 @@
 lookupIfLocalhost(JNIEnv *env, const char *hostname, jboolean includeV6)
 {
     jobjectArray result = NULL;
-    char myhostname[NI_MAXHOST+1];
+    char myhostname[NI_MAXHOST + 1];
     struct ifaddrs *ifa = NULL;
     int familyOrder = 0;
     int count = 0, i, j;
@@ -120,7 +113,7 @@
      * the name (if the name actually matches something in DNS etc.
      */
     myhostname[0] = '\0';
-    if (gethostname(myhostname, NI_MAXHOST) == -1) {
+    if (gethostname(myhostname, sizeof(myhostname)) == -1) {
         /* Something went wrong, maybe networking is not setup? */
         return NULL;
     }
@@ -445,7 +438,7 @@
         len = sizeof(struct sockaddr_in6);
     }
 
-    if (getnameinfo(&sa.sa, len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD)) {
+    if (getnameinfo(&sa.sa, len, host, sizeof(host), NULL, 0, NI_NAMEREQD)) {
         JNU_ThrowByName(env, "java/net/UnknownHostException", NULL);
     } else {
         ret = (*env)->NewStringUTF(env, host);
--- a/src/java.base/unix/native/libnet/net_util_md.h	Thu May 03 15:17:27 2018 +0200
+++ b/src/java.base/unix/native/libnet/net_util_md.h	Fri May 04 14:37:58 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,11 @@
 #define NET_NSEC_PER_SEC  1000000000
 #define NET_NSEC_PER_USEC 1000
 
+/* in case NI_MAXHOST is not defined in netdb.h */
+#ifndef NI_MAXHOST
+#define NI_MAXHOST 1025
+#endif
+
 /* Defines SO_REUSEPORT */
 #ifndef SO_REUSEPORT
 #ifdef __linux__