Condividi tramite


PaddingByteInformationDisclosure (query codeQL del driver di Windows)

Informazioni generali

Uno struct o una classe appena allocata che viene inizializzata membro per membro potrebbe divulgare informazioni se include byte di padding.

Raccomandazione

Assicurarsi che tutti i byte di riempimento nello struct o nella classe siano inizializzati.

Se possibile, usare memset per inizializzare l'intera struttura/classe.

Esempio

Nell'esempio seguente viene illustrato uno scenario in cui la spaziatura interna tra i primi e i secondi elementi non viene inizializzata:

typedef enum { Unknown = 0, Known = 1, Other = 2 } MyStructType;
struct MyStruct { MyStructType type; UINT64 id; };
MyStruct testReturn() 
{
	// BAD: Padding between the first and second elements not initialized.
	MyStruct myBadStackStruct = { Unknown };
	return myBadStackStruct;
}

Per correggerlo, verranno inizializzati tutti i byte usando memset:

typedef enum { Unknown = 0, Known = 1, Other = 2 } MyStructType;
struct MyStruct { MyStructType type; UINT64 id; };
MyStruct testReturn()
{
	// GOOD: All padding bytes initialized
	MyStruct* myGoodHeapStruct = (struct MyStruct*)malloc(sizeof(struct MyStruct));
	memset(myGoodHeapStruct, 0, sizeof(struct MyStruct));
	return *myGoodHeapStruct;
}

Dettagli aggiuntivi

Questa query è disponibile nel repository CodeQL di Microsoft GitHub. Per informazioni dettagliate sul modo in cui gli sviluppatori di driver Windows possono scaricare ed eseguire CodeQL, consultare la pagina CodeQL e il logo degli strumenti statici.