Bug 5460

Summary: glibc-2.6+ returns too many results from /etc/hosts in some cases
Product: glibc Reporter: Mike Frysinger <vapier>
Component: libcAssignee: Ulrich Drepper <drepper.fsp>
Status: RESOLVED INVALID    
Severity: normal CC: glibc-bugs, tonnerre, toolchain
Priority: P2 Flags: fweimer: security-
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Bug Depends on: 4980    
Bug Blocks:    
Attachments: getaddrinfo.c test case

Description Mike Frysinger 2007-12-09 04:33:57 UTC
if /etc/hosts looks like:
127.0.0.1       localhost localhost4
::1             localhost localhost6

and you lookup "localhost6" with getaddrinfo(), the function returns "127.0.0.1"
and "::1"

glibc-2.5 and older operates correctly and only returns "::1".  glibc-2.6 and
newer will in addition incorrectly return "127.0.0.1".
Comment 1 Mike Frysinger 2007-12-09 04:36:15 UTC
Created attachment 2128 [details]
getaddrinfo.c test case

running this test case on newer glibc's will show:
$ ./getaddrinfo localhost6
--- looking up 'localhost6' for family 0 (0)
getaddrinfo(...) = EAI_NONAME, trying again with AI_CANONNAME
getaddrinfo(...) = 0
getnameinfo(...) = 0
	name = 127.0.0.1
	service = 23
getnameinfo(...) = 0
	name = ::1
	service = 23
--- looking up 'localhost6' for family AF_INET (2)
getaddrinfo(...) = EAI_NONAME, trying again with AI_CANONNAME
getaddrinfo(...) = 0
getnameinfo(...) = 0
	name = 127.0.0.1
	service = 23
--- looking up 'localhost6' for family AF_INET6 (10)
getaddrinfo(...) = EAI_NONAME, trying again with AI_CANONNAME
getaddrinfo(...) = 0
getnameinfo(...) = 0
	name = ::1
	service = 23

on older glibc versions, that "127.0.0.1" entry wont crop up
Comment 2 Ulrich Drepper 2008-04-07 16:34:59 UTC
Returning both entries is correct.  It's the same in DNS: multiple entries are
all returned.
Comment 3 Mike Frysinger 2008-04-07 17:45:46 UTC
looking up "localhost6" should never result in "127.0.0.1"
Comment 4 Ulrich Drepper 2008-04-09 17:05:41 UTC
The code works as designed.  This is no new change.  Back in November 2006 I
added code to map V6 addresses to v4 addresses in gethostbyname2(AF_INET) calls
if it is possible.  This is what happens here.  The results are not wrong since
you did not ask for a specific protocol.

At some point, when we have unified lookup instead of separate IPv4/v6 lookups,
this might change.  But for now it works as expected and the program will work
correctly.
Comment 5 Petr Baudis 2008-04-09 17:25:53 UTC
It is not obvious that the design of the current code makes any sense, but looks
like we will never get to know the reasons behind it... c.f.
http://sourceware.org/bugzilla/show_bug.cgi?id=4980