Bug 198092 - getaddrinfo(3) fails with "Invalid value for ai_flags" (error code 3) if hint.ai_flags contains AI_V4MAPPED
Summary: getaddrinfo(3) fails with "Invalid value for ai_flags" (error code 3) if hint...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Hajimu UMEMOTO
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2015-02-28 14:30 UTC by andriys
Modified: 2015-12-25 11:36 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description andriys 2015-02-28 14:30:40 UTC
The getaddrinfo(3) manual page refer to POSIX.1 and RFC 3493 in the STANDARDS section. Both of these documents say getaddrinfo accepts AI_V4MAPPED flag in the hint. In fact if the AI_V4MAPPED bit is set in the ai_flags member of the hint addrinfo structure, getaddrinfo(3) always fails with error code 3 ("Invalid value for ai_flags").

This can be easily demonstrated with the following sample code:

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

int main(void)
{
    struct addrinfo *ai;
    struct addrinfo hint;
    int errcode;

    memset(&hint, 0, sizeof(hint));
    hint.ai_family = PF_INET6;
    hint.ai_flags = AI_V4MAPPED;

    errcode = getaddrinfo("freebsd.org", "80", &hint, &ai);
    if(errcode) {
        fprintf(stderr, "Error %d: %s\n", errcode, gai_strerror(errcode));
        return 1;
    }

    freeaddrinfo(ai);
    return 0;
}
Comment 1 Glen Barber freebsd_committer 2015-07-08 15:35:10 UTC
Confirmed this is still an issue, and not specific to 10.x.

Assigning to -bugs@ for broader exposure.
Comment 2 Hiroki Sato freebsd_committer 2015-07-08 16:18:42 UTC
AI_V4MAPPED is not supported on FreeBSD.  While the manual page mentioned RFC 3493, AI_ALL or AI_V4MAPPED returns EAI_BADFLAGS.  This is due to a decision on the API implementation.
Comment 3 Mark Felder freebsd_committer 2015-11-20 18:26:45 UTC
I hit this too trying to port Titus to FreeBSD (https://github.com/AGWA/titus)

It build successfully, but fails to run with an "Invalid value for ai_flags" error

I'll also note that AI_ALL and AI_V4MAPPED are both still mentioned in getipnodebyname(3)

I was asked to cc bz@ and ume@ as well.

r175955 claims to remove support for AI_ALL and AI_V4MAPPED, but perhaps it was incomplete?
Comment 4 John Baldwin freebsd_committer freebsd_triage 2015-11-20 18:36:18 UTC
If we don't support it, should we remove the flags from netdb.h so that software fails to compile (instead of generating a runtime error)?

RFC 3493 requires these flags (by my reading), so we are non-conforming and should not list it in STANDARDS unless we also include a caveat for the parts we don't implement (and arguably we should fix our implementation to be conforming instead).
Comment 5 Mark Felder freebsd_committer 2015-11-20 18:43:54 UTC
(In reply to John Baldwin from comment #4)

I believe the decision to be non-conforming was intentional due to security issues that RFC presents. The internet seems torn about whether or not V4 mapped V6 addresses should have ever been created because it allows some nasty backdoors if your firewalls don't cover this scenario.
Comment 6 John Baldwin freebsd_committer freebsd_triage 2015-11-20 19:08:28 UTC
We should document this then.
Comment 7 Hajimu UMEMOTO freebsd_committer 2015-11-21 01:11:25 UTC
(In reply to Mark Felder from comment #3)

> I'll also note that AI_ALL and AI_V4MAPPED are both still mentioned in getipnodebyname(3)

This issue is only for getaddrinfo(3).  getipnodebyname(3) is seeing AI_ALL and AI_V4MAPPED and does proper job.
Comment 8 Hajimu UMEMOTO freebsd_committer 2015-11-22 10:16:21 UTC
I implemented an AI_V4MAPPED support for getaddrinfo(3).  My proposed
patch is:

    https://people.freebsd.org/~ume/getaddrinfo-v4mapped.diff

However, I'm skeptical to commit it.  This issue is confirmation to
standard v.s. security consideration.  If there are some opinions to
make conformation to standard give priority, I'll commit it.
Comment 9 Hajimu UMEMOTO freebsd_committer 2015-11-22 18:55:38 UTC
(In reply to Hajimu UMEMOTO from comment #8)

Oops, the attached patch was not applied cleanly on head.
It was against stable/10.
So, I made the patch against head, again:

    https://people.freebsd.org/~ume/getaddrinfo-v4mapped-20151123.diff

Further, I made the AI_V4MAPPED support optional.  It is off by default,
and is enabled by specifying WITH_GETADDRINFO_V4MAPPED=yes.
Comment 10 Hajimu UMEMOTO freebsd_committer 2015-12-13 15:16:05 UTC
After some discussion with hrs@, I think supporting the v4mapped address by getaddrinfo(3) itself doesn't related to security.
And, the destination address selection didn't work against the v4mapped address.
So, I re-made the patch:

    https://people.freebsd.org/~ume/getaddrinfo-v4mapped-20151213.diff

If there is no objection, I'll commit it.
Comment 11 Hajimu UMEMOTO freebsd_committer 2015-12-14 07:04:20 UTC
I reworked to address the following comments from hrs@:
- We need to obey WITHOUT_INET6.                                                                                  
- We should use _map_v4v6_address().

https://people.freebsd.org/~ume/getaddrinfo-v4mapped-20151213-2.diff
Comment 12 commit-hook freebsd_committer 2015-12-18 17:54:13 UTC
A commit references this bug:

Author: ume
Date: Fri Dec 18 17:53:19 UTC 2015
New revision: 292444
URL: https://svnweb.freebsd.org/changeset/base/292444

Log:
  Add AI_V4MAPPED and AI_ALL support for getaddrinfo(3).

  PR:		198092
  MFC after:	1 week

Changes:
  head/lib/libc/net/getaddrinfo.3
  head/lib/libc/net/getaddrinfo.c
Comment 13 commit-hook freebsd_committer 2015-12-18 18:09:17 UTC
A commit references this bug:

Author: ume
Date: Fri Dec 18 18:08:53 UTC 2015
New revision: 292446
URL: https://svnweb.freebsd.org/changeset/base/292446

Log:
  Add AI_V4MAPPED and AI_ALL support for getaddrinfo(3).
  We need to change netdb.h to make it actually enabled.

  PR:		198092
  MFC after:	1 week

Changes:
  head/include/netdb.h
Comment 14 commit-hook freebsd_committer 2015-12-25 11:17:46 UTC
A commit references this bug:

Author: ume
Date: Fri Dec 25 11:17:22 UTC 2015
New revision: 292722
URL: https://svnweb.freebsd.org/changeset/base/292722

Log:
  Add AI_V4MAPPED and AI_ALL support for getaddrinfo(3).

  PR:		198092

Changes:
_U  stable/10/
  stable/10/include/netdb.h
  stable/10/lib/libc/net/getaddrinfo.3
  stable/10/lib/libc/net/getaddrinfo.c
Comment 15 commit-hook freebsd_committer 2015-12-25 11:33:49 UTC
A commit references this bug:

Author: ume
Date: Fri Dec 25 11:33:09 UTC 2015
New revision: 292724
URL: https://svnweb.freebsd.org/changeset/base/292724

Log:
  MFC r292444, r292446:
    Add AI_V4MAPPED and AI_ALL support for getaddrinfo(3).

  PR:		198092

Changes:
_U  stable/9/include/
  stable/9/include/netdb.h
_U  stable/9/lib/libc/
  stable/9/lib/libc/net/getaddrinfo.3
  stable/9/lib/libc/net/getaddrinfo.c