Skip to content

Add bounds checking to OgaSequencesGetSequence C API functions#2257

Closed
jiafatom wants to merge 2 commits into
microsoft:mainfrom
jiafatom:fix/sequences-oob-bounds-check
Closed

Add bounds checking to OgaSequencesGetSequence C API functions#2257
jiafatom wants to merge 2 commits into
microsoft:mainfrom
jiafatom:fix/sequences-oob-bounds-check

Conversation

@jiafatom

@jiafatom jiafatom commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

OgaSequencesGetSequenceCount and OgaSequencesGetSequenceData accessed the underlying vector via operator[] without validating the sequence index. When a caller passes an index >= OgaSequencesCount(), this results in an out-of-bounds read of heap memory (undefined behavior), which can disclose adjacent heap contents.

This mirrors the validation already present in OgaAppendTokenToSequence, which checks the index before access.

Changes

  • OgaSequencesGetSequenceCount: return 0 when sequence >= size().
  • OgaSequencesGetSequenceData: return nullptr when sequence >= size().
    • Returning a value (instead of throwing) keeps a valid C ABI so no C++ exception crosses the C API boundary. This protects all language bindings (C, C++, Python, C#, Java) that route through these functions.
  • OgaCreateTensorFromBuffer: reject negative shape dimensions and guard the byte-count multiplication against size_t overflow (defense-in-depth).
  • Added CAPITests.SequencesOutOfBoundsAccess unit test (no model required) verifying valid access works and out-of-bounds indices return 0 / nullptr.

Testing

Built the library and unit tests, and ran:

[ RUN      ] CAPITests.AppendTokensToSequence
[       OK ] CAPITests.AppendTokensToSequence
[ RUN      ] CAPITests.SequencesOutOfBoundsAccess
[       OK ] CAPITests.SequencesOutOfBoundsAccess
[ RUN      ] CAPITests.MaxLength
[       OK ] CAPITests.MaxLength
[  PASSED  ] 3 tests.

OgaSequencesGetSequenceCount and OgaSequencesGetSequenceData accessed the
underlying vector via operator[] without validating the sequence index,
allowing an out-of-bounds read when the caller supplied an index >= the
number of sequences. Return 0 / nullptr for out-of-bounds indices so no
C++ exception crosses the C API boundary.

Also harden OgaCreateTensorFromBuffer against negative dimensions and
byte-count multiplication overflow when computing the tensor size.

Add a CAPITests.SequencesOutOfBoundsAccess unit test covering the
out-of-bounds behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 2, 2026 23:15
@jiafatom
jiafatom requested a review from a team as a code owner July 2, 2026 23:15

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.

Pull request overview

This PR hardens the GenAI C ABI surface against unsafe inputs by adding bounds checks to sequence accessors and adding additional validation to tensor creation from user-provided buffers, plus a unit test to prevent regressions.

Changes:

  • Add out-of-bounds guarding to OgaSequencesGetSequenceCount / OgaSequencesGetSequenceData to avoid undefined behavior and potential heap disclosure.
  • Add validation in OgaCreateTensorFromBuffer for negative dimensions and size_t overflow during byte-count computation.
  • Add CAPITests.SequencesOutOfBoundsAccess to validate in-bounds behavior and ensure out-of-bounds indices return safe sentinel values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/ort_genai_c.cpp Adds bounds checks for sequence getters and strengthens tensor-shape validation/overflow checking in the C API implementation.
test/c_api_tests.cpp Adds a regression test ensuring out-of-range sequence indices don’t read past underlying storage and return 0 / nullptr.

Comment thread src/ort_genai_c.cpp
Addresses review feedback: a caller of this C ABI entry point could pass
shape_dims=nullptr with a non-zero shape_dims_count, causing a null
dereference in the shape loop. Reject that combination explicitly before
constructing the span.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jiafatom

jiafatom commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #2260, recreated as a non-fork branch in this repo so CI can access the internal build registry. Same commits and review fixes.

@jiafatom jiafatom closed this Jul 2, 2026
jiafatom added a commit that referenced this pull request Jul 10, 2026
## Description

`OgaSequencesGetSequenceCount` and `OgaSequencesGetSequenceData`
accessed the underlying vector via `operator[]` without validating the
`sequence` index. When a caller passes an index `>=
OgaSequencesCount()`, this results in an out-of-bounds read of heap
memory (undefined behavior), which can disclose adjacent heap contents.

This mirrors the validation already present in
`OgaAppendTokenToSequence`, which checks the index before access.

## Changes

- `OgaSequencesGetSequenceCount`: return `0` when `sequence >= size()`.
- `OgaSequencesGetSequenceData`: return `nullptr` when `sequence >=
size()`.
- Returning a value (instead of throwing) keeps a valid C ABI so no C++
exception crosses the C API boundary. This protects all language
bindings (C, C++, Python, C#, Java) that route through these functions.
- `OgaCreateTensorFromBuffer`: reject negative shape dimensions, guard
the byte-count multiplication against `size_t` overflow, and reject a
null `shape_dims` with a non-zero count (defense-in-depth).
- Added `CAPITests.SequencesOutOfBoundsAccess` unit test (no model
required) verifying valid access works and out-of-bounds indices return
`0` / `nullptr`.

## Testing

Built the library and unit tests, and ran:

```
[ RUN      ] CAPITests.AppendTokensToSequence
[       OK ] CAPITests.AppendTokensToSequence
[ RUN      ] CAPITests.SequencesOutOfBoundsAccess
[       OK ] CAPITests.SequencesOutOfBoundsAccess
[ RUN      ] CAPITests.MaxLength
[       OK ] CAPITests.MaxLength
[  PASSED  ] 3 tests.
```

Replaces #2257 (recreated as a non-fork branch so CI can run).

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
tianleiwu pushed a commit that referenced this pull request Jul 11, 2026
## Description

`OgaSequencesGetSequenceCount` and `OgaSequencesGetSequenceData`
accessed the underlying vector via `operator[]` without validating the
`sequence` index. When a caller passes an index `>=
OgaSequencesCount()`, this results in an out-of-bounds read of heap
memory (undefined behavior), which can disclose adjacent heap contents.

This mirrors the validation already present in
`OgaAppendTokenToSequence`, which checks the index before access.

## Changes

- `OgaSequencesGetSequenceCount`: return `0` when `sequence >= size()`.
- `OgaSequencesGetSequenceData`: return `nullptr` when `sequence >=
size()`.
- Returning a value (instead of throwing) keeps a valid C ABI so no C++
exception crosses the C API boundary. This protects all language
bindings (C, C++, Python, C#, Java) that route through these functions.
- `OgaCreateTensorFromBuffer`: reject negative shape dimensions, guard
the byte-count multiplication against `size_t` overflow, and reject a
null `shape_dims` with a non-zero count (defense-in-depth).
- Added `CAPITests.SequencesOutOfBoundsAccess` unit test (no model
required) verifying valid access works and out-of-bounds indices return
`0` / `nullptr`.

## Testing

Built the library and unit tests, and ran:

```
[ RUN      ] CAPITests.AppendTokensToSequence
[       OK ] CAPITests.AppendTokensToSequence
[ RUN      ] CAPITests.SequencesOutOfBoundsAccess
[       OK ] CAPITests.SequencesOutOfBoundsAccess
[ RUN      ] CAPITests.MaxLength
[       OK ] CAPITests.MaxLength
[  PASSED  ] 3 tests.
```

Replaces #2257 (recreated as a non-fork branch so CI can run).

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants