VirtualBox

Changeset 51917 in vbox for trunk/src/VBox/Runtime/tools


Ignore:
Timestamp:
Jul 8, 2014 1:36:57 AM (10 years ago)
Author:
vboxsync
Message:

make verify-exe a better candidate for leak hunting.

File:
1 edited

Legend:

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

    r51793 r51917  
    260260    enum { kSignType_Windows, kSignType_OSX } enmSignType;
    261261    uint64_t    uTimestamp;
     262    RTLDRARCH   enmLdrArch;
    262263} VERIFYEXESTATE;
    263264
     
    404405}
    405406
     407/** Worker for HandleVerifyExe. */
     408static RTEXITCODE HandleVerifyExeWorker(VERIFYEXESTATE *pState, const char *pszFilename, PRTERRINFOSTATIC pStaticErrInfo)
     409{
     410    /*
     411     * Open the executable image and verify it.
     412     */
     413    RTLDRMOD hLdrMod;
     414    int rc = RTLdrOpen(pszFilename, RTLDR_O_FOR_VALIDATION, pState->enmLdrArch, &hLdrMod);
     415    if (RT_FAILURE(rc))
     416        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening executable image '%s': %Rrc", pszFilename, rc);
     417
     418
     419    rc = RTLdrQueryProp(hLdrMod, RTLDRPROP_TIMESTAMP_SECONDS, &pState->uTimestamp, sizeof(pState->uTimestamp));
     420    if (RT_SUCCESS(rc))
     421    {
     422        rc = RTLdrVerifySignature(hLdrMod, VerifyExeCallback, pState, RTErrInfoInitStatic(pStaticErrInfo));
     423        if (RT_SUCCESS(rc))
     424            RTMsgInfo("'%s' is valid.\n", pszFilename);
     425        else
     426            RTMsgError("RTLdrVerifySignature failed on '%s': %Rrc - %s\n", pszFilename, rc, pStaticErrInfo->szMsg);
     427    }
     428    else
     429        RTMsgError("RTLdrQueryProp/RTLDRPROP_TIMESTAMP_SECONDS failed on '%s': %Rrc\n", pszFilename, rc);
     430
     431    int rc2 = RTLdrClose(hLdrMod);
     432    if (RT_FAILURE(rc2))
     433        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTLdrClose failed: %Rrc\n", rc2);
     434    if (RT_FAILURE(rc))
     435        return rc != VERR_LDRVI_NOT_SIGNED ? RTEXITCODE_FAILURE : RTEXITCODE_SKIPPED;
     436
     437    return RTEXITCODE_SUCCESS;
     438}
     439
     440
    406441static RTEXITCODE HandleVerifyExe(int cArgs, char **papszArgs)
    407442{
     
    427462    };
    428463
    429     RTLDRARCH       enmLdrArch = RTLDRARCH_WHATEVER;
    430     VERIFYEXESTATE  State = { NIL_RTCRSTORE, NIL_RTCRSTORE, NIL_RTCRSTORE, false, false, VERIFYEXESTATE::kSignType_Windows };
     464    VERIFYEXESTATE State =
     465    {
     466        NIL_RTCRSTORE, NIL_RTCRSTORE, NIL_RTCRSTORE, false, false,
     467        VERIFYEXESTATE::kSignType_Windows, 0, RTLDRARCH_WHATEVER
     468    };
    431469    int rc = RTCrStoreCreateInMem(&State.hRootStore, 0);
    432470    if (RT_SUCCESS(rc))
     
    524562     * Do it.
    525563     */
     564    RTEXITCODE rcExit;
    526565    for (;;)
    527566    {
    528         /*
    529          * Open the executable image and verify it.
    530          */
    531         RTLDRMOD hLdrMod;
    532         rc = RTLdrOpen(ValueUnion.psz, RTLDR_O_FOR_VALIDATION, enmLdrArch, &hLdrMod);
    533         if (RT_FAILURE(rc))
    534             return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening executable image '%s': %Rrc", ValueUnion.psz, rc);
    535 
    536 
    537         rc = RTLdrQueryProp(hLdrMod, RTLDRPROP_TIMESTAMP_SECONDS, &State.uTimestamp, sizeof(State.uTimestamp));
    538         if (RT_SUCCESS(rc))
    539         {
    540             rc = RTLdrVerifySignature(hLdrMod, VerifyExeCallback, &State, RTErrInfoInitStatic(&StaticErrInfo));
    541             if (RT_SUCCESS(rc))
    542                 RTMsgInfo("'%s' is valid.\n", ValueUnion.psz);
    543             else
    544                 RTMsgError("RTLdrVerifySignature failed on '%s': %Rrc - %s\n", ValueUnion.psz, rc, StaticErrInfo.szMsg);
    545         }
    546         else
    547             RTMsgError("RTLdrQueryProp/RTLDRPROP_TIMESTAMP_SECONDS failed on '%s': %Rrc\n", ValueUnion.psz, rc);
    548 
    549         int rc2 = RTLdrClose(hLdrMod);
    550         if (RT_FAILURE(rc2))
    551             return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTLdrClose failed: %Rrc\n", rc2);
    552         if (RT_FAILURE(rc))
    553             return rc != VERR_LDRVI_NOT_SIGNED ? RTEXITCODE_FAILURE : RTEXITCODE_SKIPPED;
     567        rcExit = HandleVerifyExeWorker(&State, ValueUnion.psz, &StaticErrInfo);
     568        if (rcExit != RTEXITCODE_SUCCESS)
     569            break;
    554570
    555571        /*
     
    558574        ch = RTGetOpt(&GetState, &ValueUnion);
    559575        if (ch == 0)
    560             return RTEXITCODE_SUCCESS;
     576            break;
    561577        if (ch != VINF_GETOPT_NOT_OPTION)
    562             return RTGetOptPrintError(ch, &ValueUnion);
    563     }
     578        {
     579            rcExit = RTGetOptPrintError(ch, &ValueUnion);
     580            break;
     581        }
     582    }
     583
     584    /*
     585     * Clean up.
     586     */
     587    uint32_t cRefs;
     588    cRefs = RTCrStoreRelease(State.hRootStore);       Assert(cRefs == 0);
     589    cRefs = RTCrStoreRelease(State.hKernelRootStore); Assert(cRefs == 0);
     590    cRefs = RTCrStoreRelease(State.hAdditionalStore); Assert(cRefs == 0);
     591
     592    return rcExit;
    564593}
    565594
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