]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: peers: bound stick-table acknowledge messages
authorFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 14:17:28 +0000 (15:17 +0100)
committerFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 14:53:46 +0000 (15:53 +0100)
peer_prepare_ackmsg() was the last message builder of the peers code
still encoding without knowing the buffer end: lengths went through
intencode(), which cannot fail, and the last acknowledged update counter
was copied with a plain memcpy().

Its payload is a bounded varint plus a 32-bit counter, i.e. 9 bytes at
most, so it could not overflow. The bounds are added anyway, for
consistency with the update and switch message builders, and they make
intencode() unused: being defined without any prototype in a header, the
compiler cannot report it as unused, so it is removed here along with its
comment, otherwise it would have stayed in the binary silently.

Must be backported to all stable versions.

src/peers.c

index 940a56df06b3242b3e8997b0db9c60c2e4d76518..6d94125331829448ca377ca40aabc11ac81b7ce2 100644 (file)
@@ -1120,23 +1120,28 @@ static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_param
  * This prepare the acknowledge message on the stick session <ts>, <st> is the considered
  * stick table.
  *  <msg> is a buffer of <size> to receive data message content
- * If function returns 0, the caller should consider we were unable to encode this message (TODO:
- * check size)
+ * If function returns 0, the caller should consider we were unable to encode this message.
  */
 static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *p)
 {
        unsigned short datalen;
-       char *cursor, *datamsg;
+       char *cursor, *datamsg, *end;
        uint32_t netinteger;
        struct shared_table *st;
 
+       if (size < PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN)
+               return 0;
+
        cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
+       end = msg + size;
 
        st = p->ack.shared_table;
-       intencode(st->remote_id, &cursor);
+       if (encode_varint(st->remote_id, &cursor, end) == -1)
+               return 0;
+
        netinteger = htonl(st->last_get);
-       memcpy(cursor, &netinteger, sizeof(netinteger));
-       cursor += sizeof(netinteger);
+       if (peer_bufcpy(&cursor, end, &netinteger, sizeof(netinteger)) == 0)
+               return 0;
 
        /* Compute datalen */
        datalen = (cursor - datamsg);
@@ -1145,7 +1150,8 @@ static int peer_prepare_ackmsg(char *msg, size_t size, struct peer_prep_params *
        msg[0] = PEER_MSG_CLASS_STICKTABLE;
        msg[1] = PEER_MSG_STKT_ACK;
        cursor = &msg[2];
-       intencode(datalen, &cursor);
+       if (encode_varint(datalen, &cursor, end) == -1)
+               return 0;
 
        /* move data after header */
        memmove(cursor, datamsg, datalen);