VirtualBox

Changeset 93393 in vbox for trunk/src/VBox/VMM/VMMR3


Ignore:
Timestamp:
Jan 21, 2022 11:54:15 AM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
149489
Message:

VMM/CFGM: Use the RTMemSafer allocator for the password strings and scramble them while not being accessed. bugref:9469

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/VMMR3/CFGM.cpp

    r93392 r93393  
    6868#include <iprt/assert.h>
    6969#include <iprt/mem.h>
     70#include <iprt/memsafer.h>
    7071#include <iprt/param.h>
    7172#include <iprt/string.h>
     
    876877 * @param   pszString       Where to store the string.
    877878 * @param   cchString       Size of the string buffer. (Includes terminator.)
     879 *
     880 * @note    Concurrent calls to this function and CFGMR3QueryPasswordDef are not
     881 *          supported.
    878882 */
    879883VMMR3DECL(int) CFGMR3QueryPassword(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString)
     
    888892            if (cchString >= cbSrc)
    889893            {
     894                RTMemSaferUnscramble(pLeaf->Value.String.psz, cbSrc);
    890895                memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
    891896                memset(pszString + cbSrc, 0, cchString - cbSrc);
     897                RTMemSaferScramble(pLeaf->Value.String.psz, cbSrc);
     898
     899                Assert(pszString[cbSrc - 1] == '\0');
    892900            }
    893901            else
     
    910918 * @param   cchString       Size of the string buffer. (Includes terminator.)
    911919 * @param   pszDef          The default value.
     920 *
     921 * @note    Concurrent calls to this function and CFGMR3QueryPassword are not
     922 *          supported.
    912923 */
    913924VMMR3DECL(int) CFGMR3QueryPasswordDef(PCFGMNODE pNode, const char *pszName, char *pszString, size_t cchString, const char *pszDef)
     
    922933            if (cchString >= cbSrc)
    923934            {
     935                RTMemSaferUnscramble(pLeaf->Value.String.psz, cbSrc);
    924936                memcpy(pszString, pLeaf->Value.String.psz, cbSrc);
    925937                memset(pszString + cbSrc, 0, cchString - cbSrc);
     938                RTMemSaferScramble(pLeaf->Value.String.psz, cbSrc);
     939
     940                Assert(pszString[cbSrc - 1] == '\0');
    926941            }
    927942            else
     
    21152130                break;
    21162131
    2117             case CFGMVALUETYPE_PASSWORD:
    2118                 RTMemWipeThoroughly(pLeaf->Value.String.psz, pLeaf->Value.String.cb, 10);
    2119                 RT_FALL_THROUGH();
    21202132            case CFGMVALUETYPE_STRING:
    21212133                cfgmR3StrFree(pVM, pLeaf->Value.String.psz);
     
    21242136                break;
    21252137
     2138            case CFGMVALUETYPE_PASSWORD:
     2139                RTMemSaferFree(pLeaf->Value.String.psz, pLeaf->Value.String.cb);
     2140                pLeaf->Value.String.psz = NULL;
     2141                pLeaf->Value.String.cb = 0;
     2142                break;
     2143
    21262144            case CFGMVALUETYPE_INTEGER:
    21272145                break;
     
    21712189
    21722190/**
    2173  * Inserts a new string value. This variant expects that the caller know the length
    2174  * of the string already so we can avoid calling strlen() here.
     2191 * Inserts a new string value.
     2192 *
     2193 * This variant expects that the caller know the length of the string already so
     2194 * we can avoid calling strlen() here.
    21752195 *
    21762196 * @returns VBox status code.
     
    22222242
    22232243/**
    2224  * Inserts a new string value. Calls strlen(pszString) internally; if you know the
    2225  * length of the string, CFGMR3InsertStringLengthKnown() is faster.
     2244 * Inserts a new string value.
     2245 *
     2246 * Calls strlen(pszString) internally; if you know the length of the string,
     2247 * CFGMR3InsertStringLengthKnown() is faster.
    22262248 *
    22272249 * @returns VBox status code.
     
    23272349
    23282350/**
    2329  * Inserts a new integer value.
     2351 * Inserts a new bytes value.
    23302352 *
    23312353 * @returns VBox status code.
     
    23782400
    23792401/**
    2380  * Inserts a new password value. This variant expects that the caller know the length
    2381  * of the password string already so we can avoid calling strlen() here.
     2402 * Inserts a new password value.
     2403 *
     2404 * This variant expects that the caller know the length of the password string
     2405 * already so we can avoid calling strlen() here.
    23822406 *
    23832407 * @returns VBox status code.
     
    23852409 * @param   pszName         Value name.
    23862410 * @param   pszString       The value. Must not be NULL.
    2387  * @param   cchString       The length of the string excluding the
    2388  *                          terminator.
     2411 * @param   cchString       The length of the string excluding the terminator.
    23892412 */
    23902413VMMR3DECL(int) CFGMR3InsertPasswordN(PCFGMNODE pNode, const char *pszName, const char *pszString, size_t cchString)
     
    23962419    {
    23972420        /*
    2398          * Allocate string object first.
     2421         * Allocate string object first using the safer memory API since this
     2422         * is considered sensitive information.
    23992423         */
    2400         char *pszStringCopy = (char *)cfgmR3StrAlloc(pNode->pVM, MM_TAG_CFGM_STRING, cchString + 1);
     2424        char *pszStringCopy = (char *)RTMemSaferAllocZ(cchString + 1);
    24012425        if (pszStringCopy)
    24022426        {
    24032427            memcpy(pszStringCopy, pszString, cchString);
    24042428            pszStringCopy[cchString] = '\0';
     2429            RTMemSaferScramble(pszStringCopy, cchString + 1);
    24052430
    24062431            /*
     
    24162441            }
    24172442            else
    2418             {
    2419                 RTMemWipeThoroughly(pszStringCopy, cchString, 10);
    2420                 cfgmR3StrFree(pNode->pVM, pszStringCopy);
    2421             }
     2443                RTMemSaferFree(pszStringCopy, cchString + 1);
    24222444        }
    24232445        else
     
    24322454
    24332455/**
    2434  * Inserts a new password value. Calls strlen(pszString) internally; if you know the
    2435  * length of the string, CFGMR3InsertStringLengthKnown() is faster.
     2456 * Inserts a new password value.
     2457 *
     2458 * Calls strlen(pszString) internally; if you know the length of the string,
     2459 * CFGMR3InsertStringLengthKnown() is faster.
    24362460 *
    24372461 * @returns VBox status code.
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