public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox v2] fix #5868: rest-server: connection: fix busy waiting on closed connections pre tls
@ 2024-11-13 10:39 Dominik Csapak
  2024-11-14 14:21 ` [pbs-devel] applied: " Thomas Lamprecht
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2024-11-13 10:39 UTC (permalink / raw)
  To: pbs-devel

when a connection is closed before we have enough data to determine
if it's tls or not, the socket stays in a readable state.

Sadly, the tokio timeout we use here gets starved by the async_io
callback.

To fix this, save the amount of bytes peek returned and if they did not
change between invocations of the callback, we assume that the
connection was closed and exit with an error.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* removed leftover unrelated test code
* fixed up the commit message with the bug #

 proxmox-rest-server/src/connection.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/proxmox-rest-server/src/connection.rs b/proxmox-rest-server/src/connection.rs
index 3815a8f4..11f29ce4 100644
--- a/proxmox-rest-server/src/connection.rs
+++ b/proxmox-rest-server/src/connection.rs
@@ -477,6 +477,7 @@ impl AcceptBuilder {
         const HANDSHAKE_BYTES_LEN: usize = 5;
 
         let future = async {
+            let mut old_peek_len = 0;
             incoming_stream
                 .async_io(tokio::io::Interest::READABLE, || {
                     let mut buf = [0; HANDSHAKE_BYTES_LEN];
@@ -500,7 +501,14 @@ impl AcceptBuilder {
                         // This means we will peek into the stream's queue until we got
                         // HANDSHAKE_BYTE_LEN bytes or an error.
                         Ok(peek_len) if peek_len < HANDSHAKE_BYTES_LEN => {
-                            Err(io::ErrorKind::WouldBlock.into())
+                            // if we detect the same peek len again but still got a readable
+                            // stream, the connection was probably closed, so abort here
+                            if peek_len == old_peek_len {
+                                Err(io::ErrorKind::UnexpectedEof.into())
+                            } else {
+                                old_peek_len = peek_len;
+                                Err(io::ErrorKind::WouldBlock.into())
+                            }
                         }
                         // Either we got Ok(HANDSHAKE_BYTES_LEN) or some error.
                         res => res.map(|_| contains_tls_handshake_fragment(&buf)),
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pbs-devel] applied: [PATCH proxmox v2] fix #5868: rest-server: connection: fix busy waiting on closed connections pre tls
  2024-11-13 10:39 [pbs-devel] [PATCH proxmox v2] fix #5868: rest-server: connection: fix busy waiting on closed connections pre tls Dominik Csapak
@ 2024-11-14 14:21 ` Thomas Lamprecht
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Lamprecht @ 2024-11-14 14:21 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Dominik Csapak

Am 13.11.24 um 11:39 schrieb Dominik Csapak:
> when a connection is closed before we have enough data to determine
> if it's tls or not, the socket stays in a readable state.
> 
> Sadly, the tokio timeout we use here gets starved by the async_io
> callback.
> 
> To fix this, save the amount of bytes peek returned and if they did not
> change between invocations of the callback, we assume that the
> connection was closed and exit with an error.
> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
> changes from v1:
> * removed leftover unrelated test code
> * fixed up the commit message with the bug #
> 
>  proxmox-rest-server/src/connection.rs | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
>

applied this as stop-gap, thanks!

Like I mentioned off-list, I changed the error to ConnectionAborted and reworded
the commit message a bit.


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-11-14 14:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-13 10:39 [pbs-devel] [PATCH proxmox v2] fix #5868: rest-server: connection: fix busy waiting on closed connections pre tls Dominik Csapak
2024-11-14 14:21 ` [pbs-devel] applied: " Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal