#1007872 libc6-dev: getaddrinfo() finds an AF_INET address when hinting AF_UNSPEC, but not when hinting AF_INET

Package:
libc6-dev
Source:
glibc
Description:
GNU C Library: Development Libraries and Header Files
Submitter:
Robert Martin
Date:
2022-03-17 20:33:03 UTC
Severity:
normal
#1007872#5
Date:
2022-03-17 20:27:28 UTC
From:
To:
Dear Maintainer,

Under certain circumstances, when calling getaddrinfo with hints.ai_family = AF_UNSPEC, the first result is an AF_INET address. When calling it with hints.ai_family = AF_INET, however, it returns 251 (No address associated with hostname).

Here is a short program that reproduces the issue:
```c
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>

int main(int argc, char *argv[]) {
	const char hostname[] = "SomeHostname";
	const char service[] = "1433";

	struct addrinfo hints, *res;
	memset(&hints, 0, sizeof(hints));

	int ret = getaddrinfo(hostname, service, &hints, &res);
	assert(ret == 0);
	assert(res->ai_family == AF_INET);
	freeaddrinfo(res);

	hints.ai_family = AF_INET;
	ret = getaddrinfo(hostname, service, &hints, &res);

	if (ret != 0) {
		printf(gai_strerror(ret));
		printf("\n");
	} else {
		freeaddrinfo(res);
	}
	return ret;
}
```

I have only been able to reproduce the bug when all of the following criteria are met:
- Running in a Docker container on a Windows host
- The hostname is unqualified
- The container's /etc/resolv.conf does not include the correct search option to resolve the hostname
- The Windows host is set up to resolve unqualified names by applying the correct DNS suffix

getaddrinfo() also returns 251 if the AI_ADDRCONFIG flag is set, even though the container has an IPV4 address.

The bug is also present in 2.34-0experimental3.

Symptoms are superficially similar to #854301, but the container is online and passing AI_ADDRCONFIG returns no result.