mirror of
https://github.com/janet-lang/janet
synced 2026-11-23 15:47:11 +00:00
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 <calsrose@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
2 files changed
+22
-5
No files matched your search
+6
-5
@@ -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++) {
|
||||
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user