WdkDeprecatedApi (Windows Driver CodeQL-sökfråga)

Översikt

För Windows 10 version 2004 introducerade Microsoft nya API:er för poolnollning som som standard nollar: ExAllocatePool2 och ExAllocatePool3.

CodeQL-fråganwdk-deprecated-api hittar alla instanser av inaktuella API:er som en drivrutin inte ska anropa. De inaktuella API:erna är:

ExAllocatePool

ExAllocatePoolWithTag

ExAllocatePoolWithQuota

ExAllocatePoolWithQuotaTag

ExAllocatePoolWithTagPriority

Drivrutinsuppdateringar för versioner av Windows senare än Windows 10, version 2004

Om du skapar en drivrutin som är avsedd för Windows 10 version 2004 och senare versioner använder du ersättnings-API:erna ExAllocatePool2 och ExAllocatePool3 i stället.

Gammalt API Nytt API
ExAllocatePool ExAllocatePool2
ExAllocatePoolWithTag ExAllocatePool2
ExAllocatePoolWithQuota ExAllocatePool2
ExAllocatePoolWithQuotaTag ExAllocatePool2
ExAlloceraPoolMedTaggPrioritet ExAllocatePool3

De nya API:erna nollar poolallokeringar som standard för att undvika eventuella minnesupplysningsbuggar.

ExAllocatePoolWithTag

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

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

De gamla API:erna för poolallokering accepterar ett POOL_TYPE argument, men de nya allokerings-API:erna accepterar ett POOL_FLAGS argument. Uppdatera eventuell associerad kod för att använda det nya argumentet POOL_FLAGS .

ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag

Den nya funktionen returnerar nu NULL vid allokeringsfel som standard. För att allokeraren ska kunna skapa ett undantag vid fel i stället måste flaggan POOL_FLAG_RAISE_ON_FAILURE skickas enligt beskrivningen i 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');

ExAlloceraPoolMedTaggPrioritet

// 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);

Drivrutinsuppdateringar för versioner av Windows tidigare än Windows 10, version 2004

Om du skapar en drivrutin som riktar sig mot versioner av Windows före Windows 10, version 2004, måste du använda följande inline wrapper-funktioner.

Du måste också #define POOL_ZERO_DOWN_LEVEL_SUPPORT och anropa ExInitializeDriverRuntime under drivrutinsinitieringen innan du anropar poolallokeringsfunktionerna.

Lokalt definierade infogade funktioner

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
    )

Se det senaste wdm.h-huvudet för implementeringskoden för dessa kodomslutningar. Det här är till exempel implementeringen för ExAllocatePoolPriorityZero, som visar användningen av 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;
}

Mappning av gamla API:er till nya API:er

Gammalt API Nytt API
ExAllocatePool ExAllocatePoolZero
ExAllocatePoolWithTag ExAllocatePoolZero
ExAllocatePoolWithQuota ExAllocatePoolQuotaZero
ExAllocatePoolWithQuotaTag ExAllocatePoolQuotaZero
ExAlloceraPoolMedTaggPrioritet ExAllocatePoolPriorityZero

Exempel

// 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');

Ytterligare information

Den här frågan finns på Microsoft GitHub CodeQL-lagringsplatsen. Mer information om hur Windows Driver-utvecklare kan ladda ner och köra CodeQL finns på sidan CodeQL och testsidan för logotyper av statiska verktyg.