WSL: AF_UNIX abstract on Windows do not work

  • Your Windows build number: (Type ver at a Windows Command Prompt) 18362.175
  • What you’re doing and what’s happening: (Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screen shots if that helps demonstrate the problem.) Trying to create an “abstract” namespace AF_UNIX application.
  • What’s wrong / what should be happening instead: connect always returns EINVAL, regardless of what I try.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <afunix.h>
#include <stdio.h>

typedef SOCKET fd_t;
#define INVALID_FD INVALID_SOCKET

int socketpair(int domain, int type, int protocol, fd_t sv[2])

{
	SOCKET fd0 = INVALID_FD;
	SOCKET fd1 = INVALID_FD;
	SOCKET fdListen = INVALID_FD;
	struct sockaddr_un sa;
	int  len = 0;
	static int counter = 666;

	memset(&sa, 0, sizeof sa);
	sa.sun_family = AF_UNIX;
	_snprintf_s(sa.sun_path, ARRAYSIZE(sa.sun_path),_TRUNCATE, " ./xyx-%d-%d", GetCurrentProcessId(),counter++);
	len = sizeof(sa);
	sa.sun_path[0] = 0;

	if (domain != AF_UNIX || type != SOCK_STREAM || protocol != PF_UNSPEC)
	{
		return -1;
	}
	fdListen = socket(domain, type, protocol);// WSASocketW(domain, type, protocol, NULL, 0, 0);
	if (fdListen == INVALID_SOCKET)
	{
		goto Error;
	}
	printf("before bind address %s\n",sa.sun_path+1);
	if (bind(fdListen, (struct sockaddr *)&sa, len) == SOCKET_ERROR) {
		goto Error;
	}
	if (listen(fdListen, 5) == SOCKET_ERROR) {
		goto Error;
	}
	fd0 = socket(domain, type, protocol);
	if (fd0 == INVALID_SOCKET)
	{
		goto Error;
	}
	len = sizeof(sa);
	getsockname(fdListen, (struct sockaddr*)&sa, &len);
	printf("bound address %s\n",sa.sun_path+1);
	if (connect(fd0, (const struct sockaddr*)&sa, len) == SOCKET_ERROR)
	{
		fprintf(stderr,"connect error %d\n",WSAGetLastError());
		goto Error;
	}
	{
		int addrLen = sizeof(sa);;
		fd1 = accept(fdListen, (struct sockaddr*) & sa, &addrLen);
	}
	if (fd1 == SOCKET_ERROR)
	{
		goto Error;
	}

	sv[0] = fd0;
	sv[1] = fd1;
	closesocket(fdListen);
	printf("success\n");
	return 0;
Error:
	if (fd0 != INVALID_FD)
	{
		closesocket(fd0);
	}
	if (fd1 != INVALID_FD)
	{
		closesocket(fd1);
	}
	if (fdListen != INVALID_FD)
	{
		closesocket(fdListen);
	}
	return -1;
}
int main(int argc, char **argv) {
	
	fd_t x[2];
	WSADATA wsd;
	WSAStartup(WINSOCK_VERSION,&wsd);
	socketpair(AF_UNIX,SOCK_STREAM,0,x);
}

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 23
  • Comments: 40 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Oh and maybe someone can correct the original blog post at https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/? I’ve lost a day’s work due to this (know) limitation 😦

Oh and maybe someone can correct the original blog post at https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/? I’ve lost a day’s work due to this (know) limitation 😦

That misleading blogpost! Lost 2 days.

Windows implementation of AF_UNIX does not support abstract sockets (as already established here so far) and we have very less desire to implement it since it lacks the Windows security model (abstract unix sockets cannot be secured).

The blog post is incorrect and we take responsibility for that (we will correct it). And, we do apologize for the confusion caused here.

well, that comment was just me ranting, so it definitely deserved to be deleted 😃 Nothing useful was lost in that deletion.

The point of the confusion is, that Microsoft announced in https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ that AF_UNIX would be fully supported. However the support is limited, and this is not documented anywhere, leading to lots of confusion and frustration for developers.

Exactly, 100%.

@microsoft overpromised, underdelivered, acknowledged the gap and promised to rectify the documentation 5 YEARS AGO, and – as I wrote back in 2021 – still haven’t done anything about it.

It’s a total lack of respect and attention to the time of developers of POSIX-y software who are going out of our ways to try to support Windows.

@keith-horton @CatalinFetoiu @OneBlue folks are you able to address this issue and the incorrect blog post? This is causing a lot of confusion, and it’s been ~5 years since the issue was reported.

@sunilmut So there are no abstract sockets in Windows and I wasted a couple of days and there are also no docs about AF_SOCKET here: https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-start-page-2

Very nice! Maybe the doc issues could be rectified too?

While it would be nice to have this for interoperability reasons, in man pages we can see:

The abstract socket namespace is a nonportable Linux extension.

I’m sorry @AleksaBajat, but that is not the point here. The point of the confusion is, that Microsoft announced in https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ that AF_UNIX would be fully supported. However the support is limited, and this is not documented anywhere, leading to lots of confusion and frustration for developers.

As we’ve hit 2024, the blog post was not yet rectified image There’s a comment beneath it from someone whose name I don’t see in this entire thread. Can someone please pick up the work of either fixing the blog post, or getting abstract sockets to work upstream rather than with robust workarounds?

Hey @microsoft!

Do you respect developers who are willing/able/required to write software for Windows?

If so, you should stop wasting everyone’s time by correcting the blog post to reflect the fact that abstract sockets are not actually supported… at all… which is completely the opposite of what it explicitly states. It appears I’m just one of many developers who’ve wasted hours trying to make them work on Windows before discovering this thread. 🤬

@sunilmut, you wrote more than a year and a half ago that it would get updated https://github.com/microsoft/WSL/issues/4240#issuecomment-620805115

Also, pretty much everyone who wants AF_UNIX sockets on Win32 wants them for the same reason: to implement socketpair without needing either to use either…

  1. Filesystem paths for the sockets (messy and easy to break due to permissions issues, collisions)
  2. Local IPv4 sockets (surprisingly, also very easy to break simply by running route /f, which will nuke the 127.0.0.0/8 route, and make it very hard to restore; see openconnect issue #228 for examples of this)

… so why not just implement socketpair properly and give everyone what they actually want?

Why does this blog post from Microsoft claim that “Windows implementation of AF_UNIX socket can also accept abstract addresses” then? And why is @microsoft deleting comments?

This feature is simply not implemented. The AfUnixTlConnect function of afunix.sys checks whether the address is abstract, and if it is, does a trace log of [0x%08x] connect to abstract address then returns STATUS_INVALID_PARAMETER, which becomes WSAEINVAL in WinSock.

@sunilmut The entire reason I tried to use abstract sockets was so that I could interrupt select. The only decent way to interrupt select is to send a byte to one of the sockets it’s using. But this requires a loopback socket, which without a socketpair API can only be created by binding a socket to a localhost port and connecting to it. I didn’t want to create an actual TCP port, so I used AF_UNIX instead. I wanted to use abstract sockets so that there wasn’t an object in the filesystem sticking around.

As for the security issue, the way I got around the problem of other processes connecting to my socket in the timing window was to use SIO_AF_UNIX_GETPEERPID to verify that I am talking to myself. So even without the ability to set ACLs on abstract sockets, it would’ve been secure.

All this could be avoided with a socketpair API in WinSock.

(Yes, you can queue a user-mode APC to a select thread, but there’s no well-defined method to cause the select to abort and return from that APC.)

Please implement correct support for socketpair. The documentation and lack of support is truely frustrating and time consuming.

If anyone needs an AF_UNIX-based implementation of socketpair() for Windows, we needed it in OpenConnect and I figured out how to make it work: https://gitlab.com/openconnect/openconnect/-/merge_requests/320/diffs

The caveat, of course, is that you need a real filesystem path for the socket. 🤕

Your solution is indeed excellent and probably the best right now.

The only thing I would do differently is use GetTempFileName() to generate the filename. The function should guarantee a unique string, the only limitation is the lack of a custom prefix (it’s always .TMP).

Will this ever be worked on?

I almost posted this one myself sometime last year after having burned an evening trying to use abstract names. [The announcement blog doesn’t say they aren’t supported, although no socketpair() makes for a unsubtle hint.] I ended up not posting after I came to the uncomfortable conclusion the ask isn’t strictly speaking WSL actionable (it is a win32 feature). Happy to hear I’m not the only one who tried tho.

Heads up we lost named AF_UNIX Windows interop in WSL2 (for now anyway). But even if we get that back 🙏, the fact that WSL2 is not in the same network namespace (nvm mount namespace) as win32 speaks to why abstract names is hard.

For time being, the way in and out of WSL (notwithstanding extreme unsupported measures) is through interop stdin/stdout/stderr and AF_INET.

Solution: call DeleteFile() right after connect().

However, in order to achieve a proper socketpair() implementation, abstract sockets are required.

@davidebeatrici, agreed, but do check out the “just about good enough” version that I’ve implemented for OpenConnect: https://gitlab.com/openconnect/openconnect/-/merge_requests/320/diffs

It works around the issue of missing abstract sockets by trying, in order…

  1. Unix socket with filename {64-bit CPU ticks in hex}-{Process ID}.$$$ in %TMP%
  2. Unix socket with same filename in %WINDIR%\Temp
  3. Unix socket with same filename in C:\Temp
  4. Unix socket with same filename in current directory
  5. AF_INET 😭

About as robust as I think we can do given the (frankly infuriating) limitations of Windows’ incomplete AF_UNIX implementation.

I have this problem as well: non-abstract AF_UNIX sockets work fine, but connect fails with WSAEINVAL with abstract AF_UNIX sockets.

The entire reason I am doing this is so that I can implement my own socketpair so I can interrupt a select/WSAPoll loop. So if you implement socketpair in WinSock, that’d work, too.

By the way, if you do implement socketpair in Win32, please add something equivalent to Linux’s SOCK_CLOEXEC to it, which would atomically set the socket as non-inheritable. (Compare WSASocket’s WSA_FLAG_NO_HANDLE_INHERIT.)

I also encountered this problem, AF_UNIX abstract on Windows, but connect always returns EINVAL, Is there any solution at the moment?