Class · High

CWE-642: External Control of Critical State Data

The product stores security-critical state information about its users, or the product itself, in a location that is accessible to unauthorized actors.

CWE-642 · Class Level ·13 CVEs ·6 Mitigations

Description

The product stores security-critical state information about its users, or the product itself, in a location that is accessible to unauthorized actors.

If an attacker can modify the state information without detection, then it could be used to perform unauthorized actions or access unexpected resources, since the application programmer does not expect that the state can be changed. State information can be stored in various locations such as a cookie, in a hidden web form field, input parameter or argument, an environment variable, a database record, within a settings file, etc. All of these locations have the potential to be modified by an attacker. When this state information is used to control security or determine resource usage, then it may create a vulnerability. For example, an application may perform authentication, then save the state in an "authenticated=true" cookie. An attacker may simply create this cookie in order to bypass the authentication.

Potential Impact

Access Control

Bypass Protection Mechanism, Gain Privileges or Assume Identity

Confidentiality

Read Application Data

Availability

DoS: Crash, Exit, or Restart

Demonstrative Examples

In the following example, an authentication flag is read from a browser cookie, thus allowing for external control of user state data.
Bad
Cookie[] cookies = request.getCookies();for (int i =0; i< cookies.length; i++) {Cookie c = cookies[i];if (c.getName().equals("authenticated") && Boolean.TRUE.equals(c.getValue())) {authenticated = true;}}
The following code uses input from an HTTP request to create a file name. The programmer has not considered the possibility that an attacker could provide a file name such as "../../tomcat/conf/server.xml", which causes the application to delete one of its own configuration files (CWE-22).
Bad
String rName = request.getParameter("reportName");File rFile = new File("/usr/local/apfr/reports/" + rName);...rFile.delete();
The following code uses input from a configuration file to determine which file to open and echo back to the user. If the program runs with privileges and malicious users can change the configuration file, they can use the program to read any file on the system that ends with the extension .txt.
Bad
fis = new FileInputStream(cfg.getProperty("sub")+".txt");amt = fis.read(arr);out.println(arr);
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.
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:
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
                     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.

Mitigations & Prevention

Architecture and Design

Understand all the potential locations that are accessible to attackers. For example, some programmers assume that cookies and hidden form fields cannot be modified by an attacker, or they may not consider that environment variables can be modified before a privileged program is invoked.

Architecture and Design

Store state information and sensitive data on the server side only. Ensure that the system definitively and unambiguously keeps track of its own state and user state and has rules defined for legitimate state transitions. Do not allow any application user to affect state directly in any way other than through legitimate actions leading to state transitions. If information must be stored on the client, do not do so without encryption and integrity checking,

Architecture and Design

Store state information on the server side only. Ensure that the system definitively and unambiguously keeps track of its own state and user state and has rules defined for legitimate state transitions. Do not allow any application user to affect state directly in any way other than through legitimate actions leading to state transitions.

Architecture and Design

Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482]. With a stateless protocol such as HTTP, use some frameworks can maintain the state for you. Examples include ASP.NET View State and the OWASP ESAPI Session Management feature. Be careful of language features that provide state support, since these might be provided as a convenience to th

Architecture and Design

For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-side checks entirely. Then, these modified values would be submitted to the server.

OperationImplementation

When using PHP, configure the application so that it does not use register_globals. During implementation, develop the application so that it does not rely on this feature, but be wary of implementing a register_globals emulation that is subject to weaknesses such as CWE-95, CWE-621, and similar issues.

Detection Methods

  • Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
  • Fuzzing — Use dynamic tools and techniques that interact with the product using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The product's operation may slow down, but it should not become unstable, crash, or
  • Manual Dynamic Analysis — Use tools and techniques that require manual (human) analysis, such as penetration testing, threat modeling, and interactive tools that allow the tester to record and modify an active session. These may be more effective than strictly automated techniques. This is especially the case with weaknesses

Real-World CVE Examples

CVE IDDescription
CVE-2005-2428Mail client stores password hashes for unrelated accounts in a hidden form field.
CVE-2008-0306Privileged program trusts user-specified environment variable to modify critical configuration settings.
CVE-1999-0073Telnet daemon allows remote clients to specify critical environment variables for the server, leading to code execution.
CVE-2007-4432Untrusted search path vulnerability through modified LD_LIBRARY_PATH environment variable.
CVE-2006-7191Untrusted search path vulnerability through modified LD_LIBRARY_PATH environment variable.
CVE-2008-5738Calendar application allows bypass of authentication by setting a certain cookie value to 1.
CVE-2008-5642Setting of a language preference in a cookie enables path traversal attack.
CVE-2008-5125Application allows admin privileges by setting a cookie value to "admin."
CVE-2008-5065Application allows admin privileges by setting a cookie value to "admin."
CVE-2008-4752Application allows admin privileges by setting a cookie value to "admin."
CVE-2000-0102Shopping cart allows price modification via hidden form field.
CVE-2000-0253Shopping cart allows price modification via hidden form field.
CVE-2008-1319Server allows client to specify the search path, which can be modified to point to a program that the client has uploaded.

Taxonomy Mappings

  • Software Fault Patterns: SFP23 — Exposed Data

Frequently Asked Questions

What is CWE-642?

CWE-642 (External Control of Critical State Data) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Class-level weakness. The product stores security-critical state information about its users, or the product itself, in a location that is accessible to unauthorized actors.

How can CWE-642 be exploited?

Attackers can exploit CWE-642 (External Control of Critical State Data) to bypass protection mechanism, gain privileges or assume identity. This weakness is typically introduced during the Architecture and Design, Implementation phase of software development.

How do I prevent CWE-642?

Key mitigations include: Understand all the potential locations that are accessible to attackers. For example, some programmers assume that cookies and hidden form fields cannot be modified by an attacker, or they may not con

What is the severity of CWE-642?

CWE-642 is classified as a Class-level weakness (High abstraction). It has been observed in 13 real-world CVEs.