WSL: AF_UNIX abstract on Windows do not work
- Your Windows build number: (Type
verat 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)
Links to this issue
Commits related to this issue
- Microsoft claims "abstract" AF_UNIX sockets work, but they do not. What I am seeing on Win10 18362 is that a process cannot see another process's abstract sockets, connect(2) fails and bind(2) works ... — committed to asveikau/pollster by asveikau 7 years ago
- Support Unix domain sockets on Windows Unix domain sockets were introduced in Insider Build 17063 [1]. There are some caveats, as only SOCK_STREAM is supported and abstract sockets are not implemente... — committed to MisterDA/ocaml by MisterDA 6 years ago
- boost 1.75 enabled support for UNIX domain sockets on Windows Windows does not support abstract unix domain sockets yet. https://github.com/microsoft/WSL/issues/4240 Revert "[boost] Add UNIX domain ... — committed to huangqinjin/minapp by huangqinjin 6 years ago
- dumb_socketpair(): try to use AF_UNIX socketpair on Windows 10 and newer As a workaround for the lack of socketpair() on Windows, we use dumb_socketpair() from https://github.com/ncm/selectable-socke... — committed to sailfishos-mirror/openconnect by dlenski 5 years ago
- dumb_socketpair(): try to use AF_UNIX socketpair on Windows 10 and newer As a workaround for the lack of socketpair() on Windows, we use dumb_socketpair() from https://github.com/ncm/selectable-socke... — committed to sailfishos-mirror/openconnect by dlenski 5 years ago
- dumb_socketpair(): generate named socket path more carefully Windows forces us to use named-path Unix sockets. Generating a path in the temporary directory, combining current high-res time and PID, ... — committed to sailfishos-mirror/openconnect by dlenski 5 years ago
- Complete support for Windows, disable "CMAKE_CXX_EXTENSIONS" https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_EXTENSIONS.html Regarding abstract sockets on Windows: https://github.com/microsof... — committed to mumble-voip/libmumble by davidebeatrici 5 years ago
- In dumb_socketpair(), delete Unix-domain socket path once no longer needed Small follow-up improvement to https://gitlab.com/openconnect/openconnect/-/merge_requests/320, which made dumb_socketpair()... — committed to sailfishos-mirror/openconnect by dlenski 5 years ago
- In dumb_socketpair(), delete Unix-domain socket path once no longer needed Small follow-up improvement to https://gitlab.com/openconnect/openconnect/-/merge_requests/320, which made dumb_socketpair()... — committed to sailfishos-mirror/openconnect by dlenski 5 years ago
- Attempt to use AF_UNIX sockets instead of AF_INET on Windows 10+ This PR is based on the improvements we made in OpenConnect in https://gitlab.com/openconnect/openconnect/-/merge_requests/320. Unfor... — committed to dlenski/selectable-socketpair by dlenski 5 years ago
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.
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?
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
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_UNIXsockets on Win32 wants them for the same reason: to implementsocketpairwithout needing either to use either…route /f, which will nuke the127.0.0.0/8route, and make it very hard to restore; see openconnect issue #228 for examples of this)… so why not just implement
socketpairproperly 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
AfUnixTlConnectfunction ofafunix.syschecks whether the address is abstract, and if it is, does a trace log of[0x%08x] connect to abstract addressthen returnsSTATUS_INVALID_PARAMETER, which becomesWSAEINVALin WinSock.@sunilmut The entire reason I tried to use abstract sockets was so that I could interrupt
select. The only decent way to interruptselectis to send a byte to one of the sockets it’s using. But this requires a loopback socket, which without asocketpairAPI 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_GETPEERPIDto 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
socketpairAPI in WinSock.(Yes, you can queue a user-mode APC to a
selectthread, but there’s no well-defined method to cause theselectto 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 ofsocketpair()for Windows, we needed it in OpenConnect and I figured out how to make it work: https://gitlab.com/openconnect/openconnect/-/merge_requests/320/diffsThe 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 afterconnect().@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…
{64-bit CPU ticks in hex}-{Process ID}.$$$in%TMP%%WINDIR%\TempC:\TempAbout 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_UNIXsockets work fine, butconnectfails withWSAEINVALwith abstractAF_UNIXsockets.The entire reason I am doing this is so that I can implement my own
socketpairso I can interrupt aselect/WSAPollloop. So if you implementsocketpairin WinSock, that’d work, too.By the way, if you do implement
socketpairin Win32, please add something equivalent to Linux’sSOCK_CLOEXECto it, which would atomically set the socket as non-inheritable. (CompareWSASocket’sWSA_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?