VirtualBox

Changeset 100521 in vbox


Ignore:
Timestamp:
Jul 11, 2023 4:47:42 PM (17 months ago)
Author:
vboxsync
Message:

Main: separate function for parsing the entries in remote USB device list. bugref:10478

Location:
trunk/src/VBox/Main
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/RemoteUSBDeviceImpl.h

    r98103 r100521  
    5151
    5252    // public initializer/uninitializer for internal purposes only
    53     HRESULT init(uint32_t u32ClientId, VRDEUSBDEVICEDESC *pDevDesc, bool fDescExt);
     53    HRESULT init(uint32_t u32ClientId, VRDEUSBDEVICEDESC const *pDevDesc, bool fDescExt);
    5454    void uninit();
    5555
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r100038 r100521  
    1076510765}
    1076610766
     10767/* Make sure that the string is null-terminated and its size is less than cchMax bytes.
     10768 * Replace invalid UTF8 bytes with '?'.
     10769 */
     10770static int validateUtf8String(char *psz, size_t cchMax)
     10771{
     10772    for (;;)
     10773    {
     10774        RTUNICP Cp;
     10775        int vrc = RTStrGetCpNEx((const char **)&psz, &cchMax, &Cp);
     10776        if (RT_SUCCESS(vrc))
     10777        {
     10778            if (!Cp)
     10779                break;
     10780        }
     10781        else
     10782        {
     10783            if (!cchMax)
     10784                return VERR_END_OF_STRING;
     10785
     10786            psz[-1] = '?';
     10787        }
     10788    }
     10789    return VINF_SUCCESS;
     10790}
     10791
     10792static int validateRemoteUSBDeviceDesc(VRDEUSBDEVICEDESC const *e, uint32_t cbRemaining, bool fDescExt)
     10793{
     10794    uint32_t const cbDesc = fDescExt ? sizeof(VRDEUSBDEVICEDESCEXT) : sizeof(VRDEUSBDEVICEDESC);
     10795    if (cbDesc > cbRemaining)
     10796        return VERR_INVALID_PARAMETER;
     10797
     10798    if (   e->oNext         >  cbRemaining /* It is OK for oNext to point to the end of buffer. */
     10799        || e->oManufacturer >= cbRemaining
     10800        || e->oProduct      >= cbRemaining
     10801        || e->oSerialNumber >= cbRemaining)
     10802        return VERR_INVALID_PARAMETER;
     10803
     10804    int vrc;
     10805    if (e->oManufacturer)
     10806    {
     10807        vrc = validateUtf8String((char *)e + e->oManufacturer, cbRemaining - e->oManufacturer);
     10808        if (RT_FAILURE(vrc))
     10809            return VERR_INVALID_PARAMETER;
     10810    }
     10811    if (e->oProduct)
     10812    {
     10813        vrc = validateUtf8String((char *)e + e->oProduct, cbRemaining - e->oProduct);
     10814        if (RT_FAILURE(vrc))
     10815            return VERR_INVALID_PARAMETER;
     10816    }
     10817    if (e->oSerialNumber)
     10818    {
     10819        vrc = validateUtf8String((char *)e + e->oSerialNumber, cbRemaining - e->oSerialNumber);
     10820        if (RT_FAILURE(vrc))
     10821            return VERR_INVALID_PARAMETER;
     10822    }
     10823
     10824    return VINF_SUCCESS;
     10825}
     10826
    1076710827/**
    1076810828 * @note Locks this object for writing.
     
    1079910859     * Process the pDevList and add devices those are not already in the mRemoteUSBDevices list.
    1080010860     */
    10801     /** @todo (sunlover) REMOTE_USB Strict validation of the pDevList. */
    1080210861    VRDEUSBDEVICEDESC *e = pDevList;
    10803 
    10804     /* The cbDevList condition must be checked first, because the function can
     10862    uint32_t cbRemaining = cbDevList;
     10863
     10864    /* The cbRemaining condition must be checked first, because the function can
    1080510865     * receive pDevList = NULL and cbDevList = 0 on client disconnect.
    1080610866     */
    10807     while (cbDevList >= 2 && e->oNext)
    10808     {
    10809         /* Sanitize incoming strings in case they aren't valid UTF-8. */
    10810         if (e->oManufacturer)
    10811             RTStrPurgeEncoding((char *)e + e->oManufacturer);
    10812         if (e->oProduct)
    10813             RTStrPurgeEncoding((char *)e + e->oProduct);
    10814         if (e->oSerialNumber)
    10815             RTStrPurgeEncoding((char *)e + e->oSerialNumber);
     10867    while (cbRemaining >= sizeof(e->oNext) && e->oNext)
     10868    {
     10869        int const vrc = validateRemoteUSBDeviceDesc(e, cbRemaining, fDescExt);
     10870        if (RT_FAILURE(vrc))
     10871            break; /* Consider the rest of the list invalid too. */
    1081610872
    1081710873        LogFlowThisFunc(("vendor %04x, product %04x, name = %s\n",
     
    1087110927        }
    1087210928
    10873         if (cbDevList < e->oNext)
    10874         {
    10875             Log1WarningThisFunc(("cbDevList %d > oNext %d\n", cbDevList, e->oNext));
    10876             break;
    10877         }
    10878 
    10879         cbDevList -= e->oNext;
     10929        AssertBreak(cbRemaining >= e->oNext); /* validateRemoteUSBDeviceDesc ensures this. */
     10930        cbRemaining -= e->oNext;
    1088010931
    1088110932        e = (VRDEUSBDEVICEDESC *)((uint8_t *)e + e->oNext);
  • trunk/src/VBox/Main/src-client/RemoteUSBBackend.cpp

    r98278 r100521  
    11071107        RTMemFree(mpvDeviceList);
    11081108        mpvDeviceList = NULL;
    1109 
    1110         mcbDeviceList = cbList;
     1109        mcbDeviceList = 0;
    11111110
    11121111        if (cbList > 0)
    11131112        {
    11141113            mpvDeviceList = RTMemAlloc(cbList);
     1114            AssertPtrReturn(mpvDeviceList, VERR_NO_MEMORY);
     1115
    11151116            memcpy(mpvDeviceList, pvList, cbList);
     1117            mcbDeviceList = cbList;
    11161118        }
    11171119
  • trunk/src/VBox/Main/src-client/RemoteUSBDeviceImpl.cpp

    r98103 r100521  
    6565 * Initializes the remote USB device object.
    6666 */
    67 HRESULT RemoteUSBDevice::init(uint32_t u32ClientId, VRDEUSBDEVICEDESC *pDevDesc, bool fDescExt)
     67HRESULT RemoteUSBDevice::init(uint32_t u32ClientId, VRDEUSBDEVICEDESC const *pDevDesc, bool fDescExt)
    6868{
    6969    LogFlowThisFunc(("u32ClientId=%d,pDevDesc=%p\n", u32ClientId, pDevDesc));
Note: See TracChangeset for help on using the changeset viewer.

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