Changeset 3914 for trunk


Ignore:
Timestamp:
Oct 24, 2014, 4:01:38 PM (11 years ago)
Author:
bird
Message:

trunk,0.6: Fixed buffer overflow in fsResolveUnix that would trigger if the input path was too long.

Location:
trunk/libc
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/libc/include/klibc/backend.h

    r3897 r3914  
    265265/** Get the native path instead, no unix root translations. */
    266266#define __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE      0x10
     267/** Direct buffer mode for testing purposes.  */
     268#define __LIBC_BACKFS_FLAGS_RESOLVE_DIRECT_BUF  0x8000
    267269/** @} */
    268270
  • trunk/libc/src/kNIX/b_fsPathResolve.c

    r3863 r3914  
    5353     */
    5454    int             fInUnixTree = 0;
    55     char            szNativePath[PATH_MAX];
    5655    unsigned int    fBackFsFlags = fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_FULL_MAYBE
    5756                                 ? BACKFS_FLAGS_RESOLVE_DIR_MAYBE | BACKFS_FLAGS_RESOLVE_FULL_MAYBE
    5857                                 : BACKFS_FLAGS_RESOLVE_DIR_MAYBE | BACKFS_FLAGS_RESOLVE_FULL;
    59     szNativePath[0] = szNativePath[1] = szNativePath[2] = szNativePath[3] = '\0';
    60     rc = __libc_back_fsResolve(pszPath, fBackFsFlags, szNativePath, &fInUnixTree);
     58    if (!(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_DIRECT_BUF))
     59    {
     60        char            szNativePath[PATH_MAX];
     61        szNativePath[0] = szNativePath[1] = szNativePath[2] = szNativePath[3] = '\0';
     62        rc = __libc_back_fsResolve(pszPath, fBackFsFlags, szNativePath, &fInUnixTree);
    6163
    62     /*
    63      * Copy the (half) result back to the caller.
    64      */
    65     char *pszSrc = &szNativePath[0];
    66     if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
    67         && fInUnixTree
    68         && *pszSrc)
     64        /*
     65         * Copy the (half) result back to the caller.
     66         */
     67        char *pszSrc = &szNativePath[0];
     68        if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
     69            && fInUnixTree
     70            && *pszSrc)
     71        {
     72            pszSrc += __libc_gcchUnixRoot;
     73            LIBC_ASSERTM(*pszSrc == '/', "bogus fInUnixTree flag! pszSrc='%s' whole thing is '%s'\n", pszSrc, szNativePath);
     74        }
     75        __libc_back_fsMutexRelease();
     76
     77        int cch = strlen(pszSrc) + 1;
     78        if (cch < cchBuf)
     79            memcpy(pszBuf, pszSrc, cchBuf);
     80        else if (!rc)
     81            rc = -ERANGE;
     82    }
     83    else
    6984    {
    70         pszSrc += __libc_gcchUnixRoot;
    71         LIBC_ASSERTM(*pszSrc == '/', "bogus fInUnixTree flag! pszSrc='%s' whole thing is '%s'\n", pszSrc, szNativePath);
    72     }
    73     __libc_back_fsMutexRelease();
     85        /*
     86         * Special case for testing purposes only.
     87         */
     88        if (cchBuf >= PATH_MAX)
     89        {
     90            rc = __libc_back_fsResolve(pszPath, fBackFsFlags, pszBuf, &fInUnixTree);
     91            if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
     92                && fInUnixTree
     93                && pszBuf)
     94            {
     95                memmove(pszBuf, pszBuf + __libc_gcchUnixRoot, strlen(pszBuf) - __libc_gcchUnixRoot + 1);
     96                LIBC_ASSERTM(*pszBuf== '/', "bogus fInUnixTree flag! pszBuf='%s'\n", pszBuf);
     97            }
     98        }
     99        else
     100            rc = EINVAL;
    74101
    75     int cch = strlen(pszSrc) + 1;
    76     if (cch < cchBuf)
    77         memcpy(pszBuf, pszSrc, cchBuf);
    78     else if (!rc)
    79         rc = -ERANGE;
     102        __libc_back_fsMutexRelease();
     103     }
    80104
    81105    if (!rc)
  • trunk/libc/src/kNIX/os2/fs-os2.c

    r3912 r3914  
    978978            {
    979979                if ((uintptr_t)(pszUserPath - pachBuffer) > SIZEOF_ACHBUFFER)
    980                     pszUserPath = strcpy(pachBuffer, pszUserPath);
     980                {
     981                    size_t cbUserPath = strlen(pszUserPath) + 1;
     982                    if (cbUserPath > PATH_MAX)
     983                    {
     984                        rcRet = -ENAMETOOLONG;
     985                        break;
     986                    }
     987                    pszUserPath = memcpy(pachBuffer, pszUserPath, cbUserPath);
     988                }
    981989                *(char *)(void *)pszUserPath += 'A' - 'a';
    982990            }
  • trunk/libc/tests/libc/Makefile

    r3813 r3914  
    158158        io/sprintf-1.c \
    159159        io/sscanf-1.c \
    160         io/pathresolving-1.c
     160        io/pathresolving-1.c \
     161        io/pathresolving-2.c
    161162
    162163
Note: See TracChangeset for help on using the changeset viewer.