NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/61108: sockaddr_snprintf(3): buffer overread for AF_LOCAL



>Number:         61108
>Category:       lib
>Synopsis:       sockaddr_snprintf(3): buffer overread for AF_LOCAL
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 01 18:55:00 +0000 2026
>Originator:     Taylor R Campbell
>Release:        current, 11, 10, ...
>Organization:
struct sockaddr_tnf02 22:54:10 fvdl Exp $
>Environment:
>Description:

	The sockaddr_snprintf(3) function does not respect the address
	length embedded in the sockaddr structure, and has no way for
	the caller to provide an address length.  For AF_LOCAL
	addresses, instead of using this framing, it incorrectly
	assumes that the sun_path member is NUL-terminated, leading to
	truncated paths in some cases and buffer overreads in others.

	(Discovered by puzzling over bizarre mbsoname text in kdump
	output.)

>How-To-Repeat:
#include <sys/socket.h>
#include <sys/un.h>

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <util.h>

struct stuff {
	union {
		struct sockaddr sa;
		struct sockaddr_un sun;
	} addr;
	char extra[128];
} u = {
	.addr = {
		.sun = {
			.sun_family = AF_LOCAL,
			.sun_path = "/short/path",
		},
	},
	.extra = "hello world",
};

int
main(void)
{
	const size_t pathlen = strlen(u.addr.sun.sun_path);
	char buf[1024];

	u.addr.sun.sun_len =
	    offsetof(struct stuff, addr.sun.sun_path[pathlen]);
	memset(&u.addr.sun.sun_path[pathlen], 'x',
	    (offsetof(struct stuff, extra) -
		offsetof(struct stuff, addr.sun.sun_path[pathlen])));
	sockaddr_snprintf(buf, sizeof(buf), "%f %l %a\n", &u.addr.sa);
	printf("%s", buf);
	fflush(stdout);
	return ferror(stdout);
}


	Expected output:

1 13 /short/path

	Actual output:

1 106 /short/pathxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxhello world

>Fix:

	For NetBSD we can make it use the sa_len member of struct
	sockaddr, but that's not portable -- it doesn't exist on Linux.
	Perhaps we should change the API to take a socklen_t length of
	the address structure.




Home | Main Index | Thread Index | Old Index