Skip to content

Commit 50a3463

Browse files
committed
Avoid illegal pointer
When loading a crafted marshal data of Random, a pointer to an illegal address was created. I don't think there is any harm since the data is normalized before access, but just to be safe, I add a check to make it an error.
1 parent 803eed6 commit 50a3463

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

random.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ rand_mt_load(VALUE obj, VALUE dump)
895895
sizeof(*mt->state), 0,
896896
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
897897
x = NUM2ULONG(left);
898-
if (x > numberof(mt->state)) {
898+
if (x > numberof(mt->state) || x == 0) {
899899
rb_raise(rb_eArgError, "wrong value");
900900
}
901901
mt->left = (unsigned int)x;

test/ruby/test_rand.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,9 @@ def test_new_seed
434434
# probability of failure <= 1/256**8
435435
assert_operator(size.fdiv(n), :>, 15)
436436
end
437+
438+
def test_broken_marshal
439+
assert_raise(ArgumentError) { Marshal.load("\x04\bU:\vRandom" + Marshal.dump([1,0,1])[2..]) }
440+
assert_raise(ArgumentError) { Marshal.load("\x04\bU:\vRandom" + Marshal.dump([1,-1,1])[2..]) }
441+
end
437442
end

0 commit comments

Comments
 (0)