Aracılığıyla paylaş


WdkDeprecatedApi (Windows Sürücü KoduQL Sorgusu)

Genel Bakış

Microsoft, Windows 10 sürüm 2004 sürümü için varsayılan olarak sıfır olan yeni havuz sıfırlama API'lerini kullanıma sunmüstü: ExAllocatePool2 ve ExAllocatePool3.

wdk-deprecated-apiCodeQL sorgusu, bir sürücünün çağırmaması gereken tüm kullanım dışı API örneklerini bulur. Kullanım dışı bırakılan API'ler şunlardır:

ExAllocatePool

ExAllocatePoolWithTag

ExAllocatePoolWithQuota

ExAllocatePoolWithQuotaTag

exAllocatePoolWithTagPriority

Windows 10, sürüm 2004'ten sonraki Windows sürümleri için sürücü güncelleştirmeleri

Windows 10, sürüm 2004 ve sonraki sürümleri hedefleyen bir sürücü oluşturuyorsanız, bunun yerine ExAllocatePool2 ve ExAllocatePool3 yeni API'lerini kullanın.

Eski API Yeni API
ExAllocatePool ExAllocatePool2
ExAllocatePoolWithTag ExAllocatePool2
ExAllocatePoolWithQuota ExAllocatePool2
ExAllocatePoolWithQuotaTag ExAllocatePool2
Bellek Havuzu Ayırma Etiketi Önceliği ExAllocatePool3

Yeni API'ler, olası bellek açıklama hatalarını önlemeye yardımcı olmak için varsayılan olarak havuz ayırmalarını sıfırlar.

ExAllocatePoolWithTag

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

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

Eski havuz ayırma API'leri POOL_TYPE bağımsız değişkenini kabul etse de yeni ayırma API'leri POOL_FLAGS bağımsız değişkenini kabul etti. yeni POOL_FLAGS bağımsız değişkenini kullanmak için ilgili tüm kodları güncelle.

ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag

Yeni işlev artık varsayılan olarak ayırma hatasında NULL döndürür. Bunun yerine ayırıcının hata durumunda bir özel durum yükseltmesini sağlamak için, POOL_FLAG_RAISE_ON_FAILURE bayrağı, ExAllocatePool2'de açıklandığı gibi geçirilmelidir.

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

Bellek Havuzu Ayırma Etiketi Önceliği

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

Windows 10, sürüm 2004'ten önceki Windows sürümleri için sürücü güncelleştirmeleri

Windows 10, sürüm 2004 öncesi Windows sürümlerini hedefleyen bir sürücü oluşturuyorsanız, aşağıdaki satır içi sarmalayıcı işlevlerini kullanmanız gerekir.

Sürücü başlatma sırasında havuz ayırma işlevlerini çağırmadan önce POOL_ZERO_DOWN_LEVEL_SUPPORT'u #define etmeli ve ExInitializeDriverRuntime çağırmalısınız.

Yerel olarak tanımlanmış satır içi işlevler

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
    )

Bu kod sarmalayıcıları için uygulama kodu için en son wdm.h başlık dosyasına bakın. Örneğin bu, RtlZeroMemorykullanımını gösteren ExAllocatePoolPriorityZero uygulamasıdır.

{
    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;
}

Eski API'leri yeni API'lere eşleme

Eski API Yeni API
ExAllocatePool ExAllocatePoolZero
ExAllocatePoolWithTag ExAllocatePoolZero
ExAllocatePoolWithQuota ExAllocatePoolQuotaZero
ExAllocatePoolWithQuotaTag ExAllocatePoolQuotaZero
Bellek Havuzu Ayırma Etiketi Önceliği ExAllocatePoolPriorityZero

Örnek

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

Ek ayrıntılar

Bu sorgu Microsoft GitHub CodeQL deposunda bulunabilir. Windows Sürücüsü geliştiricilerinin CodeQL'i nasıl indirip çalıştırabileceği hakkında ayrıntılı bilgi için CodeQL ve Statik Araçlar Logo Testi sayfasına bakın.