misc/189268: man 3 getaddrinfo - hostanme="localhost", but it returns IN_ADDR_ANY (

Dreamcat4 dreamcat4 at gmail.com
Fri May 2 22:00:01 UTC 2014

>Number:         189268
>Category:       misc
>Synopsis:       man 3 getaddrinfo - hostanme="localhost", but it returns IN_ADDR_ANY (
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 02 22:00:00 UTC 2014
>Originator:     Dreamcat4
>Release:        9.2-RELEASE-p3
FreeBSD freenas.local 9.2-RELEASE-p3 FreeBSD 9.2-RELEASE-p3 #0 r262572+7b72365: Fri Mar 14 15:50:04 PDT 2014     root at build.ixsystems.com:/home/jkh/9.2.1-BRANCH/freenas/os-base/amd64/tank/home/jkh/9.2.1-BRANCH/freenas/FreeBSD/src/sys/FREENAS.amd64  amd64

* Inside a jail, no lo0 loopback interface.
* Calling this function returns the ip "" (also known as "IN_ADDR_ANY").
* But we asked for the hostname "localhost" - which in our /etc/hosts file is set to "".

The proof:
Example 'C' test program "test.c" is attached.
getaddrinfo ~/ root~# gcc test.c -o test && ./test

This affects the gSOAP library, it's function soap_bind(), which is called by VirtualBox's SOAP webservice ("vboxwebsrv").

These applications seem to do everything correctly. But the result is a bug. Please see test script "test.c" included.

The real-life bug occurs in this code where:

    SOAP_SOCKET m, s; // master and slave sockets
    m = soap_bind(&soap,
                  g_pcszBindToHost ? g_pcszBindToHost : "localhost",    // safe default host
                  g_uBindToPort,    // port
                  g_uBacklog);      // backlog = max queue size for requests
    if (m < 0)


  err = getaddrinfo(host, soap_int2s(soap, port), &hints, &addrinfo);

Is passing in "localhost", is told "". Then later on in soap_bind(), the returned address "" is passed into bind(). And bind() then binds to ALL interfaces (not just only the localhost interface, or should error out in the jail where lo0 has no IP address).

I looked hard in these applications but the bug isn't in there - it seems to be in the result returned be "getaddrinfo()" C library call.

I confirmed this was the case with "test.c" example program (below). Which was run in the same jail.

The test jail was created like this:

$ qjail create -4 getaddrinfo
$ qjail config -k getaddrinfo
$ qjail start getaddrinfo

getaddrinfo ~/ root~# ifconfig
re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	ether 00:1e:ec:d7:3a:1f
	inet netmask 0xffffffff broadcast
	media: Ethernet autoselect (1000baseT <full-duplex>)
	status: active
ipfw0: flags=8801<UP,SIMPLEX,MULTICAST> metric 0 mtu 65536
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
getaddrinfo ~/ root~# ping -c 1 "localhost"
PING localhost ( 56 data bytes
64 bytes from icmp_seq=0 ttl=64 time=0.168 ms

--- localhost ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.168/0.168/0.168/0.000 ms
getaddrinfo ~/ root~# cat /etc/hosts | grep localhost
::1			localhost localhost.my.domain		localhost localhost.my.domain
getaddrinfo ~/ root~# cat test.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

/* man getaddrinfo */

int main()
  struct addrinfo *addrinfo = NULL;
  struct addrinfo hints;
  struct addrinfo res;
  int err;

  memset((void*)&hints, 0, sizeof(hints));
  hints.ai_family   = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags    = AI_PASSIVE;

  err = getaddrinfo("localhost", "18083", &hints, &addrinfo);

  if (addrinfo)
    res = *addrinfo;
    printf("%s\n", inet_ntoa( ((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr ));


  if (err || !addrinfo)
    printf("getaddrinfo failed with code %i.\n",err);

  return (0);


getaddrinfo ~/ root~# gcc test.c -o test && ./test
getaddrinfo ~/ root~# exit

I suspect this function (getaddrinfo) shouldn't be returning IN_ADDR_ANY. It should either fail, or return "" as per "/etc/hosts".


More information about the freebsd-bugs mailing list