From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Subject: [PATCH] Fix CVE-2026-52687 Upstream commits: doveadm compress: Move struct client earlier https://github.com/dovecot/core/commit/429762550939be2128b1c8056fa1bf85543ccd92 doveadm compress: Refactor line reading https://github.com/dovecot/core/commit/0b5eea74d4741c86df0342efdc5b3c77002e74e7 doveadm compress-connect: Fix writing server input to non-blocking stdout https://github.com/dovecot/core/commit/c781a55de87c78e9aa7cdc1325d7af355bd74e68 imap: Restrict COMPRESS to DEFLATE only https://github.com/dovecot/core/commit/a9b051233715577c88155604b516262fed10aa26 doveadm: compress-connect - Don't hang when server rejects COMPRESS https://github.com/dovecot/core/commit/2fad1f836b23e15b260ca3e5985aec126d7a90e9 doveadm compress: Delay failing if used COMPRESS algorithm is not supported locally https://github.com/dovecot/core/commit/e7ab7a24675a4f0a82fb9d7583eb49f8736e4dfb 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. --- a/src/doveadm/doveadm-zlib.c +++ b/src/doveadm/doveadm-zlib.c @@ -15,6 +15,24 @@ #include #include +enum server_input_line_type { + SERVER_INPUT_LINE_TYPE_DEFAULT, + SERVER_INPUT_LINE_TYPE_COMPRESS, + SERVER_INPUT_LINE_TYPE_COMPRESS_FAILED, +}; + +struct client { + int fd; + struct io *io_client, *io_server; + struct istream *input, *stdin_input; + struct ostream *output, *stdout_output; + const struct compression_handler *handler; + char *algorithm; + char *compress_tag; + bool compressed; + bool compress_waiting; +}; + static bool test_dump_imapzlib(const char *path) { const char *p; @@ -82,29 +100,31 @@ cmd_dump_imapzlib(const char *path, cons fflush(stdout); } -struct client { - int fd; - struct io *io_client, *io_server; - struct istream *input, *stdin_input; - struct ostream *output; - const struct compression_handler *handler; - char *algorithm; - bool compressed; - bool compress_waiting; -}; - static bool client_input_get_compress_algorithm(struct client *client, const char *line) { + const char *tag = line; + size_t tag_len; + /* skip tag */ while (*line != ' ' && *line != '\0') line++; + tag_len = line - tag; if (strncasecmp(line, " COMPRESS ", 10) != 0) return FALSE; + /* Look up the local handler needed to (de)compress the stream once the + server accepts. Don't fail here if the mechanism is unknown or not + compiled in: the server may reject it, and this client must still send + the command to let the server do so. If the server unexpectedly + accepts a mechanism we can't handle, we fail then (see server_input()). */ if (compression_lookup_handler(t_str_lcase(line+10), &client->handler) <= 0) - i_fatal("Unsupported compression mechanism: %s", line+10); + client->handler = NULL; + /* Remember the tag so we can tell whether the server accepted or + rejected this COMPRESS command. */ + i_free(client->compress_tag); + client->compress_tag = i_strndup(tag, tag_len); return TRUE; } @@ -165,25 +185,33 @@ static bool server_input_is_compress_rep return str_begins(line, " OK Begin compression"); } -static bool server_input_uncompressed(struct client *client) +static bool server_input_is_reply_to_tag(const char *line, const char *tag) { - const char *line; + size_t tag_len = strlen(tag); - while ((line = i_stream_read_next_line(client->input)) != NULL) { - if (write(STDOUT_FILENO, line, strlen(line)) < 0) - i_fatal("write(stdout) failed: %m"); - if (write(STDOUT_FILENO, "\n", 1) < 0) - i_fatal("write(stdout) failed: %m"); - if (server_input_is_compress_reply(line)) - return TRUE; - } - return FALSE; + return strncmp(line, tag, tag_len) == 0 && line[tag_len] == ' '; +} + +static enum server_input_line_type +server_input_line_type(struct client *client, const char *line) +{ + if (!client->compressed && server_input_is_compress_reply(line)) + return SERVER_INPUT_LINE_TYPE_COMPRESS; + /* The server can reject COMPRESS (e.g. an unsupported mechanism). If we + are waiting for its reply, a tagged reply that isn't the "OK Begin + compression" above means the command failed - resume sending the + pipelined input instead of waiting forever. */ + if (client->compress_waiting && client->compress_tag != NULL && + server_input_is_reply_to_tag(line, client->compress_tag)) + return SERVER_INPUT_LINE_TYPE_COMPRESS_FAILED; + return SERVER_INPUT_LINE_TYPE_DEFAULT; } static void server_input(struct client *client) { const unsigned char *data; size_t size; + const char *line; if (i_stream_read(client->input) == -1) { if (client->input->stream_errno != 0) { @@ -196,27 +224,52 @@ static void server_input(struct client * return; } - if (!client->compressed && server_input_uncompressed(client)) { - /* start compression */ - struct istream *input; - struct ostream *output; - - i_info(""); - input = client->handler->create_istream(client->input); - output = client->handler->create_ostream(client->output, 6); - i_stream_unref(&client->input); - o_stream_unref(&client->output); - client->input = input; - client->output = output; - client->compressed = TRUE; - client->compress_waiting = FALSE; - i_stream_set_input_pending(client->stdin_input, TRUE); + while ((line = i_stream_next_line(client->input)) != NULL) { + o_stream_nsend_str(client->stdout_output, line); + o_stream_nsend_str(client->stdout_output, "\n"); + + switch (server_input_line_type(client, line)) { + case SERVER_INPUT_LINE_TYPE_COMPRESS: { + /* start compression */ + struct istream *input; + struct ostream *output; + + if (client->handler == NULL) + i_fatal("Server accepted unsupported compression mechanism"); + i_info(""); + input = client->handler->create_istream(client->input); + output = client->handler->create_ostream(client->output, 6); + i_stream_unref(&client->input); + o_stream_unref(&client->output); + client->input = input; + client->output = output; + client->compressed = TRUE; + client->compress_waiting = FALSE; + i_free_and_null(client->compress_tag); + i_stream_set_input_pending(client->stdin_input, TRUE); + break; + } + case SERVER_INPUT_LINE_TYPE_COMPRESS_FAILED: + /* Server rejected COMPRESS. Resume sending input. */ + i_info(""); + client->handler = NULL; + client->compress_waiting = FALSE; + i_free_and_null(client->compress_tag); + i_stream_set_input_pending(client->stdin_input, TRUE); + break; + case SERVER_INPUT_LINE_TYPE_DEFAULT: + break; + } } data = i_stream_get_data(client->input, &size); - if (write(STDOUT_FILENO, data, size) < 0) - i_fatal("write(stdout) failed: %m"); + o_stream_nsend(client->stdout_output, data, size); i_stream_skip(client->input, size); + + if (o_stream_flush(client->stdout_output) < 0) { + i_fatal("write(stdout) failed: %s", + o_stream_get_error(client->stdout_output)); + } } static void cmd_zlibconnect(struct doveadm_cmd_context *cctx) @@ -252,6 +305,10 @@ static void cmd_zlibconnect(struct dovea client.fd = fd; fd_set_nonblock(STDIN_FILENO, TRUE); client.stdin_input = i_stream_create_fd(STDIN_FILENO, SIZE_MAX); + /* stdin and stdout may point to the same file description (e.g. a + pty), in which case stdout is now also non-blocking. Buffer stdout + writes and retry them from the ioloop on EAGAIN. */ + client.stdout_output = o_stream_create_fd(STDOUT_FILENO, SIZE_MAX); client.input = i_stream_create_fd(fd, SIZE_MAX); client.output = o_stream_create_fd(fd, 0); o_stream_set_no_error_handling(client.output, TRUE); @@ -260,9 +317,17 @@ static void cmd_zlibconnect(struct dovea master_service_run(master_service, NULL); io_remove(&client.io_client); io_remove(&client.io_server); + /* switch stdout back to blocking for the final flush */ + fd_set_nonblock(STDOUT_FILENO, FALSE); + if (o_stream_finish(client.stdout_output) < 0) { + i_fatal("write(stdout) failed: %s", + o_stream_get_error(client.stdout_output)); + } i_stream_unref(&client.stdin_input); i_stream_unref(&client.input); o_stream_unref(&client.output); + o_stream_unref(&client.stdout_output); + i_free(client.compress_tag); if (close(fd) < 0) i_fatal("close() failed: %m"); } --- a/src/plugins/imap-zlib/imap-zlib-plugin.c +++ b/src/plugins/imap-zlib/imap-zlib-plugin.c @@ -94,10 +94,14 @@ static bool cmd_compress(struct client_c } ret = compression_lookup_handler(t_str_lcase(mechanism), &handler); - if (ret <= 0) { + /* Only DEFLATE is advertised (RFC 4978) and accepted here. Other + algorithms (e.g. zstd) can require far more memory to decompress, + which especially matters now that imap-login can handle COMPRESS for + proxied connections and an attacker could open many of them. */ + if (ret <= 0 || strcmp(handler->name, "deflate") != 0) { const char * tagline = t_strdup_printf("NO %s compression mechanism", - ret == 0 ? "Unsupported" : "Unknown"); + ret < 0 ? "Unknown" : "Unsupported"); client_send_tagline(cmd, tagline); return TRUE; }