Skip to content

The rewrite preserves a sudo prefix, so rtk's filtering, SQLite writes and tee writes execute with root privileges the original command did not require for them #3412

Closed

Description

@HetCreep

sudo and env prefixes are re-attached in front of the rewritten command, so sudo <tool> becomes sudo rtk <tool>. The user elevated one program; rtk inserts itself into that elevation.

Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). The rewrites below were produced by the shipped binary. This is a privilege-hygiene report, not a vulnerability claim — see "What I am not claiming".

Observed

$ rtk rewrite sudo iptables -L     →  sudo rtk iptables -L
$ rtk rewrite sudo du -sh /var     →  sudo rtk du -sh /var
$ rtk rewrite sudo docker ps       →  sudo rtk docker ps
$ rtk rewrite env FOO=1 git status →  env FOO=1 rtk git status

ENV_PREFIX (src/discover/registry.rs:60-67) matches a leading run of sudo , env and VAR=value and re-attaches it ahead of the rewrite (registry.rs:959-977).

Why the placement matters

sudo iptables -L elevates exactly one binary. sudo rtk iptables -L elevates rtk, which then does its own work as root before and after running the real tool:

  • opens and writes the SQLite history database
  • writes tee files
  • loads and parses configuration
  • fires the telemetry ping (main.rs:1578, on every invocation)
  • resolves the target binary through PATH

None of that needed root. The user's intent was "run iptables as root", not "run a wrapper, its database layer and its config parser as root".

Two concrete consequences worth considering:

  1. File ownership. Files rtk creates during an elevated run are created by a root process. On a normal desktop that can leave root-owned entries in a user-owned data directory, which the subsequent non-elevated runs then have to write to.
  2. PATH resolution under sudo. rtk resolves the wrapped tool itself rather than letting sudo do it. Distributions that set secure_path in sudoers do so specifically to control which binaries a root invocation can reach; a wrapper that performs its own lookup inside the elevated process is working against that intent.

What I am not claiming

I checked, and want to be explicit about the negative:

  • The project-filter trust gate still applies. src/core/toml_filter.rs:191-211 routes project-local filter files through hooks::trust::gated_filter_paths() and check_trust_with_content, so the .rtk/filters.toml path fixed in GHSA-fvvm-949w-qj4w is not re-opened by this. An untrusted repository file does not get parsed as root.
  • I have not demonstrated privilege escalation, and I do not believe this is one. It is a widening of what runs with elevated privilege, not a way to gain it.

Suggested directions (untested — no programmer has reviewed these)

The cleanest fix keeps rtk outside the elevation: rewrite sudo <tool> … to rtk sudo <tool> … rather than sudo rtk <tool> …, so rtk runs as the user and sudo elevates only the tool — the same shape the user typed. If that breaks output capture in ways I have not thought through, the alternatives are to skip rewriting sudo-prefixed commands entirely (they are rare and usually short-output), or to detect an elevated run and disable the database/tee/telemetry side effects for it.

Note there is currently no root awareness anywhere in the codebase — grepping src/ for geteuid, getuid or similar returns nothing — so option three would be new machinery, whereas the first two are changes to existing logic.

Not checked

The audit machine is Windows, so I verified the rewrite but not the runtime consequences: file ownership after an elevated run, secure_path interaction, and whether sudo is even in a typical rtk user's path for these commands are all unverified. If the elevated-run consequences turn out to be harmless in practice, the rewrite shape is still worth a look, but the priority drops.

Activity

  1. pszymkowiak commented on Aug 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 72%

    Summary

    sudo/env prefixes are re-attached ahead of the rewrite (sudo rtk ) instead of after it, causing rtk's DB writes, tee writes, config parsing, PATH resolution and telemetry to run as root unnecessarily — recommend restructuring rewrite to preserve elevation scope to just the wrapped tool.

    📁 Relevant files
    • src/discover/registry.rs
    • src/main.rs
    • src/core/toml_filter.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. HetCreep commented on Sep 16, 2026

    @HetCreep
    Author

    Following up on this one — confirmed fixed at current develop (5e0f92c).

    Provenance: AI-assisted source audit, human-directed, at develop @ 5e0f92c. Built the binary (cargo build --bin rtk) and ran live probes against it, plus the actual test suite — this was not a read-only check.

    The original report showed sudo/env prefixes being re-attached in front of the rewrite (sudo docker ps → sudo rtk docker ps), via the ENV_PREFIX regex and re-attachment logic in src/discover/registry.rs. That regex no longer matches sudo at all.

    src/discover/registry.rs:69-73 now carries an explicit note:

    // NOTE: `sudo` is intentionally NOT stripped here. Rewriting `sudo docker ps`
    // to `sudo rtk docker ps` breaks at runtime because `rtk` is not on root's
    // secure_path, and (where it is) would run rtk itself as root. sudo commands
    // are left untouched so they pass through unchanged. See #146.

    and the regex right after it (registry.rs:73: Regex::new(&format!(r#"^(?:env\s+|{}\s+)+"#, env_assign))) only alternates on env and assignment prefixes — sudo is absent.

    This landed in a8bb67a ("fix(rewrite): stop rewriting sudo commands (pass them through)", 2026-07-04) — not present at 0de9610 (the baseline the original report was written against), present at current HEAD. The commit message's rationale names both the runtime breakage in #146 and the unnecessary-privilege concern raised here, so this reads as a deliberate fix of this case, not a side effect of something else.

    It's pinned by tests, not just the comment: test_rewrite_sudo_passthrough (registry.rs:5936-5961 — sudo docker ps, sudo -u root docker ps, sudo git status, FOO=1 sudo docker ps, env FOO=1 sudo docker ps, bare sudo, and sudoedit /etc/hosts, all asserted → None), test_classify_sudo_not_stripped (registry.rs:2726-2733), and test_sudo_with_builtin_not_rewritten (registry.rs:6657-6664, sudo noglob git status → None).

    I also went looking for a regression, since b17dda3 ("feat(rewrite): peel process wrapper prefixes", 2026-09-08) landed only 8 days before this HEAD and is exactly the kind of new prefix-stripping machinery that could reopen this by accident. PROCESS_WRAPPERS (registry.rs:1338-1368: timeout/time/nice/nohup) excludes sudo, and sudo is in neither ROUTABLE_WRAPPER_PREFIXES nor SHELL_KEYWORD_PREFIXES. None of the existing tests cover sudo stacked with one of those wrappers, so I built the binary and probed the compositions directly rather than trusting the gap in coverage:

    $ rtk rewrite sudo timeout 300 docker ps        # exit 1, empty stdout (passthrough)
    $ rtk rewrite timeout 300 sudo docker ps        # exit 1, empty stdout
    $ rtk rewrite sudo nice -n 10 cargo test        # exit 1, empty stdout
    $ rtk rewrite nice -n 10 sudo cargo test        # exit 1, empty stdout
    $ rtk rewrite sudo nohup cargo build            # exit 1, empty stdout
    $ rtk rewrite FOO=1 timeout 300 sudo docker ps  # exit 1, empty stdout
    $ rtk rewrite command sudo docker ps            # exit 1, empty stdout
    $ rtk rewrite sudo command git status           # exit 1, empty stdout
    $ rtk rewrite /usr/bin/sudo docker ps           # exit 1, empty stdout
    
    # control, no sudo:
    $ rtk rewrite timeout 300 cargo test            # exit 3, prints "timeout 300 rtk cargo test"
    

    Exit 1 with empty stdout is rtk's "no rewrite, pass through unchanged" path (HookDecision::Defer); every composition above hits it, none reintroduce a rewrite. Also ran cargo test --bin rtk sudo — 4 passed, all green. A src/-wide grep for sudo turns up only this ENV_PREFIX regex/comment and an unrelated permission-verdict matcher in src/hooks/permissions.rs (used for the hook allow/deny UI, not command rewriting) — no rules or filter TOML reintroduces a sudo rewrite either.

    Thanks for fixing this — closes the concern as originally filed. Fine to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:securitybugpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions