Skip to content

Latest commit

 

History

History
74 lines (63 loc) · 5.81 KB

File metadata and controls

74 lines (63 loc) · 5.81 KB

Here is the combined list of vulnerabilities in markdown format, based on the provided lists. Since the first and third lists essentially indicate an absence of vulnerabilities or no vulnerabilities meeting certain criteria, and the second list details a specific vulnerability, the combined list will primarily feature the vulnerability described in the second list.

Combined Vulnerability List

This document outlines the identified vulnerability after reviewing the provided lists.

Vulnerability: PATH Environment Variable Hijacking Leading to Arbitrary Command Execution

  • Description:
    The homedir package’s Dir() function (in the Unix-specific branch) determines the user’s home directory by falling back to system commands when the HOME environment variable is not set. In particular, on Darwin systems it runs:

    exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
    

    and in other Unix-like systems it runs:

    exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
    

    as well as a fallback:

    exec.Command("sh", "-c", "cd && pwd")
    

    None of these invocations specify an absolute path for the executables (e.g. /bin/sh or /usr/bin/getent), but rely on the process’s PATH environment variable to locate them. If an attacker is able to control or influence the PATH variable in the execution environment—such as through insecure deployment configurations—malicious executables placed in an attacker-controlled directory (with the same names as the expected binaries) would be run instead. An external attacker who can force the process into an environment with a manipulated PATH (for example, via a misconfigured startup script or vulnerable container settings) could thereby trigger execution of their malicious code.

  • Impact:
    Exploitation of this vulnerability could result in arbitrary command execution with the privileges of the running process. In a worst-case scenario—especially if the process runs with elevated privileges—this could lead to a complete system compromise.

  • Vulnerability Rank:
    Critical

  • Currently Implemented Mitigations:

    • The library does not explicitly mitigate this risk internally. It relies on invoking system commands using the default environment, which means the resolution of command names (e.g. "sh" and "getent") is entirely determined by the runtime’s PATH variable.
  • Missing Mitigations:

    • Absolute Path Specification: The code should invoke commands using an absolute path (for example, using /bin/sh instead of "sh").
    • Environment Sanitization: Alternatively, the code should sanitize or explicitly set a safe PATH environment variable for the external command invocations.
    • Reduced Shell Reliance: Avoid using sh -c when it is not necessary or limit the command’s scope to avoid shell interpretation issues.
  • Preconditions:

    • The application is running in an environment where an attacker can influence or control the PATH environment variable (e.g., due to insecure process startup or container misconfiguration).
    • The process runs in a context where these external commands (like sh or getent) are called as part of user request handling (such as through public API endpoints).
    • The affected process has not overridden or sanitized its PATH, thereby allowing an attacker to substitute malicious executables.
  • Source Code Analysis:

    • In the dirUnix() function (starting around line 47), the code first attempts to use the HOME environment variable. If that is not set, on Darwin systems it constructs and executes the command:
      exec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
      This invocation uses the command name "sh" without an absolute path, relying on PATH for its resolution.
    • In the alternative Unix branch (for non-Darwin systems), a similar approach is used with:
      exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
      Again, "getent" is resolved via the current PATH.
    • Finally, a fallback method executes:
      exec.Command("sh", "-c", "cd && pwd")
      which also depends on the PATH to locate sh.
    • Since none of these invocations enforce an absolute location for the executables, an attacker with control over the environment’s PATH variable could force the process to execute a malicious binary instead.
  • Security Test Case:

    1. Preparation:
      • Create a controlled directory (e.g., /tmp/malicious_bin) and place a malicious executable named sh (or getent). This executable could simply write a distinct marker to a log file or echo a predetermined message to prove it was invoked.
    2. Environment Setup:
      • Launch the vulnerable application (or a test harness that calls Dir()) with its PATH environment variable modified so that /tmp/malicious_bin is at the very beginning. For example:
        export PATH=/tmp/malicious_bin:$PATH
        
    3. Triggering the Vulnerability:
      • Cause the library’s home directory resolution to be invoked. This can be done by ensuring that the HOME environment variable is unset (or empty) so that the code falls back to calling one of the external commands.
    4. Observation:
      • Check the logs or outputs that would be produced by the malicious executable. If the marker or expected malicious behavior is observed (such as the creation of a log file, or an altered output from the Dir() function), this confirms that the externally controlled PATH forced the execution of the attacker’s binary.
    5. Confirmation:
      • Repeat the test under controlled conditions to verify that the vulnerability is reproducible and that the malicious executable is consistently invoked.