Hi Willy, > If you're fine with that one, I'll apply it.
Yes, that's fine with me, thanks. I ran your version through the same checks as mine, built with ASan and UBSan: 21 million values from encode_varint() (all small values, powers of two +/- 300, random values, UINT64_MAX) decode back unchanged, every truncated buffer is rejected, and your f0 ff ff ff ff ff ff ff ff 7f example, a 10-byte form that would exceed 2^64-1 and an over-long 16-byte input are all rejected, with no sanitizer report. Regards, Martin Mirchev On Thu, Nov 19, 2026 11:12 PM, Willy Tarreau <[email protected]> wrote: > Hi Martin, > > On Thu, Nov 19, 2026 at 12:54:04AM -0800, Martin Mirchev wrote: > > Hi Willy, > > > > Thanks. Noted: from now on I'll send at most two or three small patches > at > > a time, with git send-email and one message per patch. > > > > > I remember us fixing something similar recently, maybe it was for peers > > > or QUIC then > > > > The recent ones I found are the peers intdecode() fix (690801bed3) and > the > > qpack_get_varint() shift check (574aa9853c). Patch 1 is about > > decode_varint() in include/haproxy/intops.h, which the SPOE/SPOP code > uses > > on agent frames (mux_spop.c, spoe.h, tcpcheck.c), so it is a separate > > decoder. > > Ah OK that makes sense then. I was not fond of the extra checks in the > inner loop for something that never happens so I reworked it differently > to leave the pass that never happens at the end (and skipped in practice), > see below. I tested it and it's OK as well. If you're fine with that one, > I'll apply it. > > In the mean time I'm switching to the other ones. > > Willy > > --- > > From 990c076b69198f59f50de7fd85f7f29f78b05aca Mon Sep 17 00:00:00 2001 > From: Willy Tarreau <[email protected]> > Date: Thu, 19 Nov 2026 15:57:15 +0100 > Subject: BUG/MINOR: intops: reject varints that do not fit in 64 bits in > decode_varint() > > decode_varint() accepts any number of continuation bytes. From the 11th > byte on, the shift count exceeds 63, which is undefined behaviour (UBSan: > "shift exponent 67 is too large for 64-bit type"), and a 10-byte varint > whose last byte is above 15 silently wraps: a value >= 2^64 is decoded > modulo 2^64. For example an SPOP agent sending the AGENT-HELLO > max-frame-size as f0 ff ff ff ff ff ff ff ff 7f has it decoded as > 2314921131297212640 instead of being rejected. > > In order to avoid adding costly checks in the inner loop, let's only > process the first 9 bytes as usual in the current loop and make a > special case of the 10th byte where we add an overflow check if ever > met. > > This should be backported to all stable versions. > > Reported-by: Martin Mirchev <[email protected]> > Co-authored-by: Martin Mirchev <[email protected]> > --- > include/haproxy/intops.h | 24 ++++++++++++++++++++++-- > 1 file changed, 22 insertions(+), 2 deletions(-) > > diff --git a/include/haproxy/intops.h b/include/haproxy/intops.h > index 08a9abc8b8..3195dda9b7 100644 > --- a/include/haproxy/intops.h > +++ b/include/haproxy/intops.h > @@ -637,6 +637,8 @@ static inline int encode_varint(uint64_t i, char > **buf, char *end) > static inline int decode_varint(char **buf, char *end, uint64_t *i) > { > unsigned char *p = (unsigned char *)*buf; > + unsigned char *stop; > + uint64_t v; > int r; > > if (p >= (unsigned char *)end) > @@ -649,9 +651,27 @@ static inline int decode_varint(char **buf, char > *end, uint64_t *i) > } > > r = 4; > + > + /* r=4,11,18,25,32,39,46,53,60; only the 9th from here (r=60) can > + * overflow so let's stop reading before p+8 to avoid checks in the > + * common loop, and deal with the last byte at the end. > + */ > + stop = (unsigned char *)end; > + if (p + 8 < stop) > + stop = p + 8; > + > do { > - if (p >= (unsigned char *)end) > - return -1; > + if (p >= stop) { > + /* last byte (r=60): must exist, be final, and not > overflow */ > + if (p >= (unsigned char *)end || *p > 0xf) > + return -1; > + > + v = ((uint64_t)*p++ << 60); > + if (*i + v < *i) > + return -1; > + *i += v; > + break; > + } > *i += (uint64_t)*p << r; > r += 7; > } while (*p++ >= 128); > -- > 2.35.3 > >

