21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Typically, Unix systems are considerably more dependent on environment variables<br />

than are Windows systems. In fact, the only scenario common to both Unix and<br />

Windows is that there is an environment variable defining the path that the system<br />

should search to find an executable or shared library (although differently named<br />

variables are used on each platform). On Windows, one environment variable controls<br />

the search path for finding both executables and shared libraries. On Unix,<br />

these are controlled by separate environment variables. Generally, you should not<br />

specify a filename and then rely on these variables for determining the full path.<br />

Instead, you should always use absolute paths to known locations. *<br />

Certain variables expected to be present in the environment can cause insecure program<br />

behavior if they are missing or improperly set. Make sure, therefore, that you<br />

never fully purge the environment and leave it empty. Instead, variables that should<br />

exist should be forced to sane values or, at the very least, treated as highly suspect<br />

and examined closely before they’re used. Remove any unknown variables from the<br />

environment altogether.<br />

Discussion<br />

The standard C runtime library defines a global variable, † environ, asaNULL-terminated<br />

array of strings, where each string in the array is of the form “name=value”.<br />

Most systems do not declare the variable in any standard header file, Linux being the<br />

notable exception, providing a declaration in unistd.h. You can gain access to the<br />

variable by including the following extern statement in your code:<br />

extern char **environ;<br />

Several functions defined in stdlib.h, such as getenv( ) and putenv( ), provide access<br />

to environment variables, and they all operate on this variable. You can therefore<br />

make changes to the contents of the array or even build a new array and assign it to<br />

the variable.<br />

This variable also exists in the standard C runtime library on Windows; however, the<br />

C runtime on Windows is not as tightly bound to the operating system as it is on<br />

Unix. Directly manipulating the environ variable on Windows will not necessarily<br />

produce the same effects as it will on Unix; in the majority of Windows programs,<br />

the C runtime is never used at all, instead favoring the Win32 API to perform the<br />

same functions as those provided by the C runtime. Because of this, and because of<br />

Windows’ lack of dependence on environment variables, we do not recommend<br />

* Note that the shared library environment variable can be relatively benign on modern Unix-based operating<br />

systems, because the environment variable will get ignored when a program that can change permissions (i.e.,<br />

a setuid program) is invoked. Nonetheless, it is better to be safe than sorry!<br />

† The use of the term “variable” can quickly become confusing because C defines variables and the environment<br />

defines variables. In this recipe, when we are referring to a C variable, we simply say “variable,” and<br />

when we are referring to an environment variable, we say “environment variable.”<br />

2 | Chapter 1: Safe Initialization<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!