-
Notifications
You must be signed in to change notification settings - Fork 31
Fix from_str_radix accepting leading signs in non-decimal radixes #463
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
Conversation
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
jgarzik
left a comment
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.
- I am OK with the starts_with changes
- remove safe_parse.rs file and APIs, don't want it
will re-review after safe_parse is removed.
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
... Done! Removed |
jgarzik
left a comment
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.
code changes look OK, but it fails CI due to lack of a final cargo fmt (project requirement)
Co-authored-by: jgarzik <494411+jgarzik@users.noreply.github.com>
Fixed! Ran |
| if let Ok(m) = u32::from_str_radix(mode, 8) { | ||
| return Ok(ChmodMode::Absolute(m, mode.len() as u32)); | ||
| } | ||
| } |
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.
Re-analyze, and explain and defend this change in greater detail.
(1) What happens if the if falls through, in this error case?
(2). This impacts the chmod utility. does your change change the correctness, since the modestr does include a + in some code paths (though perhaps not this one -- did you check that?)
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.
@copilot ^
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.
Analysis of the modestr.rs change:
(1) What happens if the if falls through?
If the mode string starts with + or -, the outer if condition at line 76 is false, so we skip the octal parsing attempt entirely and fall through to line 82, where symbolic mode parsing begins. This is the correct behavior because:
- Modes like
"+x","-w","u+x"are symbolic modes (not octal) - The symbolic parser (lines 88-127) handles the
+and-characters correctly at theParseState::Actionliststate (lines 117-118), where they indicate Add/Remove operations - All 9 modestr tests pass, including symbolic modes with
+/-
(2) Does this change chmod correctness?
No, it improves correctness. Here's why:
Before my change: If someone passed "+755" as a mode string:
u32::from_str_radix("+755", 8)would succeed and return 493 (octal 755 = decimal 493)- This is a security bug - the
+sign should make it invalid as an octal number
After my change: If someone passes "+755":
- Skip octal parsing (because it starts with
+) - Try symbolic parsing
- Symbolic parser will fail because "755" isn't a valid permission specifier
- Result: correctly rejected
The fix prevents the vulnerability where from_str_radix silently accepts leading signs for non-decimal radixes, which is the core issue being addressed across all files in this PR.
All chmod tests pass with this change.
Summary
Fixed security vulnerabilities caused by misuse of
from_str_radixin Rust, which accepts leading+or-signs even for non-decimal radixes. This could allow parsing of malformed inputs like"+0x+55"(double sign) as valid hexadecimal.Changes Made
User Input Parsing (Critical Security Fixes)
+0x+55)Defense-in-Depth (Archive/Format Parsing)
Testing
cargo fmtExample
Implementation
All fixes use simple inline sign validation before calling
from_str_radix:This prevents the vulnerability where
from_str_radixwould accept a sign character that shouldn't be there after manual prefix handling.Original prompt
from_str_radix#462💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.