VirtualBox

Changeset 91535 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Oct 4, 2021 9:15:19 AM (3 years ago)
Author:
vboxsync
Message:

Main/{NvramStore,UefiVariableStore}: Always retain/releas the variable store reference in every externally accessible API to make sure the variable store is flushed to the NVRAM when saving avoiding data loss, bugref:9580

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

Legend:

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

    r91434 r91535  
    7272
    7373    int i_loadStore(const char *pszPath);
    74     int i_loadStoreFromTar(RTVFSFSSTREAM hVfsFssTar);
    7574    int i_saveStore(void);
    76     int i_saveStoreAsTar(void);
     75
     76#ifndef VBOX_COM_INPROC
     77    HRESULT i_retainUefiVarStore(PRTVFS phVfs, bool fReadonly);
     78    HRESULT i_releaseUefiVarStore(RTVFS hVfs);
     79#endif
    7780
    7881private:
     
    8487    // Wrapped NVRAM store members
    8588    HRESULT initUefiVariableStore(ULONG aSize);
     89
     90    int i_loadStoreFromTar(RTVFSFSSTREAM hVfsFssTar);
     91    int i_saveStoreAsTar(void);
    8692
    8793#ifdef VBOX_COM_INPROC
  • trunk/src/VBox/Main/include/UefiVariableStoreImpl.h

    r91457 r91535  
    4141
    4242    // public initializer/uninitializer for internal purposes only
    43     HRESULT init(NvramStore *aParent, Machine *pMachine, RTVFS hVfsUefiVarStore);
     43    HRESULT init(NvramStore *aParent, Machine *pMachine);
    4444    void uninit();
    4545
     
    7474    void i_uefiAttrMaskToVec(uint32_t fAttr, std::vector<UefiVariableAttributes_T> &aAttributes);
    7575
     76    HRESULT i_retainUefiVariableStore(bool fReadonly);
     77    HRESULT i_releaseUefiVariableStore(void);
     78
    7679    HRESULT i_uefiVarStoreAddVar(PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr, PRTVFSFILE phVfsFile);
    7780    HRESULT i_uefiVarStoreSetVar(PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr, const void *pvData, size_t cbData);
  • trunk/src/VBox/Main/src-all/NvramStoreImpl.cpp

    r91497 r91535  
    339339            if (it != m->bd->mapNvram.end())
    340340            {
    341                 RTVFSFILE hVfsFileNvram = it->second;
    342                 RTVFS hVfsEfiVarStore;
    343                 int vrc = RTEfiVarStoreOpenAsVfs(hVfsFileNvram, 0 /*fMntFlags*/, 0 /*fVarStoreFlags*/, &hVfsEfiVarStore,
    344                                                  NULL /*pErrInfo*/);
    345                 if (RT_SUCCESS(vrc))
    346                 {
    347                     unconst(m->pUefiVarStore).createObject();
    348                     m->pUefiVarStore->init(this, m->pParent, hVfsEfiVarStore);
    349                 }
    350                 else
    351                     hrc = setError(E_FAIL, tr("Opening the UEFI variable store failed (%Rrc)."), vrc);
     341                unconst(m->pUefiVarStore).createObject();
     342                m->pUefiVarStore->init(this, m->pParent);
    352343            }
    353344            else
     
    710701
    711702#ifndef VBOX_COM_INPROC
     703HRESULT NvramStore::i_retainUefiVarStore(PRTVFS phVfs, bool fReadonly)
     704{
     705    /* the machine needs to be mutable */
     706    AutoMutableStateDependency adep(m->pParent);
     707    if (FAILED(adep.rc())) return adep.rc();
     708
     709    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     710
     711    HRESULT hrc = S_OK;
     712    NvramStoreIter it = m->bd->mapNvram.find("efi/nvram");
     713    if (it != m->bd->mapNvram.end())
     714    {
     715        RTVFSFILE hVfsFileNvram = it->second;
     716        RTVFS hVfsEfiVarStore;
     717        uint32_t fMntFlags = fReadonly ? RTVFSMNT_F_READ_ONLY : 0;
     718
     719        int vrc = RTEfiVarStoreOpenAsVfs(hVfsFileNvram, fMntFlags, 0 /*fVarStoreFlags*/, &hVfsEfiVarStore,
     720                                         NULL /*pErrInfo*/);
     721        if (RT_SUCCESS(vrc))
     722        {
     723            *phVfs = hVfsEfiVarStore;
     724            if (!fReadonly)
     725                m->pParent->i_setModified(Machine::IsModified_NvramStore);
     726        }
     727        else
     728            hrc = setError(E_FAIL, tr("Opening the UEFI variable store failed (%Rrc)."), vrc);
     729    }
     730    else
     731        hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("The UEFI NVRAM file is not existing for this machine."));
     732
     733    return hrc;
     734}
     735
     736
     737HRESULT NvramStore::i_releaseUefiVarStore(RTVFS hVfs)
     738{
     739    RTVfsRelease(hVfs);
     740    return S_OK;
     741}
     742
     743
    712744/**
    713745 *  Loads settings from the given machine node.
     
    753785
    754786    data.strNvramPath = m->bd->strNvramPath;
    755 
    756     unconst(m->pUefiVarStore) = NULL;
    757787
    758788    int vrc = i_saveStore();
  • trunk/src/VBox/Main/src-server/UefiVariableStoreImpl.cpp

    r91490 r91535  
    8989 * @param   aParent                     The NVRAM store owning the UEFI NVRAM content.
    9090 * @param   pMachine
    91  * @param   hVfsUefiVarStore            The UEFI variable store VFS handle.
    92  */
    93 HRESULT UefiVariableStore::init(NvramStore *aParent, Machine *pMachine, RTVFS hVfsUefiVarStore)
     91 */
     92HRESULT UefiVariableStore::init(NvramStore *aParent, Machine *pMachine)
    9493{
    9594    LogFlowThisFuncEnter();
     
    105104
    106105    /* share the parent weakly */
    107     unconst(m->pParent) = aParent;
     106    unconst(m->pParent)  = aParent;
    108107    unconst(m->pMachine) = pMachine;
    109     m->hVfsUefiVarStore = hVfsUefiVarStore;
     108    m->hVfsUefiVarStore  = NIL_RTVFS;
    110109
    111110    autoInitSpan.setSucceeded();
     
    129128        return;
    130129
    131     RTVfsRelease(m->hVfsUefiVarStore);
     130    Assert(m->hVfsUefiVarStore == NIL_RTVFS);
    132131
    133132    unconst(m->pParent) = NULL;
     
    147146    if (FAILED(adep.rc())) return adep.rc();
    148147
     148    HRESULT hrc = i_retainUefiVariableStore(true /*fReadonly*/);
     149    if (FAILED(hrc)) return hrc;
     150
    149151    AutoReadLock rlock(this COMMA_LOCKVAL_SRC_POS);
    150152
    151     HRESULT hrc = S_OK;
    152153    uint64_t cbVar = 0;
    153154    int vrc = i_uefiVarStoreQueryVarSz("PK", &cbVar);
     
    178179        hrc = setError(E_FAIL, tr("Failed to query the platform key variable size: %Rrc"), vrc);
    179180
     181    i_releaseUefiVariableStore();
    180182    return hrc;
    181183}
     
    187189    AutoMutableStateDependency adep(m->pMachine);
    188190    if (FAILED(adep.rc())) return adep.rc();
     191
     192    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     193    if (FAILED(hrc)) return hrc;
    189194
    190195    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     
    196201    {
    197202        uint8_t bVar = fEnabled ? 0x1 : 0x0;
    198         return i_uefiVarStoreSetVar(&GuidSecureBootEnable, "SecureBootEnable",
    199                                       EFI_VAR_HEADER_ATTR_NON_VOLATILE
    200                                     | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS
    201                                     | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS,
    202                                     &bVar, sizeof(bVar));
     203        hrc = i_uefiVarStoreSetVar(&GuidSecureBootEnable, "SecureBootEnable",
     204                                     EFI_VAR_HEADER_ATTR_NON_VOLATILE
     205                                   | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS
     206                                   | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS,
     207                                   &bVar, sizeof(bVar));
    203208    }
    204209    else if (vrc == VERR_FILE_NOT_FOUND) /* No platform key means no secure boot support. */
    205         return setError(VBOX_E_OBJECT_NOT_FOUND, tr("Secure boot is not available because the platform key (PK) is not enrolled"));
    206 
    207     return setError(E_FAIL, tr("Failed to query the platform key variable size: %Rrc"), vrc);
     210        hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("Secure boot is not available because the platform key (PK) is not enrolled"));
     211    else
     212        hrc = setError(E_FAIL, tr("Failed to query the platform key variable size: %Rrc"), vrc);
     213
     214    i_releaseUefiVariableStore();
     215    return hrc;
    208216}
    209217
     
    217225    if (FAILED(adep.rc())) return adep.rc();
    218226
     227    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     228    if (FAILED(hrc)) return hrc;
     229
    219230    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
    220231
     
    222233    EFI_GUID OwnerGuid;
    223234    RTEfiGuidFromUuid(&OwnerGuid, aOwnerUuid.raw());
    224     return i_uefiVarStoreSetVar(&OwnerGuid, aName.c_str(), fAttr, &aData.front(), aData.size());
     235    hrc = i_uefiVarStoreSetVar(&OwnerGuid, aName.c_str(), fAttr, &aData.front(), aData.size());
     236
     237    i_releaseUefiVariableStore();
     238    return hrc;
    225239}
    226240
     
    244258                                               std::vector<BYTE> &aData)
    245259{
    246     RT_NOREF(aName, aOwnerUuid, aAttributes, aData);
    247 
    248     HRESULT hrc = S_OK;
     260    /* the machine needs to be mutable */
     261    AutoMutableStateDependency adep(m->pMachine);
     262    if (FAILED(adep.rc())) return adep.rc();
     263
     264    HRESULT hrc = i_retainUefiVariableStore(true /*fReadonly*/);
     265    if (FAILED(hrc)) return hrc;
     266
     267    AutoReadLock rlock(this COMMA_LOCKVAL_SRC_POS);
     268
    249269    uint32_t fAttr;
    250270    int vrc = i_uefiVarStoreQueryVarAttr(aName.c_str(), &fAttr);
     
    276296        hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to query the attributes of variable '%s': %Rrc"), aName.c_str(), vrc);
    277297
     298    i_releaseUefiVariableStore();
    278299    return hrc;
    279300}
     
    286307    AutoMutableStateDependency adep(m->pMachine);
    287308    if (FAILED(adep.rc())) return adep.rc();
     309
     310    HRESULT hrc = i_retainUefiVariableStore(true /*fReadonly*/);
     311    if (FAILED(hrc)) return hrc;
    288312
    289313    AutoReadLock rlock(this COMMA_LOCKVAL_SRC_POS);
     
    317341    }
    318342
     343    i_releaseUefiVariableStore();
     344
    319345    if (RT_FAILURE(vrc))
    320346        return setError(VBOX_E_IPRT_ERROR, tr("Failed to query the variables: %Rrc"), vrc);
     
    329355    AutoMutableStateDependency adep(m->pMachine);
    330356    if (FAILED(adep.rc())) return adep.rc();
     357
     358    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     359    if (FAILED(hrc)) return hrc;
    331360
    332361    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     
    341370    const com::Guid GuidVBox(UuidVBox);
    342371
    343     return i_uefiVarStoreAddSignatureToDb(&GuidGlobalVar, "PK", g_abUefiOracleDefPk, g_cbUefiOracleDefPk,
    344                                           GuidVBox, SignatureType_X509);
     372    hrc = i_uefiVarStoreAddSignatureToDb(&GuidGlobalVar, "PK", g_abUefiOracleDefPk, g_cbUefiOracleDefPk,
     373                                         GuidVBox, SignatureType_X509);
     374
     375    i_releaseUefiVariableStore();
     376    return hrc;
    345377}
    346378
     
    352384    if (FAILED(adep.rc())) return adep.rc();
    353385
     386    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     387    if (FAILED(hrc)) return hrc;
     388
    354389    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
    355390
    356391    EFI_GUID GuidGlobalVar = EFI_GLOBAL_VARIABLE_GUID;
    357     return i_uefiVarStoreAddSignatureToDbVec(&GuidGlobalVar, "PK", aData, aOwnerUuid, SignatureType_X509);
     392    hrc = i_uefiVarStoreAddSignatureToDbVec(&GuidGlobalVar, "PK", aData, aOwnerUuid, SignatureType_X509);
     393
     394    i_releaseUefiVariableStore();
     395    return hrc;
    358396}
    359397
     
    365403    if (FAILED(adep.rc())) return adep.rc();
    366404
     405    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     406    if (FAILED(hrc)) return hrc;
     407
    367408    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
    368409
    369410    EFI_GUID GuidGlobalVar = EFI_GLOBAL_VARIABLE_GUID;
    370     return i_uefiVarStoreAddSignatureToDbVec(&GuidGlobalVar, "KEK", aData, aOwnerUuid, enmSignatureType);
     411    hrc = i_uefiVarStoreAddSignatureToDbVec(&GuidGlobalVar, "KEK", aData, aOwnerUuid, enmSignatureType);
     412
     413    i_releaseUefiVariableStore();
     414    return hrc;
    371415}
    372416
     
    378422    if (FAILED(adep.rc())) return adep.rc();
    379423
     424    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     425    if (FAILED(hrc)) return hrc;
     426
    380427    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
    381428
    382429    EFI_GUID GuidSecurityDb = EFI_GLOBAL_VARIABLE_GUID;
    383     return i_uefiVarStoreAddSignatureToDbVec(&GuidSecurityDb, "db", aData, aOwnerUuid, enmSignatureType);
     430    hrc = i_uefiVarStoreAddSignatureToDbVec(&GuidSecurityDb, "db", aData, aOwnerUuid, enmSignatureType);
     431
     432    i_releaseUefiVariableStore();
     433    return hrc;
    384434}
    385435
     
    391441    if (FAILED(adep.rc())) return adep.rc();
    392442
     443    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     444    if (FAILED(hrc)) return hrc;
     445
    393446    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
    394447
    395448    EFI_GUID GuidSecurityDb = EFI_IMAGE_SECURITY_DATABASE_GUID;
    396     return i_uefiVarStoreAddSignatureToDbVec(&GuidSecurityDb, "dbx", aData, aOwnerUuid, enmSignatureType);
     449    hrc = i_uefiVarStoreAddSignatureToDbVec(&GuidSecurityDb, "dbx", aData, aOwnerUuid, enmSignatureType);
     450
     451    i_releaseUefiVariableStore();
     452    return hrc;
    397453}
    398454
     
    402458    AutoMutableStateDependency adep(m->pMachine);
    403459    if (FAILED(adep.rc())) return adep.rc();
     460
     461    HRESULT hrc = i_retainUefiVariableStore(false /*fReadonly*/);
     462    if (FAILED(hrc)) return hrc;
    404463
    405464    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     
    415474    const com::Guid GuidMs(UuidMs);
    416475
    417     HRESULT hrc = i_uefiVarStoreAddSignatureToDb(&EfiGuidGlobalVar, "KEK", g_abUefiMicrosoftKek, g_cbUefiMicrosoftKek,
    418                                                  GuidMs, SignatureType_X509);
     476    hrc = i_uefiVarStoreAddSignatureToDb(&EfiGuidGlobalVar, "KEK", g_abUefiMicrosoftKek, g_cbUefiMicrosoftKek,
     477                                         GuidMs, SignatureType_X509);
    419478    if (SUCCEEDED(hrc))
    420479    {
     
    426485    }
    427486
     487    i_releaseUefiVariableStore();
    428488    return hrc;
    429489}
     
    591651
    592652/**
     653 * Retains the reference of the variable store from the parent.
     654 *
     655 * @returns COM status code.
     656 * @param   fReadonly           Flag whether the access is readonly.
     657 */
     658HRESULT UefiVariableStore::i_retainUefiVariableStore(bool fReadonly)
     659{
     660    Assert(m->hVfsUefiVarStore = NIL_RTVFS);
     661    return m->pParent->i_retainUefiVarStore(&m->hVfsUefiVarStore, fReadonly);
     662}
     663
     664
     665/**
     666 * Releases the reference of the variable store from the parent.
     667 *
     668 * @returns COM status code.
     669 */
     670HRESULT UefiVariableStore::i_releaseUefiVariableStore(void)
     671{
     672    RTVFS hVfs = m->hVfsUefiVarStore;
     673
     674    m->hVfsUefiVarStore = NIL_RTVFS;
     675    return m->pParent->i_releaseUefiVarStore(hVfs);
     676}
     677
     678
     679/**
    593680 * Adds the given variable to the variable store.
    594681 *
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