Skip to content

Commit cc326a1

Browse files
committed
runtime_events_consumer: guard agains short msg_length
msg_length zero could cause us to loop infinitely. All messages have at least a header and a timestamp, except EV_INTERNAL (0), which is a padding event. Extend the testcase by corrupting message lengths. This also avoids some memory sanitizer warnings. Rejecting all msg_length < 2 would cause a failure in test_dropped_events.ml, because padding events would be incorrectly rejected. Suggested-by: Miod Vallat <[email protected]> Signed-off-by: Edwin Török <[email protected]>
1 parent b175627 commit cc326a1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

otherlibs/runtime_events/runtime_events_consumer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,13 @@ caml_runtime_events_read_poll(struct caml_runtime_events_cursor *cursor,
462462
continue;
463463
}
464464

465+
if (!msg_length
466+
|| (msg_length < 2
467+
&& RUNTIME_EVENTS_ITEM_TYPE(header) != EV_INTERNAL)) {
468+
atomic_store(&cursor->cursor_in_poll, 0);
469+
return E_CORRUPT_STREAM;
470+
}
471+
465472
if (RUNTIME_EVENTS_ITEM_IS_RUNTIME(header)) {
466473
switch (RUNTIME_EVENTS_ITEM_TYPE(header)) {
467474
case EV_BEGIN:

testsuite/tests/lib-runtime-events/test_corrupted.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,18 @@
114114
due to bounds error on an earlier offset
115115
*)
116116
Bytes.blit_string original 0 buf 0 (Bytes.length buf);
117-
done
117+
parse_corrupted path_pid
118+
done;
119+
for is_runtime = 0 to 1 do
120+
for event_type = 0 to 15 (* event type is 4 bits *) do
121+
for event_id = 0 to 64 (* event_id is 13 bits, but not all used yet *) do
122+
for length = 0 to 1 (* short lengths trigger uninit read bugs *) do
123+
(* modify just 1 event in the otherwise valid ring *)
124+
write_event_header is_runtime event_type event_id length;
125+
(* parse ring *)
126+
parse_corrupted path_pid
127+
done
128+
done
129+
done;
130+
done;
118131
end

0 commit comments

Comments
 (0)