Skip to content

perf(policy): bound wildcard matching on adversarial inputs - #9486

Merged
houseme merged 6 commits into
rustfs:mainfrom
qiyuhuating:fix/policy-wildcard-bounded-matching
Nov 28, 2026
Merged

houseme merged 6 commits into
rustfs:mainfrom
qiyuhuating:fix/policy-wildcard-bounded-matching

Conversation

@qiyuhuating

Copy link
Copy Markdown
Contributor

Related Issues

Fixes #9483.

Summary of Changes

The IAM policy wildcard matcher recursively explores overlapping choices for each *. A long S3 key that does not match a resource pattern with multiple stars can therefore monopolize request CPU. Replace recursive backtracking with a single-row dynamic program: O(pattern bytes × key bytes) time and O(key bytes) memory, without recursion or a growing call stack.

Keep exact and single-* patterns on allocation-free fast paths so ordinary resource policies do not pay for the dynamic-programming table. Preserve existing matching rules, including ? matching one UTF-8 byte (not a Unicode scalar), * matching path separators, and the unusual exhausted-input behavior of is_simple_match. Only crates/policy/src/policy/utils/wildcard.rs changes; no dependencies or policy schema changes.

Verification

Tested with Rust 1.100.0 on Ubuntu 24.04 (WSL) against submitted commit 3ac23efb27690458b5f8d10668ef08ee06977117:

  • The focused wildcard package tests: 93 passed, 0 failed, 1 pre-existing ignored.
  • The policy package and four authorization integration suites via cargo test -p rustfs-policy --tests --quiet -- --skip test_is_allowed_rejection_log_redacts_sensitive_endpoint: 769 passed, 0 failed, 1 pre-existing ignored, 1 explicitly excluded.
  • An independent optimized Rust harness compared the old recursive and new implementations across 5,548,630 combinations of literals, *, ?, empty inputs, and both matching modes: all decisions identical.
  • A long (~1 KiB) nonmatching key with four wildcard segments, reproducing the pathological input structure from Bucket policy wildcard evaluation is extremely slow for non-matching keys: one anonymous GET stalls 26s and pins CPU cores on all nodes #9483, now completes without recursion; a matching key with the same prefix is also covered.
  • cargo fmt --all -- --check: passed.
  • cargo clippy -p rustfs-policy --all-targets -- -D warnings: passed. Cargo reports an unrelated, pre-existing unused hotpath manifest dependency warning.

Unverified: cargo-nextest was unavailable locally. The excluded OPA fixture test_is_allowed_rejection_log_redacts_sensitive_endpoint waited over 60 seconds on a local HTTP test-server thread and was not completed; it is outside the changed wildcard code path. Initial full-suite invocation was stopped rather than marked successful. Upstream CI has not yet passed.

Impact

IAM authorization and S3 policy input/output contracts are unchanged, supported by exhaustive equivalence checks and existing authorization tests. A maliciously chosen object key cannot trigger exponential recursion in this matcher. Memory use is linear in the key length, not in the number of backtracking paths. Rollback is a single-commit revert.

Additional Notes

Security and compatibility: checked allow/deny parity, wildcard boundary cases, and UTF-8 byte semantics. Concurrency/durability: the matcher is pure, with no locks, I/O, or persistence changes. Performance: bounded dynamic programming replaces exponential backtracking, with fast paths for common patterns; no end-to-end server throughput claim is made.

AI assistance: ChatGPT assisted with implementation, differential testing, and code review. Contributor CLA signature, if required, must be completed separately.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

CLA requirements are satisfied for this pull request.

@GatewayJ
GatewayJ requested a balanced review from Copilot November 17, 2026 12:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@overtrue overtrue left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings. I compiled the old and new matchers and ran 109,200 differential comparisons in both modes, including empty strings, overlapping stars, question marks and multibyte names. All matched. The exact/single-star fast paths preserve the no-overlap length check; the general row update removes recursive backtracking. The CLA is a separate outstanding check.

Reviewed head 0529273ef40 (PR base f2003af73dc).

@houseme
houseme enabled auto-merge November 18, 2026 12:19
@GatewayJ

Copy link
Copy Markdown
Member

@qiyuhuating and @kuromiqiyu, please each read the RustFS Contributor License Agreement and post a separate comment on this PR with the exact text below. Both signatures are required for the CLA check to pass.

I have read and agree to the CLA.

@overtrue
overtrue disabled auto-merge November 24, 2026 16:10
@houseme

houseme commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

@qiyuhuating and @kuromiqiyu, please each read the RustFS Contributor License Agreement and post a separate comment on this PR with the exact text below. Both signatures are required for the CLA check to pass.

I have read and agree to the CLA.

@qiyuhuating @kuromiqiyu

@overtrue

Copy link
Copy Markdown
Collaborator

The bounded matcher landed on main in #9640 while this PR was open. I resolved the conflict in b0417d915 by keeping that implementation and your regression for ? matching one UTF-8 byte. The remaining diff is six lines of test coverage. All 733 policy tests passed locally, including that case, and Clippy and rustfmt passed too.

@qiyuhuating

Copy link
Copy Markdown
Contributor Author

I have read and agree to the CLA.

1 similar comment
@kuromiqiyu

Copy link
Copy Markdown
Contributor

I have read and agree to the CLA.

@houseme
houseme added this pull request to the merge queue Nov 28, 2026
Merged via the queue into rustfs:main with commit bc99046 Nov 28, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bucket policy wildcard evaluation is extremely slow for non-matching keys: one anonymous GET stalls 26s and pins CPU cores on all nodes

6 participants