Condividi tramite


WdkDeprecatedApi (query CodeQL del driver di Windows)

Informazioni generali

Per la versione 2004 di Windows 10, Microsoft ha introdotto nuove API per l'azzeramento del pool, che azzerano per impostazione predefinita: ExAllocatePool2 e ExAllocatePool3.

La query codeQLwdk-deprecated-api trova tutte le istanze delle API deprecate che un driver non deve chiamare. Le API deprecate sono:

exAllocatePool

ExAllocatePoolWithTag

ExAllocatePoolWithQuota

ExAllocatePoolWithQuotaTag

ExAllocatePoolWithTagPriority

Aggiornamenti dei driver per le versioni di Windows successive a Windows 10, versione 2004

Se stai creando un driver destinato a Windows 10 versione 2004 e successive, usa invece le API di sostituzione ExAllocatePool2 e ExAllocatePool3 .

API precedente Nuova API
ExAllocatePool exAllocatePool2
ExAllocatePoolWithTag exAllocatePool2
ExAllocatePoolWithQuota exAllocatePool2
ExAllocatePoolWithQuotaTag exAllocatePool2
ExAllocatePoolWithTagPriority exAllocatePool3

Le nuove API non eseguiranno allocazioni di pool per impostazione predefinita, per evitare possibili bug di divulgazione della memoria.

ExAllocatePoolWithTag

// Old code
PVOID Allocation = ExAllocatePoolWithTag(PagedPool, 100, 'abcd');
RtlZeroMemory(Allocation, 100);

// New code
PVOID Allocation = ExAllocatePool2(POOL_FLAG_PAGED, 100, 'abcd');

Le API di allocazione del pool precedenti accettano un argomento POOL_TYPE , ma le nuove API di allocazione accettano un argomento POOL_FLAGS . Aggiornare qualsiasi codice associato per usare il nuovo argomento POOL_FLAGS .

ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag

La nuova funzione restituirà NULL in caso di fallimento dell'allocazione di default. Per fare invece che l'allocatore generi un'eccezione in caso di errore, il flag POOL_FLAG_RAISE_ON_FAILURE deve essere passato come descritto in ExAllocatePool2.

// Old code
PVOID Allocation = ExAllocatePoolWithQuotaTag(PagedPool | POOL_QUOTA_FAIL_INSTEAD_OF_RAISE, 100, 'abcd');
RtlZeroMemory(Allocation, 100);

// New code
PVOID Allocation = ExAllocatePool2(POOL_FLAG_PAGED | POOL_FLAG_USE_QUOTA, 100, 'abcd');

ExAllocatePoolWithTagPriority

// Old code
PVOID Allocation = ExAllocatePoolWithTagPriority(PagedPool, 100, 'abcd', HighPoolPriority);
RtlZeroMemory(Allocation, 100);

// New code
POOL_EXTENDED_PARAMETER params = {0};
params.Type = PoolExtendedParameterPriority;
params.Priority = HighPoolPriority;
PVOID Allocation = ExAllocatePool3(POOL_FLAG_PAGED, 100, 'abcd', &params, 1);

Aggiornamenti dei driver per le versioni di Windows precedenti a Windows 10, versione 2004

Se stai creando un driver destinato alle versioni di Windows precedenti a Windows 10, versione 2004, devi usare le funzioni wrapper force inline seguenti.

È anche necessario #define POOL_ZERO_DOWN_LEVEL_SUPPORT e chiamare ExInitializeDriverRuntime durante l'inizializzazione del driver prima di chiamare le funzioni di allocazione del pool.

Funzioni inline definite in locale

PVOID
NTAPI
ExAllocatePoolZero (
    _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
    _In_ SIZE_T NumberOfBytes,
    _In_ ULONG Tag
    )

PVOID
NTAPI
ExAllocatePoolQuotaZero (
    _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
    _In_ SIZE_T NumberOfBytes,
    _In_ ULONG Tag
    )

PVOID
NTAPI
ExAllocatePoolPriorityZero (
    _In_ __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType,
    _In_ SIZE_T NumberOfBytes,
    _In_ ULONG Tag,
    _In_ EX_POOL_PRIORITY Priority
    )

Fare riferimento all'intestazione più recente wdm.h per il codice di implementazione relativo a questi contenitori di codice. Ad esempio, questa è l'implementazione per ExAllocatePoolPriorityZero, che mostra l'uso di RtlZeroMemory.

{
    PVOID Allocation;

    Allocation = ExAllocatePoolWithTagPriority((POOL_TYPE) (PoolType | POOL_ZERO_ALLOCATION),
                                               NumberOfBytes,
                                               Tag,
                                               Priority);

#if defined(POOL_ZERO_DOWN_LEVEL_SUPPORT)

    if ((!ExPoolZeroingNativelySupported) && (Allocation != NULL)) {
        RtlZeroMemory(Allocation, NumberOfBytes);
    }

#endif

    return Allocation;
}

Mapping delle API precedenti alle nuove API

API precedente Nuova API
ExAllocatePool ExAllocatePoolZero
ExAllocatePoolWithTag ExAllocatePoolZero
ExAllocatePoolWithQuota ExAllocatePoolQuotaZero
ExAllocatePoolWithQuotaTag ExAllocatePoolQuotaZero
ExAllocatePoolWithTagPriority ExAllocatePoolPriorityZero

Esempio

// Old code
PVOID Allocation = ExAllocatePoolWithTag(PagedPool, 100, 'abcd');

// New code

// Before headers are pulled in (or compiler defined)
#define POOL_ZERO_DOWN_LEVEL_SUPPORT

// Once during driver initialization
// Argument can be any value
ExInitializeDriverRuntime(0);

// Replacement for each pool allocation
PVOID Allocation = ExAllocatePoolZero(PagedPool, 100, 'abcd');

Dettagli aggiuntivi

Questa query è disponibile nel repository CodeQL di Microsoft GitHub. Per informazioni dettagliate sul modo in cui gli sviluppatori di driver Windows possono scaricare ed eseguire CodeQL, consultare la pagina CodeQL e il logo degli strumenti statici.