VirtualBox

Ignore:
Timestamp:
May 26, 2009 10:06:21 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
47743
Message:

shared folders: prevent the guest to access parent directories

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedFolders/vbsf.cpp

    r19174 r20020  
    178178}
    179179
     180static int vbsfPathCheck(const char *pUtf8Path, size_t cbPath)
     181{
     182    int rc = VINF_SUCCESS;
     183
     184    /* The pUtf8Path is what the guest sent. Verify that the path is within the root.
     185     * Count '..' and other path components and check that we do not go over the root.
     186     */
     187
     188    size_t i = 0;
     189    int cComponents = 0; /* How many normal path components. */
     190    int cParentDirs = 0; /* How many '..' components. */
     191
     192    for (;;)
     193    {
     194        /* Skip leading path delimiters. */
     195        while (   i < cbPath
     196               && (pUtf8Path[i] == '\\' || pUtf8Path[i] == '/'))
     197            i++;
     198
     199        if (i >= cbPath)
     200            break;
     201
     202        /* Check if that is a dot component. */
     203        int cDots = 0;
     204        while (i < cbPath && pUtf8Path[i] == '.')
     205        {
     206            cDots++;
     207            i++;
     208        }
     209
     210        if (   cDots >= 2 /* Consider all multidots sequences as a 'parent dir'. */
     211            && (i >= cbPath || (pUtf8Path[i] == '\\' || pUtf8Path[i] == '/')))
     212        {
     213            cParentDirs++;
     214        }
     215        else if (   cDots == 1
     216                 && (i >= cbPath || (pUtf8Path[i] == '\\' || pUtf8Path[i] == '/')))
     217        {
     218            /* Single dot, nothing changes. */
     219        }
     220        else
     221        {
     222            /* Skip this component. */
     223            while (   i < cbPath
     224                   && (pUtf8Path[i] != '\\' && pUtf8Path[i] != '/'))
     225                i++;
     226
     227            cComponents++;
     228        }
     229
     230        Assert(i >= cbPath || (pUtf8Path[i] == '\\' || pUtf8Path[i] == '/'));
     231
     232        /* Verify counters for every component. */
     233        if (cParentDirs > cComponents)
     234        {
     235            rc = VERR_INVALID_NAME;
     236            break;
     237        }
     238    }
     239
     240    return rc;
     241}
     242
    180243static int vbsfBuildFullPath (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath,
    181244                              uint32_t cbPath, char **ppszFullPath, uint32_t *pcbFullPathRoot, bool fWildCard = false)
     
    200263        char *utf8Root;
    201264
    202         rc = RTUtf16ToUtf8 (pwszRoot, &utf8Root);
     265        /* Verify that the path is under the root directory. */
     266        rc = vbsfPathCheck((char *)&pPath->String.utf8[0], pPath->u16Length);
     267
     268        if (RT_SUCCESS (rc))
     269        {
     270            rc = RTUtf16ToUtf8 (pwszRoot, &utf8Root);
     271        }
     272
    203273        if (RT_SUCCESS (rc))
    204274        {
     
    345415                    AssertFailed();
    346416#ifdef RT_OS_DARWIN
    347                         RTMemFree(pPath);
    348                         pPath = pPathParameter;
     417                    RTMemFree(pPath);
     418                    pPath = pPathParameter;
    349419#endif
    350420                    return rc;
     
    352422
    353423                uint32_t l = (uint32_t)strlen (dst);
     424
     425                /* Verify that the path is under the root directory. */
     426                rc = vbsfPathCheck(dst, l);
     427
     428                if (RT_FAILURE(rc))
     429                {
     430#ifdef RT_OS_DARWIN
     431                    RTMemFree(pPath);
     432                    pPath = pPathParameter;
     433#endif
     434                    return rc;
     435                }
    354436
    355437                cb -= l;
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