Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix_win.c: is_socket(int fd) fails at any socket whose value also exists as a file descriptor #1069

Open
datadiode opened this issue Jul 9, 2024 · 0 comments

Comments

@datadiode
Copy link

datadiode commented Jul 9, 2024

In such situation, v3.8.1+ assumes that fd is not a socket but a file descriptor,
while earlier versions always assumed fd to be a socket in first place.

In other words, v3.8.0 and earlier gave a socket precedence over a file descriptor with the same value,
while v3.8.1 and later give a file descriptor precedence over a socket with the same value.

The discussion at #266 already outlines possible fixes.

Code snippet to likely produce a socket vs. file descriptor conflict:

#include <io.h>
#include <stdio.h>
#include <WinSock2.h>

int main(int argc, const char *argv[])
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    int sd = (int)socket(AF_INET, SOCK_STREAM, 0);

    // Use up file descriptors until fopen() fails
    int fd = -1;
    while (FILE* f = fopen(argv[0], "rb"))
        fd = _fileno(f);

    intptr_t sh = _get_osfhandle(sd);
    intptr_t fh = _get_osfhandle(fd);

    printf("The operating-system file handle associated with fd is %p\n", fh);
    printf("The operating-system file handle associated with sd is %p\n", sh);

    return 0;
}

Employing _dup2() to achieve the same:

#include <io.h>
#include <stdio.h>
#include <WinSock2.h>

int main(int argc, const char *argv[])
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);

    int sd = (int)socket(AF_INET, SOCK_STREAM, 0);

    // Duplicate stdout descriptor to produce a collision with the socket
    _dup2(1, sd);

    intptr_t sh = _get_osfhandle(sd);

    printf("The operating-system file handle associated with sd is %p\n", sh);

    return 0;
}
datadiode added a commit to datadiode/portable that referenced this issue Jul 17, 2024
…n() distinguishable from sockets by having them take odd values only
datadiode added a commit to datadiode/portable that referenced this issue Jul 19, 2024
…rom socketpair() to gain evidence whether issue libressl#1069 is fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant