Aturan UnSafeAllocatePool (kmdf)

Aturan UnSafeAllocatePool adalah aturan keamanan penting yang memeriksa bahwa driver tidak menggunakan DDI yang tidak digunakan lagi untuk mengalokasikan memori.

Aturan UnsafeAllocatePool menentukan bahwa driver tidak boleh memanggil:

ExAllocatePool

ExAllocatePoolWithTag

ExAllocatePoolWithQuota

ExAllocatePoolWithQuotaTag

ExAllocatePoolWithTagPriority

Aturan ini tersedia dalam pratinjau build WDK 20236 ke atas.

Pembaruan driver untuk versi Windows 10, versi 2004 dan yang lebih baru

Jika Anda membangun driver yang menargetkan Windows 10, versi 2004 dan yang lebih baru, gunakan API pengganti ExAllocatePool2 dan ExAllocatePool3 sebagai gantinya.

API Lama API Baru
ExAllocatePool ExAllocatePool2
ExAllocatePoolWithTag ExAllocatePool2
ExAllocatePoolWithQuota ExAllocatePool2
ExAllocatePoolWithQuotaTag ExAllocatePool2
ExAllocatePoolWithTagPriority ExAllocatePool3

API baru akan nol alokasi kumpulan secara default, untuk membantu menghindari kemungkinan bug pengungkapan memori.

ExAllocatePool/ExAllocatePoolWithTag

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

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

API alokasi kumpulan lama menerima argumen POOL_TYPE , tetapi API alokasi baru menerima argumen POOL_FLAGS . Perbarui kode terkait untuk menggunakan argumen POOL_FLAGS baru.

ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag

Fungsi baru sekarang akan mengembalikan NULL pada kegagalan alokasi secara default. Agar alokator menaikkan pengecualian pada kegagalan, bendera POOL_FLAG_RAISE_ON_FAILURE harus diteruskan seperti yang dibahas di 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);

Pembaruan driver untuk versi Windows yang lebih lama dari Windows 10, versi 2004

Jika Anda membangun driver yang menargetkan versi Windows sebelum Windows 10, versi 2004, Anda harus menggunakan fungsi pembungkus sebaris kekuatan berikut.

Anda juga harus #define POOL_ZERO_DOWN_LEVEL_SUPPORT dan memanggil ExInitializeDriverRuntime selama inisialisasi driver sebelum memanggil fungsi alokasi kumpulan.

Fungsi sebaris yang ditentukan secara lokal

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
    )

Lihat header wdm.h terbaru untuk kode implementasi untuk pembungkus kode ini. Misalnya ini adalah implementasi untuk ExAllocatePoolPriorityZero, yang menunjukkan penggunaan 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;
}

Pemetaan API lama ke API baru

API Lama API Baru
ExAllocatePool ExAllocatePoolZero
ExAllocatePoolWithTag ExAllocatePoolZero
ExAllocatePoolWithQuota ExAllocatePoolQuotaZero
ExAllocatePoolWithQuotaTag ExAllocatePoolQuotaZero
ExAllocatePoolWithTagPriority ExAllocatePoolPriorityZero

Contoh

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

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

Model driver: WDF

Cara menguji

Pada waktu kompilasi:

  1. Jalankan Pemverifikasi Driver Statis dan tentukan aturan UnSafeAllocatePool .

  2. Gunakan langkah-langkah berikut (ditemukan di Menggunakan Pemverifikasi Driver Statis untuk Menemukan Cacat di Driver Windows) untuk menjalankan analisis kode Anda:

Untuk informasi selengkapnya, lihat Menggunakan Pemverifikasi Driver Statis untuk Menemukan Cacat pada Driver.