VirtualBox

Changeset 47083 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jul 10, 2013 3:38:57 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
87134
Message:

IPRT/localipc-win.cpp: Update, added testcase (work in progress).

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/win/localipc-win.cpp

    r47051 r47083  
    178178
    179179/**
     180 * Builds and allocates the security descriptor required for securing the local pipe.
     181 *
     182 * @return  IPRT status code.
     183 * @param   ppDesc              Where to store the allocated security descriptor on success.
     184 *                              Must be free'd using LocalFree().
     185 */
     186static int rtLocalIpcServerWinAllocSecurityDescriptior(PSECURITY_DESCRIPTOR *ppDesc)
     187{
     188    /** @todo Stuff this into RTInitOnce? Later. */
     189    PFNCONVERTSTRINGSECURITYDESCRIPTORTOSECURITYDESCRIPTOR
     190        pfnConvertStringSecurityDescriptorToSecurityDescriptor = NULL;
     191
     192    RTLDRMOD hAdvApi32 = NIL_RTLDRMOD;
     193    int rc = RTLdrLoadSystem("Advapi32.dll", true /*fNoUnload*/, &hAdvApi32);
     194    if (RT_SUCCESS(rc))
     195        rc = RTLdrGetSymbol(hAdvApi32, "ConvertStringSecurityDescriptorToSecurityDescriptorW",
     196                            (void**)&pfnConvertStringSecurityDescriptorToSecurityDescriptor);
     197
     198    PSECURITY_DESCRIPTOR pSecDesc = NULL;
     199    if (RT_SUCCESS(rc))
     200    {
     201        AssertPtr(pfnConvertStringSecurityDescriptorToSecurityDescriptor);
     202
     203        /*
     204         * We'll create a security descriptor from a SDDL that denies
     205         * access to network clients (this is local IPC after all), it
     206         * makes some further restrictions to prevent non-authenticated
     207         * users from screwing around.
     208         */
     209        PRTUTF16 pwszSDDL;
     210        rc = RTStrToUtf16(RTLOCALIPC_WIN_SDDL, &pwszSDDL);
     211        if (RT_SUCCESS(rc))
     212        {
     213            if (!pfnConvertStringSecurityDescriptorToSecurityDescriptor((LPCTSTR)pwszSDDL,
     214                                                                        SDDL_REVISION_1,
     215                                                                        &pSecDesc,
     216                                                                        NULL))
     217            {
     218                rc = RTErrConvertFromWin32(GetLastError());
     219            }
     220
     221            RTUtf16Free(pwszSDDL);
     222        }
     223    }
     224    else
     225    {
     226        /* Windows OSes < W2K SP2 not supported for now, bail out. */
     227        /** @todo Implement me! */
     228        rc = VERR_NOT_SUPPORTED;
     229    }
     230
     231    if (hAdvApi32 != NIL_RTLDRMOD)
     232         RTLdrClose(hAdvApi32);
     233
     234    if (RT_SUCCESS(rc))
     235    {
     236        AssertPtr(pSecDesc);
     237        *ppDesc = pSecDesc;
     238    }
     239
     240    return rc;
     241}
     242
     243/**
    180244 * Creates a named pipe instance.
    181245 *
     
    193257    *phNmPipe = INVALID_HANDLE_VALUE;
    194258
    195     /** @todo Stuff this into RTInitOnce. Later. */
    196     PFNCONVERTSTRINGSECURITYDESCRIPTORTOSECURITYDESCRIPTOR
    197         pfnConvertStringSecurityDescriptorToSecurityDescriptor = NULL;
    198 
    199     RTLDRMOD hAdvApi32 = NIL_RTLDRMOD;
    200     int rc = RTLdrLoadSystem("Advapi32.lib", true /*fNoUnload*/, &hAdvApi32);
     259    PSECURITY_DESCRIPTOR pSecDesc;
     260    int rc = rtLocalIpcServerWinAllocSecurityDescriptior(&pSecDesc);
    201261    if (RT_SUCCESS(rc))
    202         rc = RTLdrGetSymbol(hAdvApi32, "ConvertStringSecurityDescriptorToSecurityDescriptor",
    203                             (void**)&pfnConvertStringSecurityDescriptorToSecurityDescriptor);
    204 
    205     PSECURITY_DESCRIPTOR pSecDesc = NULL;
    206     if (RT_SUCCESS(rc))
    207     {
    208         AssertPtr(pfnConvertStringSecurityDescriptorToSecurityDescriptor);
    209 
    210         /*
    211          * We'll create a security descriptor from a SDDL that denies
    212          * access to network clients (this is local IPC after all), it
    213          * makes some further restrictions to prevent non-authenticated
    214          * users from screwing around.
    215          */
    216         if (!pfnConvertStringSecurityDescriptorToSecurityDescriptor(RTLOCALIPC_WIN_SDDL,
    217                                                                     SDDL_REVISION_1,
    218                                                                     &pSecDesc,
    219                                                                     NULL))
    220         {
    221             rc = RTErrConvertFromWin32(GetLastError());
    222         }
    223     }
    224     else
    225     {
    226         /* Windows OSes < W2K SP2 not supported for now, bail out. */
    227         /** @todo Implement me! */
    228         rc = VERR_NOT_SUPPORTED;
    229     }
    230 
    231     if (hAdvApi32 != NIL_RTLDRMOD)
    232          RTLdrClose(hAdvApi32);
    233 
    234     if (RT_SUCCESS(rc))
    235262    {
    236263        SECURITY_ATTRIBUTES SecAttrs;
    237         SecAttrs.nLength = sizeof(SecAttrs);
     264        SecAttrs.nLength = sizeof(SECURITY_ATTRIBUTES);
    238265        SecAttrs.lpSecurityDescriptor = pSecDesc;
    239266        SecAttrs.bInheritHandle = FALSE;
     
    276303                                         PAGE_SIZE,                     /* nInBufferSize (ditto) */
    277304                                         30*1000,                       /* nDefaultTimeOut = 30 sec */
    278                                          &SecAttrs);                    /* lpSecurityAttributes */
    279 
     305                                         NULL /** @todo !!! Fix this !!! &SecAttrs */);                    /* lpSecurityAttributes */
    280306        LocalFree(pSecDesc);
    281307        if (hNmPipe != INVALID_HANDLE_VALUE)
     
    300326    AssertReturn(*pszName, VERR_INVALID_PARAMETER);
    301327    AssertReturn(!(fFlags & ~(RTLOCALIPC_FLAGS_VALID_MASK)), VERR_INVALID_PARAMETER);
    302 
    303     AssertReturn(fFlags & RTLOCALIPC_FLAGS_MULTI_SESSION, VERR_NOT_IMPLEMENTED); /** @todo implement !RTLOCALIPC_FLAGS_MULTI_SESSION */
     328    AssertReturn((fFlags & RTLOCALIPC_FLAGS_MULTI_SESSION), VERR_INVALID_PARAMETER); /** @todo Implement !RTLOCALIPC_FLAGS_MULTI_SESSION */
    304329
    305330    /*
     
    448473
    449474        RTCritSectEnter(&pThis->CritSect);
    450         if (    !pThis->fCancelled /* Event signalled but not cancelled? */
    451             &&  pThis->u32Magic == RTLOCALIPCSERVER_MAGIC)
     475        if (   !pThis->fCancelled /* Event signalled but not cancelled? */
     476            && pThis->u32Magic == RTLOCALIPCSERVER_MAGIC)
    452477        {
    453478            /*
     
    462487            {
    463488                HANDLE hNmPipe;
    464                 DWORD err = rtLocalIpcServerWinCreatePipeInstance(&hNmPipe, pThis->szName, false /* fFirst */);
    465                 if (err == NO_ERROR)
     489                rc = rtLocalIpcServerWinCreatePipeInstance(&hNmPipe, pThis->szName, false /* fFirst */);
     490                if (RT_SUCCESS(rc))
    466491                {
    467492                    HANDLE hNmPipeSession = pThis->hNmPipe; /* consumed */
     
    475500                     * the client and fail. Don't try service the client here.
    476501                     */
    477                     rc = RTErrConvertFromWin32(err);
    478502                    fRc = DisconnectNamedPipe(pThis->hNmPipe);
    479503                    AssertMsg(fRc, ("%d\n", GetLastError()));
     
    550574{
    551575    AssertPtrReturn(phClientSession, VERR_INVALID_POINTER);
    552     AssertReturn(hNmPipeSession != INVALID_HANDLE_VALUE, VERR_INVALID_PARAMETER);
     576    AssertReturn(hNmPipeSession != INVALID_HANDLE_VALUE, VERR_INVALID_HANDLE);
    553577
    554578    int rc;
     
    598622    AssertPtrReturn(phSession, VERR_INVALID_POINTER);
    599623    AssertPtrReturn(pszName, VERR_INVALID_POINTER);
     624    AssertReturn(*pszName, VERR_INVALID_PARAMETER);
    600625    AssertReturn(!fFlags, VERR_INVALID_PARAMETER); /* Flags currently unused, must be 0. */
    601626
     
    626651            if (RTStrAPrintf(&pszPipe, "%s%s", RTLOCALIPC_WIN_PREFIX, pszName))
    627652            {
    628                 HANDLE hPipe = CreateFile(pszPipe,                  /* pipe name */
    629                                           GENERIC_READ |            /* read and write access */
    630                                           GENERIC_WRITE,
    631                                           0,                        /* no sharing */
    632                                           NULL,                     /* default security attributes */
    633                                           OPEN_EXISTING,            /* opens existing pipe */
    634                                           FILE_FLAG_OVERLAPPED,     /* default attributes */
    635                                           NULL);                    /* no template file */
    636                 RTStrFree(pszPipe);
    637                 if (hPipe != INVALID_HANDLE_VALUE)
     653                PSECURITY_DESCRIPTOR pSecDesc;
     654                rc = rtLocalIpcServerWinAllocSecurityDescriptior(&pSecDesc);
     655                if (RT_SUCCESS(rc))
    638656                {
    639                     pThis->hNmPipe = hPipe;
    640                     return VINF_SUCCESS;
     657                    SECURITY_ATTRIBUTES SecAttrs;
     658                    SecAttrs.nLength = sizeof(SECURITY_ATTRIBUTES);
     659                    SecAttrs.lpSecurityDescriptor = pSecDesc;
     660                    SecAttrs.bInheritHandle = FALSE;
     661
     662                    HANDLE hPipe = CreateFile(pszPipe,                  /* pipe name */
     663                                              GENERIC_READ |            /* read and write access */
     664                                              GENERIC_WRITE,
     665                                              0,                        /* no sharing */
     666                                              &SecAttrs,                /* default security attributes */
     667                                              OPEN_EXISTING,            /* opens existing pipe */
     668                                              FILE_FLAG_OVERLAPPED,     /* default attributes */
     669                                              NULL);                    /* no template file */
     670                    LocalFree(pSecDesc);
     671                    RTStrFree(pszPipe);
     672
     673                    if (hPipe != INVALID_HANDLE_VALUE)
     674                    {
     675                        pThis->hNmPipe = hPipe;
     676                        *phSession = pThis;
     677                        return VINF_SUCCESS;
     678                    }
     679                    else
     680                        rc = RTErrConvertFromWin32(GetLastError());
    641681                }
    642                 else
    643                     rc = RTErrConvertFromWin32(GetLastError());
    644682            }
    645683            else
     
    690728    if (hSession == NIL_RTLOCALIPCSESSION)
    691729        return VINF_SUCCESS;
    692     PRTLOCALIPCSESSIONINT pThis = (RTLOCALIPCSESSION)hSession;
     730    PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
    693731    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    694     AssertReturn(pThis->u32Magic != RTLOCALIPCSESSION_MAGIC, VERR_INVALID_MAGIC);
     732    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_MAGIC);
    695733
    696734    /*
     
    719757RTDECL(int) RTLocalIpcSessionRead(RTLOCALIPCSESSION hSession, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
    720758{
    721     PRTLOCALIPCSESSIONINT pThis = hSession;
     759    PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
    722760    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    723761    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
     
    867905RTDECL(int) RTLocalIpcSessionWrite(RTLOCALIPCSESSION hSession, const void *pvBuffer, size_t cbBuffer)
    868906{
    869     PRTLOCALIPCSESSIONINT pThis = hSession;
     907    PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
    870908    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    871909    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
     
    9641002RTDECL(int) RTLocalIpcSessionWaitForData(RTLOCALIPCSESSION hSession, uint32_t cMillies)
    9651003{
    966     PRTLOCALIPCSESSIONINT pThis = hSession;
     1004    PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
    9671005    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    9681006    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
     
    9751013        {
    9761014            pThis->cRefs++;
     1015            pThis->fCancelled = false; /* Reset canellation status. */
    9771016            RTCritSectLeave(&pThis->CritSect);
    9781017
     
    9921031            else if (dwRc == WAIT_FAILED)
    9931032                rc = RTErrConvertFromWin32(GetLastError());
     1033            else if (pThis->fCancelled)
     1034                rc = VERR_CANCELLED;
    9941035
    9951036            pThis->cRefs--;
     
    10061047RTDECL(int) RTLocalIpcSessionCancel(RTLOCALIPCSESSION hSession)
    10071048{
    1008     return VINF_SUCCESS;
     1049    PRTLOCALIPCSESSIONINT pThis = (PRTLOCALIPCSESSIONINT)hSession;
     1050    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
     1051    AssertReturn(pThis->u32Magic == RTLOCALIPCSESSION_MAGIC, VERR_INVALID_HANDLE);
     1052
     1053    /*
     1054     * Enter the critical section, then set the cancellation flag
     1055     * and signal the event (to wake up anyone in/at WaitForSingleObject).
     1056     */
     1057    int rc = RTCritSectEnter(&pThis->CritSect);
     1058    if (RT_SUCCESS(rc))
     1059    {
     1060        ASMAtomicUoWriteBool(&pThis->fCancelled, true);
     1061        BOOL fRc = SetEvent(pThis->hEvent);
     1062        AssertMsg(fRc, ("%d\n", GetLastError())); NOREF(fRc);
     1063
     1064        RTCritSectLeave(&pThis->CritSect);
     1065    }
     1066
     1067    return rc;
    10091068}
    10101069
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r46638 r47083  
    8080        tstLdrLoad \
    8181        tstRTList \
     82        tstRTLocalIpc \
    8283        tstRTLockValidator \
    8384        tstLog \
     
    400401tstRTList_SOURCES = tstRTList.cpp
    401402
     403tstRTLocalIpc_TEMPLATE = VBOXR3TSTEXE
     404tstRTLocalIpc_SOURCES = tstRTLocalIpc.cpp
     405
    402406tstRTLockValidator_TEMPLATE = VBOXR3TSTEXE
    403407tstRTLockValidator_SOURCES = tstRTLockValidator.cpp
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette