CWE-426: Untrusted Search Path
Description
The product searches for critical resources using an externally-supplied search path that can point to resources that are not under the product's direct control.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Extended Description
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.
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. 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:
// /* Raise privileges to those needed for accessing DIR. *///
#define DIR "/restricted/directory"char cmd[500];sprintf(cmd, "ls -l %480s", DIR);RaisePrivileges(...);system(cmd);DropPrivileges(...);...
<xhtml_ul><xhtml_li>The user sets the PATH to reference a directory under the attacker's control, such as "/my/dir/".</xhtml_li><xhtml_li>The attacker creates a malicious program called "ls", and puts that program in /my/dir</xhtml_li><xhtml_li>The user executes the program.</xhtml_li><xhtml_li>When system() is executed, the shell consults the PATH to find the ls program</xhtml_li><xhtml_li>The program finds the attacker's malicious program, "/my/dir/ls". It doesn't find "/bin/ls" because PATH does not contain "/bin/".</xhtml_li><xhtml_li>The program executes the attacker's malicious program with the raised privileges.</xhtml_li></xhtml_ul>
Example - 2
This code prints all of the running processes belonging to the current user. 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.
// //assume getCurrentUser() returns a username that is guaranteed to be alphanumeric (avoiding CWE-78)//
$userName = getCurrentUser();$command = 'ps aux | grep ' . $userName;system($command);
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. 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.
...System.Runtime.getRuntime().exec("make");...
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-98: Improper Control of Filename for Include/Require Statement in PHP Program ('PHP Remote File Inclusion')
CWE-427: Uncontrolled Search Path Element
CWE-428: Unquoted Search Path or Element
CWE-642: External Control of Critical State Data
CWE-668: Exposure of Resource to Wrong Sphere
CWE-673: External Influence of Sphere Definition
Visit http://cwe.mitre.org/ for more details.