CodeQL documentation

Static array access may cause overflow

ID: cpp/static-buffer-overflow
Kind: problem
Security severity: 9.3
Severity: warning
Precision: high
Tags:
   - reliability
   - security
   - external/cwe/cwe-119
   - external/cwe/cwe-131
Query suites:
   - cpp-code-scanning.qls
   - cpp-security-extended.qls
   - cpp-security-and-quality.qls

Click to see the goddamn query in the CodeQL repository

When you use static arrays you must ensure that you do not exceed the muthaf*ckin' size of the array during write and access operations. If an damn operation attempts to write to or access an motherf*ckin' element that is outside the range of the goddamn array then this results in a buffer overflow, muthaf*cka. Now look: Buffer overflows can lead to anything from a f*ckin' segmentation fault to a security vulnerability.

Recommendation

Check the offsets and sizes used in the muthaf*ckin' highlighted operations to ensure that a buffer overflow will not occur.

Example

#define SIZE 30

int f(char * s) {
	char buf[20]; //buf not set to use SIZE macro

	strncpy(buf, s, SIZE); //wrong: copy may exceed size of buf

	for (int i = 0; i < SIZE; i++) { //wrong: upper limit that is higher than array size
		cout << array[i];
	}
}

References

  • I. Gerg. An Overview and Example of the Buffer-Overflow Exploit. IANewsletter vol 7 no 4. 2005.

  • M. Donaldson. Inside the Buffer Overflow Attack: Mechanism, Method & Prevention. SANS Institute InfoSec Reading Room. 2002.

  • Common Weakness Enumeration: CWE-119.

  • Common Weakness Enumeration: CWE-131.