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.
This document outlines the identified vulnerability after reviewing the provided lists.
-
Description:
Thehomedir
package’sDir()
function (in the Unix-specific branch) determines the user’s home directory by falling back to system commands when theHOME
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’sPATH
environment variable to locate them. If an attacker is able to control or influence thePATH
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 manipulatedPATH
(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’sPATH
variable.
- 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.
-
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.
- Absolute Path Specification: The code should invoke commands using an absolute path (for example, using
-
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
orgetent
) 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.
- The application is running in an environment where an attacker can influence or control the
-
Source Code Analysis:
- In the
dirUnix()
function (starting around line 47), the code first attempts to use theHOME
environment variable. If that is not set, on Darwin systems it constructs and executes the command:This invocation uses the command nameexec.Command("sh", "-c", `dscl -q . -read /Users/"$(whoami)" NFSHomeDirectory | sed 's/^[^ ]*: //'`)
"sh"
without an absolute path, relying onPATH
for its resolution. - In the alternative Unix branch (for non-Darwin systems), a similar approach is used with:
Again,
exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
"getent"
is resolved via the currentPATH
. - Finally, a fallback method executes:
which also depends on the
exec.Command("sh", "-c", "cd && pwd")
PATH
to locatesh
. - 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.
- In the
-
Security Test Case:
- Preparation:
- Create a controlled directory (e.g.,
/tmp/malicious_bin
) and place a malicious executable namedsh
(orgetent
). This executable could simply write a distinct marker to a log file or echo a predetermined message to prove it was invoked.
- Create a controlled directory (e.g.,
- Environment Setup:
- Launch the vulnerable application (or a test harness that calls
Dir()
) with itsPATH
environment variable modified so that/tmp/malicious_bin
is at the very beginning. For example:export PATH=/tmp/malicious_bin:$PATH
- Launch the vulnerable application (or a test harness that calls
- 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.
- Cause the library’s home directory resolution to be invoked. This can be done by ensuring that the
- 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 controlledPATH
forced the execution of the attacker’s binary.
- 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
- Confirmation:
- Repeat the test under controlled conditions to verify that the vulnerability is reproducible and that the malicious executable is consistently invoked.
- Preparation: