Blame SOURCES/dovecot-2.3.8-CVE_2020_12100.patch

7bc0c9
diff -up dovecot-2.3.8/src/doveadm/doveadm-mail-fetch.c.CVE_2020_12100 dovecot-2.3.8/src/doveadm/doveadm-mail-fetch.c
7bc0c9
--- dovecot-2.3.8/src/doveadm/doveadm-mail-fetch.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/doveadm/doveadm-mail-fetch.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -265,6 +265,9 @@ static int fetch_text(struct fetch_cmd_c
7bc0c9
 
7bc0c9
 static int fetch_text_utf8(struct fetch_cmd_context *ctx)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE,
7bc0c9
+	};
7bc0c9
 	struct istream *input;
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct message_decoder_context *decoder;
7bc0c9
@@ -275,9 +278,7 @@ static int fetch_text_utf8(struct fetch_
7bc0c9
 	if (mail_get_stream(ctx->mail, NULL, NULL, &input) < 0)
7bc0c9
 		return -1;
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool_datastack_create(), input,
7bc0c9
-				     MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE,
7bc0c9
-				     0);
7bc0c9
+	parser = message_parser_init(pool_datastack_create(), input, &parser_set);
7bc0c9
 	decoder = message_decoder_init(NULL, 0);
7bc0c9
 
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &raw_block)) > 0) {
7bc0c9
diff -up dovecot-2.3.8/src/lib-imap/test-imap-bodystructure.c.CVE_2020_12100 dovecot-2.3.8/src/lib-imap/test-imap-bodystructure.c
7bc0c9
--- dovecot-2.3.8/src/lib-imap/test-imap-bodystructure.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-imap/test-imap-bodystructure.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -381,6 +381,11 @@ static const unsigned int normalize_test
7bc0c9
 static struct message_part *
7bc0c9
 msg_parse(pool_t pool, const char *message, bool parse_bodystructure)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
+			MESSAGE_HEADER_PARSER_FLAG_DROP_CR,
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK,
7bc0c9
+	};
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct istream *input;
7bc0c9
 	struct message_block block;
7bc0c9
@@ -388,10 +393,7 @@ msg_parse(pool_t pool, const char *messa
7bc0c9
 	int ret;
7bc0c9
 
7bc0c9
 	input = i_stream_create_from_data(message, strlen(message));
7bc0c9
-	parser = message_parser_init(pool, input,
7bc0c9
-			MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
-			MESSAGE_HEADER_PARSER_FLAG_DROP_CR,
7bc0c9
-			MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK);
7bc0c9
+	parser = message_parser_init(pool, input, &parser_set);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
7bc0c9
 		if (parse_bodystructure) {
7bc0c9
 			message_part_data_parse_from_header(pool, block.part,
7bc0c9
diff -up dovecot-2.3.8/src/lib-imap/test-imap-envelope.c.CVE_2020_12100 dovecot-2.3.8/src/lib-imap/test-imap-envelope.c
7bc0c9
--- dovecot-2.3.8/src/lib-imap/test-imap-envelope.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-imap/test-imap-envelope.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -118,6 +118,11 @@ static const unsigned int parse_tests_co
7bc0c9
 static struct message_part_envelope *
7bc0c9
 msg_parse(pool_t pool, const char *message)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
+			MESSAGE_HEADER_PARSER_FLAG_DROP_CR,
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK,
7bc0c9
+	};
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct message_part_envelope *envlp = NULL;
7bc0c9
 	struct istream *input;
7bc0c9
@@ -126,10 +131,7 @@ msg_parse(pool_t pool, const char *messa
7bc0c9
 	int ret;
7bc0c9
 
7bc0c9
 	input = i_stream_create_from_data(message, strlen(message));
7bc0c9
-	parser = message_parser_init(pool, input,
7bc0c9
-			MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
-			MESSAGE_HEADER_PARSER_FLAG_DROP_CR,
7bc0c9
-			MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK);
7bc0c9
+	parser = message_parser_init(pool, input, &parser_set);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
7bc0c9
 		i_assert(block.part->parent == NULL);
7bc0c9
 		message_part_envelope_parse_from_header(pool, &envlp, block.hdr);
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/istream-attachment-extractor.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/istream-attachment-extractor.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/istream-attachment-extractor.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/istream-attachment-extractor.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -696,6 +696,10 @@ i_stream_create_attachment_extractor(str
7bc0c9
 				     struct istream_attachment_settings *set,
7bc0c9
 				     void *context)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
+			MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES,
7bc0c9
+	};
7bc0c9
 	struct attachment_istream *astream;
7bc0c9
 
7bc0c9
 	i_assert(set->min_size > 0);
7bc0c9
@@ -722,9 +726,7 @@ i_stream_create_attachment_extractor(str
7bc0c9
 	astream->istream.istream.seekable = FALSE;
7bc0c9
 
7bc0c9
 	astream->pool = pool_alloconly_create("istream attachment", 1024);
7bc0c9
-	astream->parser = message_parser_init(astream->pool, input, 0,
7bc0c9
-				MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
-				MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES);
7bc0c9
+	astream->parser = message_parser_init(astream->pool, input, &parser_set);
7bc0c9
 	return i_stream_create(&astream->istream, input,
7bc0c9
 			       i_stream_get_fd(input), 0);
7bc0c9
 }
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/istream-binary-converter.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/istream-binary-converter.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/istream-binary-converter.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/istream-binary-converter.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -286,6 +286,10 @@ static void i_stream_binary_converter_cl
7bc0c9
 
7bc0c9
 struct istream *i_stream_create_binary_converter(struct istream *input)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
+			MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES,
7bc0c9
+	};
7bc0c9
 	struct binary_converter_istream *bstream;
7bc0c9
 
7bc0c9
 	bstream = i_new(struct binary_converter_istream, 1);
7bc0c9
@@ -299,9 +303,7 @@ struct istream *i_stream_create_binary_c
7bc0c9
 	bstream->istream.istream.seekable = FALSE;
7bc0c9
 
7bc0c9
 	bstream->pool = pool_alloconly_create("istream binary converter", 128);
7bc0c9
-	bstream->parser = message_parser_init(bstream->pool, input, 0,
7bc0c9
-				MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
-				MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES);
7bc0c9
+	bstream->parser = message_parser_init(bstream->pool, input, &parser_set);
7bc0c9
 	return i_stream_create(&bstream->istream, input,
7bc0c9
 			       i_stream_get_fd(input), 0);
7bc0c9
 }
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/Makefile.am.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/Makefile.am
7bc0c9
--- dovecot-2.3.8/src/lib-mail/Makefile.am.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/Makefile.am	2020-08-07 12:29:39.972827528 +0200
7bc0c9
@@ -28,6 +28,7 @@ libmail_la_SOURCES = \
7bc0c9
 	message-header-parser.c \
7bc0c9
 	message-id.c \
7bc0c9
 	message-parser.c \
7bc0c9
+	message-parser-from-parts.c \
7bc0c9
 	message-part.c \
7bc0c9
 	message-part-data.c \
7bc0c9
 	message-part-serialize.c \
7bc0c9
@@ -42,7 +43,8 @@ libmail_la_SOURCES = \
7bc0c9
 	rfc822-parser.c
7bc0c9
 
7bc0c9
 noinst_HEADERS = \
7bc0c9
-	html-entities.h
7bc0c9
+	html-entities.h \
7bc0c9
+	message-parser-private.h
7bc0c9
 
7bc0c9
 headers = \
7bc0c9
 	istream-attachment-connector.h \
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-parser.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-parser.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-parser.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-parser.c	2020-08-07 12:29:39.978827444 +0200
7bc0c9
@@ -1,54 +1,12 @@
7bc0c9
 /* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
7bc0c9
 
7bc0c9
 #include "lib.h"
7bc0c9
-#include "buffer.h"
7bc0c9
+#include "array.h"
7bc0c9
 #include "str.h"
7bc0c9
 #include "istream.h"
7bc0c9
 #include "rfc822-parser.h"
7bc0c9
 #include "rfc2231-parser.h"
7bc0c9
-#include "message-parser.h"
7bc0c9
-
7bc0c9
-/* RFC-2046 requires boundaries are max. 70 chars + "--" prefix + "--" suffix.
7bc0c9
-   We'll add a bit more just in case. */
7bc0c9
-#define BOUNDARY_END_MAX_LEN (70 + 2 + 2 + 10)
7bc0c9
-
7bc0c9
-struct message_boundary {
7bc0c9
-	struct message_boundary *next;
7bc0c9
-
7bc0c9
-	struct message_part *part;
7bc0c9
-	const char *boundary;
7bc0c9
-	size_t len;
7bc0c9
-
7bc0c9
-	bool epilogue_found:1;
7bc0c9
-};
7bc0c9
-
7bc0c9
-struct message_parser_ctx {
7bc0c9
-	pool_t parser_pool, part_pool;
7bc0c9
-	struct istream *input;
7bc0c9
-	struct message_part *parts, *part;
7bc0c9
-	const char *broken_reason;
7bc0c9
-
7bc0c9
-	enum message_header_parser_flags hdr_flags;
7bc0c9
-	enum message_parser_flags flags;
7bc0c9
-
7bc0c9
-	const char *last_boundary;
7bc0c9
-	struct message_boundary *boundaries;
7bc0c9
-
7bc0c9
-	size_t skip;
7bc0c9
-	char last_chr;
7bc0c9
-	unsigned int want_count;
7bc0c9
-
7bc0c9
-	struct message_header_parser_ctx *hdr_parser_ctx;
7bc0c9
-	unsigned int prev_hdr_newline_size;
7bc0c9
-
7bc0c9
-	int (*parse_next_block)(struct message_parser_ctx *ctx,
7bc0c9
-				struct message_block *block_r);
7bc0c9
-
7bc0c9
-	bool part_seen_content_type:1;
7bc0c9
-	bool multipart:1;
7bc0c9
-	bool preparsed:1;
7bc0c9
-	bool eof:1;
7bc0c9
-};
7bc0c9
+#include "message-parser-private.h"
7bc0c9
 
7bc0c9
 message_part_header_callback_t *null_message_part_header_callback = NULL;
7bc0c9
 
7bc0c9
@@ -58,14 +16,10 @@ static int parse_next_body_to_boundary(s
7bc0c9
 				       struct message_block *block_r);
7bc0c9
 static int parse_next_body_to_eof(struct message_parser_ctx *ctx,
7bc0c9
 				  struct message_block *block_r);
7bc0c9
-static int preparsed_parse_epilogue_init(struct message_parser_ctx *ctx,
7bc0c9
-					 struct message_block *block_r);
7bc0c9
-static int preparsed_parse_next_header_init(struct message_parser_ctx *ctx,
7bc0c9
-					    struct message_block *block_r);
7bc0c9
 
7bc0c9
 static struct message_boundary *
7bc0c9
 boundary_find(struct message_boundary *boundaries,
7bc0c9
-	      const unsigned char *data, size_t len)
7bc0c9
+	      const unsigned char *data, size_t len, bool trailing_dashes)
7bc0c9
 {
7bc0c9
 	struct message_boundary *best = NULL;
7bc0c9
 
7bc0c9
@@ -77,8 +31,18 @@ boundary_find(struct message_boundary *b
7bc0c9
 	while (boundaries != NULL) {
7bc0c9
 		if (boundaries->len <= len &&
7bc0c9
 		    memcmp(boundaries->boundary, data, boundaries->len) == 0 &&
7bc0c9
-		    (best == NULL || best->len < boundaries->len))
7bc0c9
+		    (best == NULL || best->len < boundaries->len)) {
7bc0c9
 			best = boundaries;
7bc0c9
+			/* If we see "foo--", it could either mean that there
7bc0c9
+			   is a boundary named "foo" that ends now or there's
7bc0c9
+			   a boundary "foo--" which continues. */
7bc0c9
+			if (best->len == len ||
7bc0c9
+			    (best->len == len-2 && trailing_dashes)) {
7bc0c9
+				/* This is exactly the wanted boundary. There
7bc0c9
+				   can't be a better one. */
7bc0c9
+				break;
7bc0c9
+			}
7bc0c9
+		}
7bc0c9
 
7bc0c9
 		boundaries = boundaries->next;
7bc0c9
 	}
7bc0c9
@@ -122,8 +86,8 @@ static void parse_body_add_block(struct
7bc0c9
 	ctx->part->body_size.virtual_size += block->size + missing_cr_count;
7bc0c9
 }
7bc0c9
 
7bc0c9
-static int message_parser_read_more(struct message_parser_ctx *ctx,
7bc0c9
-				    struct message_block *block_r, bool *full_r)
7bc0c9
+int message_parser_read_more(struct message_parser_ctx *ctx,
7bc0c9
+			     struct message_block *block_r, bool *full_r)
7bc0c9
 {
7bc0c9
 	int ret;
7bc0c9
 
7bc0c9
@@ -168,19 +132,18 @@ static int message_parser_read_more(stru
7bc0c9
 	return 1;
7bc0c9
 }
7bc0c9
 
7bc0c9
-static struct message_part *
7bc0c9
-message_part_append(pool_t pool, struct message_part *parent)
7bc0c9
+static void
7bc0c9
+message_part_append(struct message_parser_ctx *ctx)
7bc0c9
 {
7bc0c9
-	struct message_part *p, *part, **list;
7bc0c9
+	struct message_part *parent = ctx->part;
7bc0c9
+	struct message_part *part;
7bc0c9
 
7bc0c9
 	i_assert(parent != NULL);
7bc0c9
 	i_assert((parent->flags & (MESSAGE_PART_FLAG_MULTIPART |
7bc0c9
 				   MESSAGE_PART_FLAG_MESSAGE_RFC822)) != 0);
7bc0c9
 
7bc0c9
-	part = p_new(pool, struct message_part, 1);
7bc0c9
+	part = p_new(ctx->part_pool, struct message_part, 1);
7bc0c9
 	part->parent = parent;
7bc0c9
-	for (p = parent; p != NULL; p = p->parent)
7bc0c9
-		p->children_count++;
7bc0c9
 
7bc0c9
 	/* set child position */
7bc0c9
 	part->physical_pos =
7bc0c9
@@ -188,33 +151,78 @@ message_part_append(pool_t pool, struct
7bc0c9
 		parent->body_size.physical_size +
7bc0c9
 		parent->header_size.physical_size;
7bc0c9
 
7bc0c9
-	list = &part->parent->children;
7bc0c9
-	while (*list != NULL)
7bc0c9
-		list = &(*list)->next;
7bc0c9
+	/* add to parent's linked list */
7bc0c9
+	*ctx->next_part = part;
7bc0c9
+	/* update the parent's end-of-linked-list pointer */
7bc0c9
+	struct message_part **next_part = &part->next;
7bc0c9
+	array_push_back(&ctx->next_part_stack, &next_part);
7bc0c9
+	/* This part is now the new parent for the next message_part_append()
7bc0c9
+	   call. Its linked list begins with the children pointer. */
7bc0c9
+	ctx->next_part = &part->children;
7bc0c9
+
7bc0c9
+	ctx->part = part;
7bc0c9
+	ctx->nested_parts_count++;
7bc0c9
+	ctx->total_parts_count++;
7bc0c9
+	i_assert(ctx->nested_parts_count < ctx->max_nested_mime_parts);
7bc0c9
+	i_assert(ctx->total_parts_count <= ctx->max_total_mime_parts);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void message_part_finish(struct message_parser_ctx *ctx)
7bc0c9
+{
7bc0c9
+	struct message_part **const *parent_next_partp;
7bc0c9
+
7bc0c9
+	i_assert(ctx->nested_parts_count > 0);
7bc0c9
+	ctx->nested_parts_count--;
7bc0c9
+
7bc0c9
+	parent_next_partp = array_back(&ctx->next_part_stack);
7bc0c9
+	array_pop_back(&ctx->next_part_stack);
7bc0c9
+	ctx->next_part = *parent_next_partp;
7bc0c9
+
7bc0c9
+	message_size_add(&ctx->part->parent->body_size, &ctx->part->body_size);
7bc0c9
+	message_size_add(&ctx->part->parent->body_size, &ctx->part->header_size);
7bc0c9
+	ctx->part->parent->children_count += 1 + ctx->part->children_count;
7bc0c9
+	ctx->part = ctx->part->parent;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void message_boundary_free(struct message_boundary *b)
7bc0c9
+{
7bc0c9
+	i_free(b->boundary);
7bc0c9
+	i_free(b);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void
7bc0c9
+boundary_remove_until(struct message_parser_ctx *ctx,
7bc0c9
+		      struct message_boundary *boundary)
7bc0c9
+{
7bc0c9
+	while (ctx->boundaries != boundary) {
7bc0c9
+		struct message_boundary *cur = ctx->boundaries;
7bc0c9
 
7bc0c9
-	*list = part;
7bc0c9
-	return part;
7bc0c9
+		i_assert(cur != NULL);
7bc0c9
+		ctx->boundaries = cur->next;
7bc0c9
+		message_boundary_free(cur);
7bc0c9
+
7bc0c9
+	}
7bc0c9
+	ctx->boundaries = boundary;
7bc0c9
 }
7bc0c9
 
7bc0c9
 static void parse_next_body_multipart_init(struct message_parser_ctx *ctx)
7bc0c9
 {
7bc0c9
 	struct message_boundary *b;
7bc0c9
 
7bc0c9
-	b = p_new(ctx->parser_pool, struct message_boundary, 1);
7bc0c9
+	b = i_new(struct message_boundary, 1);
7bc0c9
 	b->part = ctx->part;
7bc0c9
 	b->boundary = ctx->last_boundary;
7bc0c9
+	ctx->last_boundary = NULL;
7bc0c9
 	b->len = strlen(b->boundary);
7bc0c9
 
7bc0c9
 	b->next = ctx->boundaries;
7bc0c9
 	ctx->boundaries = b;
7bc0c9
-
7bc0c9
-	ctx->last_boundary = NULL;
7bc0c9
 }
7bc0c9
 
7bc0c9
 static int parse_next_body_message_rfc822_init(struct message_parser_ctx *ctx,
7bc0c9
 					       struct message_block *block_r)
7bc0c9
 {
7bc0c9
-	ctx->part = message_part_append(ctx->part_pool, ctx->part);
7bc0c9
+	message_part_append(ctx);
7bc0c9
 	return parse_next_header_init(ctx, block_r);
7bc0c9
 }
7bc0c9
 
7bc0c9
@@ -239,19 +247,38 @@ boundary_line_find(struct message_parser
7bc0c9
 		return -1;
7bc0c9
 	}
7bc0c9
 
7bc0c9
+	if (ctx->total_parts_count >= ctx->max_total_mime_parts) {
7bc0c9
+		/* can't add any more MIME parts. just stop trying to find
7bc0c9
+		   more boundaries. */
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+
7bc0c9
 	/* need to find the end of line */
7bc0c9
-	if (memchr(data + 2, '\n', size - 2) == NULL &&
7bc0c9
-	    size < BOUNDARY_END_MAX_LEN &&
7bc0c9
+	data += 2;
7bc0c9
+	size -= 2;
7bc0c9
+	const unsigned char *lf_pos = memchr(data, '\n', size);
7bc0c9
+	if (lf_pos == NULL &&
7bc0c9
+	    size+2 < BOUNDARY_END_MAX_LEN &&
7bc0c9
 	    !ctx->input->eof && !full) {
7bc0c9
 		/* no LF found */
7bc0c9
 		ctx->want_count = BOUNDARY_END_MAX_LEN;
7bc0c9
 		return 0;
7bc0c9
 	}
7bc0c9
+	size_t find_size = size;
7bc0c9
+	bool trailing_dashes = FALSE;
7bc0c9
 
7bc0c9
-	data += 2;
7bc0c9
-	size -= 2;
7bc0c9
+	if (lf_pos != NULL) {
7bc0c9
+		find_size = lf_pos - data;
7bc0c9
+		if (find_size > 0 && data[find_size-1] == '\r')
7bc0c9
+			find_size--;
7bc0c9
+		if (find_size > 2 && data[find_size-1] == '-' &&
7bc0c9
+		    data[find_size-2] == '-')
7bc0c9
+			trailing_dashes = TRUE;
7bc0c9
+	} else if (find_size > BOUNDARY_END_MAX_LEN)
7bc0c9
+		find_size = BOUNDARY_END_MAX_LEN;
7bc0c9
 
7bc0c9
-	*boundary_r = boundary_find(ctx->boundaries, data, size);
7bc0c9
+	*boundary_r = boundary_find(ctx->boundaries, data, find_size,
7bc0c9
+				    trailing_dashes);
7bc0c9
 	if (*boundary_r == NULL)
7bc0c9
 		return -1;
7bc0c9
 
7bc0c9
@@ -264,7 +291,7 @@ boundary_line_find(struct message_parser
7bc0c9
 static int parse_next_mime_header_init(struct message_parser_ctx *ctx,
7bc0c9
 				       struct message_block *block_r)
7bc0c9
 {
7bc0c9
-	ctx->part = message_part_append(ctx->part_pool, ctx->part);
7bc0c9
+	message_part_append(ctx);
7bc0c9
 	ctx->part->flags |= MESSAGE_PART_FLAG_IS_MIME;
7bc0c9
 
7bc0c9
 	return parse_next_header_init(ctx, block_r);
7bc0c9
@@ -313,26 +340,25 @@ static int parse_part_finish(struct mess
7bc0c9
 			     struct message_boundary *boundary,
7bc0c9
 			     struct message_block *block_r, bool first_line)
7bc0c9
 {
7bc0c9
-	struct message_part *part;
7bc0c9
 	size_t line_size;
7bc0c9
+	size_t boundary_len = boundary->len;
7bc0c9
+	bool boundary_epilogue_found = boundary->epilogue_found;
7bc0c9
 
7bc0c9
 	i_assert(ctx->last_boundary == NULL);
7bc0c9
 
7bc0c9
 	/* get back to parent MIME part, summing the child MIME part sizes
7bc0c9
 	   into parent's body sizes */
7bc0c9
-	for (part = ctx->part; part != boundary->part; part = part->parent) {
7bc0c9
-		message_size_add(&part->parent->body_size, &part->body_size);
7bc0c9
-		message_size_add(&part->parent->body_size, &part->header_size);
7bc0c9
+	while (ctx->part != boundary->part) {
7bc0c9
+		message_part_finish(ctx);
7bc0c9
+		i_assert(ctx->part != NULL);
7bc0c9
 	}
7bc0c9
-	i_assert(part != NULL);
7bc0c9
-	ctx->part = part;
7bc0c9
 
7bc0c9
 	if (boundary->epilogue_found) {
7bc0c9
 		/* this boundary isn't needed anymore */
7bc0c9
-		ctx->boundaries = boundary->next;
7bc0c9
+		boundary_remove_until(ctx, boundary->next);
7bc0c9
 	} else {
7bc0c9
 		/* forget about the boundaries we possibly skipped */
7bc0c9
-		ctx->boundaries = boundary;
7bc0c9
+		boundary_remove_until(ctx, boundary);
7bc0c9
 	}
7bc0c9
 
7bc0c9
 	/* the boundary itself should already be in buffer. add that. */
7bc0c9
@@ -349,7 +375,7 @@ static int parse_part_finish(struct mess
7bc0c9
 		i_assert(block_r->data[0] == '\n');
7bc0c9
 		line_size = 1;
7bc0c9
 	}
7bc0c9
-	line_size += 2 + boundary->len + (boundary->epilogue_found ? 2 : 0);
7bc0c9
+	line_size += 2 + boundary_len + (boundary_epilogue_found ? 2 : 0);
7bc0c9
 	i_assert(block_r->size >= ctx->skip + line_size);
7bc0c9
 	block_r->size = line_size;
7bc0c9
 	parse_body_add_block(ctx, block_r);
7bc0c9
@@ -510,8 +536,10 @@ static void parse_content_type(struct me
7bc0c9
 	rfc2231_parse(&parser, &results);
7bc0c9
 	for (; *results != NULL; results += 2) {
7bc0c9
 		if (strcasecmp(results[0], "boundary") == 0) {
7bc0c9
+			/* truncate excessively long boundaries */
7bc0c9
+			i_free(ctx->last_boundary);
7bc0c9
 			ctx->last_boundary =
7bc0c9
-				p_strdup(ctx->parser_pool, results[1]);
7bc0c9
+				i_strndup(results[1], BOUNDARY_STRING_MAX_LEN);
7bc0c9
 			break;
7bc0c9
 		}
7bc0c9
 	}
7bc0c9
@@ -533,6 +561,11 @@ static bool block_is_at_eoh(const struct
7bc0c9
 	return FALSE;
7bc0c9
 }
7bc0c9
 
7bc0c9
+static bool parse_too_many_nested_mime_parts(struct message_parser_ctx *ctx)
7bc0c9
+{
7bc0c9
+	return ctx->nested_parts_count+1 >= ctx->max_nested_mime_parts;
7bc0c9
+}
7bc0c9
+
7bc0c9
 #define MUTEX_FLAGS \
7bc0c9
 	(MESSAGE_PART_FLAG_MESSAGE_RFC822 | MESSAGE_PART_FLAG_MULTIPART)
7bc0c9
 
7bc0c9
@@ -557,8 +590,12 @@ static int parse_next_header(struct mess
7bc0c9
 		   "\n--boundary" belongs to us or to a previous boundary.
7bc0c9
 		   this is a problem if the boundary prefixes are identical,
7bc0c9
 		   because MIME requires only the prefix to match. */
7bc0c9
-		parse_next_body_multipart_init(ctx);
7bc0c9
-		ctx->multipart = TRUE;
7bc0c9
+		if (!parse_too_many_nested_mime_parts(ctx)) {
7bc0c9
+			parse_next_body_multipart_init(ctx);
7bc0c9
+			ctx->multipart = TRUE;
7bc0c9
+		} else {
7bc0c9
+			part->flags &= ~MESSAGE_PART_FLAG_MULTIPART;
7bc0c9
+		}
7bc0c9
 	}
7bc0c9
 
7bc0c9
 	/* before parsing the header see if we can find a --boundary from here.
7bc0c9
@@ -634,7 +671,7 @@ static int parse_next_header(struct mess
7bc0c9
 		i_assert(!ctx->multipart);
7bc0c9
 		part->flags = 0;
7bc0c9
 	}
7bc0c9
-	ctx->last_boundary = NULL;
7bc0c9
+	i_free(ctx->last_boundary);
7bc0c9
 
7bc0c9
 	if (!ctx->part_seen_content_type ||
7bc0c9
 	    (part->flags & MESSAGE_PART_FLAG_IS_MIME) == 0) {
7bc0c9
@@ -662,12 +699,16 @@ static int parse_next_header(struct mess
7bc0c9
 		i_assert(ctx->last_boundary == NULL);
7bc0c9
 		ctx->multipart = FALSE;
7bc0c9
 		ctx->parse_next_block = parse_next_body_to_boundary;
7bc0c9
-	} else if ((part->flags & MESSAGE_PART_FLAG_MESSAGE_RFC822) != 0)
7bc0c9
+	} else if ((part->flags & MESSAGE_PART_FLAG_MESSAGE_RFC822) != 0 &&
7bc0c9
+		   !parse_too_many_nested_mime_parts(ctx)) {
7bc0c9
 		ctx->parse_next_block = parse_next_body_message_rfc822_init;
7bc0c9
-	else if (ctx->boundaries != NULL)
7bc0c9
-		ctx->parse_next_block = parse_next_body_to_boundary;
7bc0c9
-	else
7bc0c9
-		ctx->parse_next_block = parse_next_body_to_eof;
7bc0c9
+	} else {
7bc0c9
+		part->flags &= ~MESSAGE_PART_FLAG_MESSAGE_RFC822;
7bc0c9
+		if (ctx->boundaries != NULL)
7bc0c9
+			ctx->parse_next_block = parse_next_body_to_boundary;
7bc0c9
+		else
7bc0c9
+			ctx->parse_next_block = parse_next_body_to_eof;
7bc0c9
+	}
7bc0c9
 
7bc0c9
 	ctx->want_count = 1;
7bc0c9
 
7bc0c9
@@ -692,358 +733,21 @@ static int parse_next_header_init(struct
7bc0c9
 	return parse_next_header(ctx, block_r);
7bc0c9
 }
7bc0c9
 
7bc0c9
-static int preparsed_parse_eof(struct message_parser_ctx *ctx ATTR_UNUSED,
7bc0c9
-			       struct message_block *block_r ATTR_UNUSED)
7bc0c9
-{
7bc0c9
-	return -1;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static void preparsed_skip_to_next(struct message_parser_ctx *ctx)
7bc0c9
-{
7bc0c9
-	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
-	while (ctx->part != NULL) {
7bc0c9
-		if (ctx->part->next != NULL) {
7bc0c9
-			ctx->part = ctx->part->next;
7bc0c9
-			break;
7bc0c9
-		}
7bc0c9
-
7bc0c9
-		/* parse epilogue of multipart parent if requested */
7bc0c9
-		if (ctx->part->parent != NULL &&
7bc0c9
-		    (ctx->part->parent->flags & MESSAGE_PART_FLAG_MULTIPART) != 0 &&
7bc0c9
-		    (ctx->flags & MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS) != 0) {
7bc0c9
-			/* check for presence of epilogue */
7bc0c9
-			uoff_t part_end = ctx->part->physical_pos +
7bc0c9
-				ctx->part->header_size.physical_size +
7bc0c9
-				ctx->part->body_size.physical_size;
7bc0c9
-			uoff_t parent_end = ctx->part->parent->physical_pos +
7bc0c9
-				ctx->part->parent->header_size.physical_size +
7bc0c9
-				ctx->part->parent->body_size.physical_size;
7bc0c9
-
7bc0c9
-			if (parent_end > part_end) {
7bc0c9
-				ctx->parse_next_block = preparsed_parse_epilogue_init;
7bc0c9
-				break;
7bc0c9
-			}
7bc0c9
-		}
7bc0c9
-		ctx->part = ctx->part->parent;
7bc0c9
-	}
7bc0c9
-	if (ctx->part == NULL)
7bc0c9
-		ctx->parse_next_block = preparsed_parse_eof;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_body_finish(struct message_parser_ctx *ctx,
7bc0c9
-				       struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	i_stream_skip(ctx->input, ctx->skip);
7bc0c9
-	ctx->skip = 0;
7bc0c9
-
7bc0c9
-	preparsed_skip_to_next(ctx);
7bc0c9
-	return ctx->parse_next_block(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_prologue_finish(struct message_parser_ctx *ctx,
7bc0c9
-					   struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	i_stream_skip(ctx->input, ctx->skip);
7bc0c9
-	ctx->skip = 0;
7bc0c9
-
7bc0c9
-	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
-	ctx->part = ctx->part->children;
7bc0c9
-	return ctx->parse_next_block(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_body_more(struct message_parser_ctx *ctx,
7bc0c9
-				     struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
-		ctx->part->header_size.physical_size +
7bc0c9
-		ctx->part->body_size.physical_size;
7bc0c9
-	bool full;
7bc0c9
-	int ret;
7bc0c9
-
7bc0c9
-	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
-		return ret;
7bc0c9
-
7bc0c9
-	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
-		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
-		ctx->parse_next_block = preparsed_parse_body_finish;
7bc0c9
-	}
7bc0c9
-	ctx->skip = block_r->size;
7bc0c9
-	return 1;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_prologue_more(struct message_parser_ctx *ctx,
7bc0c9
-					 struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t boundary_min_start, end_offset;
7bc0c9
-	const unsigned char *cur;
7bc0c9
-	bool full;
7bc0c9
-	int ret;
7bc0c9
-
7bc0c9
-	i_assert(ctx->part->children != NULL);
7bc0c9
-	end_offset = ctx->part->children->physical_pos;
7bc0c9
-
7bc0c9
-	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
-		return ret;
7bc0c9
-
7bc0c9
-	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
-		/* we've got the full prologue: clip off the initial boundary */
7bc0c9
-		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
-		cur = block_r->data + block_r->size - 1;
7bc0c9
-
7bc0c9
-		/* [\r]\n--boundary[\r]\n */ 
7bc0c9
-		if (block_r->size < 5 || *cur != '\n') {
7bc0c9
-			ctx->broken_reason = "Prologue boundary end not at expected position";
7bc0c9
-			return -1;
7bc0c9
-		}
7bc0c9
-		
7bc0c9
-		cur--;
7bc0c9
-		if (*cur == '\r') cur--;
7bc0c9
-
7bc0c9
-		/* find newline just before boundary */
7bc0c9
-		for (; cur >= block_r->data; cur--) {
7bc0c9
-			if (*cur == '\n') break;
7bc0c9
-		}
7bc0c9
-
7bc0c9
-		if (cur[0] != '\n' || cur[1] != '-' || cur[2] != '-') {
7bc0c9
-			ctx->broken_reason = "Prologue boundary beginning not at expected position";
7bc0c9
-			return -1;
7bc0c9
-		}
7bc0c9
-
7bc0c9
-		if (cur != block_r->data && cur[-1] == '\r') cur--;
7bc0c9
-
7bc0c9
-		/* clip boundary */
7bc0c9
-		block_r->size = cur - block_r->data;			
7bc0c9
-
7bc0c9
-		ctx->parse_next_block = preparsed_parse_prologue_finish;
7bc0c9
-		ctx->skip = block_r->size;
7bc0c9
-		return 1;
7bc0c9
-	}
7bc0c9
-		
7bc0c9
-	/* retain enough data in the stream buffer to contain initial boundary */
7bc0c9
-	if (end_offset > BOUNDARY_END_MAX_LEN)
7bc0c9
-		boundary_min_start = end_offset - BOUNDARY_END_MAX_LEN;
7bc0c9
-	else
7bc0c9
-		boundary_min_start = 0;
7bc0c9
-
7bc0c9
-	if (ctx->input->v_offset + block_r->size >= boundary_min_start) {
7bc0c9
-		if (boundary_min_start <= ctx->input->v_offset)
7bc0c9
-			return 0;
7bc0c9
-		block_r->size = boundary_min_start - ctx->input->v_offset;
7bc0c9
-	}
7bc0c9
-	ctx->skip = block_r->size;
7bc0c9
-	return 1;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_epilogue_more(struct message_parser_ctx *ctx,
7bc0c9
-					 struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
-		ctx->part->header_size.physical_size +
7bc0c9
-		ctx->part->body_size.physical_size;
7bc0c9
-	bool full;
7bc0c9
-	int ret;
7bc0c9
-
7bc0c9
-	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
-		return ret;
7bc0c9
-
7bc0c9
-	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
-		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
-		ctx->parse_next_block = preparsed_parse_body_finish;
7bc0c9
-	}
7bc0c9
-	ctx->skip = block_r->size;
7bc0c9
-	return 1;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_epilogue_boundary(struct message_parser_ctx *ctx,
7bc0c9
-					     struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
-		ctx->part->header_size.physical_size +
7bc0c9
-		ctx->part->body_size.physical_size;
7bc0c9
-	const unsigned char *data, *cur;
7bc0c9
-	size_t size;
7bc0c9
-	bool full;
7bc0c9
-	int ret;
7bc0c9
-
7bc0c9
-	if (end_offset - ctx->input->v_offset < 7) {
7bc0c9
-		ctx->broken_reason = "Epilogue position is wrong";
7bc0c9
-		return -1;
7bc0c9
-	}
7bc0c9
-
7bc0c9
-	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
-		return ret;
7bc0c9
-
7bc0c9
-	/* [\r]\n--boundary--[\r]\n */
7bc0c9
-	if (block_r->size < 7) {
7bc0c9
-		ctx->want_count = 7;
7bc0c9
-		return 0;
7bc0c9
-	}
7bc0c9
-
7bc0c9
-	data = block_r->data;
7bc0c9
-	size = block_r->size;
7bc0c9
-	cur = data;
7bc0c9
-
7bc0c9
-	if (*cur == '\r') cur++;
7bc0c9
-
7bc0c9
-	if (cur[0] != '\n' || cur[1] != '-' || data[2] != '-') {
7bc0c9
-		ctx->broken_reason = "Epilogue boundary start not at expected position";
7bc0c9
-		return -1;
7bc0c9
-	}
7bc0c9
-
7bc0c9
-	/* find the end of the line */
7bc0c9
-	cur += 3;
7bc0c9
-	if ((cur = memchr(cur, '\n', size - (cur-data))) == NULL) {
7bc0c9
-		if (end_offset < ctx->input->v_offset + size) {
7bc0c9
-			ctx->broken_reason = "Epilogue boundary end not at expected position";
7bc0c9
-			return -1;
7bc0c9
-		} else if (ctx->input->v_offset + size < end_offset &&
7bc0c9
-			   size < BOUNDARY_END_MAX_LEN &&
7bc0c9
-			   !ctx->input->eof && !full) {
7bc0c9
-			ctx->want_count = BOUNDARY_END_MAX_LEN;
7bc0c9
-			return 0;
7bc0c9
-		}
7bc0c9
-	}
7bc0c9
-
7bc0c9
-	block_r->size = 0;
7bc0c9
-	ctx->parse_next_block = preparsed_parse_epilogue_more;
7bc0c9
-	ctx->skip = cur - data + 1;
7bc0c9
-	return 0;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_body_init(struct message_parser_ctx *ctx,
7bc0c9
-				     struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t offset = ctx->part->physical_pos +
7bc0c9
-		ctx->part->header_size.physical_size;
7bc0c9
-
7bc0c9
-	if (offset < ctx->input->v_offset) {
7bc0c9
-		/* header was actually larger than the cached size suggested */
7bc0c9
-		ctx->broken_reason = "Header larger than its cached size";
7bc0c9
-		return -1;
7bc0c9
-	}
7bc0c9
-	i_stream_skip(ctx->input, offset - ctx->input->v_offset);
7bc0c9
-
7bc0c9
-	/* multipart messages may begin with --boundary--, which makes them
7bc0c9
-	   not have any children. */
7bc0c9
-	if ((ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) == 0 ||
7bc0c9
-	    ctx->part->children == NULL)
7bc0c9
-		ctx->parse_next_block = preparsed_parse_body_more;
7bc0c9
-	else
7bc0c9
-		ctx->parse_next_block = preparsed_parse_prologue_more;
7bc0c9
-	return ctx->parse_next_block(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_epilogue_init(struct message_parser_ctx *ctx,
7bc0c9
-					 struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	uoff_t offset = ctx->part->physical_pos +
7bc0c9
-		ctx->part->header_size.physical_size +
7bc0c9
-		ctx->part->body_size.physical_size;
7bc0c9
-
7bc0c9
-	ctx->part = ctx->part->parent;
7bc0c9
-
7bc0c9
-	if (offset < ctx->input->v_offset) {
7bc0c9
-		/* last child was actually larger than the cached size
7bc0c9
-		   suggested */
7bc0c9
-		ctx->broken_reason = "Part larger than its cached size";
7bc0c9
-		return -1;
7bc0c9
-	}
7bc0c9
-	i_stream_skip(ctx->input, offset - ctx->input->v_offset);
7bc0c9
-
7bc0c9
-	ctx->parse_next_block = preparsed_parse_epilogue_boundary;
7bc0c9
-	return ctx->parse_next_block(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_finish_header(struct message_parser_ctx *ctx,
7bc0c9
-					 struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	if (ctx->part->children != NULL) {
7bc0c9
-		if ((ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) != 0 &&
7bc0c9
-		    (ctx->flags & MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS) != 0)
7bc0c9
-			ctx->parse_next_block = preparsed_parse_body_init;
7bc0c9
-		else {
7bc0c9
-			ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
-			ctx->part = ctx->part->children;
7bc0c9
-		}
7bc0c9
-	} else if ((ctx->flags & MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK) == 0) {
7bc0c9
-		ctx->parse_next_block = preparsed_parse_body_init;
7bc0c9
-	} else {
7bc0c9
-		preparsed_skip_to_next(ctx);
7bc0c9
-	}
7bc0c9
-	return ctx->parse_next_block(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_next_header(struct message_parser_ctx *ctx,
7bc0c9
-				       struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	struct message_header_line *hdr;
7bc0c9
-	int ret;
7bc0c9
-
7bc0c9
-	ret = message_parse_header_next(ctx->hdr_parser_ctx, &hdr);
7bc0c9
-	if (ret == 0 || (ret < 0 && ctx->input->stream_errno != 0)) {
7bc0c9
-		ctx->want_count = i_stream_get_data_size(ctx->input) + 1;
7bc0c9
-		return ret;
7bc0c9
-	}
7bc0c9
-
7bc0c9
-	if (hdr != NULL) {
7bc0c9
-		block_r->hdr = hdr;
7bc0c9
-		block_r->size = 0;
7bc0c9
-		return 1;
7bc0c9
-	}
7bc0c9
-	message_parse_header_deinit(&ctx->hdr_parser_ctx);
7bc0c9
-
7bc0c9
-	ctx->parse_next_block = preparsed_parse_finish_header;
7bc0c9
-
7bc0c9
-	/* return empty block as end of headers */
7bc0c9
-	block_r->hdr = NULL;
7bc0c9
-	block_r->size = 0;
7bc0c9
-
7bc0c9
-	i_assert(ctx->skip == 0);
7bc0c9
-	if (ctx->input->v_offset != ctx->part->physical_pos +
7bc0c9
-	    ctx->part->header_size.physical_size) {
7bc0c9
-		ctx->broken_reason = "Cached header size mismatch";
7bc0c9
-		return -1;
7bc0c9
-	}
7bc0c9
-	return 1;
7bc0c9
-}
7bc0c9
-
7bc0c9
-static int preparsed_parse_next_header_init(struct message_parser_ctx *ctx,
7bc0c9
-					    struct message_block *block_r)
7bc0c9
-{
7bc0c9
-	struct istream *hdr_input;
7bc0c9
-
7bc0c9
-	i_assert(ctx->hdr_parser_ctx == NULL);
7bc0c9
-
7bc0c9
-	i_assert(ctx->part->physical_pos >= ctx->input->v_offset);
7bc0c9
-	i_stream_skip(ctx->input, ctx->part->physical_pos -
7bc0c9
-		      ctx->input->v_offset);
7bc0c9
-
7bc0c9
-	/* the header may become truncated by --boundaries. limit the header
7bc0c9
-	   stream's size to what it's supposed to be to avoid duplicating (and
7bc0c9
-	   keeping in sync!) all the same complicated logic as in
7bc0c9
-	   parse_next_header(). */
7bc0c9
-	hdr_input = i_stream_create_limit(ctx->input, ctx->part->header_size.physical_size);
7bc0c9
-	ctx->hdr_parser_ctx =
7bc0c9
-		message_parse_header_init(hdr_input, NULL, ctx->hdr_flags);
7bc0c9
-	i_stream_unref(&hdr_input);
7bc0c9
-
7bc0c9
-	ctx->parse_next_block = preparsed_parse_next_header;
7bc0c9
-	return preparsed_parse_next_header(ctx, block_r);
7bc0c9
-}
7bc0c9
-
7bc0c9
-static struct message_parser_ctx *
7bc0c9
+struct message_parser_ctx *
7bc0c9
 message_parser_init_int(struct istream *input,
7bc0c9
-			enum message_header_parser_flags hdr_flags,
7bc0c9
-			enum message_parser_flags flags)
7bc0c9
+			const struct message_parser_settings *set)
7bc0c9
 {
7bc0c9
 	struct message_parser_ctx *ctx;
7bc0c9
-	pool_t pool;
7bc0c9
 
7bc0c9
-	pool = pool_alloconly_create("Message Parser", 1024);
7bc0c9
-	ctx = p_new(pool, struct message_parser_ctx, 1);
7bc0c9
-	ctx->parser_pool = pool;
7bc0c9
-	ctx->hdr_flags = hdr_flags;
7bc0c9
-	ctx->flags = flags;
7bc0c9
+	ctx = i_new(struct message_parser_ctx, 1);
7bc0c9
+	ctx->hdr_flags = set->hdr_flags;
7bc0c9
+	ctx->flags = set->flags;
7bc0c9
+	ctx->max_nested_mime_parts = set->max_nested_mime_parts != 0 ?
7bc0c9
+		set->max_nested_mime_parts :
7bc0c9
+		MESSAGE_PARSER_DEFAULT_MAX_NESTED_MIME_PARTS;
7bc0c9
+	ctx->max_total_mime_parts = set->max_total_mime_parts != 0 ?
7bc0c9
+		set->max_total_mime_parts :
7bc0c9
+		MESSAGE_PARSER_DEFAULT_MAX_TOTAL_MIME_PARTS;
7bc0c9
 	ctx->input = input;
7bc0c9
 	i_stream_ref(input);
7bc0c9
 	return ctx;
7bc0c9
@@ -1051,32 +755,17 @@ message_parser_init_int(struct istream *
7bc0c9
 
7bc0c9
 struct message_parser_ctx *
7bc0c9
 message_parser_init(pool_t part_pool, struct istream *input,
7bc0c9
-		    enum message_header_parser_flags hdr_flags,
7bc0c9
-		    enum message_parser_flags flags)
7bc0c9
+		    const struct message_parser_settings *set)
7bc0c9
 {
7bc0c9
 	struct message_parser_ctx *ctx;
7bc0c9
 
7bc0c9
-	ctx = message_parser_init_int(input, hdr_flags, flags);
7bc0c9
+	ctx = message_parser_init_int(input, set);
7bc0c9
 	ctx->part_pool = part_pool;
7bc0c9
 	ctx->parts = ctx->part = p_new(part_pool, struct message_part, 1);
7bc0c9
+	ctx->next_part = &ctx->part->children;
7bc0c9
 	ctx->parse_next_block = parse_next_header_init;
7bc0c9
-	return ctx;
7bc0c9
-}
7bc0c9
-
7bc0c9
-struct message_parser_ctx *
7bc0c9
-message_parser_init_from_parts(struct message_part *parts,
7bc0c9
-			       struct istream *input,
7bc0c9
-			       enum message_header_parser_flags hdr_flags,
7bc0c9
-			       enum message_parser_flags flags)
7bc0c9
-{
7bc0c9
-	struct message_parser_ctx *ctx;
7bc0c9
-
7bc0c9
-	i_assert(parts != NULL);
7bc0c9
-
7bc0c9
-	ctx = message_parser_init_int(input, hdr_flags, flags);
7bc0c9
-	ctx->preparsed = TRUE;
7bc0c9
-	ctx->parts = ctx->part = parts;
7bc0c9
-	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
+	ctx->total_parts_count = 1;
7bc0c9
+	i_array_init(&ctx->next_part_stack, 4);
7bc0c9
 	return ctx;
7bc0c9
 }
7bc0c9
 
7bc0c9
@@ -1103,8 +792,15 @@ int message_parser_deinit_from_parts(str
7bc0c9
 
7bc0c9
 	if (ctx->hdr_parser_ctx != NULL)
7bc0c9
 		message_parse_header_deinit(&ctx->hdr_parser_ctx);
7bc0c9
+	boundary_remove_until(ctx, NULL);
7bc0c9
+	/* caller might have stopped the parsing early */
7bc0c9
+	i_assert(ctx->nested_parts_count == 0 ||
7bc0c9
+		 i_stream_have_bytes_left(ctx->input));
7bc0c9
+
7bc0c9
 	i_stream_unref(&ctx->input);
7bc0c9
-	pool_unref(&ctx->parser_pool);
7bc0c9
+	array_free(&ctx->next_part_stack);
7bc0c9
+	i_free(ctx->last_boundary);
7bc0c9
+	i_free(ctx);
7bc0c9
 	i_assert(ret < 0 || *parts_r != NULL);
7bc0c9
 	return ret;
7bc0c9
 }
7bc0c9
@@ -1136,13 +832,8 @@ int message_parser_parse_next_block(stru
7bc0c9
 		i_assert(ctx->input->eof || ctx->input->closed ||
7bc0c9
 			 ctx->input->stream_errno != 0 ||
7bc0c9
 			 ctx->broken_reason != NULL);
7bc0c9
-		while (ctx->part->parent != NULL) {
7bc0c9
-			message_size_add(&ctx->part->parent->body_size,
7bc0c9
-					 &ctx->part->body_size);
7bc0c9
-			message_size_add(&ctx->part->parent->body_size,
7bc0c9
-					 &ctx->part->header_size);
7bc0c9
-			ctx->part = ctx->part->parent;
7bc0c9
-		}
7bc0c9
+		while (ctx->part->parent != NULL)
7bc0c9
+			message_part_finish(ctx);
7bc0c9
 	}
7bc0c9
 
7bc0c9
 	if (block_r->size == 0) {
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-parser-from-parts.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-parser-from-parts.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-parser-from-parts.c.CVE_2020_12100	2020-08-07 12:29:39.972827528 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-parser-from-parts.c	2020-08-07 12:29:39.975827486 +0200
7bc0c9
@@ -0,0 +1,365 @@
7bc0c9
+/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
7bc0c9
+
7bc0c9
+#include "lib.h"
7bc0c9
+#include "istream.h"
7bc0c9
+#include "message-parser-private.h"
7bc0c9
+
7bc0c9
+static int preparsed_parse_epilogue_init(struct message_parser_ctx *ctx,
7bc0c9
+					 struct message_block *block_r);
7bc0c9
+static int preparsed_parse_next_header_init(struct message_parser_ctx *ctx,
7bc0c9
+					    struct message_block *block_r);
7bc0c9
+
7bc0c9
+static int preparsed_parse_eof(struct message_parser_ctx *ctx ATTR_UNUSED,
7bc0c9
+			       struct message_block *block_r ATTR_UNUSED)
7bc0c9
+{
7bc0c9
+	return -1;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void preparsed_skip_to_next(struct message_parser_ctx *ctx)
7bc0c9
+{
7bc0c9
+	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
+	while (ctx->part != NULL) {
7bc0c9
+		if (ctx->part->next != NULL) {
7bc0c9
+			ctx->part = ctx->part->next;
7bc0c9
+			break;
7bc0c9
+		}
7bc0c9
+
7bc0c9
+		/* parse epilogue of multipart parent if requested */
7bc0c9
+		if (ctx->part->parent != NULL &&
7bc0c9
+		    (ctx->part->parent->flags & MESSAGE_PART_FLAG_MULTIPART) != 0 &&
7bc0c9
+		    (ctx->flags & MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS) != 0) {
7bc0c9
+			/* check for presence of epilogue */
7bc0c9
+			uoff_t part_end = ctx->part->physical_pos +
7bc0c9
+				ctx->part->header_size.physical_size +
7bc0c9
+				ctx->part->body_size.physical_size;
7bc0c9
+			uoff_t parent_end = ctx->part->parent->physical_pos +
7bc0c9
+				ctx->part->parent->header_size.physical_size +
7bc0c9
+				ctx->part->parent->body_size.physical_size;
7bc0c9
+
7bc0c9
+			if (parent_end > part_end) {
7bc0c9
+				ctx->parse_next_block = preparsed_parse_epilogue_init;
7bc0c9
+				break;
7bc0c9
+			}
7bc0c9
+		}
7bc0c9
+		ctx->part = ctx->part->parent;
7bc0c9
+	}
7bc0c9
+	if (ctx->part == NULL)
7bc0c9
+		ctx->parse_next_block = preparsed_parse_eof;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_body_finish(struct message_parser_ctx *ctx,
7bc0c9
+				       struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	i_stream_skip(ctx->input, ctx->skip);
7bc0c9
+	ctx->skip = 0;
7bc0c9
+
7bc0c9
+	preparsed_skip_to_next(ctx);
7bc0c9
+	return ctx->parse_next_block(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_prologue_finish(struct message_parser_ctx *ctx,
7bc0c9
+					   struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	i_stream_skip(ctx->input, ctx->skip);
7bc0c9
+	ctx->skip = 0;
7bc0c9
+
7bc0c9
+	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
+	ctx->part = ctx->part->children;
7bc0c9
+	return ctx->parse_next_block(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_body_more(struct message_parser_ctx *ctx,
7bc0c9
+				     struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
+		ctx->part->header_size.physical_size +
7bc0c9
+		ctx->part->body_size.physical_size;
7bc0c9
+	bool full;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
+		return ret;
7bc0c9
+
7bc0c9
+	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
+		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
+		ctx->parse_next_block = preparsed_parse_body_finish;
7bc0c9
+	}
7bc0c9
+	ctx->skip = block_r->size;
7bc0c9
+	return 1;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_prologue_more(struct message_parser_ctx *ctx,
7bc0c9
+					 struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t boundary_min_start, end_offset;
7bc0c9
+	const unsigned char *cur;
7bc0c9
+	bool full;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	i_assert(ctx->part->children != NULL);
7bc0c9
+	end_offset = ctx->part->children->physical_pos;
7bc0c9
+
7bc0c9
+	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
+		return ret;
7bc0c9
+
7bc0c9
+	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
+		/* we've got the full prologue: clip off the initial boundary */
7bc0c9
+		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
+		cur = block_r->data + block_r->size - 1;
7bc0c9
+
7bc0c9
+		/* [\r]\n--boundary[\r]\n */ 
7bc0c9
+		if (block_r->size < 5 || *cur != '\n') {
7bc0c9
+			ctx->broken_reason = "Prologue boundary end not at expected position";
7bc0c9
+			return -1;
7bc0c9
+		}
7bc0c9
+		
7bc0c9
+		cur--;
7bc0c9
+		if (*cur == '\r') cur--;
7bc0c9
+
7bc0c9
+		/* find newline just before boundary */
7bc0c9
+		for (; cur >= block_r->data; cur--) {
7bc0c9
+			if (*cur == '\n') break;
7bc0c9
+		}
7bc0c9
+
7bc0c9
+		if (cur[0] != '\n' || cur[1] != '-' || cur[2] != '-') {
7bc0c9
+			ctx->broken_reason = "Prologue boundary beginning not at expected position";
7bc0c9
+			return -1;
7bc0c9
+		}
7bc0c9
+
7bc0c9
+		if (cur != block_r->data && cur[-1] == '\r') cur--;
7bc0c9
+
7bc0c9
+		/* clip boundary */
7bc0c9
+		block_r->size = cur - block_r->data;			
7bc0c9
+
7bc0c9
+		ctx->parse_next_block = preparsed_parse_prologue_finish;
7bc0c9
+		ctx->skip = block_r->size;
7bc0c9
+		return 1;
7bc0c9
+	}
7bc0c9
+		
7bc0c9
+	/* retain enough data in the stream buffer to contain initial boundary */
7bc0c9
+	if (end_offset > BOUNDARY_END_MAX_LEN)
7bc0c9
+		boundary_min_start = end_offset - BOUNDARY_END_MAX_LEN;
7bc0c9
+	else
7bc0c9
+		boundary_min_start = 0;
7bc0c9
+
7bc0c9
+	if (ctx->input->v_offset + block_r->size >= boundary_min_start) {
7bc0c9
+		if (boundary_min_start <= ctx->input->v_offset)
7bc0c9
+			return 0;
7bc0c9
+		block_r->size = boundary_min_start - ctx->input->v_offset;
7bc0c9
+	}
7bc0c9
+	ctx->skip = block_r->size;
7bc0c9
+	return 1;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_epilogue_more(struct message_parser_ctx *ctx,
7bc0c9
+					 struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
+		ctx->part->header_size.physical_size +
7bc0c9
+		ctx->part->body_size.physical_size;
7bc0c9
+	bool full;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
+		return ret;
7bc0c9
+
7bc0c9
+	if (ctx->input->v_offset + block_r->size >= end_offset) {
7bc0c9
+		block_r->size = end_offset - ctx->input->v_offset;
7bc0c9
+		ctx->parse_next_block = preparsed_parse_body_finish;
7bc0c9
+	}
7bc0c9
+	ctx->skip = block_r->size;
7bc0c9
+	return 1;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_epilogue_boundary(struct message_parser_ctx *ctx,
7bc0c9
+					     struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t end_offset = ctx->part->physical_pos +
7bc0c9
+		ctx->part->header_size.physical_size +
7bc0c9
+		ctx->part->body_size.physical_size;
7bc0c9
+	const unsigned char *data, *cur;
7bc0c9
+	size_t size;
7bc0c9
+	bool full;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	if (end_offset - ctx->input->v_offset < 7) {
7bc0c9
+		ctx->broken_reason = "Epilogue position is wrong";
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	if ((ret = message_parser_read_more(ctx, block_r, &full)) <= 0)
7bc0c9
+		return ret;
7bc0c9
+
7bc0c9
+	/* [\r]\n--boundary--[\r]\n */
7bc0c9
+	if (block_r->size < 7) {
7bc0c9
+		ctx->want_count = 7;
7bc0c9
+		return 0;
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	data = block_r->data;
7bc0c9
+	size = block_r->size;
7bc0c9
+	cur = data;
7bc0c9
+
7bc0c9
+	if (*cur == '\r') cur++;
7bc0c9
+
7bc0c9
+	if (cur[0] != '\n' || cur[1] != '-' || data[2] != '-') {
7bc0c9
+		ctx->broken_reason = "Epilogue boundary start not at expected position";
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	/* find the end of the line */
7bc0c9
+	cur += 3;
7bc0c9
+	if ((cur = memchr(cur, '\n', size - (cur-data))) == NULL) {
7bc0c9
+		if (end_offset < ctx->input->v_offset + size) {
7bc0c9
+			ctx->broken_reason = "Epilogue boundary end not at expected position";
7bc0c9
+			return -1;
7bc0c9
+		} else if (ctx->input->v_offset + size < end_offset &&
7bc0c9
+			   size < BOUNDARY_END_MAX_LEN &&
7bc0c9
+			   !ctx->input->eof && !full) {
7bc0c9
+			ctx->want_count = BOUNDARY_END_MAX_LEN;
7bc0c9
+			return 0;
7bc0c9
+		}
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	block_r->size = 0;
7bc0c9
+	ctx->parse_next_block = preparsed_parse_epilogue_more;
7bc0c9
+	ctx->skip = cur - data + 1;
7bc0c9
+	return 0;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_body_init(struct message_parser_ctx *ctx,
7bc0c9
+				     struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t offset = ctx->part->physical_pos +
7bc0c9
+		ctx->part->header_size.physical_size;
7bc0c9
+
7bc0c9
+	if (offset < ctx->input->v_offset) {
7bc0c9
+		/* header was actually larger than the cached size suggested */
7bc0c9
+		ctx->broken_reason = "Header larger than its cached size";
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+	i_stream_skip(ctx->input, offset - ctx->input->v_offset);
7bc0c9
+
7bc0c9
+	/* multipart messages may begin with --boundary--, which makes them
7bc0c9
+	   not have any children. */
7bc0c9
+	if ((ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) == 0 ||
7bc0c9
+	    ctx->part->children == NULL)
7bc0c9
+		ctx->parse_next_block = preparsed_parse_body_more;
7bc0c9
+	else
7bc0c9
+		ctx->parse_next_block = preparsed_parse_prologue_more;
7bc0c9
+	return ctx->parse_next_block(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_epilogue_init(struct message_parser_ctx *ctx,
7bc0c9
+					 struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	uoff_t offset = ctx->part->physical_pos +
7bc0c9
+		ctx->part->header_size.physical_size +
7bc0c9
+		ctx->part->body_size.physical_size;
7bc0c9
+
7bc0c9
+	ctx->part = ctx->part->parent;
7bc0c9
+
7bc0c9
+	if (offset < ctx->input->v_offset) {
7bc0c9
+		/* last child was actually larger than the cached size
7bc0c9
+		   suggested */
7bc0c9
+		ctx->broken_reason = "Part larger than its cached size";
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+	i_stream_skip(ctx->input, offset - ctx->input->v_offset);
7bc0c9
+
7bc0c9
+	ctx->parse_next_block = preparsed_parse_epilogue_boundary;
7bc0c9
+	return ctx->parse_next_block(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_finish_header(struct message_parser_ctx *ctx,
7bc0c9
+					 struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	if (ctx->part->children != NULL) {
7bc0c9
+		if ((ctx->part->flags & MESSAGE_PART_FLAG_MULTIPART) != 0 &&
7bc0c9
+		    (ctx->flags & MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS) != 0)
7bc0c9
+			ctx->parse_next_block = preparsed_parse_body_init;
7bc0c9
+		else {
7bc0c9
+			ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
+			ctx->part = ctx->part->children;
7bc0c9
+		}
7bc0c9
+	} else if ((ctx->flags & MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK) == 0) {
7bc0c9
+		ctx->parse_next_block = preparsed_parse_body_init;
7bc0c9
+	} else {
7bc0c9
+		preparsed_skip_to_next(ctx);
7bc0c9
+	}
7bc0c9
+	return ctx->parse_next_block(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_next_header(struct message_parser_ctx *ctx,
7bc0c9
+				       struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	struct message_header_line *hdr;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	ret = message_parse_header_next(ctx->hdr_parser_ctx, &hdr);
7bc0c9
+	if (ret == 0 || (ret < 0 && ctx->input->stream_errno != 0)) {
7bc0c9
+		ctx->want_count = i_stream_get_data_size(ctx->input) + 1;
7bc0c9
+		return ret;
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	if (hdr != NULL) {
7bc0c9
+		block_r->hdr = hdr;
7bc0c9
+		block_r->size = 0;
7bc0c9
+		return 1;
7bc0c9
+	}
7bc0c9
+	message_parse_header_deinit(&ctx->hdr_parser_ctx);
7bc0c9
+
7bc0c9
+	ctx->parse_next_block = preparsed_parse_finish_header;
7bc0c9
+
7bc0c9
+	/* return empty block as end of headers */
7bc0c9
+	block_r->hdr = NULL;
7bc0c9
+	block_r->size = 0;
7bc0c9
+
7bc0c9
+	i_assert(ctx->skip == 0);
7bc0c9
+	if (ctx->input->v_offset != ctx->part->physical_pos +
7bc0c9
+	    ctx->part->header_size.physical_size) {
7bc0c9
+		ctx->broken_reason = "Cached header size mismatch";
7bc0c9
+		return -1;
7bc0c9
+	}
7bc0c9
+	return 1;
7bc0c9
+}
7bc0c9
+
7bc0c9
+static int preparsed_parse_next_header_init(struct message_parser_ctx *ctx,
7bc0c9
+					    struct message_block *block_r)
7bc0c9
+{
7bc0c9
+	struct istream *hdr_input;
7bc0c9
+
7bc0c9
+	i_assert(ctx->hdr_parser_ctx == NULL);
7bc0c9
+
7bc0c9
+	i_assert(ctx->part->physical_pos >= ctx->input->v_offset);
7bc0c9
+	i_stream_skip(ctx->input, ctx->part->physical_pos -
7bc0c9
+		      ctx->input->v_offset);
7bc0c9
+
7bc0c9
+	/* the header may become truncated by --boundaries. limit the header
7bc0c9
+	   stream's size to what it's supposed to be to avoid duplicating (and
7bc0c9
+	   keeping in sync!) all the same complicated logic as in
7bc0c9
+	   parse_next_header(). */
7bc0c9
+	hdr_input = i_stream_create_limit(ctx->input, ctx->part->header_size.physical_size);
7bc0c9
+	ctx->hdr_parser_ctx =
7bc0c9
+		message_parse_header_init(hdr_input, NULL, ctx->hdr_flags);
7bc0c9
+	i_stream_unref(&hdr_input);
7bc0c9
+
7bc0c9
+	ctx->parse_next_block = preparsed_parse_next_header;
7bc0c9
+	return preparsed_parse_next_header(ctx, block_r);
7bc0c9
+}
7bc0c9
+
7bc0c9
+struct message_parser_ctx *
7bc0c9
+message_parser_init_from_parts(struct message_part *parts,
7bc0c9
+			       struct istream *input,
7bc0c9
+			       const struct message_parser_settings *set)
7bc0c9
+{
7bc0c9
+	struct message_parser_ctx *ctx;
7bc0c9
+
7bc0c9
+	i_assert(parts != NULL);
7bc0c9
+
7bc0c9
+	ctx = message_parser_init_int(input, set);
7bc0c9
+	ctx->preparsed = TRUE;
7bc0c9
+	ctx->parts = ctx->part = parts;
7bc0c9
+	ctx->parse_next_block = preparsed_parse_next_header_init;
7bc0c9
+	return ctx;
7bc0c9
+}
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-parser.h.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-parser.h
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-parser.h.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-parser.h	2020-08-07 12:29:39.978827444 +0200
7bc0c9
@@ -17,6 +17,21 @@ enum message_parser_flags {
7bc0c9
 	MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES		= 0x08
7bc0c9
 };
7bc0c9
 
7bc0c9
+#define MESSAGE_PARSER_DEFAULT_MAX_NESTED_MIME_PARTS 100
7bc0c9
+#define MESSAGE_PARSER_DEFAULT_MAX_TOTAL_MIME_PARTS 10000
7bc0c9
+
7bc0c9
+struct message_parser_settings {
7bc0c9
+	enum message_header_parser_flags hdr_flags;
7bc0c9
+	enum message_parser_flags flags;
7bc0c9
+
7bc0c9
+	/* Maximum nested MIME parts.
7bc0c9
+	   0 = MESSAGE_PARSER_DEFAULT_MAX_NESTED_MIME_PARTS. */
7bc0c9
+	unsigned int max_nested_mime_parts;
7bc0c9
+	/* Maximum MIME parts in total.
7bc0c9
+	   0 = MESSAGE_PARSER_DEFAULT_MAX_TOTAL_MIME_PARTS. */
7bc0c9
+	unsigned int max_total_mime_parts;
7bc0c9
+};
7bc0c9
+
7bc0c9
 struct message_parser_ctx;
7bc0c9
 
7bc0c9
 struct message_block {
7bc0c9
@@ -45,8 +60,7 @@ extern message_part_header_callback_t *n
7bc0c9
    are allocated from. */
7bc0c9
 struct message_parser_ctx *
7bc0c9
 message_parser_init(pool_t part_pool, struct istream *input,
7bc0c9
-		    enum message_header_parser_flags hdr_flags,
7bc0c9
-		    enum message_parser_flags flags);
7bc0c9
+		    const struct message_parser_settings *set);
7bc0c9
 /* Deinitialize message parser. The ctx must NOT have been created by
7bc0c9
    message_parser_init_from_parts(). */
7bc0c9
 void message_parser_deinit(struct message_parser_ctx **ctx,
7bc0c9
@@ -55,8 +69,7 @@ void message_parser_deinit(struct messag
7bc0c9
 struct message_parser_ctx *
7bc0c9
 message_parser_init_from_parts(struct message_part *parts,
7bc0c9
 			       struct istream *input,
7bc0c9
-			       enum message_header_parser_flags hdr_flags,
7bc0c9
-			       enum message_parser_flags flags);
7bc0c9
+			       const struct message_parser_settings *set);
7bc0c9
 /* Same as message_parser_deinit(), but return an error message describing
7bc0c9
    why the preparsed parts didn't match the message. This can also safely be
7bc0c9
    called even when preparsed parts weren't used - it'll always just return
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-parser-private.h.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-parser-private.h
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-parser-private.h.CVE_2020_12100	2020-08-07 12:29:39.972827528 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-parser-private.h	2020-08-07 12:29:39.978827444 +0200
7bc0c9
@@ -0,0 +1,62 @@
7bc0c9
+#ifndef MESSAGE_PARSER_PRIVATE_H
7bc0c9
+#define MESSAGE_PARSER_PRIVATE_H
7bc0c9
+
7bc0c9
+#include "message-parser.h"
7bc0c9
+
7bc0c9
+/* RFC-2046 requires boundaries are max. 70 chars + "--" prefix + "--" suffix.
7bc0c9
+   We'll add a bit more just in case. */
7bc0c9
+#define BOUNDARY_STRING_MAX_LEN (70 + 10)
7bc0c9
+#define BOUNDARY_END_MAX_LEN (BOUNDARY_STRING_MAX_LEN + 2 + 2)
7bc0c9
+
7bc0c9
+struct message_boundary {
7bc0c9
+	struct message_boundary *next;
7bc0c9
+
7bc0c9
+	struct message_part *part;
7bc0c9
+	char *boundary;
7bc0c9
+	size_t len;
7bc0c9
+
7bc0c9
+	bool epilogue_found:1;
7bc0c9
+};
7bc0c9
+
7bc0c9
+struct message_parser_ctx {
7bc0c9
+	pool_t part_pool;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts, *part;
7bc0c9
+	const char *broken_reason;
7bc0c9
+	unsigned int nested_parts_count;
7bc0c9
+	unsigned int total_parts_count;
7bc0c9
+
7bc0c9
+	enum message_header_parser_flags hdr_flags;
7bc0c9
+	enum message_parser_flags flags;
7bc0c9
+	unsigned int max_nested_mime_parts;
7bc0c9
+	unsigned int max_total_mime_parts;
7bc0c9
+
7bc0c9
+	char *last_boundary;
7bc0c9
+	struct message_boundary *boundaries;
7bc0c9
+
7bc0c9
+	struct message_part **next_part;
7bc0c9
+	ARRAY(struct message_part **) next_part_stack;
7bc0c9
+
7bc0c9
+	size_t skip;
7bc0c9
+	char last_chr;
7bc0c9
+	unsigned int want_count;
7bc0c9
+
7bc0c9
+	struct message_header_parser_ctx *hdr_parser_ctx;
7bc0c9
+	unsigned int prev_hdr_newline_size;
7bc0c9
+
7bc0c9
+	int (*parse_next_block)(struct message_parser_ctx *ctx,
7bc0c9
+				struct message_block *block_r);
7bc0c9
+
7bc0c9
+	bool part_seen_content_type:1;
7bc0c9
+	bool multipart:1;
7bc0c9
+	bool preparsed:1;
7bc0c9
+	bool eof:1;
7bc0c9
+};
7bc0c9
+
7bc0c9
+struct message_parser_ctx *
7bc0c9
+message_parser_init_int(struct istream *input,
7bc0c9
+			const struct message_parser_settings *set);
7bc0c9
+int message_parser_read_more(struct message_parser_ctx *ctx,
7bc0c9
+			     struct message_block *block_r, bool *full_r);
7bc0c9
+
7bc0c9
+#endif
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-search.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-search.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-search.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-search.c	2020-08-07 12:29:39.976827472 +0200
7bc0c9
@@ -196,8 +196,9 @@ message_search_msg_real(struct message_s
7bc0c9
 			struct istream *input, struct message_part *parts,
7bc0c9
 			const char **error_r)
7bc0c9
 {
7bc0c9
-	const enum message_header_parser_flags hdr_parser_flags =
7bc0c9
-		MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE;
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE,
7bc0c9
+	};
7bc0c9
 	struct message_parser_ctx *parser_ctx;
7bc0c9
 	struct message_block raw_block;
7bc0c9
 	struct message_part *new_parts;
7bc0c9
@@ -207,10 +208,10 @@ message_search_msg_real(struct message_s
7bc0c9
 
7bc0c9
 	if (parts != NULL) {
7bc0c9
 		parser_ctx = message_parser_init_from_parts(parts,
7bc0c9
-						input, hdr_parser_flags, 0);
7bc0c9
+						input, &parser_set);
7bc0c9
 	} else {
7bc0c9
 		parser_ctx = message_parser_init(pool_datastack_create(),
7bc0c9
-						 input, hdr_parser_flags, 0);
7bc0c9
+						 input, &parser_set);
7bc0c9
 	}
7bc0c9
 
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser_ctx,
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/message-snippet.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/message-snippet.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/message-snippet.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/message-snippet.c	2020-08-07 12:30:01.710525318 +0200
7bc0c9
@@ -99,6 +99,7 @@ int message_snippet_generate(struct istr
7bc0c9
 			     unsigned int max_snippet_chars,
7bc0c9
 			     string_t *snippet)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = { .flags = 0 };
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct message_part *parts;
7bc0c9
 	struct message_decoder_context *decoder;
7bc0c9
@@ -112,7 +113,7 @@ int message_snippet_generate(struct istr
7bc0c9
 	ctx.snippet = snippet;
7bc0c9
 	ctx.chars_left = max_snippet_chars;
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool_datastack_create(), input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool_datastack_create(), input, &parser_set);
7bc0c9
 	decoder = message_decoder_init(NULL, 0);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &raw_block)) > 0) {
7bc0c9
 		if (!message_decoder_decode_next_block(decoder, &raw_block, &block))
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/test-message-decoder.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/test-message-decoder.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/test-message-decoder.c.CVE_2020_12100	2020-08-07 12:29:39.970827555 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/test-message-decoder.c	2020-08-07 12:29:39.976827472 +0200
7bc0c9
@@ -105,6 +105,7 @@ static void test_message_decoder_multipa
7bc0c9
 		"\n"
7bc0c9
 		"?garbage\n"
7bc0c9
 		"--foo--\n";
7bc0c9
+	const struct message_parser_settings parser_set = { .flags = 0, };
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct message_decoder_context *decoder;
7bc0c9
 	struct message_part *parts;
7bc0c9
@@ -116,7 +117,8 @@ static void test_message_decoder_multipa
7bc0c9
 	test_begin("message decoder multipart");
7bc0c9
 
7bc0c9
 	istream = test_istream_create(test_message_input);
7bc0c9
-	parser = message_parser_init(pool_datastack_create(), istream, 0, 0);
7bc0c9
+	parser = message_parser_init(pool_datastack_create(), istream,
7bc0c9
+				     &parser_set);
7bc0c9
 	decoder = message_decoder_init(NULL, 0);
7bc0c9
 
7bc0c9
 	test_istream_set_allow_eof(istream, FALSE);
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/test-message-parser.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/test-message-parser.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/test-message-parser.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/test-message-parser.c	2020-08-07 12:29:39.979827431 +0200
7bc0c9
@@ -39,6 +39,8 @@ static const char test_msg[] =
7bc0c9
 "\n";
7bc0c9
 #define TEST_MSG_LEN (sizeof(test_msg)-1)
7bc0c9
 
7bc0c9
+static const struct message_parser_settings set_empty = { .flags = 0 };
7bc0c9
+
7bc0c9
 static bool msg_parts_cmp(struct message_part *p1, struct message_part *p2)
7bc0c9
 {
7bc0c9
 	while (p1 != NULL || p2 != NULL) {
7bc0c9
@@ -59,6 +61,7 @@ static bool msg_parts_cmp(struct message
7bc0c9
 		    p1->body_size.physical_size != p2->body_size.physical_size ||
7bc0c9
 		    p1->body_size.virtual_size != p2->body_size.virtual_size ||
7bc0c9
 		    p1->body_size.lines != p2->body_size.lines ||
7bc0c9
+		    p1->children_count != p2->children_count ||
7bc0c9
 		    p1->flags != p2->flags)
7bc0c9
 			return FALSE;
7bc0c9
 
7bc0c9
@@ -70,6 +73,9 @@ static bool msg_parts_cmp(struct message
7bc0c9
 
7bc0c9
 static void test_parsed_parts(struct istream *input, struct message_part *parts)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK,
7bc0c9
+	};
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct message_block block;
7bc0c9
 	struct message_part *parts2;
7bc0c9
@@ -81,8 +87,7 @@ static void test_parsed_parts(struct ist
7bc0c9
 	if (i_stream_get_size(input, TRUE, &input_size) < 0)
7bc0c9
 		i_unreached();
7bc0c9
 
7bc0c9
-	parser = message_parser_init_from_parts(parts, input, 0,
7bc0c9
-					MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK);
7bc0c9
+	parser = message_parser_init_from_parts(parts, input, &parser_set);
7bc0c9
 	for (i = 1; i <= input_size*2+1; i++) {
7bc0c9
 		test_istream_set_size(input, i/2);
7bc0c9
 		if (i > TEST_MSG_LEN*2)
7bc0c9
@@ -112,9 +117,11 @@ static void test_message_parser_small_bl
7bc0c9
 	output = t_str_new(128);
7bc0c9
 
7bc0c9
 	/* full parsing */
7bc0c9
-	parser = message_parser_init(pool, input, 0,
7bc0c9
-		MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
-		MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES);
7bc0c9
+	const struct message_parser_settings full_parser_set = {
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_INCLUDE_MULTIPART_BLOCKS |
7bc0c9
+			MESSAGE_PARSER_FLAG_INCLUDE_BOUNDARIES,
7bc0c9
+	};
7bc0c9
+	parser = message_parser_init(pool, input, &full_parser_set);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
7bc0c9
 		if (block.hdr != NULL)
7bc0c9
 			message_header_line_write(output, block.hdr);
7bc0c9
@@ -130,7 +137,7 @@ static void test_message_parser_small_bl
7bc0c9
 	i_stream_seek(input, 0);
7bc0c9
 	test_istream_set_allow_eof(input, FALSE);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	for (i = 1; i <= TEST_MSG_LEN*2+1; i++) {
7bc0c9
 		test_istream_set_size(input, i/2);
7bc0c9
 		if (i > TEST_MSG_LEN*2)
7bc0c9
@@ -148,8 +155,11 @@ static void test_message_parser_small_bl
7bc0c9
 	test_istream_set_allow_eof(input, FALSE);
7bc0c9
 
7bc0c9
 	end_of_headers_idx = (strstr(test_msg, "\n-----") - test_msg);
7bc0c9
-	parser = message_parser_init_from_parts(parts, input, 0,
7bc0c9
-					MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK);
7bc0c9
+	const struct message_parser_settings preparsed_parser_set = {
7bc0c9
+		.flags = MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK,
7bc0c9
+	};
7bc0c9
+	parser = message_parser_init_from_parts(parts, input,
7bc0c9
+						&preparsed_parser_set);
7bc0c9
 	for (i = 1; i <= TEST_MSG_LEN*2+1; i++) {
7bc0c9
 		test_istream_set_size(input, i/2);
7bc0c9
 		if (i > TEST_MSG_LEN*2)
7bc0c9
@@ -167,6 +177,36 @@ static void test_message_parser_small_bl
7bc0c9
 	test_end();
7bc0c9
 }
7bc0c9
 
7bc0c9
+static void test_message_parser_stop_early(void)
7bc0c9
+{
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts;
7bc0c9
+	struct message_block block;
7bc0c9
+	unsigned int i;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser in stop early");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(test_msg);
7bc0c9
+
7bc0c9
+	test_istream_set_allow_eof(input, FALSE);
7bc0c9
+	for (i = 1; i <= TEST_MSG_LEN+1; i++) {
7bc0c9
+		i_stream_seek(input, 0);
7bc0c9
+		test_istream_set_size(input, i);
7bc0c9
+		parser = message_parser_init(pool, input, &set_empty);
7bc0c9
+		while ((ret = message_parser_parse_next_block(parser,
7bc0c9
+							      &block)) > 0) ;
7bc0c9
+		test_assert(ret == 0);
7bc0c9
+		message_parser_deinit(&parser, &parts;;
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
 static void test_message_parser_truncated_mime_headers(void)
7bc0c9
 {
7bc0c9
 static const char input_msg[] =
7bc0c9
@@ -191,12 +231,13 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
 	test_assert((parts->flags & MESSAGE_PART_FLAG_MULTIPART) != 0);
7bc0c9
+	test_assert(parts->children_count == 4);
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 48);
7bc0c9
 	test_assert(parts->header_size.virtual_size == 48+2);
7bc0c9
@@ -220,6 +261,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->children->next->next->next->header_size.virtual_size == 23);
7bc0c9
 	test_assert(parts->children->next->next->next->header_size.lines == 0);
7bc0c9
 	for (part = parts->children; part != NULL; part = part->next) {
7bc0c9
+		test_assert(part->children_count == 0);
7bc0c9
 		test_assert(part->body_size.physical_size == 0);
7bc0c9
 		test_assert(part->body_size.virtual_size == 0);
7bc0c9
 	}
7bc0c9
@@ -254,12 +296,13 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(parts->children_count == 2);
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 46);
7bc0c9
 	test_assert(parts->header_size.virtual_size == 46+2);
7bc0c9
@@ -267,6 +310,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->body_size.physical_size == 86);
7bc0c9
 	test_assert(parts->body_size.virtual_size == 86+8);
7bc0c9
 
7bc0c9
+	test_assert(parts->children->children_count == 0);
7bc0c9
 	test_assert(parts->children->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->physical_pos == 51);
7bc0c9
 	test_assert(parts->children->header_size.lines == 1);
7bc0c9
@@ -276,6 +320,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->children->body_size.physical_size == 0);
7bc0c9
 	test_assert(parts->children->children == NULL);
7bc0c9
 
7bc0c9
+	test_assert(parts->children->next->children_count == 0);
7bc0c9
 	test_assert(parts->children->next->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->next->physical_pos == 101);
7bc0c9
 	test_assert(parts->children->next->header_size.lines == 2);
7bc0c9
@@ -307,11 +352,12 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
+	test_assert(parts->children_count == 0);
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->header_size.lines == 1);
7bc0c9
 	test_assert(parts->header_size.physical_size == 45);
7bc0c9
@@ -344,11 +390,12 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
+	test_assert(parts->children_count == 0);
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 46);
7bc0c9
@@ -388,11 +435,12 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
+	test_assert(parts->children_count == 2);
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 45);
7bc0c9
@@ -400,6 +448,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->body_size.lines == 7);
7bc0c9
 	test_assert(parts->body_size.physical_size == 84);
7bc0c9
 	test_assert(parts->body_size.virtual_size == 84+7);
7bc0c9
+	test_assert(parts->children->children_count == 1);
7bc0c9
 	test_assert(parts->children->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->physical_pos == 49);
7bc0c9
 	test_assert(parts->children->header_size.lines == 2);
7bc0c9
@@ -408,6 +457,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->children->body_size.lines == 4);
7bc0c9
 	test_assert(parts->children->body_size.physical_size == 35);
7bc0c9
 	test_assert(parts->children->body_size.virtual_size == 35+4);
7bc0c9
+	test_assert(parts->children->children->children_count == 0);
7bc0c9
 	test_assert(parts->children->children->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->children->physical_pos == 98);
7bc0c9
 	test_assert(parts->children->children->header_size.lines == 2);
7bc0c9
@@ -446,11 +496,12 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
+	test_assert(parts->children_count == 2);
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 45);
7bc0c9
@@ -458,6 +509,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->body_size.lines == 7);
7bc0c9
 	test_assert(parts->body_size.physical_size == 86);
7bc0c9
 	test_assert(parts->body_size.virtual_size == 86+7);
7bc0c9
+	test_assert(parts->children->children_count == 1);
7bc0c9
 	test_assert(parts->children->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->physical_pos == 50);
7bc0c9
 	test_assert(parts->children->header_size.lines == 2);
7bc0c9
@@ -466,6 +518,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->children->body_size.lines == 4);
7bc0c9
 	test_assert(parts->children->body_size.physical_size == 36);
7bc0c9
 	test_assert(parts->children->body_size.virtual_size == 36+4);
7bc0c9
+	test_assert(parts->children->children->children_count == 0);
7bc0c9
 	test_assert(parts->children->children->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->children->physical_pos == 100);
7bc0c9
 	test_assert(parts->children->children->header_size.lines == 2);
7bc0c9
@@ -481,6 +534,51 @@ static const char input_msg[] =
7bc0c9
 	test_end();
7bc0c9
 }
7bc0c9
 
7bc0c9
+static void test_message_parser_trailing_dashes(void)
7bc0c9
+{
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"a--\"\n"
7bc0c9
+"\n"
7bc0c9
+"--a--\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"a----\"\n"
7bc0c9
+"\n"
7bc0c9
+"--a----\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"body\n"
7bc0c9
+"--a------\n"
7bc0c9
+"Content-Type: text/html\n"
7bc0c9
+"\n"
7bc0c9
+"body2\n"
7bc0c9
+"--a----";
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser trailing dashes");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	test_assert(parts->children_count == 2);
7bc0c9
+	test_assert(parts->children->next == NULL);
7bc0c9
+	test_assert(parts->children->children_count == 1);
7bc0c9
+	test_assert(parts->children->children->next == NULL);
7bc0c9
+	test_assert(parts->children->children->children_count == 0);
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
 static void test_message_parser_continuing_mime_boundary(void)
7bc0c9
 {
7bc0c9
 static const char input_msg[] =
7bc0c9
@@ -504,11 +602,12 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
+	test_assert(parts->children_count == 2);
7bc0c9
 	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->header_size.lines == 2);
7bc0c9
 	test_assert(parts->header_size.physical_size == 45);
7bc0c9
@@ -516,6 +615,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->body_size.lines == 7);
7bc0c9
 	test_assert(parts->body_size.physical_size == 86);
7bc0c9
 	test_assert(parts->body_size.virtual_size == 86+7);
7bc0c9
+	test_assert(parts->children->children_count == 1);
7bc0c9
 	test_assert(parts->children->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->physical_pos == 49);
7bc0c9
 	test_assert(parts->children->header_size.lines == 2);
7bc0c9
@@ -524,6 +624,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(parts->children->body_size.lines == 4);
7bc0c9
 	test_assert(parts->children->body_size.physical_size == 36);
7bc0c9
 	test_assert(parts->children->body_size.virtual_size == 36+4);
7bc0c9
+	test_assert(parts->children->children->children_count == 0);
7bc0c9
 	test_assert(parts->children->children->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(parts->children->children->physical_pos == 100);
7bc0c9
 	test_assert(parts->children->children->header_size.lines == 2);
7bc0c9
@@ -563,12 +664,13 @@ static const char input_msg[] =
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
 	test_assert(ret < 0);
7bc0c9
 	message_parser_deinit(&parser, &parts;;
7bc0c9
 
7bc0c9
 	part = parts;
7bc0c9
+	test_assert(part->children_count == 3);
7bc0c9
 	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(part->header_size.lines == 2);
7bc0c9
 	test_assert(part->header_size.physical_size == 45);
7bc0c9
@@ -578,6 +680,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(part->body_size.virtual_size == 112+9);
7bc0c9
 
7bc0c9
 	part = parts->children;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
 	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(part->physical_pos == 49);
7bc0c9
 	test_assert(part->header_size.lines == 1);
7bc0c9
@@ -591,6 +694,7 @@ static const char input_msg[] =
7bc0c9
 	   we could make it, but it would complicate the message-parser even
7bc0c9
 	   more. */
7bc0c9
 	part = parts->children->next;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
 	test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(part->physical_pos == 117);
7bc0c9
 	test_assert(part->header_size.lines == 1);
7bc0c9
@@ -601,6 +705,7 @@ static const char input_msg[] =
7bc0c9
 	test_assert(part->children == NULL);
7bc0c9
 
7bc0c9
 	part = parts->children->next->next;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
 	test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
 	test_assert(part->header_size.lines == 0);
7bc0c9
 	test_assert(part->header_size.physical_size == 0);
7bc0c9
@@ -615,6 +720,80 @@ static const char input_msg[] =
7bc0c9
 	test_end();
7bc0c9
 }
7bc0c9
 
7bc0c9
+static void test_message_parser_continuing_mime_boundary_reverse(void)
7bc0c9
+{
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"ab\"\n"
7bc0c9
+"\n"
7bc0c9
+"--ab\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"a\"\n"
7bc0c9
+"\n"
7bc0c9
+"--a\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"body\n"
7bc0c9
+"--ab\n"
7bc0c9
+"Content-Type: text/html\n"
7bc0c9
+"\n"
7bc0c9
+"body2\n";
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser continuing mime boundary reverse");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	test_assert(parts->children_count == 3);
7bc0c9
+	test_assert(parts->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(parts->header_size.lines == 2);
7bc0c9
+	test_assert(parts->header_size.physical_size == 46);
7bc0c9
+	test_assert(parts->header_size.virtual_size == 46+2);
7bc0c9
+	test_assert(parts->body_size.lines == 11);
7bc0c9
+	test_assert(parts->body_size.physical_size == 121);
7bc0c9
+	test_assert(parts->body_size.virtual_size == 121+11);
7bc0c9
+	test_assert(parts->children->children_count == 1);
7bc0c9
+	test_assert(parts->children->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(parts->children->physical_pos == 51);
7bc0c9
+	test_assert(parts->children->header_size.lines == 2);
7bc0c9
+	test_assert(parts->children->header_size.physical_size == 45);
7bc0c9
+	test_assert(parts->children->header_size.virtual_size == 45+2);
7bc0c9
+	test_assert(parts->children->body_size.lines == 3);
7bc0c9
+	test_assert(parts->children->body_size.physical_size == 34);
7bc0c9
+	test_assert(parts->children->body_size.virtual_size == 34+3);
7bc0c9
+	test_assert(parts->children->children->children_count == 0);
7bc0c9
+	test_assert(parts->children->children->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(parts->children->children->physical_pos == 100);
7bc0c9
+	test_assert(parts->children->children->header_size.lines == 2);
7bc0c9
+	test_assert(parts->children->children->header_size.physical_size == 26);
7bc0c9
+	test_assert(parts->children->children->header_size.virtual_size == 26+2);
7bc0c9
+	test_assert(parts->children->children->body_size.lines == 0);
7bc0c9
+	test_assert(parts->children->children->body_size.physical_size == 4);
7bc0c9
+	test_assert(parts->children->children->body_size.virtual_size == 4);
7bc0c9
+	test_assert(parts->children->next->children_count == 0);
7bc0c9
+	test_assert(parts->children->next->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(parts->children->next->physical_pos == 136);
7bc0c9
+	test_assert(parts->children->next->header_size.lines == 2);
7bc0c9
+	test_assert(parts->children->next->header_size.physical_size == 25);
7bc0c9
+	test_assert(parts->children->next->header_size.virtual_size == 25+2);
7bc0c9
+	test_assert(parts->children->next->body_size.lines == 1);
7bc0c9
+	test_assert(parts->children->next->body_size.physical_size == 6);
7bc0c9
+	test_assert(parts->children->next->body_size.virtual_size == 6+1);
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
 static void test_message_parser_no_eoh(void)
7bc0c9
 {
7bc0c9
 	static const char input_msg[] = "a:b\n";
7bc0c9
@@ -628,7 +807,7 @@ static void test_message_parser_no_eoh(v
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = test_istream_create(input_msg);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
 	test_assert(message_parser_parse_next_block(parser, &block) > 0 &&
7bc0c9
 		    block.hdr != NULL && strcmp(block.hdr->name, "a") == 0 &&
7bc0c9
 		    block.hdr->value_len == 1 && block.hdr->value[0] == 'b');
7bc0c9
@@ -643,19 +822,335 @@ static void test_message_parser_no_eoh(v
7bc0c9
 	test_end();
7bc0c9
 }
7bc0c9
 
7bc0c9
+static void test_message_parser_long_mime_boundary(void)
7bc0c9
+{
7bc0c9
+	/* Close the boundaries in wrong reverse order. But because all
7bc0c9
+	   boundaries are actually truncated to the same size (..890) it
7bc0c9
+	   works the same as if all of them were duplicate boundaries. */
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"1234567890123456789012345678901234567890123456789012345678901234567890123456789012\"\n"
7bc0c9
+"\n"
7bc0c9
+"--1234567890123456789012345678901234567890123456789012345678901234567890123456789012\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"123456789012345678901234567890123456789012345678901234567890123456789012345678901\"\n"
7bc0c9
+"\n"
7bc0c9
+"--123456789012345678901234567890123456789012345678901234567890123456789012345678901\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"12345678901234567890123456789012345678901234567890123456789012345678901234567890\"\n"
7bc0c9
+"\n"
7bc0c9
+"--12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"1\n"
7bc0c9
+"--1234567890123456789012345678901234567890123456789012345678901234567890123456789012\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"22\n"
7bc0c9
+"--123456789012345678901234567890123456789012345678901234567890123456789012345678901\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"333\n"
7bc0c9
+"--12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"4444\n";
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts, *part;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser long mime boundary");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &set_empty);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	part = parts;
7bc0c9
+	test_assert(part->children_count == 6);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 126);
7bc0c9
+	test_assert(part->header_size.virtual_size == 126+2);
7bc0c9
+	test_assert(part->body_size.lines == 22);
7bc0c9
+	test_assert(part->body_size.physical_size == 871);
7bc0c9
+	test_assert(part->body_size.virtual_size == 871+22);
7bc0c9
+
7bc0c9
+	part = parts->children;
7bc0c9
+	test_assert(part->children_count == 5);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 125);
7bc0c9
+	test_assert(part->header_size.virtual_size == 125+2);
7bc0c9
+	test_assert(part->body_size.lines == 19);
7bc0c9
+	test_assert(part->body_size.physical_size == 661);
7bc0c9
+	test_assert(part->body_size.virtual_size == 661+19);
7bc0c9
+
7bc0c9
+	part = parts->children->children;
7bc0c9
+	test_assert(part->children_count == 4);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 124);
7bc0c9
+	test_assert(part->header_size.virtual_size == 124+2);
7bc0c9
+	test_assert(part->body_size.lines == 16);
7bc0c9
+	test_assert(part->body_size.physical_size == 453);
7bc0c9
+	test_assert(part->body_size.virtual_size == 453+16);
7bc0c9
+
7bc0c9
+	part = parts->children->children->children;
7bc0c9
+	for (unsigned int i = 1; i <= 3; i++, part = part->next) {
7bc0c9
+		test_assert(part->children_count == 0);
7bc0c9
+		test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+		test_assert(part->header_size.lines == 2);
7bc0c9
+		test_assert(part->header_size.physical_size == 26);
7bc0c9
+		test_assert(part->header_size.virtual_size == 26+2);
7bc0c9
+		test_assert(part->body_size.lines == 0);
7bc0c9
+		test_assert(part->body_size.physical_size == i);
7bc0c9
+		test_assert(part->body_size.virtual_size == i);
7bc0c9
+	}
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void test_message_parser_mime_part_nested_limit(void)
7bc0c9
+{
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"1\"\n"
7bc0c9
+"\n"
7bc0c9
+"--1\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"2\"\n"
7bc0c9
+"\n"
7bc0c9
+"--2\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"1\n"
7bc0c9
+"--2\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"22\n"
7bc0c9
+"--1\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"333\n";
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.max_nested_mime_parts = 2,
7bc0c9
+	};
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts, *part;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser mime part nested limit");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &parser_set);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	part = parts;
7bc0c9
+	test_assert(part->children_count == 2);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 45);
7bc0c9
+	test_assert(part->header_size.virtual_size == 45+2);
7bc0c9
+	test_assert(part->body_size.lines == 15);
7bc0c9
+	test_assert(part->body_size.physical_size == 148);
7bc0c9
+	test_assert(part->body_size.virtual_size == 148+15);
7bc0c9
+
7bc0c9
+	part = parts->children;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
+	test_assert(part->flags == MESSAGE_PART_FLAG_IS_MIME);
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 45);
7bc0c9
+	test_assert(part->header_size.virtual_size == 45+2);
7bc0c9
+	test_assert(part->body_size.lines == 7);
7bc0c9
+	test_assert(part->body_size.physical_size == 64);
7bc0c9
+	test_assert(part->body_size.virtual_size == 64+7);
7bc0c9
+
7bc0c9
+	part = parts->children->next;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 26);
7bc0c9
+	test_assert(part->header_size.virtual_size == 26+2);
7bc0c9
+	test_assert(part->body_size.lines == 1);
7bc0c9
+	test_assert(part->body_size.physical_size == 4);
7bc0c9
+	test_assert(part->body_size.virtual_size == 4+1);
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void test_message_parser_mime_part_nested_limit_rfc822(void)
7bc0c9
+{
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: message/rfc822\n"
7bc0c9
+"\n"
7bc0c9
+"Content-Type: message/rfc822\n"
7bc0c9
+"\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"1\n";
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.max_nested_mime_parts = 2,
7bc0c9
+	};
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts, *part;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser mime part nested limit rfc822");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &parser_set);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	part = parts;
7bc0c9
+	test_assert(part->children_count == 1);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MESSAGE_RFC822 | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 30);
7bc0c9
+	test_assert(part->header_size.virtual_size == 30+2);
7bc0c9
+	test_assert(part->body_size.lines == 5);
7bc0c9
+	test_assert(part->body_size.physical_size == 58);
7bc0c9
+	test_assert(part->body_size.virtual_size == 58+5);
7bc0c9
+
7bc0c9
+	part = parts->children;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
+	test_assert(part->flags == MESSAGE_PART_FLAG_IS_MIME);
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 30);
7bc0c9
+	test_assert(part->header_size.virtual_size == 30+2);
7bc0c9
+	test_assert(part->body_size.lines == 3);
7bc0c9
+	test_assert(part->body_size.physical_size == 28);
7bc0c9
+	test_assert(part->body_size.virtual_size == 28+3);
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
+static void test_message_parser_mime_part_limit(void)
7bc0c9
+{
7bc0c9
+static const char input_msg[] =
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"1\"\n"
7bc0c9
+"\n"
7bc0c9
+"--1\n"
7bc0c9
+"Content-Type: multipart/mixed; boundary=\"2\"\n"
7bc0c9
+"\n"
7bc0c9
+"--2\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"1\n"
7bc0c9
+"--2\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"22\n"
7bc0c9
+"--1\n"
7bc0c9
+"Content-Type: text/plain\n"
7bc0c9
+"\n"
7bc0c9
+"333\n";
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.max_total_mime_parts = 4,
7bc0c9
+	};
7bc0c9
+	struct message_parser_ctx *parser;
7bc0c9
+	struct istream *input;
7bc0c9
+	struct message_part *parts, *part;
7bc0c9
+	struct message_block block;
7bc0c9
+	pool_t pool;
7bc0c9
+	int ret;
7bc0c9
+
7bc0c9
+	test_begin("message parser mime part limit");
7bc0c9
+	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
+	input = test_istream_create(input_msg);
7bc0c9
+
7bc0c9
+	parser = message_parser_init(pool, input, &parser_set);
7bc0c9
+	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) ;
7bc0c9
+	test_assert(ret < 0);
7bc0c9
+	message_parser_deinit(&parser, &parts;;
7bc0c9
+
7bc0c9
+	part = parts;
7bc0c9
+	test_assert(part->children_count == 3);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 45);
7bc0c9
+	test_assert(part->header_size.virtual_size == 45+2);
7bc0c9
+	test_assert(part->body_size.lines == 15);
7bc0c9
+	test_assert(part->body_size.physical_size == 148);
7bc0c9
+	test_assert(part->body_size.virtual_size == 148+15);
7bc0c9
+
7bc0c9
+	part = parts->children;
7bc0c9
+	test_assert(part->children_count == 2);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_MULTIPART | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 45);
7bc0c9
+	test_assert(part->header_size.virtual_size == 45+2);
7bc0c9
+	test_assert(part->body_size.lines == 12);
7bc0c9
+	test_assert(part->body_size.physical_size == 99);
7bc0c9
+	test_assert(part->body_size.virtual_size == 99+12);
7bc0c9
+
7bc0c9
+	part = parts->children->children;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 26);
7bc0c9
+	test_assert(part->header_size.virtual_size == 26+2);
7bc0c9
+	test_assert(part->body_size.lines == 0);
7bc0c9
+	test_assert(part->body_size.physical_size == 1);
7bc0c9
+	test_assert(part->body_size.virtual_size == 1);
7bc0c9
+
7bc0c9
+	part = parts->children->children->next;
7bc0c9
+	test_assert(part->children_count == 0);
7bc0c9
+	test_assert(part->flags == (MESSAGE_PART_FLAG_TEXT | MESSAGE_PART_FLAG_IS_MIME));
7bc0c9
+	test_assert(part->header_size.lines == 2);
7bc0c9
+	test_assert(part->header_size.physical_size == 26);
7bc0c9
+	test_assert(part->header_size.virtual_size == 26+2);
7bc0c9
+	test_assert(part->body_size.lines == 5);
7bc0c9
+	test_assert(part->body_size.physical_size == 37);
7bc0c9
+	test_assert(part->body_size.virtual_size == 37+5);
7bc0c9
+
7bc0c9
+	test_parsed_parts(input, parts);
7bc0c9
+	i_stream_unref(&input);
7bc0c9
+	pool_unref(&pool);
7bc0c9
+	test_end();
7bc0c9
+}
7bc0c9
+
7bc0c9
 int main(void)
7bc0c9
 {
7bc0c9
 	static void (*const test_functions[])(void) = {
7bc0c9
 		test_message_parser_small_blocks,
7bc0c9
+		test_message_parser_stop_early,
7bc0c9
 		test_message_parser_truncated_mime_headers,
7bc0c9
 		test_message_parser_truncated_mime_headers2,
7bc0c9
 		test_message_parser_truncated_mime_headers3,
7bc0c9
 		test_message_parser_empty_multipart,
7bc0c9
 		test_message_parser_duplicate_mime_boundary,
7bc0c9
 		test_message_parser_garbage_suffix_mime_boundary,
7bc0c9
+		test_message_parser_trailing_dashes,
7bc0c9
 		test_message_parser_continuing_mime_boundary,
7bc0c9
 		test_message_parser_continuing_truncated_mime_boundary,
7bc0c9
+		test_message_parser_continuing_mime_boundary_reverse,
7bc0c9
+		test_message_parser_long_mime_boundary,
7bc0c9
 		test_message_parser_no_eoh,
7bc0c9
+		test_message_parser_mime_part_nested_limit,
7bc0c9
+		test_message_parser_mime_part_nested_limit_rfc822,
7bc0c9
+		test_message_parser_mime_part_limit,
7bc0c9
 		NULL
7bc0c9
 	};
7bc0c9
 	return test_run(test_functions);
7bc0c9
diff -up dovecot-2.3.8/src/lib-mail/test-message-part.c.CVE_2020_12100 dovecot-2.3.8/src/lib-mail/test-message-part.c
7bc0c9
--- dovecot-2.3.8/src/lib-mail/test-message-part.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-mail/test-message-part.c	2020-08-07 12:29:39.976827472 +0200
7bc0c9
@@ -65,6 +65,7 @@ static const char test_msg[] =
7bc0c9
 
7bc0c9
 static void test_message_part_idx(void)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings set = { .flags = 0 };
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
 	struct istream *input;
7bc0c9
 	struct message_part *parts, *part, *prev_part;
7bc0c9
@@ -77,7 +78,7 @@ static void test_message_part_idx(void)
7bc0c9
 	pool = pool_alloconly_create("message parser", 10240);
7bc0c9
 	input = i_stream_create_from_data(test_msg, TEST_MSG_LEN);
7bc0c9
 
7bc0c9
-	parser = message_parser_init(pool, input, 0, 0);
7bc0c9
+	parser = message_parser_init(pool, input, &set);
7bc0c9
 	while ((ret = message_parser_parse_next_block(parser, &block)) > 0) {
7bc0c9
 		part_idx = message_part_to_idx(block.part);
7bc0c9
 		test_assert(part_idx >= prev_idx);
7bc0c9
diff -up dovecot-2.3.8/src/lib-storage/index/index-mail-headers.c.CVE_2020_12100 dovecot-2.3.8/src/lib-storage/index/index-mail-headers.c
7bc0c9
--- dovecot-2.3.8/src/lib-storage/index/index-mail-headers.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/lib-storage/index/index-mail-headers.c	2020-08-07 12:31:09.948576696 +0200
7bc0c9
@@ -16,11 +16,11 @@
7bc0c9
 #include "index-storage.h"
7bc0c9
 #include "index-mail.h"
7bc0c9
 
7bc0c9
-static const enum message_header_parser_flags hdr_parser_flags =
7bc0c9
-	MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
-	MESSAGE_HEADER_PARSER_FLAG_DROP_CR;
7bc0c9
-static const enum message_parser_flags msg_parser_flags =
7bc0c9
-	MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK;
7bc0c9
+static const struct message_parser_settings msg_parser_set = {
7bc0c9
+	.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_SKIP_INITIAL_LWSP |
7bc0c9
+		MESSAGE_HEADER_PARSER_FLAG_DROP_CR,
7bc0c9
+	.flags = MESSAGE_PARSER_FLAG_SKIP_BODY_BLOCK,
7bc0c9
+};
7bc0c9
 
7bc0c9
 static int header_line_cmp(const struct index_mail_line *l1,
7bc0c9
 			   const struct index_mail_line *l2)
7bc0c9
@@ -397,7 +397,7 @@ index_mail_cache_parse_init(struct mail
7bc0c9
 	mail->data.parser_input = input;
7bc0c9
 	mail->data.parser_ctx =
7bc0c9
 		message_parser_init(mail->mail.data_pool, input,
7bc0c9
-				    hdr_parser_flags, msg_parser_flags);
7bc0c9
+				    &msg_parser_set);
7bc0c9
 	i_stream_unref(&input);
7bc0c9
 	return input2;
7bc0c9
 }
7bc0c9
@@ -426,14 +426,12 @@ static void index_mail_init_parser(struc
7bc0c9
 		data->parser_input = data->stream;
7bc0c9
 		data->parser_ctx = message_parser_init(mail->mail.data_pool,
7bc0c9
 						       data->stream,
7bc0c9
-						       hdr_parser_flags,
7bc0c9
-						       msg_parser_flags);
7bc0c9
+						       &msg_parser_set);
7bc0c9
 	} else {
7bc0c9
 		data->parser_ctx =
7bc0c9
 			message_parser_init_from_parts(data->parts,
7bc0c9
 						       data->stream,
7bc0c9
-						       hdr_parser_flags,
7bc0c9
-						       msg_parser_flags);
7bc0c9
+						       &msg_parser_set);
7bc0c9
 	}
7bc0c9
 }
7bc0c9
 
7bc0c9
@@ -466,7 +464,7 @@ int index_mail_parse_headers(struct inde
7bc0c9
 		i_assert(!data->save_bodystructure_body ||
7bc0c9
 			 data->parser_ctx != NULL);
7bc0c9
 		message_parse_header(data->stream, &data->hdr_size,
7bc0c9
-				     hdr_parser_flags,
7bc0c9
+				     msg_parser_set.hdr_flags,
7bc0c9
 				     index_mail_parse_header_cb, mail);
7bc0c9
 	}
7bc0c9
 	if (index_mail_stream_check_failure(mail) < 0)
7bc0c9
@@ -521,7 +519,7 @@ int index_mail_headers_get_envelope(stru
7bc0c9
 	if (mail->data.envelope == NULL && stream != NULL) {
7bc0c9
 		/* we got the headers from cache - parse them to get the
7bc0c9
 		   envelope */
7bc0c9
-		message_parse_header(stream, NULL, hdr_parser_flags,
7bc0c9
+		message_parse_header(stream, NULL, msg_parser_set.hdr_flags,
7bc0c9
 				     imap_envelope_parse_callback, mail);
7bc0c9
 		if (stream->stream_errno != 0) {
7bc0c9
 			index_mail_stream_log_failure_for(mail, stream);
7bc0c9
diff -up dovecot-2.3.8/src/plugins/fts/fts-build-mail.c.CVE_2020_12100 dovecot-2.3.8/src/plugins/fts/fts-build-mail.c
7bc0c9
--- dovecot-2.3.8/src/plugins/fts/fts-build-mail.c.CVE_2020_12100	2019-10-08 10:46:18.000000000 +0200
7bc0c9
+++ dovecot-2.3.8/src/plugins/fts/fts-build-mail.c	2020-08-07 12:29:39.977827458 +0200
7bc0c9
@@ -475,6 +475,9 @@ fts_build_mail_real(struct fts_backend_u
7bc0c9
 		    const char **retriable_err_msg_r,
7bc0c9
 		    bool *may_need_retry_r)
7bc0c9
 {
7bc0c9
+	const struct message_parser_settings parser_set = {
7bc0c9
+		.hdr_flags = MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE,
7bc0c9
+	};
7bc0c9
 	struct fts_mail_build_context ctx;
7bc0c9
 	struct istream *input;
7bc0c9
 	struct message_parser_ctx *parser;
7bc0c9
@@ -503,9 +506,7 @@ fts_build_mail_real(struct fts_backend_u
7bc0c9
 		ctx.pending_input = buffer_create_dynamic(default_pool, 128);
7bc0c9
 
7bc0c9
 	prev_part = NULL;
7bc0c9
-	parser = message_parser_init(pool_datastack_create(), input,
7bc0c9
-				     MESSAGE_HEADER_PARSER_FLAG_CLEAN_ONELINE,
7bc0c9
-				     0);
7bc0c9
+	parser = message_parser_init(pool_datastack_create(), input, &parser_set);
7bc0c9
 
7bc0c9
 	decoder = message_decoder_init(update_ctx->normalizer, 0);
7bc0c9
 	for (;;) {