From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Subject: [PATCH] Fix CVE-2026-33263 Upstream commits: *-login: Fix error code for "Maximum number of connections from user+IP exceeded" https://github.com/dovecot/core/commit/906c97993469adf9743b2f25ac8aeddace005a73 *-commin: Rename LOGIN_PROXY_FAILURE_TYPE_AUTH to LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED https://github.com/dovecot/core/commit/4d016e968857437ca1a6aa804c473e06079539f2 *-login: Add LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED https://github.com/dovecot/core/commit/5fde53de4ebdf791a5fe583d4f8e52b89215bc7c pop3-login: If mail_max_userip_connections is reached, reply to login with [IN-USE] prefix https://github.com/dovecot/core/commit/75cfd0a0f0b88b42541570dc18b6a65b67ba9f71 login-common, *-login: Add proxy_dest_connection_limit error_code to proxy_session_finished https://github.com/dovecot/core/commit/db0835c84bf754a260ce0d3dca1d69eb90f87518 submission-login: Handle CLIENT_AUTH_RESULT_LIMIT_REACHED as temporary error https://github.com/dovecot/core/commit/6b89deb54e197aae41a2a3ad2d3998f2df024866 login-common: client-common - Add client_disconnect() vfunc https://github.com/dovecot/core/commit/9cbe5c9a5e0526b799c98e0e299821a3390599d3 submission-login: client - Fix panic occurring at mail_max_userip_connections limit transgression https://github.com/dovecot/core/commit/a2b74bac07c8a231b859203fe2e624f3f0806e65 lib-smtp: smtp-server - Expose smtp_server_connection_reply_immediate https://github.com/dovecot/core/commit/2a1ae65f59bd462b18d815e1fb5971bb4be86d52 submission-login: client-authenticate - Reply 421 4.7.0 on mail_max_userip_connections https://github.com/dovecot/core/commit/cae8cf70d24939f515e2a039acb0b8e2fea5ccb2 submission-login: submission-proxy - Recognize 421 4.7.0 as connection-limit reply https://github.com/dovecot/core/commit/432177f997e74843ba8b3c271d28697675b34a0f 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. The Pigeonhole part of this fix is in mail/pigeonhole/patches. --- a/src/imap-login/client-authenticate.c +++ b/src/imap-login/client-authenticate.c @@ -95,6 +95,10 @@ void imap_client_auth_result(struct clie client_send_reply_code(client, IMAP_CMD_REPLY_NO, IMAP_RESP_CODE_UNAVAILABLE, text); break; + case CLIENT_AUTH_RESULT_LIMIT_REACHED: + client_send_reply_code(client, IMAP_CMD_REPLY_NO, + IMAP_RESP_CODE_LIMIT, text); + break; case CLIENT_AUTH_RESULT_SSL_REQUIRED: case CLIENT_AUTH_RESULT_MECH_SSL_REQUIRED: client_send_reply_code(client, IMAP_CMD_REPLY_NO, --- a/src/imap-login/imap-proxy.c +++ b/src/imap-login/imap-proxy.c @@ -254,6 +254,12 @@ static bool auth_resp_code_is_tempfail(c return strcasecmp(resp_code, IMAP_RESP_CODE_UNAVAILABLE) == 0; } +static bool auth_resp_code_is_limit(const char *resp) +{ + return strncasecmp(resp, IMAP_RESP_CODE_LIMIT"]", + strlen(IMAP_RESP_CODE_LIMIT"]")) == 0; +} + int imap_proxy_parse_line(struct client *client, const char *line) { struct imap_client *imap_client = (struct imap_client *)client; @@ -359,7 +365,7 @@ int imap_proxy_parse_line(struct client if (strncasecmp(log_line, "NO ", 3) == 0) log_line += 3; enum login_proxy_failure_type failure_type = - LOGIN_PROXY_FAILURE_TYPE_AUTH; + LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED; #define STR_NO_IMAP_RESP_CODE_AUTHFAILED "NO ["IMAP_RESP_CODE_AUTHFAILED"]" if (str_begins(line, STR_NO_IMAP_RESP_CODE_AUTHFAILED)) { /* the remote sent a generic "authentication failed" @@ -376,6 +382,8 @@ int imap_proxy_parse_line(struct client if (auth_resp_code_is_tempfail(resp_code)) failure_type = LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL; else { + if (auth_resp_code_is_limit(line + 4)) + failure_type = LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED; client_send_raw(client, t_strconcat( imap_client->cmd_tag, " ", line, "\r\n", NULL)); } @@ -486,6 +494,7 @@ imap_proxy_send_failure_reply(struct ima break; case LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG: case LOGIN_PROXY_FAILURE_TYPE_INTERNAL_CONFIG: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED: client_send_reply_code(&imap_client->common, IMAP_CMD_REPLY_NO, IMAP_RESP_CODE_SERVERBUG, LOGIN_PROXY_FAILURE_MSG); @@ -494,7 +503,8 @@ imap_proxy_send_failure_reply(struct ima client_send_raw(&imap_client->common, t_strconcat( imap_client->cmd_tag, " NO ", reason, "\r\n", NULL)); break; - case LOGIN_PROXY_FAILURE_TYPE_AUTH: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED: /* reply was already sent */ break; } --- a/src/lib-smtp/smtp-server-connection.c +++ b/src/lib-smtp/smtp-server-connection.c @@ -1303,6 +1303,11 @@ void smtp_server_connection_close(struct smtp_server_connection_unref(&conn); } +bool smtp_server_connection_is_closed(struct smtp_server_connection *conn) +{ + return conn->closed; +} + void smtp_server_connection_terminate(struct smtp_server_connection **_conn, const char *enh_code, const char *reason) { --- a/src/lib-smtp/smtp-server-private.h +++ b/src/lib-smtp/smtp-server-private.h @@ -325,9 +325,6 @@ void smtp_server_connection_reply_lines( unsigned int status, const char *enh_code, const char *const *text_lines); -void smtp_server_connection_reply_immediate( - struct smtp_server_connection *conn, unsigned int status, - const char *fmt, ...) ATTR_FORMAT(3, 4); void smtp_server_connection_reset_state(struct smtp_server_connection *conn); void smtp_server_connection_set_state(struct smtp_server_connection *conn, --- a/src/lib-smtp/smtp-server.h +++ b/src/lib-smtp/smtp-server.h @@ -473,6 +473,17 @@ void smtp_server_connection_set_ssl_stre void smtp_server_connection_close(struct smtp_server_connection **_conn, const char *reason) ATTR_NULL(2); +bool smtp_server_connection_is_closed(struct smtp_server_connection *conn); +/* Send a reply line directly to the output stream, bypassing the per-command + reply queue. Useful when the caller is about to close the connection and + needs the reply on the wire before close/destroy aborts queued replies. + The formatted text must be a single line (no CR/LF); unlike + smtp_server_reply_add_text(), this function does not split on \n nor add + the SMTP continuation prefix to subsequent lines. */ +void smtp_server_connection_reply_immediate(struct smtp_server_connection *conn, + unsigned int status, + const char *fmt, ...) + ATTR_FORMAT(3, 4); void smtp_server_connection_terminate(struct smtp_server_connection **_conn, const char *enh_code, const char *reason) ATTR_NULL(3); --- a/src/login-common/client-common-auth.c +++ b/src/login-common/client-common-auth.c @@ -413,7 +413,9 @@ void client_common_proxy_failed(struct c case LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG: case LOGIN_PROXY_FAILURE_TYPE_PROTOCOL: break; - case LOGIN_PROXY_FAILURE_TYPE_AUTH: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED: case LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL: client->proxy_auth_failed = TRUE; break; @@ -763,7 +765,8 @@ sasl_callback(struct client *client, enu i_assert(!client->destroyed || sasl_reply == SASL_SERVER_REPLY_AUTH_ABORTED || - sasl_reply == SASL_SERVER_REPLY_MASTER_FAILED); + sasl_reply == SASL_SERVER_REPLY_MASTER_FAILED || + sasl_reply == SASL_SERVER_REPLY_MASTER_FAILED_LIMIT); client->last_auth_fail = CLIENT_AUTH_FAIL_CODE_NONE; i_zero(&reply); @@ -812,11 +815,17 @@ sasl_callback(struct client *client, enu client_auth_failed(client); break; case SASL_SERVER_REPLY_MASTER_FAILED: - if (data != NULL) { + case SASL_SERVER_REPLY_MASTER_FAILED_LIMIT: + if (data == NULL) + ; + else if (sasl_reply == SASL_SERVER_REPLY_MASTER_FAILED) { /* authentication itself succeeded, we just hit some internal failure. */ client_auth_result(client, CLIENT_AUTH_RESULT_TEMPFAIL, &reply, data); + } else { + client_auth_result(client, + CLIENT_AUTH_RESULT_LIMIT_REACHED, &reply, data); } /* the fd may still be hanging somewhere in kernel or another --- a/src/login-common/client-common.c +++ b/src/login-common/client-common.c @@ -285,6 +285,8 @@ void client_disconnect(struct client *cl if (!client->login_success) { bool unref = FALSE; + if (client->v.disconnect != NULL) + client->v.disconnect(client, reason); io_remove(&client->io); ssl_iostream_destroy(&client->ssl_iostream); if (client->iostream_fd_proxy != NULL) { --- a/src/login-common/client-common.h +++ b/src/login-common/client-common.h @@ -72,6 +72,7 @@ enum client_auth_result { CLIENT_AUTH_RESULT_AUTHFAILED_REASON, CLIENT_AUTH_RESULT_AUTHZFAILED, CLIENT_AUTH_RESULT_TEMPFAIL, + CLIENT_AUTH_RESULT_LIMIT_REACHED, CLIENT_AUTH_RESULT_PASS_EXPIRED, CLIENT_AUTH_RESULT_SSL_REQUIRED, CLIENT_AUTH_RESULT_INVALID_BASE64, @@ -119,6 +120,7 @@ struct client_auth_reply { struct client_vfuncs { struct client *(*alloc)(pool_t pool); void (*create)(struct client *client, void **other_sets); + void (*disconnect)(struct client *client, const char *reason); void (*destroy)(struct client *client); void (*notify_auth_ready)(struct client *client); void (*notify_disconnect)(struct client *client, --- a/src/login-common/login-proxy.c +++ b/src/login-common/login-proxy.c @@ -634,7 +634,9 @@ bool login_proxy_failed(struct login_pro case LOGIN_PROXY_FAILURE_TYPE_PROTOCOL: log_prefix = "Remote server sent invalid input: "; break; - case LOGIN_PROXY_FAILURE_TYPE_AUTH: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED: log_prefix = ""; try_reconnect = FALSE; break; @@ -653,7 +655,9 @@ bool login_proxy_failed(struct login_pro return TRUE; } - if (type != LOGIN_PROXY_FAILURE_TYPE_AUTH && + if (type != LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED && + type != LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED && + type != LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED && type != LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL) e_error(event, "%s%s", log_prefix, reason); else if (proxy->client->set->auth_verbose) --- a/src/login-common/login-proxy.h +++ b/src/login-common/login-proxy.h @@ -41,10 +41,16 @@ enum login_proxy_failure_type { LOGIN_PROXY_FAILURE_TYPE_PROTOCOL, /* Authentication failed to backend. The LOGIN/AUTH command reply was already sent to the client. */ - LOGIN_PROXY_FAILURE_TYPE_AUTH, + LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED, + /* Authentication failed to backend. The LOGIN/AUTH command reply was + NOT sent to the client yet. */ + LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED, /* Authentication failed with a temporary failure code. Attempting it again might work. */ LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL, + /* Authentication failed because user has reached some limit. + The LOGIN/AUTH command reply was already sent to the client. */ + LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED, }; struct login_proxy_settings { --- a/src/login-common/sasl-server.c +++ b/src/login-common/sasl-server.c @@ -226,7 +226,7 @@ anvil_lookup_callback(const char *reply, const struct login_settings *set = client->set; const char *errmsg; unsigned int conn_count; - int ret; + enum sasl_server_reply sasl_reply = SASL_SERVER_REPLY_SUCCESS; client->anvil_query = NULL; client->anvil_request = NULL; @@ -238,18 +238,18 @@ anvil_lookup_callback(const char *reply, /* reply=NULL if we didn't need to do anvil lookup, or if the anvil lookup failed. allow failed anvil lookups in. */ if (reply == NULL || conn_count < set->mail_max_userip_connections) { - ret = master_send_request(req); + if (master_send_request(req) < 0) + sasl_reply = SASL_SERVER_REPLY_MASTER_FAILED; errmsg = NULL; /* client will see internal error */ } else { - ret = -1; + sasl_reply = SASL_SERVER_REPLY_MASTER_FAILED_LIMIT; errmsg = t_strdup_printf(ERR_TOO_MANY_USERIP_CONNECTIONS, set->mail_max_userip_connections); } - if (ret < 0) { + if (sasl_reply != SASL_SERVER_REPLY_SUCCESS) { client->authenticating = FALSE; auth_client_send_cancel(auth_client, client->master_auth_id); - call_client_callback(client, SASL_SERVER_REPLY_MASTER_FAILED, - errmsg, NULL); + call_client_callback(client, sasl_reply, errmsg, NULL); } i_free(req); } --- a/src/login-common/sasl-server.h +++ b/src/login-common/sasl-server.h @@ -8,6 +8,7 @@ enum sasl_server_reply { SASL_SERVER_REPLY_AUTH_FAILED, SASL_SERVER_REPLY_AUTH_ABORTED, SASL_SERVER_REPLY_MASTER_FAILED, + SASL_SERVER_REPLY_MASTER_FAILED_LIMIT, SASL_SERVER_REPLY_CONTINUE }; --- a/src/pop3-login/client-authenticate.c +++ b/src/pop3-login/client-authenticate.c @@ -59,6 +59,9 @@ void pop3_client_auth_result(struct clie case CLIENT_AUTH_RESULT_TEMPFAIL: client_send_reply(client, POP3_CMD_REPLY_TEMPFAIL, text); break; + case CLIENT_AUTH_RESULT_LIMIT_REACHED: + client_send_reply(client, POP3_CMD_REPLY_LIMIT, text); + break; case CLIENT_AUTH_RESULT_AUTHFAILED: case CLIENT_AUTH_RESULT_AUTHFAILED_REASON: case CLIENT_AUTH_RESULT_AUTHZFAILED: --- a/src/pop3-login/client.c +++ b/src/pop3-login/client.c @@ -292,6 +292,9 @@ void client_send_reply(struct client *cl case POP3_CMD_REPLY_TEMPFAIL: prefix = "-ERR [SYS/TEMP]"; break; + case POP3_CMD_REPLY_LIMIT: + prefix = "-ERR [IN-USE]"; + break; case POP3_CMD_REPLY_AUTH_ERROR: if (text[0] == '[') prefix = "-ERR"; --- a/src/pop3-login/client.h +++ b/src/pop3-login/client.h @@ -31,7 +31,8 @@ enum pop3_cmd_reply { POP3_CMD_REPLY_OK, POP3_CMD_REPLY_ERROR, POP3_CMD_REPLY_AUTH_ERROR, - POP3_CMD_REPLY_TEMPFAIL + POP3_CMD_REPLY_TEMPFAIL, + POP3_CMD_REPLY_LIMIT, }; void client_send_reply(struct client *client, enum pop3_cmd_reply reply, --- a/src/pop3-login/pop3-proxy.c +++ b/src/pop3-login/pop3-proxy.c @@ -249,7 +249,7 @@ int pop3_proxy_parse_line(struct client shouldn't be a real problem since of course everyone will be using only Dovecot as their backend :) */ enum login_proxy_failure_type failure_type = - LOGIN_PROXY_FAILURE_TYPE_AUTH; + LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED; if (!str_begins(line, "-ERR ")) { client_send_reply(client, POP3_CMD_REPLY_ERROR, AUTH_FAILED_MSG); @@ -258,6 +258,8 @@ int pop3_proxy_parse_line(struct client failure_type = LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL; line += 5; } else { + if (str_begins(line, "-ERR [IN-USE]")) + failure_type = LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED; client_send_raw(client, t_strconcat(line, "\r\n", NULL)); line += 5; } @@ -290,6 +292,7 @@ pop3_proxy_send_failure_reply(struct cli break; case LOGIN_PROXY_FAILURE_TYPE_INTERNAL_CONFIG: case LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED: client_send_reply(client, POP3_CMD_REPLY_ERROR, LOGIN_PROXY_FAILURE_MSG); break; @@ -297,7 +300,8 @@ pop3_proxy_send_failure_reply(struct cli /* [SYS/TEMP] prefix is already in the reason string */ client_send_reply(client, POP3_CMD_REPLY_ERROR, reason); break; - case LOGIN_PROXY_FAILURE_TYPE_AUTH: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED: /* reply was already sent */ break; } --- a/src/submission-login/client-authenticate.c +++ b/src/submission-login/client-authenticate.c @@ -135,6 +135,24 @@ void submission_client_auth_result(struc */ smtp_server_reply(cmd, 454, "4.7.0", "%s", text); break; + case CLIENT_AUTH_RESULT_LIMIT_REACHED: + /* The user has too many concurrent connections. Reply with + 421 4.7.0: 421 means "service shutting down, closing + transmission channel" (RFC 5321 Section 4.2.1) and + signals the client that retrying on this same connection + is pointless. The proxy uses the 421 + 4.7.0 combination + to recognize this specifically as a connection-limit + response (rather than a generic 421 internal/shutdown + reply) and avoid reconnecting. + + Use reply_immediate() rather than the queued + smtp_server_reply() because the caller (sasl-server) + immediately tears the connection down after this returns, + which would abort a queued reply before it reaches the + wire. */ + smtp_server_connection_reply_immediate(subm_client->conn, + 421, "4.7.0 %s", text); + break; case CLIENT_AUTH_RESULT_ABORTED: /* RFC4954, Section 4: --- a/src/submission-login/client.c +++ b/src/submission-login/client.c @@ -122,6 +122,21 @@ static void submission_client_create(str &smtp_set, &smtp_callbacks, subm_client); } +static void +submission_client_disconnect(struct client *client, const char *reason) +{ + struct submission_client *subm_client = + container_of(client, struct submission_client, common); + + /* If the smtp-server connection is already in its close cascade (i.e. + we're being called via its conn_disconnect callback), skip closing + it again. Doing so would null subm_client->conn and prevent the + conn_free callback from invoking client_destroy(). */ + if (subm_client->conn != NULL && + !smtp_server_connection_is_closed(subm_client->conn)) + smtp_server_connection_close(&subm_client->conn, reason); +} + static void submission_client_destroy(struct client *client) { struct submission_client *subm_client = @@ -285,6 +300,7 @@ static const struct smtp_server_callback static struct client_vfuncs submission_client_vfuncs = { .alloc = submission_client_alloc, .create = submission_client_create, + .disconnect = submission_client_disconnect, .destroy = submission_client_destroy, .notify_auth_ready = submission_client_notify_auth_ready, .notify_disconnect = submission_client_notify_disconnect, --- a/src/submission-login/submission-proxy.c +++ b/src/submission-login/submission-proxy.c @@ -626,8 +626,22 @@ int submission_proxy_parse_line(struct c shouldn't be a real problem since of course everyone will be using only Dovecot as their backend :) */ enum login_proxy_failure_type failure_type = - LOGIN_PROXY_FAILURE_TYPE_AUTH; - if ((status / 100) == 4) + LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED; + if (status == 421) { + /* The backend won't accept further AUTH attempts on this + connection. If it signaled 4.7.0, the user's connection + limit (mail_max_userip_connections) was reached; report + that specifically. Otherwise treat as a generic auth + reply (e.g. backend shutting down). The reply was + prepared from the backend's 421 line; forward it to the + client and detach the AUTH command. */ + failure_type = (null_strcmp(enh_code, "4.7.0") == 0) ? + LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED : + LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED; + i_assert(subm_client->proxy_reply != NULL); + smtp_server_reply_submit(subm_client->proxy_reply); + subm_client->pending_auth = NULL; + } else if ((status / 100) == 4) failure_type = LOGIN_PROXY_FAILURE_TYPE_AUTH_TEMPFAIL; else { i_assert((status / 100) != 2); @@ -669,6 +683,7 @@ submission_proxy_send_failure_reply(stru case LOGIN_PROXY_FAILURE_TYPE_REMOTE: case LOGIN_PROXY_FAILURE_TYPE_REMOTE_CONFIG: case LOGIN_PROXY_FAILURE_TYPE_PROTOCOL: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_NOT_REPLIED: i_assert(cmd != NULL); subm_client->pending_auth = NULL; smtp_server_reply(cmd, 454, "4.7.0", LOGIN_PROXY_FAILURE_MSG); @@ -680,7 +695,8 @@ submission_proxy_send_failure_reply(stru i_assert(subm_client->proxy_reply != NULL); smtp_server_reply_submit(subm_client->proxy_reply); break; - case LOGIN_PROXY_FAILURE_TYPE_AUTH: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_REPLIED: + case LOGIN_PROXY_FAILURE_TYPE_AUTH_LIMIT_REACHED_REPLIED: /* reply was already sent */ i_assert(cmd == NULL); break;