Skip to content

Latest commit

 

History

History
157 lines (135 loc) · 12.2 KB

File metadata and controls

157 lines (135 loc) · 12.2 KB

Here is the combined list of vulnerabilities from the provided lists, formatted as markdown:

Combined Vulnerability List

This document outlines the identified security vulnerabilities in the rootcerts library. Each vulnerability is detailed with its description, impact, rank, existing and missing mitigations, preconditions, source code analysis, and a security test case.

1. Path Traversal in Certificate Loading

  • Description: The rootcerts library is vulnerable to path traversal. When loading CA certificates from a file path (CAFile) or a directory path (CAPath) as specified in the Config, the library directly uses these paths without proper sanitization. If an attacker can control these paths—for example, through environment variables—they can inject traversal sequences like "../" to access files outside the intended certificate directories. This allows reading arbitrary files on the system.

    Step-by-step triggering scenario:

    1. An application using the rootcerts library allows external configuration of CAFile or CAPath.
    2. An attacker gains control over the values of these configuration parameters, for instance, by manipulating environment variables.
    3. The attacker sets CAFile or CAPath to a path containing traversal sequences (e.g., ../../../sensitive_file.txt).
    4. When the application initializes TLS using rootcerts, the library attempts to load certificates from the attacker-controlled path. Due to the lack of sanitization, the library reads the file specified by the path traversal.
  • Impact: Successful path traversal can lead to arbitrary file reading. An attacker could potentially access sensitive information such as application configuration files, source code, or other system files. This information disclosure can severely compromise the application's security and potentially the underlying system.

  • Vulnerability Rank: High

  • Currently implemented mitigations: None. The library directly uses the provided paths with ioutil.ReadFile and filepath.Walk without any input validation or sanitization.

  • Missing mitigations: Input sanitization for CAFile and CAPath in the Config is crucial. The library should implement validation and sanitization of these paths to prevent traversal attacks. Recommended mitigations include:

    • Verifying that the provided paths are within an expected base directory.
    • Rejecting paths that contain ".." components.
    • Ensuring that resolved paths do not escape the intended base directory.
  • Preconditions:

    • An application utilizes the rootcerts library for TLS configuration.
    • The application permits external configuration of CAFile or CAPath, possibly through environment variables or user-provided input.
    • An attacker is capable of controlling the values of these configuration parameters.
  • Source code analysis:

    • The vulnerability is located in rootcerts.go within the LoadCAFile and LoadCAPath functions.
    • In LoadCAFile, the caFile path from the Config is directly passed to ioutil.ReadFile() without any validation:
      func LoadCAFile(caFile string) (*x509.CertPool, error) {
          // ...
          pem, err := ioutil.ReadFile(caFile) // Vulnerable line: caFile is not sanitized
          // ...
      }
    • Similarly, in LoadCAPath, the caPath and the path variable within the filepath.Walk function are used directly with ioutil.ReadFile() without sanitization:
      func LoadCAPath(caPath string) (*x509.CertPool, error) {
          // ...
          walkFn := func(path string, info os.FileInfo, err error) error {
              // ...
              pem, err := ioutil.ReadFile(path) // Vulnerable line: path is not sanitized
              // ...
          }
          err := filepath.Walk(caPath, walkFn) // Vulnerable line: caPath is not sanitized
          // ...
      }
    • The absence of path sanitization in both functions allows an attacker to manipulate the paths to read files outside the intended certificate directories.
  • Security test case:

    1. Setup: Create a test directory structure:
      /tmp/test_rootcerts/app/
      /tmp/test_rootcerts/certs/cacert.pem  (Valid PEM certificate file)
      /tmp/test_rootcerts/sensitive_file.txt (File to be accessed via path traversal)
      
      Populate cacert.pem with a valid PEM certificate and sensitive_file.txt with sensitive content.
    2. Application: Create a Go application test_app.go in /tmp/test_rootcerts/app/ that uses rootcerts, configurable via the TEST_CAFILE environment variable:
      package main
      // ... (code from provided example) ...
    3. Compilation: Compile the application: go build test_app.go
    4. Baseline Test (No Traversal): Run the application with a valid certificate path:
      export TEST_CAFILE=/tmp/test_rootcerts/certs/cacert.pem
      cd /tmp/test_rootcerts/app/
      ./test_app
      Observe the expected x509 error, but not the content of sensitive_file.txt.
    5. Path Traversal Test: Run the application with a path traversal payload:
      export TEST_CAFILE='../../../sensitive_file.txt'
      cd /tmp/test_rootcerts/app/
      ./test_app
      Expected Outcome: The application will attempt to read and parse /tmp/test_rootcerts/sensitive_file.txt as a certificate. The output will either display the content of sensitive_file.txt if os.ReadFile succeeds, or show an error indicating PEM parsing failure on the content of sensitive_file.txt, confirming successful path traversal.

2. Unvalidated HOME Environment Variable Allows Keychain Path Manipulation (Darwin-specific)

  • Description: This vulnerability is specific to Darwin (macOS) systems. The rootcerts library, in its Darwin-specific implementation, retrieves certificates from system and user keychains. While system keychain paths are hardcoded, the path to the user's login keychain is dynamically constructed using the user's home directory, obtained via homedir.Dir(). This function typically relies on the HOME environment variable without validation. If an attacker can control the HOME environment variable (e.g., in containerized environments or through misconfiguration), they can manipulate the login keychain path. By placing a crafted keychain file at this attacker-controlled path, they can inject malicious CA certificates into the TLS certificate pool when LoadSystemCAs() is called.

    Step-by-step triggering scenario:

    1. An attacker gains control over the HOME environment variable of the process running the application.
    2. The attacker sets HOME to a malicious directory, for example, /tmp/malicious_home.
    3. When certKeychains() is executed on a Darwin system, it uses the manipulated HOME value to construct the login keychain path, such as /tmp/malicious_home/Library/Keychains/login.keychain.
    4. The attacker creates the directory structure /tmp/malicious_home/Library/Keychains/ and places a crafted login.keychain file containing attacker-controlled PEM certificates within it.
    5. During TLS configuration, LoadSystemCAs() iterates over keychain paths, including the manipulated login keychain path, and executes /usr/bin/security find-certificate -a -p <malicious_keychain_path>.
    6. The output from the security command, now including certificates from the attacker's keychain, is incorporated into the TLS certificate pool.
  • Impact: Successfully injecting malicious CA certificates allows an attacker to perform man-in-the-middle attacks on TLS connections. The attacker can issue fraudulent certificates for any domain, compromising session confidentiality and data integrity. This can lead to complete compromise of TLS-protected communications.

  • Vulnerability Rank: High (potentially Critical in vulnerable deployments)

  • Currently implemented mitigations:

    • The library uses the macOS security tool (/usr/bin/security) to extract certificates from keychains, which is a trusted system utility.
    • Two of the three keychain paths are hardcoded, pointing to system-level keychains. However, there is no validation of the HOME environment variable or the path returned by homedir.Dir(), making it vulnerable to manipulation.
  • Missing mitigations:

    • HOME environment variable validation: The library should validate the directory obtained from homedir.Dir() to ensure it aligns with expected patterns or is within a trusted location.
    • Configurable login keychain path: Provide an option to explicitly configure the login keychain path, overriding the derivation from HOME, allowing for secure deployments where HOME might be untrusted.
    • Path whitelisting/sandboxing: Before using a keychain file, the library should verify that the file resides within a set of predefined, trusted directories.
  • Preconditions:

    • The application is running on a Darwin (macOS) system, triggering the Darwin-specific code path in rootcerts.
    • An attacker can influence the environment in which the application is launched, such as through container misconfiguration, insecure orchestration, or manipulated startup scripts.
    • The attacker can create a crafted keychain file at the location derived from the manipulated HOME environment variable.
  • Source code analysis:

    • In /code/rootcerts_darwin.go, the certKeychains() function constructs the keychain paths:
      func certKeychains() []string {
          keychains := []string{
              "/System/Library/Keychains/SystemRootCertificates.keychain",
              "/Library/Keychains/System.keychain",
          }
          home, err := homedir.Dir()
          if err == nil {
              loginKeychain := path.Join(home, "Library", "Keychains", "login.keychain")
              keychains = append(keychains, loginKeychain)
          }
          return keychains
      }
      The loginKeychain path is derived directly from the output of homedir.Dir(), which is typically the HOME environment variable, without any validation.
    • LoadSystemCAs() iterates through these keychain paths and executes the security command:
      func LoadSystemCAs() (*x509.CertPool, error) {
          // ...
          for _, keychain := range certKeychains() {
              cmd := exec.Command("/usr/bin/security", "find-certificate", "-a", "-p", keychain)
              data, err := cmd.Output()
              // ...
          }
          // ...
      }
    • The lack of validation on the loginKeychain path allows an attacker to control the input to the security command by manipulating the HOME environment variable and placing a malicious keychain file at the derived path.
  • Security test case:

    1. Environment Setup: Execute the test on a Darwin system or a simulated Darwin environment. Set the HOME environment variable to a temporary directory, e.g., /tmp/malicious_home, before running the test.
    2. Malicious Keychain Creation:
      • Create the directory structure: /tmp/malicious_home/Library/Keychains/.
      • Create a crafted login.keychain file within this directory. This keychain should include a self-signed certificate with a known fingerprint that is not part of the system's trusted certificates.
    3. LoadSystemCAs() Execution: Invoke the LoadSystemCAs() function within the test environment where HOME is set to /tmp/malicious_home.
    4. Validation: Examine the returned certificate pool. Verify that it contains the certificate from the crafted malicious keychain. This can be done by checking the SHA-256 fingerprint of the certificates in the pool against the known fingerprint of the injected certificate.
    5. Control Test: Repeat the test with HOME set to a trusted user directory. Confirm that the malicious certificate is not loaded in this scenario.
    6. Expected Result: When HOME is manipulated, LoadSystemCAs() should load certificates from the attacker-controlled keychain, demonstrating the vulnerability. When HOME is set to a trusted path, the malicious certificate should not be loaded.