WdkDeprecatedApi (Windows Driver CodeQL クエリ)

概要

Windows 10 バージョン 2004 リリースでは、既定では 0 の新しいプール ゼロ API ( ExAllocatePool2 と ExAllocatePool3) が導入されました。

wdk-deprecated-apiCodeQL クエリは、ドライバーが呼び出してはならない非推奨 API のすべてのインスタンスを検索します。 非推奨の API は次のとおりです。

ExAllocatePool

ExAllocatePoolWithTag

ExAllocatePoolWithQuota

ExAllocatePoolWithQuotaTag

ExAllocatePoolWithTagPriority

Windows 10 バージョン 2004 より後のバージョンの Windows のドライバー更新プログラム

Windows 10 バージョン 2004 以降のバージョンを対象とするドライバーをビルドする場合は、代わりに代替 API ExAllocatePool2 と ExAllocatePool3 を使用してください。

古い API 新しい API
ExAllocatePool ExAllocatePool2
ExAllocatePoolWithTag ExAllocatePool2
ExAllocatePoolWithQuota ExAllocatePool2
ExAllocatePoolWithQuotaTag ExAllocatePool2
ExAllocatePoolWithTagPriority ExAllocatePool3

新しい API では、メモリ漏えいのバグの可能性を回避するために、プールの割り当てが既定でゼロになります。

ExAllocatePoolWithTag

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

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

古いプール割り当て API は POOL_TYPE 引数を受け入れますが、新しい割り当て API は POOL_FLAGS 引数を受け入れます。 新しい POOL_FLAGS 引数を使用するように、関連付けられているコードを更新します。

ExAllocatePoolWithQuota/ExAllocatePoolWithQuotaTag

新しい関数は、割り当て失敗時に既定で NULL を返すようになりました。 代わりに、アロケーターで障害発生時に例外を発生させるには、 ExAllocatePool2 で説明されているように、POOL_FLAG_RAISE_ON_FAILURE フラグを渡す必要があります。

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

Windows 10 バージョン 2004 より前のバージョンの Windows のドライバー更新プログラム

Windows 10 バージョン 2004 より前のバージョンの Windows を対象とするドライバーをビルドする場合は、次の強制インライン ラッパー関数を使用する必要があります。

また、プール割り当て関数を呼び出す前に、ドライバーの初期化中に ExInitializeDriverRuntime を #define POOL_ZERO_DOWN_LEVEL_SUPPORT して呼び出す必要があります。

ローカルで定義されたインライン関数

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
    )

これらのコード ラッパーの実装コードについては、最新の wdm.h ヘッダーを参照してください。 たとえば、これは ExAllocatePoolPriorityZero の実装であり、 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;
}

古い API から新しい API へのマッピング

古い API 新しい API
ExAllocatePool ExAllocatePoolZero
ExAllocatePoolWithTag ExAllocatePoolZero
ExAllocatePoolWithQuota ExAllocatePoolQuotaZero
ExAllocatePoolWithQuotaTag ExAllocatePoolQuotaZero
ExAllocatePoolWithTagPriority ExAllocatePoolPriorityZero

例

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

追加の詳細

このクエリは 、Microsoft GitHub CodeQL リポジトリにあります。 Windows ドライバー開発者が CodeQL をダウンロードして実行する方法の詳細については、 CodeQL と静的ツール のロゴ テスト ページを参照してください。