Bug 289198 - inet_net_pton is broken with AF_INET6
Summary: inet_net_pton is broken with AF_INET6
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 14.3-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: Lexi Winter
URL: https://reviews.freebsd.org/D53491
Keywords:
Depends on:
Blocks:
 
Reported: 2025-08-31 06:45 UTC by Nico Sonack
Modified: 2026-05-04 08:25 UTC (History)
4 users (show)

See Also:
ivy: mfc-stable15+
ivy: mfc-stable14+


Attachments
test code (769 bytes, text/plain)
2025-08-31 06:45 UTC, Nico Sonack
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nico Sonack 2025-08-31 06:45:28 UTC
Created attachment 263348 [details]
test code

I believe inet_net_pton(3) with the first argument set to AF_INET6
is utterly broken:

The manual page claims:

	The inet_net_pton() function converts a presentation format Internet
	network number ... to network format (usually a struct in_addr...).
	It returns the number of bits (...specified with /CIDR).

	The currently supported values for af are AF_INET and AF_INET6.

See the test code in the attachment.

The code first uses inet_net_pton to parse a string representation
of an address in argv[1] and then converts it back to a string using
inet_ntop(3):

	$ make bug
	cc -O2 -pipe  bug.c  -o bug
	$
	$ ./bug fe80::1/64
	 input : fe80::1/64
	network : fe80:0:0:1::
	$
	$ ./bug fe80::f000:74ff:fe54:bed2/64
	 input : fe80::f000:74ff:fe54:bed2/64
	network : bed2::
	$
	$
	$ ./bug fe80::f000:74ff:fe54:bed2
	 input : fe80::f000:74ff:fe54:bed2
	network : fe80::f000:74ff:fe54:bed2
	$
	$
	$ ./bug ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64
	bug: bad address: No such file or directory
	$
	$ ./bug ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
	 input : ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
	network : ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff

Staring at the current implementation for a bit reveals that it is
in fact a copy of inet_pton with a few changes. Except that it
doesn't work.

Looking at the OpenBSD version of the same function it feels like
that would be easy to import it. It is exactly implemented how one
would expect it to be.

See https://cvsweb.openbsd.org/src/lib/libc/net/inet_net_pton.c?rev=1.14&content-type=text/x-cvsweb-markup at the very bottom.

I tested this on 14.3-RELEASE-p2 amd64. The code hasn't been touched
in main so I believe it's broken there too.

Nico
Comment 1 paul vixie 2025-09-19 19:00:05 UTC
openbsd's upstream is:

https://iso.netbsd.org/pub/pkgsrc/current/pkgsrc/net/libbind/index.html

i plan to pull this into github since my own sources are far newer.
Comment 2 Lexi Winter freebsd_committer freebsd_triage 2025-09-19 19:01:26 UTC
i was planning to pull this from libbind, but that was abandoned years ago and OpenBSD's version has newer fixes.

however des@ identified at least one bug in the OpenBSD version, so we'll see.

in the mean time i am writing a test case.
Comment 3 commit-hook freebsd_committer freebsd_triage 2025-10-18 14:47:29 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8f4a0d2f7b96099001dbc51e06114df1a0e6d291

commit 8f4a0d2f7b96099001dbc51e06114df1a0e6d291
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-18 14:18:40 +0000
Commit:     Lexi Winter <ivy@FreeBSD.org>
CommitDate: 2025-10-18 14:45:42 +0000

    libc: Import OpenBSD's inet_net_{ntop,pton}

    Our versions of these functions (originally taken from BIND) simply
    don't work correctly for AF_INET6.  These were removed from BIND itself
    quite a while ago, but OpenBSD has made several fixes in the mean time,
    so import their code.

    Add tests for both functions.

    PR:             289198
    Reported by:    Nico Sonack <nsonack@herrhotzenplotz.de>
    MFC after:      1 week
    Reviewed by:    des
    Obtained from:  OpenBSD (lib/libc/net)
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52629

 lib/libc/inet/inet_net_ntop.c             | 206 +++++------------
 lib/libc/inet/inet_net_pton.c             | 363 +++++++++---------------------
 lib/libc/tests/net/Makefile               |   2 +
 lib/libc/tests/net/inet_net_test.cc (new) | 333 +++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 412 deletions(-)
Comment 4 Lexi Winter freebsd_committer freebsd_triage 2025-10-18 14:50:26 UTC
i plan to land this for 15.0-RELEASE, re@ is aware of the issue.
Comment 5 commit-hook freebsd_committer freebsd_triage 2025-10-23 00:38:38 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ff7a10d0e1d1498724c6a1e02a70926944a71590

commit ff7a10d0e1d1498724c6a1e02a70926944a71590
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-18 14:18:40 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-23 00:36:51 +0000

    libc: Import OpenBSD's inet_net_{ntop,pton}

    Our versions of these functions (originally taken from BIND) simply
    don't work correctly for AF_INET6.  These were removed from BIND itself
    quite a while ago, but OpenBSD has made several fixes in the mean time,
    so import their code.

    Add tests for both functions.

    PR:             289198
    Reported by:    Nico Sonack <nsonack@herrhotzenplotz.de>
    MFC after:      1 week
    Reviewed by:    des
    Obtained from:  OpenBSD (lib/libc/net)
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52629

    (cherry picked from commit 8f4a0d2f7b96099001dbc51e06114df1a0e6d291)

 lib/libc/inet/inet_net_ntop.c             | 206 +++++------------
 lib/libc/inet/inet_net_pton.c             | 363 +++++++++---------------------
 lib/libc/tests/net/Makefile               |   2 +
 lib/libc/tests/net/inet_net_test.cc (new) | 333 +++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 412 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2025-10-23 04:27:29 UTC
A commit in branch releng/15.0 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7bdd04a687d8118c8e76cbca634ac7639059c200

commit 7bdd04a687d8118c8e76cbca634ac7639059c200
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-18 14:18:40 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-23 04:26:07 +0000

    libc: Import OpenBSD's inet_net_{ntop,pton}

    Our versions of these functions (originally taken from BIND) simply
    don't work correctly for AF_INET6.  These were removed from BIND itself
    quite a while ago, but OpenBSD has made several fixes in the mean time,
    so import their code.

    Add tests for both functions.

    Approved by:    re (cperciva)
    PR:             289198
    Reported by:    Nico Sonack <nsonack@herrhotzenplotz.de>
    MFC after:      1 week
    Reviewed by:    des
    Obtained from:  OpenBSD (lib/libc/net)
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52629

    (cherry picked from commit 8f4a0d2f7b96099001dbc51e06114df1a0e6d291)
    (cherry picked from commit ff7a10d0e1d1498724c6a1e02a70926944a71590)

 lib/libc/inet/inet_net_ntop.c             | 206 +++++------------
 lib/libc/inet/inet_net_pton.c             | 363 +++++++++---------------------
 lib/libc/tests/net/Makefile               |   2 +
 lib/libc/tests/net/inet_net_test.cc (new) | 333 +++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 412 deletions(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-10-25 04:30:37 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=b4871be3490de56975777079c1767d1fd346ac7a

commit b4871be3490de56975777079c1767d1fd346ac7a
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-18 14:18:40 +0000
Commit:     Lexi Winter <ivy@FreeBSD.org>
CommitDate: 2025-10-25 04:29:04 +0000

    libc: Import OpenBSD's inet_net_{ntop,pton}

    Our versions of these functions (originally taken from BIND) simply
    don't work correctly for AF_INET6.  These were removed from BIND itself
    quite a while ago, but OpenBSD has made several fixes in the mean time,
    so import their code.

    Add tests for both functions.

    PR:             289198
    Reported by:    Nico Sonack <nsonack@herrhotzenplotz.de>
    MFC after:      1 week
    Reviewed by:    des
    Obtained from:  OpenBSD (lib/libc/net)
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52629

    (cherry picked from commit 8f4a0d2f7b96099001dbc51e06114df1a0e6d291)

    inet_net_test: Compare pointers against nullptr

    GCC does not like passing NULL (__null) to std::ostringstream::operator<<
    inside of ATF_REQUIRE_EQ:

    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_ntop_invalid::body() const':
    lib/libc/tests/net/inet_net_test.cc:306:9: error: passing NULL to non-pointer argument 1 of 'std::__1::basic_ostream<_CharT, _Traits>& std::__1::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::__1::char_traits<char>]' [-Werror=conversion-null]
      306 |         ATF_REQUIRE_EQ(ret, NULL);
          |         ^~~~~~~~~~~~~~
    In file included from /usr/obj/.../amd64.amd64/tmp/usr/include/c++/v1/sstream:317,
                     from /usr/obj/.../amd64.amd64/tmp/usr/include/atf-c++/macros.hpp:29,
                     from /usr/obj/.../amd64.amd64/tmp/usr/include/atf-c++.hpp:29,
                     from lib/libc/tests/net/inet_net_test.cc:33:
    /usr/obj/.../amd64.amd64/tmp/usr/include/c++/v1/__ostream/basic_ostream.h:338:81: note:   declared here
      338 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
          |                                                                            ~~~~~^~~
    ...

    Fixes:          8f4a0d2f7b96 ("libc: Import OpenBSD's inet_net_{ntop,pton}")
    (cherry picked from commit aa358ce3ca8e1fcfb305025fd00beb2a119c7c77)

    inet_net_test: Use int to hold expected return values from inet_net_pton

    GCC warns about the sign mismatch in comparisons:

    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_inet4::body() const':
    lib/libc/tests/net/inet_net_test.cc:86:17: error: comparison of integer expressions of different signedness: 'int' and 'const unsigned int' [-Werror=sign-compare]
       86 |                 ATF_REQUIRE_EQ(bits, addr.bits);
          |                 ^~~~~~~~~~~~~~
    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_inet6::body() const':
    lib/libc/tests/net/inet_net_test.cc:205:17: error: comparison of integer expressions of different signedness: 'int' and 'const unsigned int' [-Werror=sign-compare]
      205 |                 ATF_REQUIRE_EQ(bits, addr.bits);
          |                 ^~~~~~~~~~~~~~

    Fixes:          8f4a0d2f7b96 ("libc: Import OpenBSD's inet_net_{ntop,pton}")
    (cherry picked from commit e1aeb58cbbc3839db93ec38ce491b7b9383d5649)

 lib/libc/inet/inet_net_ntop.c             | 206 +++++------------
 lib/libc/inet/inet_net_pton.c             | 363 +++++++++---------------------
 lib/libc/tests/net/Makefile               |   2 +
 lib/libc/tests/net/inet_net_test.cc (new) | 333 +++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 412 deletions(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2025-10-25 04:42:41 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=857897013b01818814c1019a1b47164dfeb7951f

commit 857897013b01818814c1019a1b47164dfeb7951f
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-10-25 04:41:37 +0000
Commit:     Lexi Winter <ivy@FreeBSD.org>
CommitDate: 2025-10-25 04:41:37 +0000

    libc: Import OpenBSD's inet_net_{ntop,pton}

    Our versions of these functions (originally taken from BIND) simply
    don't work correctly for AF_INET6.  These were removed from BIND itself
    quite a while ago, but OpenBSD has made several fixes in the mean time,
    so import their code.

    Add tests for both functions.

    PR:             289198
    Reported by:    Nico Sonack <nsonack@herrhotzenplotz.de>
    MFC after:      1 week
    Reviewed by:    des
    Obtained from:  OpenBSD (lib/libc/net)
    Sponsored by:   https://www.patreon.com/bsdivy
    Differential Revision:  https://reviews.freebsd.org/D52629

    (cherry picked from commit 8f4a0d2f7b96099001dbc51e06114df1a0e6d291)

    inet_net_test: Compare pointers against nullptr

    GCC does not like passing NULL (__null) to std::ostringstream::operator<<
    inside of ATF_REQUIRE_EQ:

    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_ntop_invalid::body() const':
    lib/libc/tests/net/inet_net_test.cc:306:9: error: passing NULL to non-pointer argument 1 of 'std::__1::basic_ostream<_CharT, _Traits>& std::__1::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::__1::char_traits<char>]' [-Werror=conversion-null]
      306 |         ATF_REQUIRE_EQ(ret, NULL);
          |         ^~~~~~~~~~~~~~
    In file included from /usr/obj/.../amd64.amd64/tmp/usr/include/c++/v1/sstream:317,
                     from /usr/obj/.../amd64.amd64/tmp/usr/include/atf-c++/macros.hpp:29,
                     from /usr/obj/.../amd64.amd64/tmp/usr/include/atf-c++.hpp:29,
                     from lib/libc/tests/net/inet_net_test.cc:33:
    /usr/obj/.../amd64.amd64/tmp/usr/include/c++/v1/__ostream/basic_ostream.h:338:81: note:   declared here
      338 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
          |                                                                            ~~~~~^~~
    ...

    Fixes:          8f4a0d2f7b96 ("libc: Import OpenBSD's inet_net_{ntop,pton}")
    (cherry picked from commit aa358ce3ca8e1fcfb305025fd00beb2a119c7c77)

    inet_net_test: Use int to hold expected return values from inet_net_pton

    GCC warns about the sign mismatch in comparisons:

    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_inet4::body() const':
    lib/libc/tests/net/inet_net_test.cc:86:17: error: comparison of integer expressions of different signedness: 'int' and 'const unsigned int' [-Werror=sign-compare]
       86 |                 ATF_REQUIRE_EQ(bits, addr.bits);
          |                 ^~~~~~~~~~~~~~
    lib/libc/tests/net/inet_net_test.cc: In member function 'virtual void {anonymous}::atfu_tc_inet_net_inet6::body() const':
    lib/libc/tests/net/inet_net_test.cc:205:17: error: comparison of integer expressions of different signedness: 'int' and 'const unsigned int' [-Werror=sign-compare]
      205 |                 ATF_REQUIRE_EQ(bits, addr.bits);
          |                 ^~~~~~~~~~~~~~

    Fixes:          8f4a0d2f7b96 ("libc: Import OpenBSD's inet_net_{ntop,pton}")
    (cherry picked from commit e1aeb58cbbc3839db93ec38ce491b7b9383d5649)
    (cherry picked from commit b4871be3490de56975777079c1767d1fd346ac7a)

 lib/libc/inet/inet_net_ntop.c             | 206 +++++------------
 lib/libc/inet/inet_net_pton.c             | 363 +++++++++---------------------
 lib/libc/tests/net/Makefile               |   2 +
 lib/libc/tests/net/inet_net_test.cc (new) | 333 +++++++++++++++++++++++++++
 4 files changed, 492 insertions(+), 412 deletions(-)
Comment 9 Lexi Winter freebsd_committer freebsd_triage 2025-10-25 04:43:16 UTC
thank you for the bug report and the test cases.
Comment 10 Lexi Winter freebsd_committer freebsd_triage 2025-10-30 23:28:21 UTC
this is still broken.

new review: https://reviews.freebsd.org/D53491