Skip to content

Commit 101a418

Browse files
samancaMongoDB Bot
authored andcommitted
SERVER-95708 Fix infinite loop during TLS operation (#28017) (#28081)
GitOrigin-RevId: 60cb06318d01ecbe42f6b849ee1979ae881a6cb8
1 parent 62423b2 commit 101a418

File tree

4 files changed

+158
-12
lines changed

4 files changed

+158
-12
lines changed

src/third_party/asio-master/asio/include/asio/ssl/detail/impl/engine.ipp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,23 @@ engine::want engine::perform(int (engine::* op)(void*, std::size_t),
240240
{
241241
ec = asio::error_code(sys_error,
242242
asio::error::get_ssl_category());
243-
return want_nothing;
243+
return pending_output_after > pending_output_before
244+
? want_output : want_nothing;
244245
}
245246

246247
if (ssl_error == SSL_ERROR_SYSCALL)
247248
{
248-
ec = asio::error_code(sys_error,
249-
asio::error::get_system_category());
250-
return want_nothing;
249+
if (sys_error == 0)
250+
{
251+
ec = asio::ssl::error::unspecified_system_error;
252+
}
253+
else
254+
{
255+
ec = asio::error_code(sys_error,
256+
asio::error::get_ssl_category());
257+
}
258+
return pending_output_after > pending_output_before
259+
? want_output : want_nothing;
251260
}
252261

253262
if (result > 0 && bytes_transferred)
@@ -268,16 +277,21 @@ engine::want engine::perform(int (engine::* op)(void*, std::size_t),
268277
ec = asio::error_code();
269278
return want_input_and_retry;
270279
}
271-
else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
280+
else if (ssl_error == SSL_ERROR_ZERO_RETURN)
272281
{
273282
ec = asio::error::eof;
274283
return want_nothing;
275284
}
276-
else
285+
else if (ssl_error == SSL_ERROR_NONE)
277286
{
278287
ec = asio::error_code();
279288
return want_nothing;
280289
}
290+
else
291+
{
292+
ec = asio::ssl::error::unexpected_result;
293+
return want_nothing;
294+
}
281295
}
282296

283297
int engine::do_accept(void*, std::size_t)

src/third_party/asio-master/asio/include/asio/ssl/error.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,24 @@ enum stream_errors
4444
{
4545
#if defined(GENERATING_DOCUMENTATION)
4646
/// The underlying stream closed before the ssl stream gracefully shut down.
47-
stream_truncated
48-
#elif (OPENSSL_VERSION_NUMBER < 0x10100000L) && !defined(OPENSSL_IS_BORINGSSL)
49-
stream_truncated = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)
50-
#else
51-
stream_truncated = 1
52-
#endif
47+
stream_truncated,
48+
49+
/// The underlying SSL library returned a system error without providing
50+
/// further information.
51+
unspecified_system_error,
52+
53+
/// The underlying SSL library generated an unexpected result from a function
54+
/// call.
55+
unexpected_result
56+
#else // defined(GENERATING_DOCUMENTATION)
57+
# if (OPENSSL_VERSION_NUMBER < 0x10100000L) && !defined(OPENSSL_IS_BORINGSSL)
58+
stream_truncated = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
59+
# else
60+
stream_truncated = 1,
61+
# endif
62+
unspecified_system_error = 2,
63+
unexpected_result = 3
64+
#endif // defined(GENERATING_DOCUMENTATION)
5365
};
5466

5567
extern ASIO_DECL

src/third_party/asio-master/asio/include/asio/ssl/impl/error.ipp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public:
7676
switch (value)
7777
{
7878
case stream_truncated: return "stream truncated";
79+
case unspecified_system_error: return "unspecified system error";
80+
case unexpected_result: return "unexpected result";
7981
default: return "asio.ssl.stream error";
8082
}
8183
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
From e7765e5647ee29d21ec0ea0ccc0c8320190e0695 Mon Sep 17 00:00:00 2001
2+
From: Amirsaman Memaripour <amirsaman.memaripour@mongodb.com>
3+
Date: Fri, 11 Oct 2024 17:37:49 +0000
4+
Subject: [PATCH] MONGO: Fix infinite loop during TLS operation
5+
6+
---
7+
.../include/asio/ssl/detail/impl/engine.ipp | 26 ++++++++++++++-----
8+
.../asio/include/asio/ssl/error.hpp | 24 ++++++++++++-----
9+
.../asio/include/asio/ssl/impl/error.ipp | 2 ++
10+
3 files changed, 40 insertions(+), 12 deletions(-)
11+
12+
diff --git a/src/third_party/asio-master/asio/include/asio/ssl/detail/impl/engine.ipp b/src/third_party/asio-master/asio/include/asio/ssl/detail/impl/engine.ipp
13+
index e60e8d6f88e..315bb287043 100644
14+
--- a/src/third_party/asio-master/asio/include/asio/ssl/detail/impl/engine.ipp
15+
+++ b/src/third_party/asio-master/asio/include/asio/ssl/detail/impl/engine.ipp
16+
@@ -240,14 +240,23 @@ engine::want engine::perform(int (engine::* op)(void*, std::size_t),
17+
{
18+
ec = asio::error_code(sys_error,
19+
asio::error::get_ssl_category());
20+
- return want_nothing;
21+
+ return pending_output_after > pending_output_before
22+
+ ? want_output : want_nothing;
23+
}
24+
25+
if (ssl_error == SSL_ERROR_SYSCALL)
26+
{
27+
- ec = asio::error_code(sys_error,
28+
- asio::error::get_system_category());
29+
- return want_nothing;
30+
+ if (sys_error == 0)
31+
+ {
32+
+ ec = asio::ssl::error::unspecified_system_error;
33+
+ }
34+
+ else
35+
+ {
36+
+ ec = asio::error_code(sys_error,
37+
+ asio::error::get_ssl_category());
38+
+ }
39+
+ return pending_output_after > pending_output_before
40+
+ ? want_output : want_nothing;
41+
}
42+
43+
if (result > 0 && bytes_transferred)
44+
@@ -268,16 +277,21 @@ engine::want engine::perform(int (engine::* op)(void*, std::size_t),
45+
ec = asio::error_code();
46+
return want_input_and_retry;
47+
}
48+
- else if (::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN)
49+
+ else if (ssl_error == SSL_ERROR_ZERO_RETURN)
50+
{
51+
ec = asio::error::eof;
52+
return want_nothing;
53+
}
54+
- else
55+
+ else if (ssl_error == SSL_ERROR_NONE)
56+
{
57+
ec = asio::error_code();
58+
return want_nothing;
59+
}
60+
+ else
61+
+ {
62+
+ ec = asio::ssl::error::unexpected_result;
63+
+ return want_nothing;
64+
+ }
65+
}
66+
67+
int engine::do_accept(void*, std::size_t)
68+
diff --git a/src/third_party/asio-master/asio/include/asio/ssl/error.hpp b/src/third_party/asio-master/asio/include/asio/ssl/error.hpp
69+
index 6165c5cf764..9cbf7d7fae8 100644
70+
--- a/src/third_party/asio-master/asio/include/asio/ssl/error.hpp
71+
+++ b/src/third_party/asio-master/asio/include/asio/ssl/error.hpp
72+
@@ -44,12 +44,24 @@ enum stream_errors
73+
{
74+
#if defined(GENERATING_DOCUMENTATION)
75+
/// The underlying stream closed before the ssl stream gracefully shut down.
76+
- stream_truncated
77+
-#elif (OPENSSL_VERSION_NUMBER < 0x10100000L) && !defined(OPENSSL_IS_BORINGSSL)
78+
- stream_truncated = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)
79+
-#else
80+
- stream_truncated = 1
81+
-#endif
82+
+ stream_truncated,
83+
+
84+
+ /// The underlying SSL library returned a system error without providing
85+
+ /// further information.
86+
+ unspecified_system_error,
87+
+
88+
+ /// The underlying SSL library generated an unexpected result from a function
89+
+ /// call.
90+
+ unexpected_result
91+
+#else // defined(GENERATING_DOCUMENTATION)
92+
+# if (OPENSSL_VERSION_NUMBER < 0x10100000L) && !defined(OPENSSL_IS_BORINGSSL)
93+
+ stream_truncated = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
94+
+# else
95+
+ stream_truncated = 1,
96+
+# endif
97+
+ unspecified_system_error = 2,
98+
+ unexpected_result = 3
99+
+#endif // defined(GENERATING_DOCUMENTATION)
100+
};
101+
102+
extern ASIO_DECL
103+
diff --git a/src/third_party/asio-master/asio/include/asio/ssl/impl/error.ipp b/src/third_party/asio-master/asio/include/asio/ssl/impl/error.ipp
104+
index 98e8c91b43b..01ab34e0655 100644
105+
--- a/src/third_party/asio-master/asio/include/asio/ssl/impl/error.ipp
106+
+++ b/src/third_party/asio-master/asio/include/asio/ssl/impl/error.ipp
107+
@@ -76,6 +76,8 @@ public:
108+
switch (value)
109+
{
110+
case stream_truncated: return "stream truncated";
111+
+ case unspecified_system_error: return "unspecified system error";
112+
+ case unexpected_result: return "unexpected result";
113+
default: return "asio.ssl.stream error";
114+
}
115+
}
116+
--
117+
2.34.1
118+

0 commit comments

Comments
 (0)