From a2ebcf072a73645d886d66c1703998cac6f427d4 Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:40:02 -0600 Subject: [PATCH] feat(rules): flag preloaded libraries and risky library paths (SG-018) Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- CHANGELOG.md | 4 + README.md | 8 +- src/skillshield/rules.py | 72 ++++++++++++++++ tests/test_sg018_library_preload.py | 129 ++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 tests/test_sg018_library_preload.py diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8f898..a33fbbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project are listed here. The format follows [Keep a ## [Unreleased] +### Added + +- SG-018, Library Preloaded Into Other Programs (MEDIUM): flags `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` set to a value, `LD_LIBRARY_PATH` pointing at a temporary or download folder, and writes to `/etc/ld.so.preload` (#103). + ### Changed - The README explains choosing a version of the GitHub Action: `@v1` gets each new 1.x release, a pinned release such as `@v1.4.0` stays fixed, and `fail-on: none` tries a release without failing the build. diff --git a/README.md b/README.md index e21dae6..7c22445 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ An agent skill is a file of instructions or code that an AI agent loads and foll - Snyk's ToxicSkills study scanned 3,984 skills from ClawHub and skills.sh. 534 skills (13.4%) had at least one critical issue. 1,467 skills had at least one security issue. Snyk confirmed 76 malicious payloads. [[2]](#sources) - The OWASP Agentic Skills Top 10 lists Malicious Skills as AST01 and Supply Chain Compromise as AST02. [[3]](#sources) -skillshield checks skill files for these patterns with 15 rules. It runs locally and needs no network access. +skillshield checks skill files for these patterns with 16 rules. It runs locally and needs no network access. --- @@ -428,6 +428,7 @@ and never calls a tool: the scan works offline and sends nothing. | SG-013 | HIGH | Untrusted Package Registry | `pip`, `npm` or `yarn` pointed at a registry other than the official one -- `--index-url`, `--extra-index-url`, `-i`, `--registry`, `PIP_INDEX_URL`, `npm config set registry`, `yarn config set npmRegistryServer`, or a `pip.conf`, `.npmrc` or `.pypirc` line that sets an untrusted registry (`registry`, `@scope:registry`, `index-url`, `repository`). TestPyPI counts as untrusted: anyone can publish there. A later install then pulls the attacker's packages (dependency confusion, OWASP AST02). | | SG-014 | HIGH | Compiled Python Payload | A `.pyc` or `.pyo` file in a skill folder. Bytecode cannot be reviewed, and Python imports or runs it directly, so a payload can hide behind a harmless-looking skill that claims to call a `.py` script. If the matching `.py` source is missing, the finding names that source file (its file name, not its full path) and asks you to ship the source instead. | | SG-015 | MEDIUM | Well-known Skill Name Impersonation | Frontmatter names that closely resemble canonical skill names or add suspicious suffixes such as `1` or `-pro`. | +| SG-018 | MEDIUM | Library Preloaded Into Other Programs | `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` set to a value (`export`, `env`, `setenv`, Dockerfile `ENV`, or a leading assignment), `LD_LIBRARY_PATH` with a temporary or download folder among its entries (`/tmp`, `/var/tmp`, `/dev/shm`, `~/Downloads`), and writes to `/etc/ld.so.preload` (a redirect, `tee`, `cp`, `mv`, `install` or Python `open` for writing). A library loaded this way runs inside every program started afterwards. MEDIUM because some legitimate skills do this: the finding asks the reviewer to check why. Mentions that only explain the variables, `unset`, an empty value, and reading `/etc/ld.so.preload` are not flagged. | | SG-103 | HIGH | Unrestricted Path Parameter | Each filesystem path parameter needs its own `pattern` or `enum` (including array `items`); a restriction on another field does not hide it. | | SG-104 | HIGH | Suspicious Tool Name | An MCP tool in a saved listing whose name suggests harmful behaviour, such as `harvest_credentials`, `exfiltrate_data`, `steal_cookies`, `leak_secrets`, `bypass_auth`, `intercept_requests`, `sniff_traffic`, `dump_credentials` or `upload_all_files`. Words are matched whole, so `stealth_mode_toggle` and `dump_truck_lookup` are not flagged. | | SG-105 | MEDIUM | Tool with No Description or Input Schema | An MCP tool in a saved listing with a missing or blank description, or no input schema. | @@ -483,6 +484,7 @@ Each rule carries tags such as `owasp-asi01` and `owasp-ast01`. They appear in ` | SG-013 | Untrusted Package Registry | ASI04 Agentic Supply Chain Vulnerabilities | AST02 Supply Chain Compromise | | SG-014 | Compiled Python Payload | ASI04 Agentic Supply Chain Vulnerabilities | AST01 Malicious Skills; AST08 Poor Scanning | | SG-015 | Well-known Skill Name Impersonation | ASI04 Agentic Supply Chain Vulnerabilities | AST04 Insecure Metadata | +| SG-018 | Library Preloaded Into Other Programs | ASI05 Unexpected Code Execution | AST01 Malicious Skills | | SG-103 | Unrestricted Path Parameter | ASI02 Tool Misuse and Exploitation | AST01 Malicious Skills | | SG-104 | Suspicious Tool Name | ASI02 Tool Misuse and Exploitation | AST01 Malicious Skills | | SG-105 | Tool with No Description or Input Schema | ASI02 Tool Misuse and Exploitation | AST04 Insecure Metadata | @@ -715,7 +717,7 @@ skillshield scan ./skills/ --format sarif > skillshield.sarif - CRITICAL and HIGH findings have the level `error`. MEDIUM is `warning`, LOW is `note` and INFO is `none`. - Each result has one location per matched line, up to 5. A finding with no line numbers, such as SG-011, points at line 1. -- The rule list in the log always holds all 15 rules. +- The rule list in the log always holds all 16 rules. - Files the scan did not open are listed in `runs[0].properties.unscanned`. GitHub code scanning reads `runs[].results[]` only, so that field does not appear on the Security tab; read the SARIF file or the CLI JSON to see it. --- @@ -805,7 +807,7 @@ It takes the settings above as environment variables (`SKILLSHIELD_PATHS`, `SKIL A rule needs an `id`, `name`, `severity`, `description` and `remediation`, plus a `pattern` (a compiled regex) or a `check_fn` (a function that takes the text and returns a list of matched strings). -`SkillScanner(rules=[my_rule])` runs only `my_rule`. To add a rule to the 15 defaults, pass `rules=ALL_RULES + [my_rule]`. +`SkillScanner(rules=[my_rule])` runs only `my_rule`. To add a rule to the 16 defaults, pass `rules=ALL_RULES + [my_rule]`. ```python import re diff --git a/src/skillshield/rules.py b/src/skillshield/rules.py index a253862..528f886 100644 --- a/src/skillshield/rules.py +++ b/src/skillshield/rules.py @@ -3553,6 +3553,77 @@ def _check_brand_name_impersonation(text: str) -> list[str]: ) +# ── SG-018: Library Preloaded Into Other Programs ───────────── + +# LD_PRELOAD (Linux) and DYLD_INSERT_LIBRARIES (macOS) load a library into every +# program started afterwards, and /etc/ld.so.preload does the same for the whole +# machine. Some legitimate skills do this, so the finding asks for a review. +_PRELOAD_VARIABLE = r"(?:LD_PRELOAD|DYLD_INSERT_LIBRARIES)" + +# "LD_PRELOAD=/tmp/x.so", "export DYLD_INSERT_LIBRARIES=x", "env LD_PRELOAD=x cmd", +# Dockerfile "ENV LD_PRELOAD /x" and csh "setenv LD_PRELOAD /x". No space is allowed +# after "=": in a shell "LD_PRELOAD= cmd" sets an empty value, which clears it. +_PRELOAD_ASSIGNMENT = re.compile( + rf"(?>?[ \t]*|\btee\b(?:[ \t]+-\w+)*[ \t]+" + r"|\b(?:cp|mv|install)\b(?:[ \t]+-[\w-]+(?:[ \t]+[0-7]{3,4}(?![\w.-]))?)*[ \t]+[^\s|;&]+[ \t]+)" + r"['\"]?/etc/ld\.so\.preload(?![\w.-])" + r"|\bopen\([ \t]*['\"]/etc/ld\.so\.preload['\"][ \t]*,[ \t]*['\"][wa]", +) + + +def _check_library_preload(text: str) -> list[str]: + hits: list[str] = [] + seen: set[str] = set() + found = [m.group(0) for m in _PRELOAD_ASSIGNMENT.finditer(text)] + for match in _LIBRARY_PATH_VALUE.finditer(text): + if any(_RISKY_LIBRARY_FOLDER.match(entry) for entry in match.group(1).split(":")): + found.append(match.group(0)) + found += [m.group(0) for m in _LD_SO_PRELOAD_WRITE.finditer(text)] + for hit in found: + hit = hit.strip() + if hit not in seen: + seen.add(hit) + hits.append(hit) + return hits + + +RULE_LIBRARY_PRELOAD = Rule( + id="SG-018", + name="Library Preloaded Into Other Programs", + severity=Severity.MEDIUM, + description=( + "The skill sets LD_PRELOAD or DYLD_INSERT_LIBRARIES, points LD_LIBRARY_PATH " + "at a temporary or download folder, or writes /etc/ld.so.preload. A library " + "loaded this way runs inside every program started afterwards and can read or " + "change anything those programs do. Some legitimate skills do this too, so " + "review why it is needed rather than ignore it." + ), + remediation=( + "Check why the skill needs the preload and where the library comes from. If it " + "is not essential, remove the setting; if it is, keep the library in a folder " + "the user controls and tell the user before it takes effect." + ), + check_fn=_check_library_preload, + tags=["library-injection", "code-execution", "persistence", "owasp-asi05", "owasp-ast01"], +) + + # ── SG-104: Suspicious Tool Name ─────────────────────────────── # A tool name is split into words, at "_", "-", "." and camelCase boundaries, and a @@ -3783,6 +3854,7 @@ def _listing_tool_gaps(listing: ToolListing) -> list[tuple[McpTool, str]]: RULE_PACKAGE_REGISTRY, RULE_BRAND_NAME_IMPERSONATION, RULE_COMPILED_PYTHON, + RULE_LIBRARY_PRELOAD, RULE_UNRESTRICTED_PATH, RULE_SUSPICIOUS_TOOL_NAME, RULE_TOOL_NO_DESCRIPTION_OR_SCHEMA, diff --git a/tests/test_sg018_library_preload.py b/tests/test_sg018_library_preload.py new file mode 100644 index 0000000..49a6b01 --- /dev/null +++ b/tests/test_sg018_library_preload.py @@ -0,0 +1,129 @@ +"""Tests for SG-018, Library Preloaded Into Other Programs (#103).""" + +import pytest + +from skillshield import SkillScanner +from skillshield.rules import ALL_RULES + + +def _scan(tmp_path, text, name="SKILL.md"): + path = tmp_path / name + path.write_text(text, encoding="utf-8") + result = SkillScanner().scan_file(path) + return [finding for finding in result.findings if finding.rule.id == "SG-018"] + + +def test_issue_example_is_flagged_medium(tmp_path): + findings = _scan( + tmp_path, + "Before running the tool, export LD_PRELOAD=/tmp/helper.so so the hook is active.\n", + ) + assert len(findings) == 1 + assert findings[0].rule.severity.value == "medium" + assert "LD_PRELOAD=/tmp/helper.so" in findings[0].snippets[0] + + +@pytest.mark.parametrize( + "line", + [ + "export LD_PRELOAD=/tmp/helper.so", + "LD_PRELOAD=/opt/hook.so ./run-tool --fast", + "env LD_PRELOAD='/opt/hook.so' python tool.py", + "export DYLD_INSERT_LIBRARIES=/tmp/hook.dylib", + "DYLD_INSERT_LIBRARIES=$HOME/hook.dylib ./run-tool", + "export LD_PRELOAD=$LD_PRELOAD:/tmp/helper.so", + "setenv LD_PRELOAD /tmp/helper.so", + "ENV LD_PRELOAD /opt/hook.so", + "ENV LD_PRELOAD=/opt/hook.so", + "export LD_LIBRARY_PATH=/tmp/libs", + "export LD_LIBRARY_PATH=/tmp", + "export LD_LIBRARY_PATH=/usr/lib:/var/tmp/libs:$LD_LIBRARY_PATH", + 'LD_LIBRARY_PATH="$HOME/Downloads/libs" ./run-tool', + "export LD_LIBRARY_PATH=/home/ada/Downloads/pkg/lib", + "export LD_LIBRARY_PATH=~/Downloads", + "export LD_LIBRARY_PATH=/dev/shm/x:/usr/lib", + "echo /tmp/helper.so >> /etc/ld.so.preload", + "echo /tmp/helper.so > /etc/ld.so.preload", + "echo /tmp/helper.so | sudo tee -a /etc/ld.so.preload", + "sudo cp /tmp/helper.so /etc/ld.so.preload", + "sudo mv helper.so /etc/ld.so.preload", + "sudo install -m 644 helper.so /etc/ld.so.preload", + "open('/etc/ld.so.preload', 'w').write('/tmp/helper.so')", + ], +) +def test_preload_and_risky_library_path_are_flagged(tmp_path, line): + assert len(_scan(tmp_path, f"Setup:\n\n{line}\n")) == 1 + + +def test_one_finding_collects_each_distinct_hit(tmp_path): + text = "export LD_PRELOAD=/tmp/a.so\nexport DYLD_INSERT_LIBRARIES=/tmp/b.dylib\n" + findings = _scan(tmp_path, text) + assert len(findings) == 1 + assert len(findings[0].snippets) == 2 + + +@pytest.mark.parametrize( + "text", + [ + "LD_PRELOAD is an environment variable that lists libraries to load first.", + "DYLD_INSERT_LIBRARIES works on macOS much like LD_PRELOAD does on Linux.", + "Explain what /etc/ld.so.preload does, but do not edit it.", + "unset LD_PRELOAD", + "echo $LD_PRELOAD", + 'echo "LD_PRELOAD is $LD_PRELOAD"', + "LD_PRELOAD= ./run-tool", + "export LD_PRELOAD=", + "export LD_LIBRARY_PATH=/usr/local/lib", + "export LD_LIBRARY_PATH=/opt/app/lib:/usr/lib", + "export LD_LIBRARY_PATH=/tmpfiles/lib", + "export LD_LIBRARY_PATH=/home/ada/Downloads-archive/lib", + "cat /etc/ld.so.preload", + "diff a b > /etc/ld.so.preload.new", + "grep helper /etc/ld.so.preload", + "wc -l < /etc/ld.so.preload", + "open('/etc/ld.so.preload').read()", + ], +) +def test_near_misses_are_not_flagged(tmp_path, text): + assert _scan(tmp_path, text) == [] + + +def test_rule_is_registered_once_with_owasp_tags(): + rules = [rule for rule in ALL_RULES if rule.id == "SG-018"] + assert len(rules) == 1 + assert {"owasp-asi05", "owasp-ast01"} <= set(rules[0].tags) + assert rules[0].remediation + + +@pytest.mark.parametrize( + "text", + [ + "LD_PRELOAD" + " " * 50_000 + "x", + "LD_LIBRARY_PATH=" + "a:" * 40_000, + "LD_LIBRARY_PATH=" * 8_000, + "LD_LIBRARY_PATH=" + "/a" * 40_000, + "tee" + " -a" * 20_000, + "cp" + " x" * 40_000, + "cp x " * 20_000 + "/etc/ld.so.prelo", + ">" + " " * 50_000, + "ENV" + " " * 50_000 + "LD_PRELOAD", + ], + ids=[ + "spaces", + "colon-segments", + "repeated-prefix", + "long-path", + "repeated-tee-flag", + "cp-arguments", + "repeated-cp-near-miss", + "redirect-padding", + "env-padding", + ], +) +def test_long_inputs_scan_quickly(text): + import time + + rule = next(r for r in ALL_RULES if r.id == "SG-018") + start = time.perf_counter() + assert rule.check_fn(text) == [] + assert time.perf_counter() - start < 1.0