From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Subject: [PATCH] Fix CVE-2026-42391 Upstream commits: imap-login: Fix excessive memory growth with pre-login ID command https://github.com/dovecot/core/commit/dbeb18dfc317be3c9839702c35c3fb77b7af90ff Taken from the Debian bookworm security update (dovecot 1:2.3.19.1+dfsg1-2.1+deb12u7), which carries these fixes for 2.3, and applied to 2.3.21.1. Adjusted for this version: - also limit the pairs added to the imap_id_log reply to IMAP_ID_MAX_ACCOUNTED_PAIRS, with a counter of its own, as the upstream commit does for its log entries. The Debian patch limits only client_id, which 2.3 already caps by length, so with imap_id_log set the log reply still grew with every pair. --- a/src/imap-login/imap-login-client.h +++ b/src/imap-login/imap-login-client.h @@ -53,6 +53,12 @@ struct imap_client_cmd_id { char **log_keys; string_t *log_reply; + /* Number of external (non internal x-*) key/value pairs accounted so + far. Used to bound the pre-login accounting (logging, event fields, + client_id) done per external pair. */ + unsigned int processed_pairs_count; + /* Number of key/value pairs added to log_reply so far. */ + unsigned int logged_pairs_count; }; struct imap_client { --- a/src/imap-login/imap-login-cmd-id.c +++ b/src/imap-login/imap-login-cmd-id.c @@ -7,6 +7,12 @@ #include "imap-login-settings.h" #include "imap-login-client.h" +/* RFC 2971 says a client SHOULD NOT send more than 30 field-value pairs. + Allow that many to be accounted for (logged / kept in client_id); pairs + beyond this are still parsed and internal x-* handlers still run, but they + are not added to the logging/client_id accounting that grows per pair. */ +#define IMAP_ID_MAX_ACCOUNTED_PAIRS 30 + struct imap_id_param_handler { const char *key; bool key_is_prefix; @@ -125,7 +131,17 @@ static void cmd_id_handle_keyvalue(struc client_id_str = !client_try_update_info(client, key, value); - if (client->set->imap_id_retain && client_id_str && + /* Only external pairs are logged / kept in client_id, and only those + grow per-pair with count, so only they need bounding to avoid a + pre-login CPU/memory DoS. Internal x-* handlers must keep running + regardless (e.g. a proxy may legitimately forward more than 30 + fields) and are not counted against the limit. */ + bool account_pair = client_id_str && + client->cmd_id->processed_pairs_count < IMAP_ID_MAX_ACCOUNTED_PAIRS; + if (account_pair) + client->cmd_id->processed_pairs_count++; + + if (account_pair && client->set->imap_id_retain && (client->common.client_id == NULL || str_len(client->common.client_id) + kvlen < LOGIN_MAX_CLIENT_ID_LEN)) { if (client->common.client_id == NULL) { @@ -142,9 +158,12 @@ static void cmd_id_handle_keyvalue(struc } if (client->cmd_id->log_reply != NULL && + client->cmd_id->logged_pairs_count < IMAP_ID_MAX_ACCOUNTED_PAIRS && (client->cmd_id->log_keys == NULL || - str_array_icase_find((void *)client->cmd_id->log_keys, key))) + str_array_icase_find((void *)client->cmd_id->log_keys, key))) { + client->cmd_id->logged_pairs_count++; imap_id_log_reply_append(client->cmd_id->log_reply, key, value); + } } static int cmd_id_handle_args(struct imap_client *client,