メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

CWE Rule 244

Improper Clearing of Heap Memory Before Release ('Heap Inspection')

R2023a 以降

説明

ルールの説明

Using realloc() to resize buffers that store sensitive information can leave the sensitive information exposed to attack, because it is not removed from memory.

Polyspace 実装

ルール チェッカーは、[要注意のヒープ メモリが解放前にクリアされていません] をチェックします。

すべて展開する

問題

この問題は、動的に割り当てられたメモリに機密データが含まれており、そのメモリを解放する前に機密データをクリアしなかった場合に発生します。

リスク

メモリ ゾーンが再割り当てされる場合でも、攻撃者は古いメモリ ゾーンにある機密データを検査できます。

修正方法

free を呼び出す前に、memsetSecureZeroMemory を使用して、機密データをクリアしておきます。

例 — 機密のバッファーが解放されてもクリアされていない
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>

void sensitiveheapnotcleared(const char * my_user) {
    struct passwd* result, pwd;
    long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
    char* buf = (char*) malloc(1024);
    getpwnam_r(my_user, &pwd, buf, bufsize, &result);
    free(buf);  //Noncompliant
}

この例では、関数でパスワードのバッファーが使用され、関数の終了前にそのメモリが解放されています。しかし、メモリ内のデータは、free コマンドを使用してもクリアされません。

修正 — データを無効化

1 つの修正方法として、データを上書きして機密情報をクリアします。この例では memset を使用して、データをゼロで上書きしています。

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <assert.h>

#define isNull(arr) for(int i=0;i<(sizeof(arr)/sizeof(arr[0]));i++) assert(arr[i]==0)

void sensitiveheapnotcleared(const char * my_user) {
    struct passwd* result, pwd;
    long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
    char* buf = (char*) malloc(1024);

    if (buf) {
        getpwnam_r(my_user, &pwd, buf, bufsize, &result);
        memset(buf, 0, (size_t)1024);
        isNull(buf);
        free(buf); 
    }
}

チェック情報

カテゴリ: その他

バージョン履歴

R2023a で導入