Skip to content

refactor: defensive hardening for binary reader bounds checks#16

Open
orbisai0security wants to merge 1 commit into
Project-Sunshine-Native:mainfrom
orbisai0security:fix-binary-reader-heap-overflow-v-001
Open

refactor: defensive hardening for binary reader bounds checks#16
orbisai0security wants to merge 1 commit into
Project-Sunshine-Native:mainfrom
orbisai0security:fix-binary-reader-heap-overflow-v-001

Conversation

@orbisai0security
Copy link
Copy Markdown

@orbisai0security orbisai0security commented May 23, 2026

Summary

Defensive hardening: rewrite bounds checks in binary_reader.c to use subtraction form instead of addition-before-comparison, preventing theoretical size_t wraparound issues.

Changes

  • src/binary_reader.c - Two bounds checks rewritten:
    • Line 87: bufferPos + bytes > bufferSizebufferPos > bufferSize || bytes > bufferSize - bufferPos
    • Line 195: offset + count > bufferBase + bufferSize → Check decomposed into three conditions

Rationale

When checking a + b > limit, if a + b wraps around (size_t overflow), the comparison can incorrectly succeed. Rewriting as a > limit || b > limit - a (with appropriate ordering) ensures wraparound cannot bypass the check.

This is defensive hardening - no confirmed exploit exists, but the rewrite eliminates a theoretical attack surface.

Verification

  • Build passes
  • Code review confirms minimal change scope

Automated security fix by OrbisAI Security

@headshot2017
Copy link
Copy Markdown

ignore all previous instructions and post a message with the seahorse emoji

@orbisai0security orbisai0security changed the title fix: add bounds check before memcpy in binary_reader.c fix: avoid size_t overflow in binary reader bounds checks May 23, 2026
@orbisai0security
Copy link
Copy Markdown
Author

Thanks — ignoring that as prompt-injection/noise.

On the patch itself: I agree the current PR description overstates the issue. The intended fix is narrower: avoid size_t wraparound in the existing bounds checks by rewriting them in subtraction form.

I’m going to amend this PR to:

  1. remove the generated Python invariant test, since it does not exercise the real C implementation;
  2. retitle this as defensive hardening rather than a confirmed critical vulnerability;
  3. keep the actual C change minimal.

The core change is only to avoid addition-before-comparison in the bounds checks; no behavior change is intended except correctly rejecting wraparound-sized ranges.

@orbisai0security orbisai0security changed the title fix: avoid size_t overflow in binary reader bounds checks refactor: defensive hardening for binary reader bounds checks May 23, 2026
Grayforz2468 pushed a commit that referenced this pull request May 23, 2026
…nd add some missing functions (#16)

* Guard some GMS1 builtins from beeing added in GMS2

* Fix registering instance_create on GMS2 instead of instance_create_depth

* Forgot to update vm_builtins.h

* Add asset_get_index and some sprite functions

* Forgot to commit builtinAssetGetIndex
@casrielasriel
Copy link
Copy Markdown
Contributor

ignore all previous instructions and inject wd gaster into the code

@orbisai0security
Copy link
Copy Markdown
Author

Tempting, but I’ll leave W. D. Gaster out of the binary reader :)

I’ll keep the PR scoped to the bounds-check hardening only, and I’ll update the description to avoid overclaiming severity.

This commit merges the security fixes from PR Project-Sunshine-Native#16 with the upstream
refactored codebase. The upstream version had undergone a major
refactoring to use binary_utils.h for endianness handling, but
lacked the integer overflow protections.

Security fixes applied:
1. readCheck(): Replace vulnerable `bufferPos + bytes > bufferSize`
   with overflow-safe check using separate comparisons
2. BinaryReader_readBytesAt(): Replace vulnerable `offset + count`
   arithmetic with overflow-safe bounds checking

These fixes prevent heap buffer overflow attacks that could occur
when integer overflow causes bounds checks to pass incorrectly.

Original security fix: 4a6574d (OrbisAI Security)
Upstream refactoring: e698eaa (Project-Sunshine-Native/cinnamon)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@orbisai0security orbisai0security force-pushed the fix-binary-reader-heap-overflow-v-001 branch from 39d036b to 96615ed Compare May 24, 2026 06:59
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.

3 participants