peer_prepare_updatemsg() builds a stick-table update in the <size>-byte
trash buffer but never checked that the encoded data actually fit, and
still carried its "TODO: check size" comment. Fixed-size fields were
copied with memcpy() and every length was encoded with intencode(),
which cannot fail and has no knowledge of the buffer end.
An entry learned from a peer may hold a string key up to key_size - 1,
which the peers protocol accepts in a full-size message. Re-encoding it
adds a 2-byte header, the update identifier and, for timed updates, an
expiration before the key, so the copy runs past the end of the trash
buffer. ASan reports a heap-buffer-overflow WRITE right after the
16384-byte trash region, and on a normal build the two overflowing bytes
land in the next chunk.
Let's check the room left before every copy and every varint encoding.
A new peer_bufcpy() helper copies at most what the buffer can take,
advances the cursor on success and leaves it untouched on failure, so
the success path is unchanged. Lengths are now encoded with
encode_varint(), whose bounds are those of the destination buffer, and
all failures return 0 through a single exit point that releases the
session lock.
The payload length becomes a size_t: as an unsigned short it silently
truncated messages larger than 65535 bytes, which tune.bufsize above 64k
makes reachable.
This should be backported to all supported versions.