Skip to content

Commit 48672b2

Browse files
authored
parser lyb BUGFIX integer overflow and OOM (#2513)
lyb_read_string: when str_len == UINT32_MAX, (str_len + 1) wraps to 0, malloc(0) returns non-NULL, and the subsequent write to (*str)[UINT32_MAX] causes a WRITE SEGV (memory corruption). lyb_read_value: when lyb_size_bits == UINT32_MAX with VARIABLE_BYTES, LYPLG_BITS2BYTES() produces ~4 GiB, causing calloc to attempt a 4 GiB allocation which triggers OOM / DoS. Both paths are reachable by supplying a malformed LYB input with length field set to 0xFFFFFFFF. Reported-by: Dominik Blain <dominik@qreativelab.io>, Cobalt AI
1 parent ca68827 commit 48672b2

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/parser_lyb.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ lyb_read_string(char **str, struct lylyb_parse_ctx *lybctx)
284284
/* read length in bytes */
285285
lyb_read_size(&str_len, lybctx);
286286

287+
/* str_len + 1 wraps to 0 when str_len == UINT32_MAX, causing malloc(0) followed by an out-of-bounds write */
288+
LY_CHECK_ERR_RET(str_len == UINT32_MAX, LOGERR(lybctx->ctx, LY_EINVAL, "LYB string length overflow."), LY_EINVAL);
289+
287290
/* allocate mem */
288291
*str = malloc(((uint64_t)str_len + 1) * sizeof **str);
289292
LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
@@ -340,6 +343,11 @@ lyb_read_value(const struct lysc_type *type, uint8_t **val, uint64_t *val_size_b
340343
}
341344
}
342345

346+
/* LYPLG_BITS2BYTES(val_size_bits) + 1 can reach 4 GiB when lyb_size_bits == UINT32_MAX and
347+
* size_type == VARIABLE_BYTES, causing calloc to attempt a 4 GiB allocation (OOM / DoS) */
348+
LY_CHECK_ERR_RET(LYPLG_BITS2BYTES(*val_size_bits) >= UINT32_MAX,
349+
LOGERR(lybctx->ctx, LY_EINVAL, "LYB value size overflow."), LY_EINVAL);
350+
343351
/* allocate zeroed memory with an addition zero byte */
344352
*val = calloc(LYPLG_BITS2BYTES(*val_size_bits) + 1, sizeof **val);
345353
LY_CHECK_ERR_RET(!*val, LOGMEM(lybctx->ctx), LY_EMEM);

0 commit comments

Comments
 (0)