Changeset 3914


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.

Files:
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/libc-0.6/src/emx/include/InnoTekLIBC/backend.h

    r3859 r3914  
    189189/** Get the native path instead, no unix root translations. */
    190190#define __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE      0x10
     191/** Direct buffer mode for testing purposes.  */
     192#define __LIBC_BACKFS_FLAGS_RESOLVE_DIRECT_BUF  0x8000
    191193/** @} */
    192194
  • branches/libc-0.6/src/emx/src/lib/sys/b_fsPathResolve.c

    r3771 r3914  
    6565     */
    6666    int             fInUnixTree = 0;
    67     char            szNativePath[PATH_MAX];
    6867    unsigned int    fBackFsFlags = fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_FULL_MAYBE
    6968                                 ? BACKFS_FLAGS_RESOLVE_DIR_MAYBE | BACKFS_FLAGS_RESOLVE_FULL_MAYBE
    7069                                 : BACKFS_FLAGS_RESOLVE_DIR_MAYBE | BACKFS_FLAGS_RESOLVE_FULL;
    71     szNativePath[0] = szNativePath[1] = szNativePath[2] = szNativePath[3] = '\0';
    72     rc = __libc_back_fsResolve(pszPath, fBackFsFlags, szNativePath, &fInUnixTree);
     70    if (!(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_DIRECT_BUF))
     71    {
     72        char        szNativePath[PATH_MAX];
     73        szNativePath[0] = szNativePath[1] = szNativePath[2] = szNativePath[3] = '\0';
     74        rc = __libc_back_fsResolve(pszPath, fBackFsFlags, szNativePath, &fInUnixTree);
    7375
    74     /*
    75      * Copy the (half) result back to the caller.
    76      */
    77     char *pszSrc = &szNativePath[0];
    78     if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
    79         && fInUnixTree
    80         && *pszSrc)
     76        /*
     77         * Copy the (half) result back to the caller.
     78         */
     79        char *pszSrc = &szNativePath[0];
     80        if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
     81            && fInUnixTree
     82            && *pszSrc)
     83        {
     84            pszSrc += __libc_gcchUnixRoot;
     85            LIBC_ASSERTM(*pszSrc == '/', "bogus fInUnixTree flag! pszSrc='%s' whole thing is '%s'\n", pszSrc, szNativePath);
     86        }
     87        __libc_back_fsMutexRelease();
     88
     89        int cch = strlen(pszSrc) + 1;
     90        if (cch < cchBuf)
     91            memcpy(pszBuf, pszSrc, cchBuf);
     92        else if (!rc)
     93            rc = -ERANGE;
     94    }
     95    else
    8196    {
    82         pszSrc += __libc_gcchUnixRoot;
    83         LIBC_ASSERTM(*pszSrc == '/', "bogus fInUnixTree flag! pszSrc='%s' whole thing is '%s'\n", pszSrc, szNativePath);
     97        /*
     98         * Special case for testing purposes only.
     99         */
     100        if (cchBuf >= PATH_MAX)
     101        {
     102            rc = __libc_back_fsResolve(pszPath, fBackFsFlags, pszBuf, &fInUnixTree);
     103            if (   !(fFlags & __LIBC_BACKFS_FLAGS_RESOLVE_NATIVE)
     104                && fInUnixTree
     105                && pszBuf)
     106            {
     107                memmove(pszBuf, pszBuf + __libc_gcchUnixRoot, strlen(pszBuf) - __libc_gcchUnixRoot + 1);
     108                LIBC_ASSERTM(*pszBuf== '/', "bogus fInUnixTree flag! pszBuf='%s'\n", pszBuf);
     109            }
     110        }
     111        else
     112            rc = EINVAL;
     113
     114        __libc_back_fsMutexRelease();
    84115    }
    85     __libc_back_fsMutexRelease();
    86 
    87     int cch = strlen(pszSrc) + 1;
    88     if (cch < cchBuf)
    89         memcpy(pszBuf, pszSrc, cchBuf);
    90     else if (!rc)
    91         rc = -ERANGE;
    92116
    93117    if (!rc)
  • branches/libc-0.6/src/emx/src/lib/sys/fs.c

    r3912 r3914  
    10131013            {
    10141014                if ((uintptr_t)(pszUserPath - pachBuffer) > SIZEOF_ACHBUFFER)
    1015                     pszUserPath = strcpy(pachBuffer, pszUserPath);
     1015                {
     1016                    size_t cbUserPath = strlen(pszUserPath) + 1;
     1017                    if (cbUserPath > PATH_MAX)
     1018                    {
     1019                        rcRet = -ENAMETOOLONG;
     1020                        break;
     1021                    }
     1022                    pszUserPath = memcpy(pachBuffer, pszUserPath, cbUserPath);
     1023                }
    10161024                *(char *)(void *)pszUserPath += 'A' - 'a';
    10171025            }
  • 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.