-
Notifications
You must be signed in to change notification settings - Fork 0
fix(serializers): bound untrusted msgpack decode depth and header allocation (LAB-2503) #276
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
27Bslash6
wants to merge
10
commits into
main
Choose a base branch
from
lab-2503-decode-bounds
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4941f34
fix(serializers): bound untrusted msgpack decode depth and header all…
27Bslash6 d9eee87
fix(serializers): apply LAB-2503 panel findings to the decode bound
27Bslash6 385798a
perf(serializers): zero-copy Rust header walk for the msgpack decode …
27Bslash6 0ba205b
fix(serializers): apply LAB-2503 panel round 2 to the Rust decode walk
27Bslash6 72e8ef5
fix(serializers): apply LAB-2503 round-3 review to the decode bound
d4a226b
fix(serializers): gate forged columnar documents by shape before pand…
f2fb321
Merge remote-tracking branch 'origin/main' into agent/winston/f9b79b3…
b120fd9
fix(serializers): snapshot mutable exporters before the decode walk; …
338c6dc
fix(serializers): drop the unreachable NumPy fallback on the plain de…
d0b5980
fix(serializers): refuse unknown column type markers on the columnar …
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
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
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,87 @@ | ||
| //! Structural bound for untrusted MessagePack (LAB-2503; protocol spec/interop-mode.md → | ||
| //! Decode bounds). The one algorithm this crate owns rather than delegates to cachekit-core; | ||
| //! a core-shared walk usable from py/rs/wasm is the follow-up. Mirrors the opcode table of | ||
| //! cachekit-rs `check_structure` so the two SDKs reject the same documents. | ||
|
|
||
| /// Header-only walk over one `MessagePack` document: str/bin/ext payloads are skipped by | ||
| /// offset, never read, and nothing is allocated beyond one `u64` per open collection. | ||
| /// | ||
| /// Trailing bytes after the root element are left to the decoder (`ExtraData`). | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Names the violated bound, before any decoder pre-allocates a container, for: | ||
| /// - nesting deeper than `max_depth`; | ||
| /// - a header declaring more payload bytes than the input holds; | ||
| /// - more pending elements (across every open collection) than remaining bytes can back — | ||
| /// every element costs >= 1 byte, so a decoder's total container pre-allocation is then | ||
| /// bounded by the input length instead of by `depth × declared_len`; | ||
| /// - the reserved marker 0xc1 and input that ends mid-document. | ||
| pub fn check_msgpack_structure(bytes: &[u8], max_depth: usize) -> Result<(), String> { | ||
| fn be(bytes: &[u8], pos: usize, width: usize) -> Result<u64, String> { | ||
| let end = pos | ||
| .checked_add(width) | ||
| .filter(|e| *e <= bytes.len()) | ||
| .ok_or_else(|| "ends inside a length prefix".to_owned())?; | ||
| Ok(bytes[pos..end] | ||
| .iter() | ||
| .fold(0u64, |acc, b| (acc << 8) | u64::from(*b))) | ||
| } | ||
|
|
||
| let mut pos = 0usize; | ||
| let mut pending: u64 = 1; // elements owed across all open collections (the root is one) | ||
| let mut open: Vec<u64> = Vec::new(); // elements still owed per open collection = depth | ||
| while pending > 0 { | ||
| while open.last() == Some(&0) { | ||
| open.pop(); | ||
| } | ||
| let marker = *bytes | ||
| .get(pos) | ||
| .ok_or_else(|| "ends before the document is complete".to_owned())?; | ||
| pos += 1; | ||
| pending -= 1; | ||
| if let Some(innermost) = open.last_mut() { | ||
| *innermost -= 1; | ||
| } | ||
| // (length-prefix bytes, payload bytes after the prefix, child elements) | ||
| let (prefix, payload, children): (usize, u64, u64) = match marker { | ||
| 0x00..=0x7f | 0xc0 | 0xc2 | 0xc3 | 0xe0..=0xff => (0, 0, 0), | ||
| 0x80..=0x8f => (0, 0, 2 * u64::from(marker & 0x0f)), | ||
| 0x90..=0x9f => (0, 0, u64::from(marker & 0x0f)), | ||
| 0xa0..=0xbf => (0, u64::from(marker & 0x1f), 0), | ||
| 0xc1 => return Err("contains the reserved marker 0xc1".to_owned()), | ||
| 0xc4 | 0xd9 => (1, be(bytes, pos, 1)?, 0), | ||
| 0xc5 | 0xda => (2, be(bytes, pos, 2)?, 0), | ||
| 0xc6 | 0xdb => (4, be(bytes, pos, 4)?, 0), | ||
| 0xc7 => (1, be(bytes, pos, 1)? + 1, 0), // ext: length prefix, then type byte + data | ||
| 0xc8 => (2, be(bytes, pos, 2)? + 1, 0), | ||
| 0xc9 => (4, be(bytes, pos, 4)? + 1, 0), | ||
| 0xca..=0xd3 => (0, 1u64 << (marker & 0x03), 0), // f32/f64/u8..u64/i8..i64: 4,8,1,2,4,8,1,2,4,8 | ||
| 0xd4..=0xd8 => (0, 1 + (1u64 << (marker - 0xd4)), 0), // fixext: type byte + 1/2/4/8/16 | ||
| 0xdc => (2, 0, be(bytes, pos, 2)?), | ||
| 0xdd => (4, 0, be(bytes, pos, 4)?), | ||
| 0xde => (2, 0, 2 * be(bytes, pos, 2)?), | ||
| 0xdf => (4, 0, 2 * be(bytes, pos, 4)?), | ||
| }; | ||
| pos += prefix; | ||
| let remaining = (bytes.len() - pos) as u64; | ||
| if payload > remaining { | ||
| return Err("declares more bytes than the input holds".to_owned()); | ||
| } | ||
| // <= remaining, so this cannot fail; `try_from` rather than `as usize` satisfies | ||
| // clippy::cast_possible_truncation, line-for-line with cachekit-rs `check_structure`. | ||
| pos += usize::try_from(payload) | ||
| .map_err(|_| "declares more bytes than the input holds".to_owned())?; | ||
| if children > 0 { | ||
| if open.len() >= max_depth { | ||
| return Err(format!("nests deeper than {max_depth} levels")); | ||
| } | ||
| open.push(children); | ||
| } | ||
| pending += children; | ||
| if pending > remaining - payload { | ||
| return Err("declares more elements than the input can back".to_owned()); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.