CWE-426¶
Untrusted Search Path. [File-Handling-Issues, Improper-Control-Of-A-Resource-Through-Its-Lifetime]
Required inputs: IR
This might allow attackers to execute their own programs, access unauthorized data files, or modify configuration in unexpected ways. If the product uses a search path to locate critical resources such as programs, then an attacker could modify that search path to point to a malicious program, which the targeted product would then execute. The problem extends to any type of critical resource that the product trusts.
Some of the most common variants of untrusted search path are:
- In various UNIX and Linux-based systems, the PATH environment variable may be consulted to locate executable programs, and LD_PRELOAD may be used to locate a separate library.
- In various Microsoft-based systems, the PATH environment variable is consulted to locate a DLL, if the DLL is not found in other paths that appear earlier in the search order.
Demonstrative Examples Functional Areas
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
#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:
(attack code)
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
The program finds the attacker's malicious program, "/my/dir/ls". It doesn't find "/bin/ls" because PATH does not contain "/bin/".
The program executes the attacker's malicious program with the raised privileges.
Example 2
This code prints all of the running processes belonging to the current user.
Example Language:PHP (Unsupported language for documentation only)
//assume getCurrentUser() returns a username that is guaranteed to be alphanumeric (avoiding CWE-78)
$userName = getCurrentUser();
$command = 'ps aux | grep ' . $userName;
system($command);
If invoked by an unauthorized web user, it is providing a web page of potentially sensitive information on the underlying system, such as command-line arguments (CWE-497). This program is also potentially vulnerable to a PATH based attack (CWE-426), as an attacker may be able to create malicious versions of the ps or grep commands. While the program does not explicitly raise privileges to run the system commands, the PHP interpreter may by default be running with higher privileges than users.
Example 3
The following code is from a web application that allows users access to an interface through which they can update their password on the system. In this environment, user passwords can be managed using the Network Information System (NIS), which is commonly used on UNIX systems. When performing NIS updates, part of the process for updating passwords is to run a make command in the /var/yp directory. Performing NIS updates requires extra privileges.
Example Language:Java (Unsupported language for documentation only)
...
System.Runtime.getRuntime().exec("make");
...
The problem here is that the program does not specify an absolute path for make and does not clean its environment prior to executing the call to Runtime.exec(). If an attacker can modify the $PATH variable to point to a malicious binary called make and cause the program to be executed in their environment, then the malicious binary will be loaded instead of the one intended. Because of the nature of the application, it runs with the privileges necessary to perform system operations, which means the attacker's make will now be run with these privileges, possibly giving the attacker complete control of the system.
Demonstrative Examples Functional Areas
- Program Invocation
- Code Libraries
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
exec_relative_path |
Untrusted search path ‘{}’. |
None |
False |
Options¶
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
functions¶
functions
Map of process execution functions with the command line string argument position.Type: dict[bauhaus.analysis.config.QualifiedName, int]
Default:
{ 'CreateProcess': 0, 'CreateProcessA': 0, 'CreateProcessW': 0, '_exec': 0, '_execl': 0, '_execle': 0, '_execlp': 0, '_execv': 0, '_execvp': 0, '_execvpe': 0, '_popen': 0, '_wexec': 0, '_wexecl': 0, '_wexecle': 0, '_wexeclp': 0, '_wexecv': 0, '_wexecvp': 0, '_wexecvpe': 0, '_wpopen': 0, 'exec': 0, 'execl': 0, 'execle': 0, 'execlp': 0, 'execv': 0, 'execvp': 0, 'execvpe': 0, 'popen': 0, 'system': 0 }
string_copy_functions¶
string_copy_functions
Map of string copy kind of functions with the target and source argument positions.Type: dict[bauhaus.analysis.config.QualifiedName, tuple]
Default:
{ '__builtin___strcpy_chk': (0, 1), 'strcpy': (0, 1), 'strcpy_s': (0, 2), 'strncpy': (0, 1), 'wcscpy': (0, 1), 'wcscpy_s': (0, 2) }
string_format_functions¶
string_format_functions
Map of sprintf kind of functions with the target and format string argument positions.Type: dict[bauhaus.analysis.config.QualifiedName, tuple]
Default:
{ '__builtin___sprintf_chk': (0, 3), 'snprintf': (0, 2), 'snprintf_s': (0, 2), 'sprintf': (0, 1) }