21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

pointer or a different value altogether, if the environment is modified between the<br />

time that you called getenv( ) and the time you use the pointer it returns.<br />

There is a race condition here even after you call getenv() and before<br />

you copy. Be careful to only manipulate the process environment from<br />

a single thread at a time.<br />

Never make any assumptions about the length or the contents of an environment<br />

variable’s value. It can be extremely dangerous to simply copy the value into a statically<br />

allocated buffer or even a dynamically allocated buffer that was not allocated<br />

based on the actual size of the environment variable’s value. Always compute the size<br />

of the environment variable’s value yourself, and dynamically allocate a buffer to<br />

hold the copy.<br />

Another problem with environment variables is that a malicious program could<br />

manipulate the environment so that two or more environment variables with the<br />

same name exist in your process’s environment. It is easy to detect this situation, but<br />

it usually is not worth concerning yourself with it. Most, if not all, implementations<br />

of getenv( ) will always return the first occurrence of an environment variable.<br />

As a convenience, you can use the function spc_getenv( ), shown in the following<br />

code, to obtain the value of an environment variable. It will return a copy of the environment<br />

variable’s value allocated with strdup( ), which means that you will be<br />

responsible for freeing the memory with free( ).<br />

#include <br />

#include <br />

char *spc_getenv(const char *name) {<br />

char *value;<br />

if (!(value = getenv(name))) return 0;<br />

return strdup(value);<br />

}<br />

Changing the value of an environment variable<br />

The standard C runtime function putenv( ) is normally used to modify the value of<br />

an environment variable. In some implementations, putenv( ) can even be used to<br />

delete environment variables, but this behavior is nonstandard and therefore is not<br />

portable. If you have sanitized the environment as described in Recipe 1.1, and particularly<br />

if you use the code in that recipe, using putenv( ) could cause problems<br />

because of the way that code manages the memory allocated to the environment. We<br />

recommend that you avoid using the putenv( ) function altogether.<br />

Another reason to avoid putenv( ) is that an attacker could have manipulated the<br />

environment before spawning your process, in such a way that two or more environment<br />

variables share the same name. You want to make certain that changing the<br />

Using Environment Variables Securely | 93<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!