Some environments provide environment pointers that are valid when main() is called, but may be invalided by operations that modify the environment.
According to C99: \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] |
In a hosted environment, the main function receives a third argument, {{char \*envp\[\]}}, that points to a null-terminated array of pointers to {{char}}, each of which points to a string that provides information about the environment for this execution of the program.
Consequently, under a hosted environments it is possible to access the environment through a modified form of main():
main(int argc, char *argv[], char *envp[]) |
However, modifying the environment by using the {{setenv()}} or {{putenv()}} functions, or by any other means, may cause the environment memory to be reallocated, with the result that {{envp}} now references an incorrect location. For example, POSIX says the following: \[[Open Group 04|AA. C References#Open Group 04]\] |
Unanticipated results may occur if
setenv()changes the external variableenviron. In particular, if the optionalenvpargument tomain()is present, it is not changed, and as a result may point to an obsolete copy of the environment (as may any other copy ofenviron).
Microsoft notes the following about {{getenv()}}: \[[MSDN|AA. C References#MSDN]\] |
getenvand_putenvuse the copy of the environment pointed to by the global variable_environto access the environment.getenvoperates only on the data structures accessible to the runtime library and not on the environment "segment" created for the process by the operating system. Consequently, programs that use theenvpargument tomainorwmainmay retrieve invalid information.
The Visual C++ reference notes the following about {{envp}}: \[[MSDN|AA. C References#MSDN]\] |
The environment block passed to main and wmain is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change.
When compiled with GCC version 3.4.6 and run on a 32-bit Intel GNU/Linux machine, the following code:
extern char **environ;
/* ... */
int main(int argc, char const *argv[], char const *envp[]) {
printf("environ: %p\n", environ);
printf("envp: %p\n", envp);
setenv("MY_NEW_VAR", "new_value", 1);
puts("--Added MY_NEW_VAR--");
printf("environ: %p\n", environ);
printf("envp: %p\n", envp);
}
|
Yields:
% ./envp-environ environ: 0xbf8656ec envp: 0xbf8656ec --Added MY_NEW_VAR-- environ: 0x804a008 envp: 0xbf8656ec |
It is evident from these results that the environment has been relocated as a result of the call to setenv().
After a call to the POSIX setenv() function, or other function that modifies the environment, the envp pointer may no longer reference the environment.
int main(int argc, char const *argv[], char const *envp[]) {
size_t i;
setenv("MY_NEW_VAR", "new_value", 1);
if (envp != NULL) {
for (i = 0; envp[i] != NULL; i++) {
puts(envp[i]);
}
}
return 0;
}
|
Because envp no longer points to the current environment, this program has undefined behavior.
Use environ in place of envp when defined.
extern char **environ;
/* ... */
int main(int argc, char const *argv[]) {
size_t i;
setenv("MY_NEW_VAR", "new_value", 1);
if (environ != NULL) {
for (i = 0; environ[i] != NULL; i++) {
puts(environ[i]);
}
}
return 0;
}
|
Use _environ in place of envp when defined.
_CRTIMP extern char **_environ;
/* ... */
int main(int argc, char const *argv[]) {
size_t i;
_putenv_s("MY_NEW_VAR", "new_value", 1);
if (_environ != NULL) {
for (i = 0; _environ[i] != NULL; i++) {
puts(_environ[i]);
}
}
return 0;
}
|
Note: if you have a great deal of unsafe envp code, you can save time in your remediation by aliasing. Change:
int main(int argc, char *argv[], char *envp[]) {
/* ... */
}
|
To:
#if defined (_POSIX_) || defined (__USE_POSIX)
extern char **environ;
#define envp environ
#else
_CRTIMP extern char **_environ;
#define envp _environ
#endif
int main(int argc, char *argv[]) {
/* ... */
}
|
Using the envp environment pointer after the environment has been modified may result in undefined behavior.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
ENV31-C |
low |
probable |
medium |
P4 |
L3 |
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section J.5.1, "Environment Arguments"
\[[MSDN|AA. C References#MSDN]\] [getenv, _wgetenv|http://msdn.microsoft.com/en-us/library/tehxacec.aspx], [_environ, _wenviron|http://msdn.microsoft.com/en-us/library/stxk41x1.aspx], [_putenv_s, _wputenv_s|http://msdn.microsoft.com/en-us/library/eyw7eyfw.aspx]
\[[Open Group 04|AA. C References#Open Group 04]\] [{{setenv()}}|http://www.opengroup.org/onlinepubs/009695399/functions/setenv.html] |
ENV30-C. Do not modify the string returned by getenv() 10. Environment (ENV) ENV32-C. No atexit handler should terminate in any way other than by returning