pgpool2.git / summary / log / commit / refs

commit    ca890ad403139eb5ef021968b63ddb4725668371
Author:   Taiki Koshino <koshino@sraoss.co.jp>
Date:     Sun Sep 20 03:37:42 2026 +0000

    Add parse_peer_count helper and bound backend_desc array parsing.
    
    The wd_json_data.c parsers consume JSON payloads supplied by watchdog
    peers (any TCP host that can reach wd_port; auth defeated by the
    default-empty wd_authkey) and write the parsed contents into
    fixed-size arrays inside palloc'd POOL_CONFIG / BackendDesc /
    WdNodeInfo storage. get_pool_config_from_json() in particular copied
    value->u.array.length verbatim into config->backend_desc->num_backends
    without any upper bound, so a peer-supplied "backend_desc" array of
    more than MAX_NUM_BACKENDS (128) entries drove the loop past the
    embedded BackendInfo backend_info[MAX_NUM_BACKENDS] array, writing
    attacker-controlled port and hostname bytes into adjacent heap. The
    sibling parser get_pg_backend_node_status_from_json() already clamps
    the same way, so this is a per-handler omission, not a structural
    limit; sibling wd_nodes parsing in the same function has the same
    bug and will be migrated in a follow-up patch.
    
    Introduce a static helper
    
        static int
        parse_peer_count(const json_value *jv, const char *field,
                         int max_count);
    
    that validates the value is a JSON array, reads its length, and
    rejects with ereport(ERROR, ...) when the count is negative or
    exceeds the caller-supplied bound. The longjmp out of the parser
    matches the existing exit channel for json_get_*_value_for_key on
    malformed input, so callers do not need new error-path handling.
    
    Migrate the backend_desc loop in get_pool_config_from_json() to use
    the helper, capped at MAX_NUM_BACKENDS. Replace the per-element
    strncpy(... sizeof(field) - 1) with strlcpy(... sizeof(field)) so
    the destination is guaranteed NUL-terminated when the peer-supplied
    hostname is exactly MAX_DB_HOST_NAMELEN (254) bytes.
    
    Reported-by: Emond Papegaaij <emond.papegaaij@topicus.nl>
    Reported-by: Claude
    Author: Bo Peng <pengbo@sraoss.co.jp>
    CVE-2026-92869
    Backpatch-through: v4.3


src/watchdog/wd_json_data.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/watchdog/wd_json_data.c b/src/watchdog/wd_json_data.c index 41ac92e7c..44d6ccb47 100644 --- a/src/watchdog/wd_json_data.c +++ b/src/watchdog/wd_json_data.c @@ -33,6 +33,33 @@ #include "pool.h" +/* + * Read the length of a JSON array supplied by an untrusted peer and + * cap it at the caller-supplied bound. Rejects non-array values and + * out-of-range counts via ereport(ERROR); the longjmp out of the + * parser is the same exit channel callers already expect from + * json_get_*_value_for_key on malformed input. + */ +static int +parse_peer_count(const json_value *jv, const char *field, int max_count) +{ + int count; + + if (jv == NULL || jv->type != json_array) + ereport(ERROR, + (errmsg("invalid watchdog JSON payload"), + errdetail("field \"%s\" missing or not an array", field))); + + count = (int) jv->u.array.length; + if (count < 0 || count > max_count) + ereport(ERROR, + (errmsg("invalid watchdog JSON payload"), + errdetail("field \"%s\" length %u exceeds maximum %d", + field, jv->u.array.length, max_count))); + + return count; +} + POOL_CONFIG * get_pool_config_from_json(char *json_data, int data_len) { @@ -124,10 +151,8 @@ get_pool_config_from_json(char *json_data, int data_len) /* backend_desc array */ value = json_get_value_for_key(root, "backend_desc"); - if (value == NULL || value->type != json_array) - goto ERROR_EXIT; - - config->backend_desc->num_backends = value->u.array.length; + config->backend_desc->num_backends = parse_peer_count(value, "backend_desc", + MAX_NUM_BACKENDS); for (i = 0; i < config->backend_desc->num_backends; i++) { json_value *arr_value = value->u.array.values[i]; @@ -138,7 +163,8 @@ get_pool_config_from_json(char *json_data, int data_len) ptr = json_get_string_value_for_key(arr_value, "backend_hostname"); if (ptr == NULL) goto ERROR_EXIT; - strncpy(config->backend_desc->backend_info[i].backend_hostname, ptr, sizeof(config->backend_desc->backend_info[i].backend_hostname) - 1); + strlcpy(config->backend_desc->backend_info[i].backend_hostname, ptr, + sizeof(config->backend_desc->backend_info[i].backend_hostname)); } value = json_get_value_for_key(root, "health_check_params"); [parent: d0a783343ce8]