Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Sat, 27 Jan 2024 16:03:32 +0100
From: Michał Górny <mgorny@...too.org>
To: musl@...ts.openwall.com
Subject: [Bug?] getaddrinfo() fills .ai_canonname when AI_CANONNAME isn't
 passed

Hello,

While debugging a test suite failure in dnspython [1], we've discovered
that the musl implementation of getaddrinfo() fills .ai_canonname field
even if flags do not include AI_CANONNAME.  We think that this could
incorrect.  Per POSIX:

> If nodename is not null, and if requested by the AI_CANONNAME flag,
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> the ai_canonname field of the first returned addrinfo structure shall
> point to a null-terminated string containing the canonical name
> corresponding to the input nodename; [...]
>
> All fields in socket address structures returned by getaddrinfo() that
> are not filled in through an explicit argument (for example,
> sin6_flowinfo) shall be set to zero.  [2]

I think the correct behavior would be to set the field to zero (i.e.
null) when AI_CANONNAME is not present in flags.


I've reproduced the problem on Gentoo Linux amd64 with musl 1.2.4, using
the following test program:

```
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int main(void)
{
    struct addrinfo hints = {0};
    struct addrinfo *res;
    struct addrinfo *p;

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;

    if (getaddrinfo("example.com", NULL, &hints, &res) == 0) {
        printf("%s\n", res->ai_canonname ? res->ai_canonname
                                         : "(null)");
        freeaddrinfo(res);
    }
    return 0;
}
```

On glibc system, it produces "(null)".  On musl system, it produces
"example.com".


[1] https://github.com/rthalley/dnspython/issues/1035
[2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html

-- 
Best regards,
Michał Górny


Download attachment "signature.asc" of type "application/pgp-signature" (513 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.