Repository navigation
Fix integer overflow in file read path - #252
Merged
Merged
Conversation
petrelharp
approved these changes
Jun 25, 2026
petrelharp
left a comment
Contributor
There was a problem hiding this comment.
Code LGTM. Looks like this needs the updated actions (I didn't do those here).
Harden kastore_read_descriptors against crafted files: the bounds checks key_start + key_len and array_start + array_len * type_size were computed in 64-bit arithmetic on untrusted values and could wrap around past file_size. A file declaring e.g. array_len = 2^61 of KAS_UINT64 passed all validation (including the structural checks, which accumulate the same wrapping product) but caused kastore_read_item to under-allocate while reporting a huge array_len, leading to an out-of-bounds read in the caller. The checks are now written with subtraction and division so they cannot overflow. Also reject zero-length keys on read (matching the write-side invariant) and replace the reachable assert(size > offset) in kastore_read_file with a KAS_ERR_BAD_FILE_FORMAT return, and guard the (long) cast of array_start in kastore_read_item's fseek against truncation on LLP64 platforms. Add tests covering the array_len and key_len overflow cases, zero-length keys, and a positive control. Add changelog notes for read-path overflow security fix Document the integer-overflow vulnerability in the file read path (crafted descriptors leading to an under-allocated buffer and an out-of-bounds read) in both the C and Python changelogs.
jeromekelleher
force-pushed
the
fix-security-vuln
branch
from
June 26, 2026 09:59
2ec7396 to
6c85018
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #252 +/- ##
==========================================
+ Coverage 91.30% 91.40% +0.09%
==========================================
Files 8 8
Lines 1346 1350 +4
Branches 194 196 +2
==========================================
+ Hits 1229 1234 +5
Misses 106 106
+ Partials 11 10 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Two error conditions added in the read-path security fix were not exercised by any test: - The size <= offset geometry guard in kastore_read_file was dead code: kastore_read_descriptors already rejects zero-length keys, which guarantees items[0].array_start > offset. Remove the redundant guard and document that the subtraction cannot underflow. - The LLP64 fseek-truncation guard in kastore_read_item (array_start > LONG_MAX - file_offset) was unreachable from a single-item file. Add test_array_start_seek_overflow, a crafted two-item file whose second array starts beyond LONG_MAX. It opens in lazy mode (only the keys are read) and fetching the second key reaches kastore_read_item, which now returns KAS_ERR_IO instead of seeking to a truncated offset.
jeromekelleher
force-pushed
the
fix-security-vuln
branch
from
June 26, 2026 10:37
de306c1 to
4376306
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Harden kastore_read_descriptors against crafted files: the bounds checks key_start + key_len and array_start + array_len * type_size were computed in 64-bit arithmetic on untrusted values and could wrap around past file_size. A file declaring e.g. array_len = 2^61 of KAS_UINT64 passed all validation (including the structural checks, which accumulate the same wrapping product) but caused kastore_read_item to under-allocate while reporting a huge array_len, leading to an out-of-bounds read in the caller. The checks are now written with subtraction and division so they cannot overflow.
Also reject zero-length keys on read (matching the write-side invariant) and replace the reachable assert(size > offset) in kastore_read_file with a KAS_ERR_BAD_FILE_FORMAT return, and guard the (long) cast of array_start in kastore_read_item's fseek against truncation on LLP64 platforms.
Add tests covering the array_len and key_len overflow cases, zero-length keys, and a positive control.
Closes #251