From 74b07bdd8da7ce98dbafdb8377ac94ca4d7b8a64 Mon Sep 17 00:00:00 2001 From: nabeel-dev21 Date: Fri, 30 Oct 2026 06:18:18 +0530 Subject: [PATCH] validate channel image before allocating in janet_chanat_unmarshal (#1934) * validate channel image before allocating in janet_chanat_unmarshal Fix panic by changing allocation order of abstract and channel. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Calvin Rose Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/core/ev.c | 11 ++++++----- test/suite-marsh.janet | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/core/ev.c b/src/core/ev.c index 304b0ac6..66ab3944 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -1524,17 +1524,18 @@ static void janet_chanat_marshal(void *p, JanetMarshalContext *ctx) { static void *janet_chanat_unmarshal(JanetMarshalContext *ctx) { uint8_t is_threaded = janet_unmarshal_byte(ctx); + uint8_t is_closed = janet_unmarshal_byte(ctx); + int32_t limit = janet_unmarshal_int(ctx); + int32_t count = janet_unmarshal_int(ctx); + if (count < 0) janet_panic("invalid negative channel count"); + if (count > limit) janet_panic("invalid channel count"); + /* Validate before allocating so a panic can't leave an uninitialized channel */ JanetChannel *abst; if (is_threaded) { abst = janet_unmarshal_abstract_threaded(ctx, sizeof(JanetChannel)); } else { abst = janet_unmarshal_abstract(ctx, sizeof(JanetChannel)); } - uint8_t is_closed = janet_unmarshal_byte(ctx); - int32_t limit = janet_unmarshal_int(ctx); - int32_t count = janet_unmarshal_int(ctx); - if (count < 0) janet_panic("invalid negative channel count"); - if (count > limit) janet_panic("invalid channel count"); janet_chan_init(abst, limit, 0); abst->closed = !!is_closed; for (int32_t i = 0; i < count; i++) { diff --git a/test/suite-marsh.janet b/test/suite-marsh.janet index a236cf9c..5ca92844 100644 --- a/test/suite-marsh.janet +++ b/test/suite-marsh.janet @@ -286,4 +286,20 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02 (assert-error "unmarshal frameless resumable fiber" (resume (unmarshal tampered)))) +# A malformed channel image must be rejected before the channel is allocated. +# Previously the abstract was allocated first, so a panic while reading the +# header left an uninitialized channel in the heap and the next collection ran +# its finalizer on garbage queue pointers and a garbage mutex. +(compwhen (dyn 'ev/chan) + (def b (buffer (marshal (ev/chan 2)))) + # the image ends with is_threaded, closed, limit and count, one byte each + (def n (length b)) + (assert (and (= (b (- n 2)) 2) (= (b (- n 1)) 0)) "unexpected channel marshal layout") + # claim more queued items than the limit allows + (put b (- n 1) 3) + (assert-error "unmarshal channel count over limit" (unmarshal b)) + # cut the image off right after the is_threaded byte + (assert-error "unmarshal truncated channel" (unmarshal (buffer/slice b 0 (- n 3)))) + (gccollect)) + (end-suite)