]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: peers: bound stick-table switch messages
authorFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 14:03:37 +0000 (15:03 +0100)
committerFrederic Lecaille <flecaille@haproxy.com>
Wed, 18 Nov 2026 14:53:46 +0000 (15:53 +0100)
peer_prepare_switchmsg() builds the table definition message in the
<size>-byte trash buffer but, like its update counterpart before commit
8e0f311ff, never checked that the encoded data fit. The table name was
copied with a plain memcpy() following an intencode() of its length, and
all other fields were encoded with intencode(), which cannot fail.

Unlike the update message, this one is mostly made of a variable-length
field, the table name: the peers section name, a '/', and the table name,
which gives a nid as large as the configuration line allows. The rest is
a fixed 33-byte overhead plus the data type parameters. Nothing accounts
for that name when validating the stick-table at configuration time, so
a table whose name is close to tune.bufsize is accepted and its copy runs
past the end of the trash buffer when the peer sessions start.

Let's check the room left before every copy and every varint encoding,
using the same peer_bufcpy() helper and the same single-exit error
convention as for the update message. The data type parameters are
staged in a trash chunk, not in <msg>, so their encoding is bounded by
the chunk itself through a distinct chunk_end pointer, while the varints
written to <msg>, the bitfield and the expiration are bounded by the
message end.

The payload length becomes a size_t for consistency with the update
message, though the definition message is inherently small.

Thank you to @h110hawk for having reported this issue in GH #3576.

This should be backported to all supported versions.

src/peers.c

index de563c7e94448da3064abd07c2d5a6cf2bd319bc..940a56df06b3242b3e8997b0db9c60c2e4d76518 100644 (file)
@@ -979,42 +979,51 @@ int peer_prepare_updatemsg(char *msg, size_t size, struct peer_prep_params *p)
 /*
  * This prepare the switch table message to targeted share table <st>.
  *  <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_switchmsg(char *msg, size_t size, struct peer_prep_params *params)
 {
        int len;
-       unsigned short datalen;
+       size_t datalen;
        struct buffer *chunk;
-       char *cursor, *datamsg, *chunkp, *chunkq;
+       char *cursor, *end, *datamsg, *chunkp, *chunkq, *chunk_end;
        uint64_t data = 0;
        unsigned int data_type;
        struct shared_table *st;
 
        st = params->swtch.shared_table;
-       cursor = datamsg = msg + PEER_MSG_HEADER_LEN + PEER_MSG_ENC_LENGTH_MAXLEN;
 
+       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;
        /* Encode data */
 
        /* encode local id */
-       intencode(st->local_id, &cursor);
+       if (encode_varint(st->local_id, &cursor, end) == -1)
+               return 0;
 
        /* encode table name */
        len = strlen(st->table->nid);
-       intencode(len, &cursor);
-       memcpy(cursor, st->table->nid, len);
-       cursor += len;
+       if (encode_varint(len, &cursor, end) == -1)
+               return 0;
+
+       if (peer_bufcpy(&cursor, end, st->table->nid, len) == 0)
+               return 0;
 
        /* encode table type */
 
-       intencode(peer_net_key_type[st->table->type], &cursor);
+       if (encode_varint(peer_net_key_type[st->table->type], &cursor, end) == -1)
+               return 0;
 
        /* encode table key size */
-       intencode(st->table->key_size, &cursor);
+       if (encode_varint(st->table->key_size, &cursor, end) == -1)
+               return 0;
 
        chunk = get_trash_chunk();
        chunkp = chunkq = chunk->area;
+       chunk_end = chunk->area + chunk->size;
        /* encode available known data types in table */
        for (data_type = 0 ; data_type < STKTABLE_DATA_TYPES ; data_type++) {
                if (st->table->data_ofs[data_type]) {
@@ -1047,40 +1056,47 @@ static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_param
                                /* This is an array type so we first encode
                                 * the data_type itself to prefix parameters
                                 */
-                               intencode(data_type, &chunkq);
+                               if (encode_varint(data_type, &chunkq, chunk_end) == -1)
+                                       return 0;
 
                                /* We encode the first parameter which is
                                 * the number of elements of this array
                                 */
-                               intencode(st->table->data_nbelem[data_type], &chunkq);
+                               if (encode_varint(st->table->data_nbelem[data_type], &chunkq, chunk_end) == -1)
+                                       return 0;
 
                                /* for array of freq counters, there is an additional
                                 * period parameter to encode
                                 */
-                               if (stktable_data_types[data_type].std_type == STD_T_FRQP)
-                                       intencode(st->table->data_arg[data_type].u, &chunkq);
+                               if (stktable_data_types[data_type].std_type == STD_T_FRQP &&
+                                   encode_varint(st->table->data_arg[data_type].u, &chunkq, chunk_end) == -1)
+                                       return 0;
                        }
                        else if (stktable_data_types[data_type].std_type == STD_T_FRQP) {
                                /* this datatype is a simple freq counter not part
                                 * of an array. We encode the data_type itself
                                 * to prefix the 'period' parameter
                                 */
-                               intencode(data_type, &chunkq);
-                               intencode(st->table->data_arg[data_type].u, &chunkq);
+                               if (encode_varint(data_type, &chunkq, chunk_end) == -1 ||
+                                   encode_varint(st->table->data_arg[data_type].u, &chunkq, chunk_end) == -1)
+                                       return 0;
                        }
                        /* set the bit corresponding to stored data type */
                        data |= 1ULL << data_type;
                }
        }
-       intencode(data, &cursor);
+
+       if (encode_varint(data, &cursor, end) == -1)
+               return 0;
 
        /* Encode stick-table entries duration. */
-       intencode(st->table->expire, &cursor);
+       if (encode_varint(st->table->expire, &cursor, end) == -1)
+               return 0;
 
        if (chunkq > chunkp) {
                chunk->data = chunkq - chunkp;
-               memcpy(cursor, chunk->area, chunk->data);
-               cursor += chunk->data;
+               if (peer_bufcpy(&cursor, end, chunk->area, chunk->data) == 0)
+                       return 0;
        }
 
        /* Compute datalen */
@@ -1090,7 +1106,8 @@ static int peer_prepare_switchmsg(char *msg, size_t size, struct peer_prep_param
        msg[0] = PEER_MSG_CLASS_STICKTABLE;
        msg[1] = PEER_MSG_STKT_DEFINE;
        cursor = &msg[2];
-       intencode(datalen, &cursor);
+       if (encode_varint(datalen, &cursor, end) == -1)
+               return 0;
 
        /* move data after header */
        memmove(cursor, datamsg, datalen);