How to duplicate a socket for a target process from another user

I am facing an issue with WSADuplicateSocket which I am using to duplicate a socket for use by another process. It works when both processes are running under the same Windows user, but fails with error code 10022 (WSAEINVAL) when they are running under different users.

Specifically, the process calling WSADuplicateSocket runs under the administrator user account, and the target process runs under the system account.

Searching the internet, I found other links to this problem but no solutions. Does anyone know how to solve this?

Here's the current code:

bool Duplicate(
    SOCKET s,
    WSAPROTOCOL_INFO* pSocketInfo,
    int targetProcessID,
    int& errorNum
)
{
    memset(pSocketInfo, 0, sizeof(WSAPROTOCOL_INFO));
    if (::WSADuplicateSocket(s, targetProcessID, pSocketInfo)
        == SOCKET_ERROR)
    {
        errorNum = ::WSAGetLastError();
        return false;
    }
    return true;
}

      

+1


source to share


2 answers


Perhaps the target user (system) does not have network access? I think a dedicated service account (network service) was created around windows xp to separate services that need network access. have you checked your code for a user other than the system?



+1


source


MSDN is a bit unclear on this and I don't have time to test it myself, but maybe the socket descriptor, like a kernel object, has a security descriptor attached that doesn't allow anyone other than the creator to access it.



Try calling GetKernelObjectSecurity to check the ACLs attached to the handle, and then try calling SetKernelObjectSecurity to allow other users to access the handle. Maybe then WSADuplicateSocket will work correctly?

+1


source







All Articles