Description
The code calls sizeof() on a pointer type, which can be an incorrect calculation if the programmer intended to determine the size of the data that is being pointed to.
The use of sizeof() on a pointer can sometimes generate useful information. An obvious case is to find out the wordsize on a platform. More often than not, the appearance of sizeof(pointer) indicates a bug.
Potential Impact
Integrity, Confidentiality
Modify Memory, Read Memory
Demonstrative Examples
double *foo;...foo = (double *)malloc(sizeof(foo));double *foo;...foo = (double *)malloc(sizeof(*foo));/* Ignore CWE-259 (hard-coded password) and CWE-309 (use of password system for authentication) for this example. */
char *username = "admin";char *pass = "password";
int AuthenticateUser(char *inUser, char *inPass) {
printf("Sizeof username = %d\n", sizeof(username));printf("Sizeof pass = %d\n", sizeof(pass));
if (strncmp(username, inUser, sizeof(username))) {printf("Auth failure of username using sizeof\n");return(AUTH_FAIL);}
/* Because of CWE-467, the sizeof returns 4 on many platforms and architectures. */
if (! strncmp(pass, inPass, sizeof(pass))) {printf("Auth success of password using sizeof\n");return(AUTH_SUCCESS);}else {printf("Auth fail of password using sizeof\n");return(AUTH_FAIL);}
}
int main (int argc, char **argv){
int authResult;
if (argc < 3) {ExitError("Usage: Provide a username and password");}authResult = AuthenticateUser(argv[1], argv[2]);if (authResult != AUTH_SUCCESS) {ExitError("Authentication failed");}else {DoAuthenticatedTask(argv[1]);}
}pass5passABCDEFGHpassWORDMitigations & Prevention
Use expressions such as "sizeof(*pointer)" instead of "sizeof(pointer)", unless you intend to run sizeof() on a pointer type to gain some platform independence or if you are allocating a variable on the stack.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Related Weaknesses
Taxonomy Mappings
- CLASP: — Use of sizeof() on a pointer type
- CERT C Secure Coding: ARR01-C — Do not apply the sizeof operator to a pointer when taking the size of an array
- CERT C Secure Coding: MEM35-C — Allocate sufficient memory for an object
- Software Fault Patterns: SFP10 — Incorrect Buffer Length Computation
Frequently Asked Questions
What is CWE-467?
CWE-467 (Use of sizeof() on a Pointer Type) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The code calls sizeof() on a pointer type, which can be an incorrect calculation if the programmer intended to determine the size of the data that is being pointed to.
How can CWE-467 be exploited?
Attackers can exploit CWE-467 (Use of sizeof() on a Pointer Type) to modify memory, read memory. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-467?
Key mitigations include: Use expressions such as "sizeof(*pointer)" instead of "sizeof(pointer)", unless you intend to run sizeof() on a pointer type to gain some platform independence or if you are allocating a variable on t
What is the severity of CWE-467?
CWE-467 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.