Skip to content

Commit

Permalink
ensure ibf_load_setup is only passed String params
Browse files Browse the repository at this point in the history
In cases where RubyVM::InstructionSequence.load_from_binary() is
passed a param other than a String, we attempt to call the
RSTRING_LENINT macro on it which can cause a segfault.

ex:
```
var_0 = 0
RubyVM::InstructionSequence.load_from_binary(var_0)
```

This commit adds a type check to raise unless we are provided
a String.
  • Loading branch information
fresh-eggs authored and nobu committed Apr 20, 2024
1 parent 23be659 commit 9555a99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compile.c
Expand Up @@ -14214,6 +14214,8 @@ ibf_load_setup_bytes(struct ibf_load *load, VALUE loader_obj, const char *bytes,
static void
ibf_load_setup(struct ibf_load *load, VALUE loader_obj, VALUE str)
{
StringValue(str);

if (RSTRING_LENINT(str) < (int)sizeof(struct ibf_header)) {
rb_raise(rb_eRuntimeError, "broken binary format");
}
Expand Down
7 changes: 7 additions & 0 deletions test/ruby/test_iseq.rb
Expand Up @@ -850,4 +850,11 @@ def test_compile_prism_with_invalid_object_type
RubyVM::InstructionSequence.compile_prism(Object.new)
end
end

def test_load_from_binary_only_accepts_string_param
assert_raise(TypeError) do
var_0 = 0
RubyVM::InstructionSequence.load_from_binary(var_0)
end
end
end

0 comments on commit 9555a99

Please sign in to comment.