Skip to content

security(gguf): add ParseLimits budgets and checked host-size conversions - #76

Open
rmems wants to merge 1 commit into
mainfrom
cursor/gguf-parse-limits-24ce
Open

rmems wants to merge 1 commit into
mainfrom
cursor/gguf-parse-limits-24ce

Conversation

@rmems

@rmems rmems commented Sep 15, 2026

Copy link
Copy Markdown
Owner

User description

Fixes RM-1358.

GGUF files are untrusted. This adds one documented ParseLimits policy and fail-closed host-size conversions so crafted headers cannot request unbounded allocation or iteration.

What changed

  • ParseLimits with generous defaults covering KV count, tensor count, string bytes, array work items, tensor rank, and metadata bytes.
  • Typed ParserError::LimitExceeded (names the budget) and ParserError::HostSizeOverflow (names the field).
  • File-declared u64 sizes convert with try_from instead of lossy as usize.
  • Checked add/multiply/round-up for alignment, relative/absolute tensor offsets, element counts, and packed byte sizes.
  • Limits are rejected before allocation or loops proportional to the declared value.
  • Iterative nested-array skip is unchanged; total work is now bounded.
  • load_gguf_with_limits / parse_bytes_with_limits / load_gguf_mmap_with_limits let trusted callers override without changing default safety.
  • Default and mmap readers share parse_layout and therefore the same budgets.

Tests

Required cases in tests/gguf_limits.rs plus cursor/limits unit tests:

  • Exact boundary and boundary+1 for every limit
  • u64::MAX string/count/offset/array fields
  • Deep nested arrays with bounded total work
  • Pathological rank (u32::MAX) and element-count multiplication overflow
  • Truncated buffers at each header/directory stage
  • Default vs mmap error parity

Local CI evidence

cargo fmt --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --all-features

All three passed on this branch (84 lib tests + 16 limits tests + existing smoke/mmap/safetensors + doctests).

Linear Issue: RM-1358

Open in Web Open in Cursor 

Summary by cubic

Adds resource budgets and checked host-size conversions to GGUF parsing so crafted headers cannot request unbounded allocation or iteration. New ParseLimits with generous defaults, typed errors for limit and size overflow, and *_with_limits entry points for trusted callers. Addresses RM-1358.

Details

  • Introduces ParseLimits covering KV count, tensor count, string bytes, array work, tensor rank, and metadata bytes.
  • File-declared u64 sizes now convert with try_from instead of lossy as usize.
  • Limits are rejected before allocation or loops proportional to declared values.
  • Default and mmap readers share the same policy.
  • Adds load_gguf_with_limits, parse_bytes_with_limits, load_gguf_mmap_with_limits for trusted overrides.

Written for commit 4578766. Summary will update on new commits.

Review in cubic


CodeAnt-AI Description

Protect GGUF parsing from untrusted resource requests and arithmetic overflow

What Changed

  • GGUF parsing now enforces configurable budgets for metadata entries, tensors, strings, nested-array work, tensor rank, and metadata size before allocating or iterating.
  • Malformed 64-bit sizes, offsets, dimensions, alignments, and byte counts now fail safely instead of being truncated or wrapping.
  • Parsing errors identify the exceeded budget or file field, making failures clearer to callers.
  • Added explicit limit controls for file, in-memory, and mmap readers; existing entry points retain safe defaults while trusted callers can opt into higher limits.
  • Default and mmap parsing now apply the same safety checks.

Impact

✅ Prevented unbounded allocation from crafted GGUF headers
✅ Fewer parser crashes and wraparound layout errors
✅ Clearer resource-limit and size-overflow errors

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

…ions

Fail closed on untrusted GGUF headers before allocation or loops
proportional to declared counts. Typed LimitExceeded and HostSizeOverflow
errors name the budget or field. Default and mmap readers share the
same policy; trusted callers can override without changing defaults.

Linear: RM-1358

Co-authored-by: Raul Cardenas Montoya <montoyaraul34@gmail.com>
@linear-code

linear-code Bot commented Sep 15, 2026

Copy link
Copy Markdown

RM-1358

@coderabbitai

coderabbitai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 52 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: d219d8d6-230b-42c4-a4ad-9074c616afed

📥 Commits

Reviewing files that changed from the base of the PR and between 9bc66ff and 4578766.

📒 Files selected for processing (12)
  • CHANGELOG.md
  • README.md
  • src/error.rs
  • src/gguf/cursor.rs
  • src/gguf/layout.rs
  • src/gguf/limits.rs
  • src/gguf/map.rs
  • src/gguf/mod.rs
  • src/gguf/tensor.rs
  • src/lib.rs
  • tests/common/mod.rs
  • tests/gguf_limits.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@rmems
rmems marked this pull request as ready for review September 15, 2026 05:17
@codeant-ai

codeant-ai Bot commented Sep 15, 2026

Copy link
Copy Markdown

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Reviewed your PR 4578766 Sep 15, 2026 · 05:17 05:20

@codeant-ai

codeant-ai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-15T05:21:19.741112Z 4578766 Draft marked ready
🔒 Security Review Completed 2026-09-15T05:23:00.888042Z 4578766 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a comprehensive security enhancement that properly addresses resource exhaustion and integer overflow risks when parsing untrusted GGUF files. The implementation follows security best practices with fail-closed design, checked arithmetic throughout, and excellent test coverage. No blocking issues found.

Key strengths:

  • Proper use of checked_add/checked_mul to prevent integer overflows
  • Type-safe conversions with try_from instead of lossy casts
  • Limits checked before allocation/iteration
  • Comprehensive error types with specific field names
  • Thorough test coverage including boundary conditions

The code is production-ready and properly implements defense-in-depth for untrusted input handling.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

@codeant-ai codeant-ai Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files label Sep 15, 2026
Comment thread src/gguf/layout.rs
Comment on lines +435 to +442
tensor.absolute_offset = tensor_data_offset
.checked_add(tensor.relative_offset)
.ok_or_else(|| {
invalid_layout(
path,
format!("tensor '{}' absolute offset overflow", tensor.name),
)
})?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: A relative offset of zero places the tensor at tensor_data_offset, but smaller or overlapping offsets are not rejected, so tensor reads can expose metadata or another tensor's bytes. [logic error]

Assessment: 🟠 Major · 🔁 Occurrence: Rarely

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** src/gguf/layout.rs
**Line:** 435:442
**Comment:**
	*Logic Error: A relative offset of zero places the tensor at `tensor_data_offset`, but smaller or overlapping offsets are not rejected, so tensor reads can expose metadata or another tensor's bytes.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants