From a439a61f8faefbb4074494ee126774f8a5aafddc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 8 Jul 2026 00:30:40 -0600 Subject: [PATCH] [vector] Guard base64 output-length overflow in SVG image embedding hb_vector_buf_t::append_base64() sized its output as unsigned out_len = ((len + 2) / 3) * 4; which overflows 32-bit for a very large len (len + 2 wraps, and the * 4 wraps for len above ~3.2 GB). len is the attacker-controlled embedded PNG length on the vector SVG paint path (hb-vector-paint-svg.cc), so a wrapped out_len under-sizes the buffer while the encode loop still writes 4 bytes per 3 input bytes -- a heap out-of-bounds write. Compute the group count without the +2 (len / 3 + (len % 3 != 0)) and use hb_unsigned_mul_overflows()/hb_unsigned_add_overflows() so an oversized input fails the allocation instead of wrapping. resize_full() already rejects a size that does not fit in int, so the remaining path is safe. Assisted-by: Claude Opus 4.8 (1M context) --- src/hb-vector-buf.hh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hb-vector-buf.hh b/src/hb-vector-buf.hh index 543cbddd7..5b2321dec 100644 --- a/src/hb-vector-buf.hh +++ b/src/hb-vector-buf.hh @@ -155,10 +155,13 @@ struct hb_vector_buf_t : hb_vector_t bool append_base64 (const uint8_t *data, unsigned len) { - unsigned out_len = ((len + 2) / 3) * 4; + /* Output is 4 bytes per 3 input bytes; compute without overflowing. */ + unsigned ngroups = len / 3 + (len % 3 != 0); + unsigned out_len; unsigned old_len = length; unsigned new_len; - if (unlikely (hb_unsigned_add_overflows (old_len, out_len, &new_len) || + if (unlikely (hb_unsigned_mul_overflows (ngroups, 4, &out_len) || + hb_unsigned_add_overflows (old_len, out_len, &new_len) || !resize_dirty ((int) new_len))) return false;