T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/capture_idf_help.sh:14
- Finding
- Shell Command Injection Through IDF_PATH<![CDATA[ ## Vulnerability Details **File Location**: `scripts/capture_idf_help.sh`, line 14 **Vulnerability Type**: Shell command injection through an environment variable **Risk Level**: High ### Vulnerable Code ```bash if [[ -n "${IDF_PATH:-}" && -f "$ESPIDF_ROOT/export.sh" ]]; then bash -lc "set -euo pipefail; source \"$ESPIDF_ROOT/export.sh\" >/dev/null; idf.py --help" > "$OUT_REL" elif command -v idf.py >/dev/null 2>&1; then idf.py --help > "$OUT_REL" fi ``` ### Technical Analysis `IDF_PATH` is copied into `ESPIDF_ROOT` and then interpolated into a command string passed to `bash -lc`. Although double quotes are added around the intended path, embedded quotes, command substitutions, shell operators, or other shell syntax in the environment-variable value can escape the intended `source` argument. The preliminary `-f "$ESPIDF_ROOT/export.sh"` check does not make interpolation into a second shell safe. A specially named directory can satisfy the file check while its path remains syntactically dangerous when incorporated into the nested command string. ### Attack Path 1. An attacker influences the `IDF_PATH` environment variable or persuades the user to configure ESP-IDF under a specially crafted directory name. 2. The crafted directory contains an `export.sh` file, allowing the file-existence check to pass. 3. The user executes `scripts/capture_idf_help.sh`. 4. The path is embedded into the argument supplied to `bash -lc`. 5. The nested shell parses attacker-controlled syntax as commands rather than treating the entire value as a literal path. 6. Those commands execute with the privileges of the user running the script. ### Impact Assessment Successful exploitation provides arbitrary local command execution under the invoking user's account. The attacker could read or modify files accessible to that user, alter source code or firmware artifacts, tamper with the generated reference file, or execute additional locally available programs. If the scr ...[truncated 92 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Avoid constructing a command string for `bash -lc`. Source the script directly in the current shell while preserving normal shell argument boundaries: ```bash if [[ -n "${IDF_PATH:-}" && -f "$ESPIDF_ROOT/export.sh" ]]; then source "$ESPIDF_ROOT/export.sh" >/dev/null idf.py --help > "$OUT_REL" elif command -v idf.py >/dev/null 2>&1; then idf.py --help > "$OUT_REL" else echo "ERROR: idf.py not found." >&2 exit 1 fi ``` Additionally: 1. Canonicalize `IDF_PATH` with `realpath`. 2. Require `export.sh` to be a regular file in an expected, trusted ESP-IDF checkout. 3. Reject paths containing control characters. 4. Avoid evaluating any environment-derived value as shell source code. ]]>
