1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Marco Bettini <marco.bettini@open-xchange.com>
Subject: [PATCH] Fix CVE-2026-33604
Upstream commits:
lib-mail: o_stream_dot_sendv() - Use I_MIN()
https://github.com/dovecot/core/commit/8e254544cd8314092b4e3b2e7654df34b73e721b
lib-mail: o_stream_dot_sendv() - Fix boundary off by one
https://github.com/dovecot/core/commit/b5921303c9ba9ae51b68a3a9f0ef2be1fdb35cc0
lib-mail: o_stream_dot_sendv() - Use enumeration to define the items to inject
https://github.com/dovecot/core/commit/0e9af24dc0d191be3c8ba7747a00a54337c6fd9a
lib-mail: o_stream_dot_sendv() - Do not send unguarded '.' after bare '\r'
https://github.com/dovecot/core/commit/5d8bea193dec2d2a065868f383738ba6625f839e
Taken from the Debian bookworm security update (dovecot
1:2.3.19.1+dfsg1-2.1+deb12u7), which carries these fixes for 2.3, and
applied to 2.3.21.1.
--- a/src/lib-mail/ostream-dot.c
+++ b/src/lib-mail/ostream-dot.c
@@ -71,6 +71,14 @@ o_stream_dot_close(struct iostream_priva
o_stream_close(dstream->ostream.parent);
}
+#define ADD_MAX 3
+enum o_stream_dot_sendv_add {
+ ADD_NONE,
+ ADD_CR,
+ ADD_DOT,
+ ADD_LF_DOT,
+};
+
static ssize_t
o_stream_dot_sendv(struct ostream_private *stream,
const struct const_iovec *iov, unsigned int iov_count)
@@ -105,8 +113,8 @@ o_stream_dot_sendv(struct ostream_privat
p = data;
pend = CONST_PTR_OFFSET(data, size);
- for (; p < pend && (size_t)(p-data)+2 < max_bytes; p++) {
- char add = 0;
+ for (; p < pend && ((size_t)(p - data) + ADD_MAX) <= max_bytes; p++) {
+ enum o_stream_dot_sendv_add add = ADD_NONE;
switch (dstream->state) {
/* none */
@@ -114,8 +122,7 @@ o_stream_dot_sendv(struct ostream_privat
switch (*p) {
case '\n':
dstream->state = STREAM_STATE_CRLF;
- /* add missing CR */
- add = '\r';
+ add = ADD_CR;
break;
case '\r':
dstream->state = STREAM_STATE_CR;
@@ -130,6 +137,9 @@ o_stream_dot_sendv(struct ostream_privat
case '\n':
dstream->state = STREAM_STATE_CRLF;
break;
+ case '.':
+ add = ADD_LF_DOT;
+ /* fall through */
default:
dstream->state = STREAM_STATE_NONE;
break;
@@ -144,12 +154,10 @@ o_stream_dot_sendv(struct ostream_privat
break;
case '\n':
dstream->state = STREAM_STATE_CRLF;
- /* add missing CR */
- add = '\r';
+ add = ADD_CR;
break;
case '.':
- /* add dot */
- add = '.';
+ add = ADD_DOT;
/* fall through */
default:
dstream->state = STREAM_STATE_NONE;
@@ -172,22 +180,60 @@ o_stream_dot_sendv(struct ostream_privat
max_bytes -= chunk;
sent += chunk;
}
- /* insert byte (substitute one with pair) */
data++;
- iovn.iov_base = (add == '\r' ? "\r\n" : "..");
- iovn.iov_len = 2;
+
+ /* Apply the modifications required according to RFC 5321 to the
+ stream :
+
+ ADD_DOT - Apply standard SMTP dot-stuffing
+
+ ADD_CR - Convert invalid lone LFs to CRLF to comply with
+ DATA line termination requirements, and avoid
+ potentially emitting bare "\n." sequences (same
+ threat as below for "\r." sequences).
+
+ ADD_LF_DOT - Guard against invalid "\r." sequences.
+ A downstream MTA that incorrectly handles bare CRs
+ as line terminators could interpret a '.' at the
+ start of the next line as end-of-DATA. This would
+ allow any following bytes to be processed as SMTP
+ commands.
+
+ ADD_CR and ADD_LF_DOT make the istream-dot round-trip lossy,
+ as these introduce bytes not present in the original input.
+ We accept this tradeoff, since preserving exact byte fidelity
+ for malformed input is less important than eliminating an
+ injection vector. */
+
+ switch(add) {
+ case ADD_DOT:
+ iovn.iov_base = "..";
+ iovn.iov_len = 2;
+ break;
+ case ADD_CR:
+ iovn.iov_base = "\r\n";
+ iovn.iov_len = 2;
+ break;
+ case ADD_LF_DOT:
+ iovn.iov_base = "\n..";
+ iovn.iov_len = 3;
+ break;
+ default:
+ i_unreached();
+ }
+
array_push_back(&iov_arr, &iovn);
- i_assert(max_bytes >= 2);
- max_bytes -= 2;
- added++;
+ i_assert(iovn.iov_len <= ADD_MAX);
+ i_assert(iovn.iov_len <= max_bytes);
+ max_bytes -= iovn.iov_len;
+ added += iovn.iov_len - 1;
sent++;
}
}
if (max_bytes == 0)
break;
- chunk = ((size_t)(p-data) >= max_bytes ?
- max_bytes : (size_t)(p - data));
+ chunk = I_MIN((size_t)(p - data), max_bytes);
if (chunk > 0) {
iovn.iov_base = data;
iovn.iov_len = chunk;
--- a/src/lib-mail/test-ostream-dot.c
+++ b/src/lib-mail/test-ostream-dot.c
@@ -57,6 +57,9 @@ static void test_ostream_dot(void)
{ "foo\n.\n", "foo\r\n..\r\n.\r\n" },
{ ".foo\r\n.\r\nfoo\r\n", "..foo\r\n..\r\nfoo\r\n.\r\n" },
{ ".foo\n.\nfoo\n", "..foo\r\n..\r\nfoo\r\n.\r\n" },
+ { ".", "..\r\n.\r\n" },
+ { "\r.", "\r\n..\r\n.\r\n" },
+ { "\r\r.", "\r\r\n..\r\n.\r\n" },
{ "\r\n", "\r\n.\r\n" },
{ "\n", "\r\n.\r\n" },
{ "", "\r\n.\r\n" },
@@ -92,11 +95,94 @@ static void test_ostream_dot_parent_almo
test_end();
}
+static void test_ostream_dot_parent_max_bytes_boundary(void)
+{
+ buffer_t *output_data;
+ struct ostream *test_output, *output;
+ ssize_t ret;
+
+ test_begin("dot ostream parent exact fit");
+ output_data = t_buffer_create(1024);
+ test_output = test_ostream_create_nonblocking(output_data, 3);
+ test_ostream_set_max_output_size(test_output, 3);
+
+ output = o_stream_create_dot(test_output, FALSE);
+ ret = o_stream_send(output, ".", 1);
+ test_assert(ret == 1);
+ test_assert_ucmp(output_data->used, ==, 2);
+ test_assert_memcmp(output_data->data, output_data->used, "..", 2);
+
+ o_stream_unref(&output);
+ o_stream_unref(&test_output);
+ test_end();
+}
+
+/* STATE_CR must persist across sendv calls so that a bare CR ending one send
+ followed by '.' starting the next still triggers ADD_LF_DOT.
+ A stateless per-call implementation would emit the '.' unguarded and let a
+ downstream MTA that treats bare CR as EOL interpret it as end-of-DATA. */
+static void test_ostream_dot_cr_across_sendv(void)
+{
+ buffer_t *output_data;
+ struct ostream *test_output, *output;
+ ssize_t ret;
+ const char *expected = "\r\n..\r\n.\r\n";
+
+ test_begin("dot ostream CR across sendv calls");
+ output_data = t_buffer_create(1024);
+ test_output = o_stream_create_buffer(output_data);
+
+ output = o_stream_create_dot(test_output, FALSE);
+ ret = o_stream_send(output, "\r", 1);
+ test_assert(ret == 1);
+ ret = o_stream_send(output, ".", 1);
+ test_assert(ret == 1);
+ test_assert(o_stream_finish(output) > 0);
+
+ o_stream_unref(&output);
+ o_stream_unref(&test_output);
+
+ test_assert_ucmp(str_len(output_data), ==, strlen(expected));
+ test_assert_memcmp(str_c(output_data), str_len(output_data),
+ expected, strlen(expected));
+ test_end();
+}
+
+/* Off-by-one regression guard for the bare-CR + '.' injection.
+ Input "\r." places '.' at offset 1; the ADD_LF_DOT pair injects 3 bytes
+ ("\n.."). The loop guard check becomes (1 + 3) <= max_bytes, so max_bytes==4
+ is the exact-fit case. */
+static void test_ostream_dot_cr_dot_exact_fit(void)
+{
+ buffer_t *output_data;
+ struct ostream *test_output, *output;
+ ssize_t ret;
+
+ test_begin("dot ostream CR+dot exact fit");
+ output_data = t_buffer_create(1024);
+ test_output = test_ostream_create_nonblocking(output_data, 4);
+ test_ostream_set_max_output_size(test_output, 4);
+
+ output = o_stream_create_dot(test_output, FALSE);
+ ret = o_stream_send(output, "\r.", 2);
+ test_assert(ret == 2);
+ test_assert_ucmp(output_data->used, ==, 4);
+ test_assert_memcmp(output_data->data, output_data->used,
+ "\r\n..", 4);
+
+ o_stream_unref(&output);
+ o_stream_unref(&test_output);
+ test_end();
+}
+
int main(void)
{
static void (*const test_functions[])(void) = {
test_ostream_dot,
test_ostream_dot_parent_almost_full,
+ test_ostream_dot_parent_max_bytes_boundary,
+ test_ostream_dot_cr_across_sendv,
+ test_ostream_dot_cr_dot_exact_fit,
NULL
};
return test_run(test_functions);
|