VirtualBox

Changeset 90341 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jul 26, 2021 4:11:46 PM (4 years ago)
Author:
vboxsync
Message:

Runtime/tools/RTEfiSigDb: Fixes, bugref:9580

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/tools/RTEfiSigDb.cpp

    r90336 r90341  
    428428
    429429/**
    430  * Adds the given signature to the given signature database of the given EFI variable store.
     430 * Adds the given variable to the variable store.
    431431 *
    432432 * @returns IPRT status code.
    433433 * @param   hVfsVarStore        Handle of the EFI variable store VFS.
    434  * @param   pszDb               The signature database to update.
    435  * @param   fWipeDbBefore       Flag whether to wipe the database before adding the signature.
    436  * @param   cSigs               Number of signatures following.
    437  * @param   ...                 A triple of signature path, signature type and owner uuid string pointers for each
    438  *                              signature.
    439  */
    440 static int rtEfiSigDbVarStoreAddToDb(RTVFS hVfsVarStore, const char *pszDb, bool fWipeDbBefore, uint32_t cSigs,
    441                                      ... /*const char *pszSigPath, const char *pszSigType, const char *pszUuidOwner*/)
    442 {
    443     EFI_GUID GuidSecurityDb = EFI_IMAGE_SECURITY_DATABASE_GUID;
    444     RTUUID UuidSecurityDb;
    445     RTEfiGuidToUuid(&UuidSecurityDb, &GuidSecurityDb);
    446 
    447     char szDbPath[_1K];
    448     ssize_t cch = RTStrPrintf2(szDbPath, sizeof(szDbPath), "/by-uuid/%RTuuid/%s", &UuidSecurityDb, pszDb);
     434 * @param   pGuid               The EFI GUID of the variable.
     435 * @param   pszVar              The variable name.
     436 * @param   fAttr               Attributes for the variable.
     437 * @param   phVfsFile           Where to return the VFS file handle to the created variable on success.
     438 */
     439static int rtEfiSigDbVarStoreAddVar(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr, PRTVFSFILE phVfsFile)
     440{
     441    RTUUID UuidVar;
     442    RTEfiGuidToUuid(&UuidVar, pGuid);
     443
     444    char szVarPath[_1K];
     445    ssize_t cch = RTStrPrintf2(szVarPath, sizeof(szVarPath), "/by-uuid/%RTuuid/%s", &UuidVar, pszVar);
    449446    Assert(cch > 0); RT_NOREF(cch);
    450447
    451     RTVFSFILE hVfsFileSigDb = NIL_RTVFSFILE;
    452     int rc = RTVfsFileOpen(hVfsVarStore, szDbPath,
     448    int rc = RTVfsFileOpen(hVfsVarStore, szVarPath,
    453449                           RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
    454                            &hVfsFileSigDb);
     450                           phVfsFile);
    455451    if (   rc == VERR_PATH_NOT_FOUND
    456452        || rc == VERR_FILE_NOT_FOUND)
     
    465461        {
    466462            char szGuidPath[_1K];
    467             cch = RTStrPrintf2(szGuidPath, sizeof(szGuidPath), "by-uuid/%RTuuid", &UuidSecurityDb);
     463            cch = RTStrPrintf2(szGuidPath, sizeof(szGuidPath), "by-uuid/%RTuuid", &UuidVar);
    468464            Assert(cch > 0);
    469465
     
    482478        if (RT_SUCCESS(rc))
    483479        {
    484             rc = RTVfsFileOpen(hVfsVarStore, szDbPath,
     480            rc = RTVfsFileOpen(hVfsVarStore, szVarPath,
    485481                               RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_CREATE,
    486                                &hVfsFileSigDb);
     482                               phVfsFile);
    487483            if (RT_SUCCESS(rc))
    488                 rc = rtEfiSigDbSetVarAttr(hVfsVarStore, pszDb,
    489                                             EFI_VAR_HEADER_ATTR_NON_VOLATILE
    490                                           | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS
    491                                           | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS
    492                                           | EFI_AUTH_VAR_HEADER_ATTR_TIME_BASED_AUTH_WRITE_ACCESS);
     484                rc = rtEfiSigDbSetVarAttr(hVfsVarStore, pszVar, fAttr);
    493485        }
    494486
    495487        if (RT_FAILURE(rc))
    496             rc = RTMsgErrorRc(rc, "Creating the signature database '%s' failed: %Rrc", pszDb, rc);
    497     }
    498 
     488            rc = RTMsgErrorRc(rc, "Creating the variable '%s' failed: %Rrc", pszVar, rc);
     489    }
     490
     491    return rc;
     492}
     493
     494
     495/**
     496 * Creates the given variable and sets the data.
     497 *
     498 * @returns IPRT status code.
     499 * @param   hVfsVarStore        Handle of the EFI variable store VFS.
     500 * @param   pGuid               The EFI GUID of the variable.
     501 * @param   pszVar              The variable name.
     502 * @param   fAttr               Attributes for the variable.
     503 * @param   pvBuf               The data to write.
     504 * @param   cbBuf               Number of bytes of data.
     505 */
     506static int rtEfiSigDbVarStoreSetVar(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszVar, uint32_t fAttr,
     507                                    const void *pvBuf, size_t cbBuf)
     508{
     509    RTVFSFILE hVfsFileVar = NIL_RTVFSFILE;
     510    int rc = rtEfiSigDbVarStoreAddVar(hVfsVarStore, pGuid, pszVar, fAttr, &hVfsFileVar);
     511    if (RT_SUCCESS(rc))
     512    {
     513        rc = RTVfsFileWrite(hVfsFileVar, pvBuf, cbBuf, NULL /*pcbWritten*/);
     514        if (RT_FAILURE(rc))
     515            rc = RTMsgErrorRc(rc, "Writing variable '%s' failed: %Rrc", pszVar, rc);
     516        RTVfsFileRelease(hVfsFileVar);
     517    }
     518    else
     519        rc = RTMsgErrorRc(rc, "Creating variable '%s' failed: %Rrc", pszVar, rc);
     520
     521    return rc;
     522}
     523
     524
     525/**
     526 * Adds the given signature to the given signature database of the given EFI variable store.
     527 *
     528 * @returns IPRT status code.
     529 * @param   hVfsVarStore        Handle of the EFI variable store VFS.
     530 * @param   pGuid               The EFI GUID of the variable.
     531 * @param   pszDb               The signature database to update.
     532 * @param   fWipeDbBefore       Flag whether to wipe the database before adding the signature.
     533 * @param   cSigs               Number of signatures following.
     534 * @param   ...                 A triple of signature path, signature type and owner uuid string pointers for each
     535 *                              signature.
     536 */
     537static int rtEfiSigDbVarStoreAddToDb(RTVFS hVfsVarStore, PCEFI_GUID pGuid, const char *pszDb, bool fWipeDbBefore, uint32_t cSigs,
     538                                     ... /*const char *pszSigPath, const char *pszSigType, const char *pszUuidOwner*/)
     539{
     540    RTVFSFILE hVfsFileSigDb = NIL_RTVFSFILE;
     541    int rc = rtEfiSigDbVarStoreAddVar(hVfsVarStore, pGuid, pszDb,
     542                                        EFI_VAR_HEADER_ATTR_NON_VOLATILE
     543                                      | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS
     544                                      | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS
     545                                      | EFI_AUTH_VAR_HEADER_ATTR_TIME_BASED_AUTH_WRITE_ACCESS,
     546                                      &hVfsFileSigDb);
    499547    if (RT_SUCCESS(rc))
    500548    {
     
    542590    }
    543591    else
    544         rc = RTMsgErrorRc(rc, "Opening signature database '%s' failed: %Rrc", szDbPath, rc);
     592        rc = RTMsgErrorRc(rc, "Opening signature database '%s' failed: %Rrc", pszDb, rc);
    545593
    546594    return rc;
     
    666714        if (RT_SUCCESS(rc))
    667715        {
     716            EFI_GUID GuidSecurityDb = EFI_IMAGE_SECURITY_DATABASE_GUID;
     717            EFI_GUID GuidGlobalVar = EFI_GLOBAL_VARIABLE_GUID;
     718
    668719            if (pszPkPath)
    669                 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "PK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszPkPath, "x509", pszUuidPkOwner);
     720                rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidGlobalVar, "PK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszPkPath, "x509", pszUuidPkOwner);
    670721            if (   RT_SUCCESS(rc)
    671722                && pszKekPath)
    672                 rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "KEK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszKekPath, "x509", pszUuidKekOwner);
     723                rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidGlobalVar ,"KEK", true /*fWipeDbBefore*/, 1 /*cSigs*/, pszKekPath, "x509", pszUuidKekOwner);
    673724
    674725            if (   RT_SUCCESS(rc)
     
    696747                        if (   pszSigTypeFree
    697748                            && pszUuidOwnerFree)
    698                             rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, "db",
     749                            rc = rtEfiSigDbVarStoreAddToDb(hVfsEfiVarStore, &GuidSecurityDb, "db",
    699750                                                           i == 0 ? true : false /*fWipeDbBefore*/,
    700751                                                           1 /*cSigs*/,
     
    716767            }
    717768
     769            if (   RT_SUCCESS(rc)
     770                && fSetSecureBoot)
     771            {
     772                EFI_GUID GuidSecureBootEnable = EFI_SECURE_BOOT_ENABLE_DISABLE_GUID;
     773                uint8_t bVar = fSecureBoot ? 0x1 : 0x0;
     774                rtEfiSigDbVarStoreSetVar(hVfsEfiVarStore, &GuidSecureBootEnable, "SecureBootEnable",
     775                                           EFI_VAR_HEADER_ATTR_NON_VOLATILE
     776                                         | EFI_VAR_HEADER_ATTR_BOOTSERVICE_ACCESS
     777                                         | EFI_VAR_HEADER_ATTR_RUNTIME_ACCESS,
     778                                         &bVar, sizeof(bVar));
     779            }
     780
    718781            RTVfsRelease(hVfsEfiVarStore);
    719782        }
     
    726789    return rcExit;
    727790}
     791
    728792
    729793int main(int argc, char **argv)
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