Use of automatic variable as putenv-family function argument
putenv-family function argument not accessible outside its scope
Description
This defect occurs when the argument of a putenv-family function
is a local variable with automatic duration.
Risk
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.
Fix
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.
Examples
Result Information
| Group: Static memory |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax:
PUTENV_AUTO_VAR
|
| Impact: High |
Version History
Introduced in R2017b
See Also
Pointer or reference to stack variable leaving scope | Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)