3.6. Using Environment Variables Securely
You need to obtain the value of, alter the value of, or delete an environment variable.
A process inherits its environment variables from its parent process. While the parent process most often will not do anything to tarnish the environment passed on to its children, your program’s environment variables are still external inputs, and you must therefore treat them as such.
The process that parents your own process could be a malicious process that has manipulated the environment in an attempt to confuse your program and exploit that confusion to nefarious ends. As much as possible, it is best to avoid depending on the environment, but we recognize that is not always possible.
In the following subsections, we’ll look at obtaining the value of an environment variable as well as changing and deleting environment variables.
Obtaining the value of an environment variable
normal means by which you obtain the value of an environment variable
is by calling
with the name of the environment variable
whose value is to be retrieved. The problem with
) is that it simply returns a pointer into the environment,
rather than returning a copy of the environment
If you do not immediately make a copy of the value returned by
getenv( ), but instead store the pointer somewhere for later use, you could end up with a dangling pointer or a different value altogether, if the environment is modified between ...