VirtualBox

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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