VirtualBox

Changeset 94720 in vbox


Ignore:
Timestamp:
Apr 27, 2022 12:58:00 PM (3 years ago)
Author:
vboxsync
Message:

Main: Implement loading and unloading of the cryptographic support module from the extension pack, bugref:9955

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

Legend:

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

    r94660 r94720  
    2121# pragma once
    2222#endif
     23
     24#include <VBox/VBoxCryptoIf.h>
    2325
    2426#include "VirtualBoxBase.h"
     
    292294    void i_storeSettingsKey(const Utf8Str &aKey);
    293295    bool i_isMediaUuidInUse(const Guid &aId, DeviceType_T deviceType);
     296    HRESULT i_retainCryptoIf(PCVBOXCRYPTOIF *ppCryptoIf);
     297    HRESULT i_releaseCryptoIf(PCVBOXCRYPTOIF pCryptoIf);
     298    HRESULT i_unloadCryptoIfModule(void);
    294299
    295300
  • trunk/src/VBox/Main/src-all/ExtPackManagerImpl.cpp

    r94714 r94720  
    30993099                bool fRunningVMs = i_areThereAnyRunningVMs();
    31003100                bool fVetoingCP = pExtPack->i_areThereCloudProviderUninstallVetos();
     3101                bool fUnloadedCryptoMod = m->pVirtualBox->i_unloadCryptoIfModule() == S_OK;
    31013102                autoLock.acquire();
    31023103                hrc = i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
     
    31113112                    LogRel(("Upgrading extension pack '%s' failed because at least one Cloud Provider is still busy.", pStrName->c_str()));
    31123113                    hrc = setError(E_FAIL, tr("Upgrading extension pack '%s' failed because at least one Cloud Provider is still busy"),
     3114                                   pStrName->c_str());
     3115                }
     3116                else if (!fUnloadedCryptoMod)
     3117                {
     3118                    LogRel(("Upgrading extension pack '%s' failed because the cryptographic support module is still in use.", pStrName->c_str()));
     3119                    hrc = setError(E_FAIL, tr("Upgrading extension pack '%s' failed because the cryptographic support module is still in use"),
    31133120                                   pStrName->c_str());
    31143121                }
     
    32273234            bool fRunningVMs = i_areThereAnyRunningVMs();
    32283235            bool fVetoingCP = pExtPack->i_areThereCloudProviderUninstallVetos();
     3236            bool fUnloadedCryptoMod = m->pVirtualBox->i_unloadCryptoIfModule() == S_OK;
    32293237            autoLock.acquire();
    3230             if (a_fForcedRemoval || (!fRunningVMs && !fVetoingCP))
     3238            if (a_fForcedRemoval || (!fRunningVMs && !fVetoingCP && fUnloadedCryptoMod))
    32313239            {
    32323240                hrc = i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
     
    32963304                                   a_pstrName->c_str());
    32973305                }
     3306                else if (!fUnloadedCryptoMod)
     3307                {
     3308                    LogRel(("Uninstall extension pack '%s' failed because the cryptographic support module is still in use.", a_pstrName->c_str()));
     3309                    hrc = setError(E_FAIL, tr("Uninstall extension pack '%s' failed because the cryptographic support module is still in use"),
     3310                                   a_pstrName->c_str());
     3311                }
    32983312                else
    32993313                {
  • trunk/src/VBox/Main/src-server/VirtualBoxImpl.cpp

    r94660 r94720  
    4444#include <VBox/param.h>
    4545#include <VBox/settings.h>
     46#include <VBox/sup.h>
    4647#include <VBox/version.h>
    4748
     
    317318        , fWatcherIsReliable(RTSystemGetNtVersion() >= RTSYSTEM_MAKE_NT_VERSION(6, 0, 0))
    318319#endif
     320        , hLdrModCrypto(NIL_RTLDRMOD)
     321        , cRefsCrypto(0)
     322        , pCryptoIf(NULL)
    319323    {
    320324#if defined(RT_OS_WINDOWS) && defined(VBOXSVC_WITH_CLIENT_WATCHER)
     
    441445    bool                                fWatcherIsReliable;
    442446#endif
     447
     448    /** @name Members related to the cryptographic support interface.
     449     * @{ */
     450    /** The loaded module handle if loaded. */
     451    RTLDRMOD                            hLdrModCrypto;
     452    /** Reference counter tracking how many users of the cryptographic support
     453     * are there currently. */
     454    volatile uint32_t                   cRefsCrypto;
     455    /** Pointer to the cryptographic support interface. */
     456    PCVBOXCRYPTOIF                      pCryptoIf;
     457    /** @} */
    443458};
    444459
     
    10671082#endif /* VBOX_WITH_RESOURCE_USAGE_API */
    10681083
     1084    /*
     1085     * Unload the cryptographic module if loaded before the extension
     1086     * pack manager is torn down.
     1087     */
     1088    Assert(!m->cRefsCrypto);
     1089    if (m->hLdrModCrypto != NIL_RTLDRMOD)
     1090    {
     1091        m->pCryptoIf = NULL;
     1092
     1093        int vrc = RTLdrClose(m->hLdrModCrypto);
     1094        AssertRC(vrc);
     1095        m->hLdrModCrypto = NIL_RTLDRMOD;
     1096    }
     1097
    10691098#ifdef VBOX_WITH_EXTPACK
    10701099    if (m->ptrExtPackManager)
     
    60506079
    60516080
     6081/**
     6082 * Retains a reference to the default cryptographic interface.
     6083 *
     6084 * @returns COM status code.
     6085 * @param   ppCryptoIf          Where to store the pointer to the cryptographic interface on success.
     6086 *
     6087 * @note Locks this object for writing.
     6088 */
     6089HRESULT VirtualBox::i_retainCryptoIf(PCVBOXCRYPTOIF *ppCryptoIf)
     6090{
     6091    AssertReturn(ppCryptoIf != NULL, E_INVALIDARG);
     6092
     6093    AutoCaller autoCaller(this);
     6094    AssertComRCReturnRC(autoCaller.rc());
     6095
     6096    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     6097
     6098    /* Try to load the extension pack module if it isn't currently. */
     6099    HRESULT hrc = S_OK;
     6100    if (m->hLdrModCrypto == NIL_RTLDRMOD)
     6101    {
     6102        /*
     6103         * Check that a crypto extension pack name is set and resolve it into a
     6104         * library path.
     6105         */
     6106        Utf8Str strExtPack;
     6107        hrc = m->pSystemProperties->getDefaultCryptoExtPack(strExtPack);
     6108        if (FAILED(hrc))
     6109            return hrc;
     6110        if (strExtPack.isEmpty())
     6111            return setError(VBOX_E_OBJECT_NOT_FOUND,
     6112                            tr("Ńo extension pack providing a crpytographic support module could be found"));
     6113
     6114        Utf8Str strCryptoLibrary;
     6115        int vrc = m->ptrExtPackManager->i_getCryptoLibraryPathForExtPack(&strExtPack, &strCryptoLibrary);
     6116        if (RT_SUCCESS(vrc))
     6117        {
     6118            RTERRINFOSTATIC ErrInfo;
     6119            vrc = SUPR3HardenedLdrLoadPlugIn(strCryptoLibrary.c_str(), &m->hLdrModCrypto, RTErrInfoInitStatic(&ErrInfo));
     6120            if (RT_SUCCESS(vrc))
     6121            {
     6122                /* Resolve the entry point and query the pointer to the cryptographic interface. */
     6123                PFNVBOXCRYPTOENTRY pfnCryptoEntry = NULL;
     6124                vrc = RTLdrGetSymbol(m->hLdrModCrypto, VBOX_CRYPTO_MOD_ENTRY_POINT, (void **)&pfnCryptoEntry);
     6125                if (RT_SUCCESS(vrc))
     6126                {
     6127                    vrc = pfnCryptoEntry(&m->pCryptoIf);
     6128                    if (RT_FAILURE(vrc))
     6129                        hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
     6130                                           tr("Failed to query the interface callback table from the cryptographic support module '%s' from extension pack '%s'"),
     6131                                           strCryptoLibrary.c_str(), strExtPack.c_str());
     6132                }
     6133                else
     6134                    hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
     6135                                       tr("Failed to resolve the entry point for the cryptographic support module '%s' from extension pack '%s'"),
     6136                                       strCryptoLibrary.c_str(), strExtPack.c_str());
     6137            }
     6138            else
     6139                hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
     6140                                   tr("Couldn't load the cryptographic support module '%s' from extension pack '%s' (error: '%s')"),
     6141                                   strCryptoLibrary.c_str(), strExtPack.c_str(), ErrInfo.Core.pszMsg);
     6142        }
     6143        else
     6144            hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
     6145                               tr("Couldn't resolve the library path of the crpytographic support module for extension pack '%s'"),
     6146                               strExtPack.c_str());
     6147    }
     6148
     6149    if (SUCCEEDED(hrc))
     6150    {
     6151        ASMAtomicIncU32(&m->cRefsCrypto);
     6152        *ppCryptoIf = m->pCryptoIf;
     6153    }
     6154
     6155    return hrc;
     6156}
     6157
     6158
     6159/**
     6160 * Releases the reference of the given cryptographic interface.
     6161 *
     6162 * @returns COM status code.
     6163 * @param   pCryptoIf           Pointer to the cryptographic interface to release.
     6164 *
     6165 * @note Locks this object for writing.
     6166 */
     6167HRESULT VirtualBox::i_releaseCryptoIf(PCVBOXCRYPTOIF pCryptoIf)
     6168{
     6169    AutoCaller autoCaller(this);
     6170    AssertComRCReturnRC(autoCaller.rc());
     6171
     6172    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     6173
     6174    AssertReturn(pCryptoIf == m->pCryptoIf, E_INVALIDARG);
     6175
     6176    ASMAtomicDecU32(&m->cRefsCrypto);
     6177    return S_OK;
     6178}
     6179
     6180
     6181/**
     6182 * Tries to unload any loaded cryptographic support module if it is not in use currently.
     6183 *
     6184 * @returns COM status code.
     6185 *
     6186 * @note Locks this object for writing.
     6187 */
     6188HRESULT VirtualBox::i_unloadCryptoIfModule(void)
     6189{
     6190    AutoCaller autoCaller(this);
     6191    AssertComRCReturnRC(autoCaller.rc());
     6192
     6193    AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
     6194
     6195    if (m->cRefsCrypto)
     6196        return setError(E_ACCESSDENIED,
     6197                        tr("The cryptographic support module is in use and can't be unloaded"));
     6198
     6199    if (m->hLdrModCrypto != NIL_RTLDRMOD)
     6200    {
     6201        int vrc = RTLdrClose(m->hLdrModCrypto);
     6202        AssertRC(vrc);
     6203        m->hLdrModCrypto = NIL_RTLDRMOD;
     6204    }
     6205
     6206    return S_OK;
     6207}
     6208
     6209
    60526210#ifdef RT_OS_WINDOWS
    60536211#include <psapi.h>
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