-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[RSI, security] Refuse sudo/doas privilege escalation in kernel bash #2429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sethkarten
wants to merge
37
commits into
main
Choose a base branch
from
cb-fix-sudo-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,502
−12
Open
Changes from 29 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
e75eabc
[RSI, security] Refuse sudo/doas privilege escalation in kernel bash
sethkarten 0acb8dc
fix(runtime): close sudo-guard quoting, wrapper, stdin, and alias byp…
sethkarten 716d825
fix(runtime): judge bundled wrapper flags and cap brace expansion in …
sethkarten 35de5ca
fix(runtime): gate heredoc runners on the sudo guard command-word walk
sethkarten f39a9de
fix(runtime): treat alias bodies that name a runner as runners in the…
sethkarten 526598b
fix(runtime): resolve alias bodies through the sudo guard walk
sethkarten e3d6268
fix(runtime): close brace, launcher, here-string, and syntax bypasses…
sethkarten 2e77d95
fix(runtime): correct launcher options and glued here-string payloads…
sethkarten c8d4358
fix(runtime): take the value options strace, ltrace, and systemd-run …
sethkarten 2b16707
fix(runtime): encode launcher value options from their synopses and s…
sethkarten dd7e35f
fix(runtime): complete the launcher option tables and judge plain com…
sethkarten 18e24f6
docs(changeset): name the option-abbreviation gap in the sudo guard's…
sethkarten 69dd9bf
fix(runtime): judge `time` flags and read glued -c payloads only from…
sethkarten 56242ef
fix(runtime): read POSIX classes in the sudo guard's glob patterns
sethkarten 30b4670
fix(runtime): read bracket negation before expanding POSIX classes
sethkarten e457c68
fix(runtime): resolve `hash -p` command-hash entries in the sudo guard
sethkarten 72bd9dc
fix(runtime): read every `hash -p` spelling and re-walk registered ta…
sethkarten 8015f8d
fix(runtime): close the hash-shadowing and float-duration bypasses
sethkarten 9963831
fix(runtime): keep hash registrations from hiding builtins, patterns,…
sethkarten 41d4030
fix(runtime): judge registered wrappers twice, coproc names, nested a…
sethkarten cf66036
fix(runtime): fail closed when an alias chain outruns the scan depth
sethkarten 1755058
Merge origin/main into cb-fix-sudo-guard: keep the sudo guard and mai…
f0ff2bc
Merge branch 'main' into cb-fix-sudo-guard
sethkarten ab506c5
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 2af8c06
Merge branch 'main' into cb-fix-sudo-guard
sethkarten aed1bfb
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 4aff3b9
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 6fa799c
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 6fcf5f8
fix(runtime): align _matching_paren with the guard family and pin one…
sethkarten 44f501f
Merge branch 'main' into cb-fix-sudo-guard
sethkarten fd88b1d
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 612ac63
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 4d14983
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 2b0a7bb
Merge branch 'main' into cb-fix-sudo-guard
sethkarten ef187be
Merge branch 'main' into cb-fix-sudo-guard
sethkarten c1ffe19
Merge branch 'main' into cb-fix-sudo-guard
sethkarten 5f2bd75
Merge branch 'main' into cb-fix-sudo-guard
sethkarten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Added a privilege-escalation guard to the kernel bash tool: sudo/doas command words are refused before any process starts, because silent privilege escalation outranks every other guard on passwordless-sudo setups. The scan and the spawn share one read of the command prefix, and a handle constructed directly from the BashHandle class is guarded the same way, so the class cannot bypass the scan. Covered: any path spelling, quote- and escape-folded spellings (`su"do"`, `"sudo"`, ANSI-C `$'su\x64o'` with out-of-range escapes kept literal instead of raising), brace/glob/letter obfuscation of the command word (`su{d,}o`, `{sudo,} id`, `sud[o]`, `su?do`, sequences such as `s{u..u}do` or `{1..9}`), and a brace group whose element count times the group chain exceeds the enumeration cap fails closed before it is built, wrapper chains (env/nice/timeout/nohup/setsid/exec/busybox/stdbuf/ionice/strace/ltrace/watch/faketime/systemd-run/chroot, including their value-taking options such as `env -S`, `env -C`, `timeout -s`, `stdbuf -o`, `nice -n`, `exec -a`, `watch -n` (its `-d`/`-t` and `faketime`'s `-m`/`-f` are boolean), each launcher's required-argument long options (strace's `--user`/`--argv0`/`--color` and the rest of its longopts, ltrace's `--indent`/`--library`, systemd-run's `-H`/`--uid`/`--gid` and its timer and property options, parallel's `-C`/`-d`/`-J`/`-P`/`-s`/`-E` and their long forms, `env -a`/`--argv0` and BSD `env -P`, `watch -q`/`-s`), bundled short flags such as `env -vu NAME`, and the operands of xargs options such as `-n`, `-I`, `-P`, plus the leading operands of `chroot`/`faketime` and `timeout`'s integer, floating-point, and suffixed durations), assignment prefixes, redirections, group/subshell/brace, `coproc`, and compound-command positions (`if`/`then`/`elif`/`else`/`while`/`until`/`for`/`do`/`case` bodies, with `for`/`select` loop variables and `case` subjects and labels read as syntax rather than commands, and `time`'s own flags skipped so `time -p sudo id` still reaches the command word), pipelines (`echo x | sudo tee /etc/hosts`), xargs and `parallel` operands and option operands (judged fail-closed because BSD and GNU disagree on which operands are optional), `find -exec`/`-execdir`/`-ok`/`-okdir` and `fd -x`/`-X` commands, alias bodies and `hash -p` command-hash entries in every spelling (a registered name scans as the command it runs, shell builtins such as `eval`/`command`/`exec` keep their own meaning because the hash table cannot shadow them, the word's own spelling is judged as well so `hash -r` cannot hide it, and an entry built from expansion is refused because the command it hides cannot be resolved), interpreter payloads (`sh -c`, bundled `-ce`, and glued payloads only from short bundles, so `bash --rcfile FILE -c 'sudo id'` is refused, `eval`), quoted `$(...)` spans (matched quote-aware, so `echo "$(printf ')'; sudo id)"` is refused), backticks, process substitutions (`<(cmd)`, `>(cmd)`) including a runner's script source, here-strings (`bash <<< 'sudo id'`, `bash<<<'sudo id'`, and glued multi-token payloads such as `bash<<<'sh -c "sudo id"'`, whose quoted target the tokenizer keeps as one word), and heredoc bodies owned by a shell or piped to one in the same text (the gate reuses the guard's own command-word walk, so `command`, `command -p`, `command --`, wrapper chains, xargs operands, and `find -exec`/`-execdir` operands are all treated as shell runners, while a `command -v`/`-V` lookup or a plain operand mention is not; an alias whose body reaches a runner (wrapper chains such as `env sh`, `command sh`, or `xargs -I{} sh -c {}` included) counts as a runner for the same gate and for the script-source scan). Lookup forms (`command -v sudo`, `command -V sudo`, `which sudo`) stay allowed, and `command` itself stays gated to those lookup flags instead of short-circuiting the walk, so `command sudo id` is refused; operand mentions (`man sudo`, `grep sudo file`), data heredocs, and comments stay allowed; unresolvable command-position words carrying a sudo/doas mention (`$CMD id`, `$SUDO id`) fail closed, while a name assembled so that no such spelling appears (a `printf` with hex escapes, a base64 payload piped to a shell) stays out of a name-based scan's reach, as do payloads nested deeper than the scan follows. Out of scope: `su`, trap actions, other interpreters' string payloads (`python -c`), Windows runas, script-file contents, launchers outside the modeled set that pick their own program or domain (`ssh`, `script` with its diverging util-linux and BSD option shapes, package runners such as `dx`/`npx`), and make recipes. Explicit bypasses: `bash(command, allow_sudo=True)` or `PI_BASH_ALLOW_SUDO=1`, where the environment bypass is frozen at kernel start so mid-session `os.environ` writes cannot neuter the guard; they are ignored and warned about loudly. It is a foot-gun guard, not a sandbox: an unmodeled launcher or value option, an abbreviated long option (`strace --verb 5 sudo id`), a renamed or copied tool at another path (a symlink to the tool and a plain copy both run the real binary, and `realpath` cannot see a copy), or an extglob spelling such as `s@(u|x)do` with `shopt -s extglob` on (the tokenizer splits the group, and shell-option state is not statically knowable), can still hide a command word, and `su`, other interpreters' string payloads, script files, and those bypasses all still reach root. A name that merely contains `sudo` is judged by its case-folded basename, so `sudoku`, `sudo-report`, and `s-u-d-o` stay runnable while `SUDO` still is the tool on a case-insensitive filesystem; a glob in the basename is judged as a pattern (`/usr/bin/su*` refuses), and a word that carries quoting or expansion still falls back to its letters. The launcher tables name only options their tool really has, because an entry for a phantom option swallows the command word. A glob pattern that can match `sudo`/`doas` is read as a pattern, POSIX classes included, so `[[:lower:]]udo` and `[[:alpha:]]udo` refuse exactly like `sud[o]` and `su?do`, while a spelling that cannot match either name (`s[[:upper:]]do`, `[[:digit:]]udo`, `[![:lower:]]udo`) stays allowed. Known measured cost, accepted for this pass and named as the follow-up: a command carrying many heredocs is resolved quadratically, because each heredoc scans the remaining text for its delimiter and then sweeps the words to mark the body as data. Measured on this module: 0.066s at 1000 heredocs and 1.34s at 5000 (about 100 KB of command text), reproduced by timing `_apply_heredocs` alone, and extrapolating to a command near the argv limit gives minutes. The fix is a single sweep over the collected body ranges; it is not taken here because it changes the scan machinery rather than closing a bypass. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
.changes/kernel-sudo-guard.md:1Direct
BashHandle("display", script="sudo id")bypasses_guard_sudobecause the guard only runs whenscript is None, so the unscanned script can launchsudo. This changelog claim that directBashHandleconstruction is guarded is therefore incorrect; either guard the suppliedscriptor remove that claim.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: