putenv-family function argument not accessible outside its scope
This defect occurs when the argument of a putenv-family function
is a local variable with automatic duration.
The function putenv(char *string) inserts a pointer to its supplied
argument into the environment array, instead of making a copy of the argument. If the
argument is an automatic variable, its memory can be overwritten after the function
containing the putenv() call returns. A subsequent call to
getenv() from another function returns the address of an out-of-scope
variable that cannot be dereferenced legally. This out-of-scope variable can cause
environment variables to take on unexpected values, cause the program to stop responding, or
allow arbitrary code execution vulnerabilities.
Use setenv()/unsetenv() to set and unset
environment variables. Alternatively, use putenv-family function
arguments with dynamically allocated memory, or, if your application has no reentrancy
requirements, arguments with static duration. For example, a single thread execution with no
recursion or interrupts does not require reentrancy. It cannot be called (reentered) during
its execution.
| Group: Static memory |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax:
PUTENV_AUTO_VAR
|
| Impact: High |
| CWE ID: 562, 686, 825 |