Repository navigation
fix(security): read only the last word for an rm floor fd prefix - #38898
Conversation
Sensitive Change ReviewSensitive files changed:
|
Security Scope Review -- ✅ PASSWhich legitimate operations does Verdict basis: adjudicated, zero confirmed regressions. Security scope review -- adjudicated candidates
No candidate flipped from allowed to refused. Scope-Verdict: PASS Reason: This change rewrites TIGHTENING: rm-floor fd-prefix recognition in ADJUDICATED: 17 candidates, 0 confirmed newly-refused PLATFORM COVERAGE:
[SCOPE-REVIEWED] e8c0470 |
72bc726 to
f411085
Compare
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the fix removes the quadratic cost where it starts, the per-step join plus end-anchored regex, and it keeps the same answers. Notes
[DESIGN-REVIEWED] e8c0470 Description read[DESCRIPTION-READ] ebb3aa0a919a65ee93bb09d302c3ccd25f3cb61354ed08a037bc53854c217798 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. This is a linear rewrite of one helper, at the mechanism level. The defect is tracked as #37216 and pinned by a read-count test that fails on base, and the helper still gives the same answers. What this change shipsInventory (4 items) — 4 justifiedIntent: stop one shell command line packed with
[FIRST-PRINCIPLES-REVIEWED] e8c0470 Description read[DESCRIPTION-READ] e5728f663c9841e0cf942145079c00213b872debae24dc16f0e7adb7ece2c552 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
|
Rebased: main moved in no file this PR changes; rebased onto current main for the test-floor fix that landed there (8301e68 -> bdbec31); CI re-running.
[operator: bolichen97] |
f411085 to
283f13e
Compare
The rm floor keeps a redirection's fd prefix (2>, {fd}>) on the
redirection word. At every live redirection it joined the whole segment
read so far and searched it with an end-anchored regex, so a line with N
redirections cost O(N^2): a short command full of << held the tool-call
decision for seconds (4.4 s at N = 6000).
The prefix can only be the segment's last blank-delimited word, because
no character of either prefix form is a blank. The helper now walks back
from the end of the segment to the last blank, checks that word with the
regex's own character classes, and trims only the tail elements. Each
redirection piece starts with a blank, so the total work is linear. The
answers are unchanged; _FD_PREFIX_RE had no other reader and is removed.
A test pins the cost by counting element reads, without a clock. A
security line-budget row records the 14 lines this adds to the package.
Closes #37216
|
Rebased: main moved in no file this PR changes; rebased onto current main for the Windows test-shard fix that landed there (bdbec31 -> be1ce64); CI re-running.
[operator: bolichen97] |
283f13e to
e8c0470
Compare
Sensitive change review
[operator: bolichen97] |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
I reviewed head e8c0470. The rewritten fd-prefix helper in the rm floor is a linear walk back to the segment's last blank, and I checked that it is equivalent to the regex it replaces. A differential probe over hundreds of thousands of random segments and every code point in several word shapes found no difference in the returned prefix or the remaining joined segment. The only caller joins the segment right away, so the refuse and allow answers do not change. The new read-count test fails on the base and passes here, and the related security and shell-reader suites pass on this head. Nothing blocks.
Problem / Motivation
Goal: A shell command line full of
<<no longer holds the tool-call decision for seconds.The recursive-force
rmfloor's run time grows with the square of the number of<<operators on one line."((1" + "<<1" * N + ")); rm -f scratch"is a short, valid command, butdeny_gate.is_deniedtakes 4.4 s on it at N = 6000, and doubling N roughly quadruples the time.Why it matters
Every shell tool call goes through this check before it runs. One line built this way blocks the gate worker for seconds, and the delay grows quadratically with the line, so a single command can stall the decision for as long as its author likes.
Not a goal
fnmatchcost tracked in ssh-to-self floor time grows quadratically on a long run of [ (fnmatch) #37373._rm_wipe_targets's leading-redirection skip (see Manual verification). It is measured and named here, not fixed.What changed (motivation → approach → change)
At every live redirection, the rm floor checks whether the text just before it is a file-descriptor prefix (
2>,{fd}>). That check joined the whole command read so far and ran an end-anchored regex over it, so each check cost the length of the line and N redirections cost O(N²). At N = 3000 under cProfile, 0.93 s of 1.8 s went tore.Pattern.search, called 6000 times fromrm_floor._pull_trailing_fd_prefix.A prefix only counts as a whole word (the
2in/tmp/2>xis part of the path), and neither prefix form holds a blank, so the regex could only ever match the line's last blank-delimited word. The helper now walks back from the end to the last blank or\x00, without joining, checks that word with the regex's own character classes (str.isdecimal;isalnumor_, with an ASCII letter or_first), and on a hit trims only the tail. Each redirection the floor appends starts with a blank, so the next walk stops there and the total work is linear._FD_PREFIX_REhad no other reader and is removed. A row intest/security_line_budget/records the 14 lines this adds to the security package.Backwards compatibility
Compatible: the helper returns the same prefix and leaves the same joined segment for every input. A throwaway equivalence check compared the old join + regex with the new walk on every code point in six shapes and on 200,000 random multi-element segments: 0 mismatches.
Reader: src/kiro_crew/security/rm_floor.py:_FD_PREFIX_RE -- entry: chat|cron|subagent (every shell tool call) -- its only reader was
_pull_trailing_fd_prefix, now rewritten;git grep _FD_PREFIX_RE -- src testfinds no other reader. Pinned by the existing fd-prefix rows inTestRecursiveForceRmFloorandtest/test_shell_commands_reader.py, unchanged and passing.Tests
Proof: PROVEN test/test_denied_commands_security_rows.py::TestRecursiveForceRmFloor::test_an_fd_prefix_check_reads_only_the_segment_tail[1000], test/test_denied_commands_security_rows.py::TestRecursiveForceRmFloor::test_an_fd_prefix_check_reads_only_the_segment_tail[2000] (head d83c8f66605b)
prove.pyran on a Linux test host ond83c8f66605b: this same patch on main36881767809c(git range-diffagainst this head:=).TestRecursiveForceRmFloor::test_an_fd_prefix_check_reads_only_the_segment_tail[1000|2000]passes the helper aMutableSequencethat counts element reads, in the issue's segment shape. It must return""after at most 8 reads at both sizes (3 measured), with no clock, like the neighbouringtest_nested_open_expansions_are_read_a_bounded_number_of_times. On the base,"".joinreads every element: 2004 and 4004 reads.-n0, and the whole file passed 384/384 in a seeded shuffled order (seed 37216).test_denied_commands_security_rows.py,test_shell_commands_reader.py,test_shell_heredoc_reader.pyandtest_security_gate_liveness.py(the line budget, with the new row), 536 passed. The existing fd-prefix rows are unchanged.Manual verification
Run on a Linux test host (Ubuntu 24.04, Python 3.12) against main
cf61d1dce0a8, before and after this commit. Thread CPU time (time.thread_time, min of 3) ofdeny_gate.is_denied("((1" + "<<1" * N + ")); rm -f scratch"):N = 6,000 is about 18 KB, close to the 20 KiB command cap. A cProfile at N = 3,000 shows
re.Pattern.searchat 6,315 calls / 0.953 s before (6,000 of them from_pull_trailing_fd_prefix) and 315 calls / 0.023 s after; the helper itself is 0.022 s cumulative after.The PreToolUse gate itself (
HookManager.on_tool_call("bash", command=..., is_shell=True), isolatedKIROCREW_HOME) takes 4.999 s before and 0.764 s after on the issue's command at N = 6,000.Same answers before and after, from
is_deniedand from the PreToolUse gate:2>/dev/null rm -r -f /X=1 2>x rm -r -f ~{fd}>x rm -rf /2>&1 rm -rf ~rm -fr / 2>/dev/nullrm -fr /tmp/2>/dev/null2>&1 rm -fr /tmp/xgit statusrm -f scratchThe rest of the growth (about x2.1 to x2.3 per doubling) is mostly
_rm_wipe_targetsstepping over each leading redirection withwords = words[1:], which copies the word list once per redirection. It is small up to the command cap and is left out of this change.Real-machine verification
Machine: EC2 m7i.2xlarge, Ubuntu 24.04.5 LTS x86_64, 8 vCPU / 30 GiB, Python 3.12.3
Before (main cf61d1d):
python3 -m pytest -n0 "test/test_denied_commands_security_rows.py::TestRecursiveForceRmFloor::test_an_fd_prefix_check_reads_only_the_segment_tail", with this PR's test file on the base source -> 2 failed,assert 2004 <= 8andassert 4004 <= 8; the issue's command at N = 6,000 takes 4.969 s of thread CPU indeny_gate.is_deniedAfter (head e8c0470, run on efa0bc375a60, the same patch on main cf61d1d): the same command -> 2 passed, 20 of 20 runs at
-n0; the issue's command at N = 6,000 takes 0.677 sCleanup: instance terminated (describe-instances and describe-volumes for its id return nothing); it had no key pair and no inbound rules
Related Issues
Closes #37216
Sibling sweep
Swept: git grep -n '"".join(seg)' -- src/kiro_crew/security (its other hit, the join that ends each command in
rm_floor._segments, runs once per segment: linear)Swept: git grep -nE '\Z' -- src/kiro_crew/security, then each call site of an end-anchored pattern that runs once per step
Sibling: src/kiro_crew/security/rm_floor.py:48 -- changed here
Sibling: src/kiro_crew/security/shell_normalizer.py:715 -- not affected --
_PLAIN_REDIRECTION_REmatches one token at a time, never a growing bufferSibling: src/kiro_crew/security/argv_floor_self.py:483 -- not affected --
_RAW_ASSIGNMENT_REmatches one word at a timeSibling: src/kiro_crew/security/stream_aws_anchor.py:76 -- not affected -- the search is windowed to at most
_KEY_NAME_MAXcharacters before the endSibling: src/kiro_crew/security/inline_payload.py:496 -- tracked in #37482
Sibling: src/kiro_crew/security/argv_floor_ssh.py:656 -- tracked in #37373
Pattern harvest
Rule candidate: review-prompt
Pattern: a per-step helper that joins or regex-searches the whole growing buffer
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits