mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Philip Homburg <philip.homburg@ripe.net>
To: musl@lists.openwall.com
Subject: inet_ntop bug in 1.1.19
Date: Mon, 4 Jun 2018 15:57:09 +0200	[thread overview]
Message-ID: <a7e32d88-2b5f-e9f9-8108-7fdb3958a130@ripe.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 389 bytes --]

(Please CC me on any replies, I'm not subscribed to the list)

inet_ntop doesn't conform to RFC 5952 (A Recommendation for IPv6 Address
Text Representation).

I attached a test program to demonstrate the issue and a patch:
$ cc inet_ntop_test.c musl-1.1.19/src/network/inet_ntop.c
$ ./a.out
Section 4.2.2 test failed: got 2001:db8::1:1:1:1:1, expected
2001:db8:0:1:1:1:1:1
Found 1 error.


[-- Attachment #2: inet_ntop_test.c --]
[-- Type: text/plain, Size: 2273 bytes --]

/*
Check if inet_ntop is according to RFC 5952
*/

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

/* Section 4.1: Handling Leading Zeros in a 16-Bit Field */
struct in6_addr s41_input = { {
			0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } };
char *s41_str = "2001:db8::1";

/* Section 4.2.1: Shorten as Much as Possible */
struct in6_addr s421_input = { {
			0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01 } };
char *s421_str = "2001:db8::2:1";

/* Section 4.2.2: Handling One 16-Bit 0 Field */
struct in6_addr s422_input = { {
			0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x01,
			0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01 } };
char *s422_str = "2001:db8:0:1:1:1:1:1";

/* Section 4.2.3a: Choice in Placement of "::" */
struct in6_addr s423a_input = { {
			0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } };
char *s423a_str = "2001:0:0:1::1";

/* Section 4.2.3b: Choice in Placement of "::" */
struct in6_addr s423b_input = { {
			0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } };
char *s423b_str = "2001:db8::1:0:0:1";

struct tests {
	char *label;
	struct in6_addr *addrp;
	char **strp;
} tests[] = {
	{ "Section 4.1", &s41_input, &s41_str },
	{ "Section 4.2.1", &s421_input, &s421_str },
	{ "Section 4.2.2", &s422_input, &s422_str },
	{ "Section 4.2.3a", &s423a_input, &s423a_str },
	{ "Section 4.2.3b", &s423b_input, &s423b_str },
	{ NULL, NULL, NULL }
};

int main(void)
{
	int i, errors;
	char buf[INET6_ADDRSTRLEN];

	errors= 0;
	for (i= 0; tests[i].label != NULL; i++)
	{
		if (inet_ntop(AF_INET6, tests[i].addrp, buf, sizeof(buf)) ==
			NULL)
		{
			fprintf(stderr, "inet_ntop failed for test %s: %s\n",
				tests[i].label, strerror(errno));
			exit(1);
		}
		if (strcmp(buf, *tests[i].strp) != 0)
		{
			fprintf(stderr, "%s test failed: got %s, expected %s\n",
				tests[i].label, buf, *tests[i].strp);
			errors++;
		}
	}
	if (errors)
	{
		fprintf(stderr, "Found %d error%s.\n",
			errors, errors == 1 ? "" : "s");
		exit(1);
	}
	exit(0);
}
	

[-- Attachment #3: inet_ntop.c.patch --]
[-- Type: text/plain, Size: 327 bytes --]

--- musl-1.1.19/src/network/inet_ntop.c	2018-02-22 19:39:19.000000000 +0100
+++ inet_ntop.c	2018-06-04 15:51:23.192207973 +0200
@@ -34,6 +34,7 @@
 		for (i=best=0, max=2; buf[i]; i++) {
 			if (i && buf[i] != ':') continue;
 			j = strspn(buf+i, ":0");
+			if (j<4) continue;
 			if (j>max) best=i, max=j;
 		}
 		if (max>2) {

             reply	other threads:[~2018-06-04 13:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-04 13:57 Philip Homburg [this message]
2018-06-04 14:23 ` Laurent Bercot
2018-06-04 18:39   ` Timo Teras
2018-06-05  0:37     ` Rich Felker
2018-06-26 20:56       ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a7e32d88-2b5f-e9f9-8108-7fdb3958a130@ripe.net \
    --to=philip.homburg@ripe.net \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).