CWE-114: Process Control

Learn about CWE-114 (Process Control), its security impact, exploitation methods, and prevention guidelines.

What is Process Control?

• Overview: Process Control (CWE-114) is a vulnerability where software executes commands or loads libraries from untrusted sources or environments, potentially allowing attackers to run malicious commands.

• Exploitation Methods:

  • Attackers exploit this vulnerability by injecting data that alters the command the application executes.
  • Common techniques include command injection and manipulating environment variables to change the execution context.

• Security Impact:

  • Direct consequences include unauthorized command execution, which can lead to data breaches, system compromise, or service disruptions.
  • Potential cascading effects involve further exploitation of system vulnerabilities, leading to broader network penetration.
  • Business impact includes loss of data integrity, reputational damage, and financial losses due to system downtime or data theft.

• Prevention Guidelines:

  • Validate and sanitize all input data to ensure it doesn’t alter command execution paths.
  • Implement least privilege principles, restricting permissions to only what is necessary for execution.
  • Use secure APIs and libraries that limit direct command execution from user inputs.
  • Employ security tools like static code analyzers to detect potential vulnerabilities early in the development process.
  • Regularly update and patch systems to protect against known vulnerabilities.
Corgea can automatically detect and fix Process Control in your codebase. [Try Corgea free today](https://corgea.app).

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: Not Language-Specific

Affected Technologies: Not specified

Vulnerable Code Example

import os

def load_module(module_name):
    # Vulnerable code: loading a module based on user input without validation
    # This can lead to loading malicious modules if the input is controlled by an attacker
    os.system(f"python3 -m {module_name}")  # Executes input directly in the shell

user_input = input("Enter the module to load: ")
load_module(user_input)  # User input is directly passed to the function

Explanation

In the vulnerable code example, the load_module function uses os.system to execute a command that includes user input without any validation. This can lead to a security vulnerability known as CWE-114 (Process Control), where an attacker could input a malicious module name or command, leading to arbitrary code execution.

How to fix Process Control?

To fix the process control vulnerability, it's essential to ensure that only trusted and verified modules can be loaded. This can be achieved by:

  1. Whitelisting: Define a list of allowed modules that can be loaded. Only allow modules that are explicitly listed.

  2. Input Validation: Sanitize and validate the user input to ensure it matches the expected format and content.

  3. Avoiding Direct Shell Execution: Use safer alternatives to execute commands or load modules, such as using internal APIs or functions that don't rely on shell execution.

These practices ensure that the application only performs actions that are explicitly intended, preventing unauthorized or malicious module execution.

Fixed Code Example

import importlib

def load_module(module_name):
    # Fixed code: Use a whitelist to ensure only trusted modules are loaded
    trusted_modules = ["math", "os", "sys"]  # Define a list of allowed modules
    if module_name in trusted_modules:  # Check if the module is in the trusted list
        # Use a safer method to load modules rather than relying on shell execution
        importlib.import_module(module_name)  # Safely import the module
    else:
        print("Error: Untrusted module")  # Inform the user if the module is not trusted

user_input = input("Enter the module to load: ")
load_module(user_input)  # Process the user input safely

Explanation

In the fixed code example, we mitigate the vulnerability by implementing a whitelist of trusted modules. The importlib.import_module() function is used instead of os.system(), which avoids direct shell execution. This change ensures that only modules explicitly listed in trusted_modules can be loaded, significantly enhancing the security of the module-loading functionality. The user is informed if they attempt to load an untrusted module, preventing any unauthorized or malicious actions.

Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-114: Process Control and get remediation guidance

Start for free and no credit card needed.