Skip to content

compile.c: reject negative object index in the IBF loader (pinned_list_store / pinned_list_fetch) - #17452

Merged
ko1 merged 1 commit into
ruby:masterfrom
Gengyskan:ibf-pinned-list-negative-index
Aug 5, 2026
Merged

ko1 merged 1 commit into
ruby:masterfrom
Gengyskan:ibf-pinned-list-negative-index

Conversation

@Gengyskan

@Gengyskan Gengyskan commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

compile.c: reject negative object index in the IBF loader (pinned_list_store / pinned_list_fetch)

pinned_list_store and pinned_list_fetch in compile.c validate only the upper bound
of the object index (a signed long):

if (offset >= ptr->size) {
    rb_raise(rb_eIndexError, "object index out of range: %ld", offset);
}

They accept negative offsets, so a malformed or truncated IBF blob whose object index
(e.g. the TS_CDHASH operand in ibf_load_code) is negative causes an out-of-bounds
write (pinned_list_store) or read (pinned_list_fetch) through ptr->buffer[negative]
instead of failing cleanly.

This is not a security boundary (load_from_binary is documented as requiring trusted
input, per https://hackerone.com/reports/3816955), but in practice the loader is reached by
caching layers (e.g. Bootsnap) on possibly-corrupted on-disk caches, so a clean exception is
much friendlier than an OOB access when debugging a corrupted cache, and is consistent with
the existing upper-bound check.

The fix adds a lower-bound check to both functions, turning the OOB access into the same
IndexError already raised for the upper bound:

if (offset < 0 || offset >= ptr->size) {
    rb_raise(rb_eIndexError, "object index out of range: %ld", offset);
}

Verification: test/ruby/test_iseq.rb passes with the change, and a crafted IBF blob with a
negative object index now raises IndexError instead of performing a silent out-of-bounds
write.

(I initially included a regression test using a fixed crafted blob, but dropped it: IBF
binaries are platform-specific (endianness / word size), so the blob was rejected as
"broken binary format" on big-endian / 32-bit CI before reaching the code path. Happy to add
a regression test that constructs the malformed input at test time if you'd like one.)

@Gengyskan
Gengyskan force-pushed the ibf-pinned-list-negative-index branch from dd8d360 to fe48d5d Compare June 23, 2026 17:45
pinned_list_store and pinned_list_fetch validated only the upper bound
(offset >= ptr->size) on a signed long index, so a malformed or truncated
IBF blob with a negative object index caused an out-of-bounds write/read
through ptr->buffer[negative] instead of failing cleanly. Add the missing
lower-bound check, raising the same IndexError already used for the upper
bound.
@Gengyskan
Gengyskan force-pushed the ibf-pinned-list-negative-index branch from fe48d5d to 5506c54 Compare June 23, 2026 18:25
@nobu
nobu requested a review from ko1 June 24, 2026 10:24
@ko1
ko1 merged commit 91cfd2c into ruby:master Aug 5, 2026
142 of 144 checks passed
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