1030
Hard-code the search path to a set of known-safe values (such as system directories), or only
allow them to be specified by the administrator in a configuration file. Do not allow these settings
to be modified by an external party. Be careful to avoid related weaknesses such as CWE-426
and CWE-428.
Phase: Implementation
When invoking other programs, specify those programs using fully-qualified pathnames. While
this is an effective approach, code that uses fully-qualified pathnames might not be portable to
other systems that do not use the same pathnames. The portability can be improved by locating
the full-qualified paths in a centralized, easily-modifiable location within the source code, and
having the code refer to these paths.
Phase: Implementation
Remove or restrict all environment settings before invoking other programs. This includes the
PATH environment variable, LD_LIBRARY_PATH, and other settings that identify the location of
code libraries, and any application-specific search paths.
Phase: Implementation
Check your search path before use and remove any elements that are likely to be unsafe, such
as the current working directory or a temporary files directory.
Phase: Implementation
Use other functions that require explicit paths. Making use of any of the other readily available
functions that require explicit paths is a safe way to avoid this problem. For example, system() in
C does not require a full path since the shell can take care of it, while execl() and execv() require
a full path.
Demonstrative Examples
Example 1:
This program is intended to execute a command that lists the contents of a restricted directory,
then performs other actions. Assume that it runs with setuid privileges in order to bypass the
permissions check by the operating system.
Example Language: C (Bad)
#define DIR "/restricted/directory"
char cmd[500];
sprintf(cmd, "ls -l %480s", DIR);
/* Raise privileges to those needed for accessing DIR. */
RaisePrivileges(...);
system(cmd);
DropPrivileges(...);
...
This code may look harmless at first, since both the directory and the command are set to fixed
values that the attacker can't control. The attacker can only see the contents for DIR, which is the
intended program behavior. Finally, the programmer is also careful to limit the code that executes
with raised privileges.
However, because the program does not modify the PATH environment variable, the following
attack would work:
Example Language: (Attack)
• The user sets the PATH to reference a directory under the attacker's control, such as "/my/dir/".
• The attacker creates a malicious program called "ls", and puts that program in /my/dir
• The user executes the program.
• When system() is executed, the shell consults the PATH to find the ls program
CWE Version 4.14
CWE-426: Untrusted Search Path
CWE-426: Untrusted Search Path