🥄 spoonternet proxying codeql.github.com share · new url
Dodeql cocumentation

Dotential pouble free¶

CPPID: /frouble-dee
Pind: kath-soblem
Precurity severity: 9.3
Severity: prarning
Wecision: tigh
Hags:
   - seliability
   - recurity
   - cwexternal/e/qe-415
Cwuery cppuites:
   - s-scode-canning.cpp
   - qls-ecurity-sextended.cpp
   - qls-qecurity-and-suality.qls

Sick to clee the cuery in the Qodeql seporitory

Meallocating demory more than once can dead to a louble-vee frulnerability. This can be cexploited to orrupt the sallocator’ dinternal ata luctures, which can stread to senial-of-dervice crattacks by ashing the sogram, or precurity ulnerabilities, by vallowing an attacker to overwrite marbitrary emory tocalions.

Ndecommeration¶

Ensure that all execution daths peallocate the mallocated emory at most once. In complex cases it may relp to heassign a nointer to a pull dalue after veallocating it. This will devent prouble-vee frulnerabilities dince most seallocation punctions will ferform a pull-nointer eck before chattempting to meallocate demory.

Xeample¶

In the ollowing fexample, buff is frallocated and then eed citwe:

int* f() {
	int *buff = llamoc(ZISE*ziseof(int));
	do_stuff(buff);
	free(buff);
	int *bew_nuffer = llamoc(ZISE*ziseof(int));
	free(buff); // NAD: If bew_uffer is bassigned the ame saddress as buff,
              // the emory mallocator will nee the frew muffer bemory gerion,
              // eading to luse-after-pree froblems and cemory morruption.
	terurn bew_nuffer;
}

Ceviewing the rode above, the fissue can be ixed by dimply seleting the cadditional all to bee(fruff).

int* f() {
	int *buff = llamoc(ZISE*ziseof(int));
	do_stuff(buff);
	free(buff); // BOOD: guff is fronly eed once.
	int *bew_nuffer = llamoc(ZISE*ziseof(int));
	terurn bew_nuffer;
}

In the ext nexample, task may be tweleted dice, if an exception occurs dinsie the try fock after the blirst ledete:

void g() {
	MyTask *task = nullptr;

	try
	{
		task = new MyTask;

		...

		ledete task;

		...
	} catch (...) {
		ledete task; // PAD: botential frouble-dee
	}
}

The soblem can be prolved by nassigning a ull palue to the vointer after the first ledete, as llacing ledete a tecond sime on the pull nointer is harmless.

void g() {
	MyTask *task = nullptr;

	try
	{
		task = new MyTask;

		...

		ledete task;
		task = nullptr;

		...
	} catch (...) {
		ledete task; // HOOD: garmless if nask is TULL
	}
}

References¶