From 548331dc2bc47352097780fda9f215c75fc2eb17 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Fri, 4 Jul 2025 00:16:42 +0100 Subject: [PATCH] avoid message copy deadlock in message writers and stop copy when alert was sent --- .../internet/tlsmirror/mirrorbase/conn.go | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/transport/internet/tlsmirror/mirrorbase/conn.go b/transport/internet/tlsmirror/mirrorbase/conn.go index 676161058..75fa607cf 100644 --- a/transport/internet/tlsmirror/mirrorbase/conn.go +++ b/transport/internet/tlsmirror/mirrorbase/conn.go @@ -165,8 +165,14 @@ func (c *conn) c2sWorker() { } go func() { for c.ctx.Err() == nil { - // implicit memory consistency synchronization capture read for c.tls12ExplicitNonce - record := <-c.c2sInsert + var record *tlsmirror.TLSRecord + select { + case <-c.ctx.Done(): + return + case record = <-c.c2sInsert: + // implicit memory consistency synchronization capture read for c.tls12ExplicitNonce + } + // memory consistency synchronization for value c.tls12ExplicitNonce is required!!! if *c.tls12ExplicitNonce { if record.RecordType == mirrorcommon.TLSRecord_RecordType_application_data || @@ -183,6 +189,11 @@ func (c *conn) c2sWorker() { newError("failed to write C2S message").Base(err).AtWarning().WriteToLog() return } + if record.RecordType == mirrorcommon.TLSRecord_RecordType_alert { + c.done() + newError("alert sent, ending copy").AtWarning().WriteToLog() + return + } } }() explicitNonceSessionAndChangeCipherSpecWasLastMessage := false @@ -289,8 +300,13 @@ func (c *conn) s2cWorker() { } go func() { for c.ctx.Err() == nil { - // implicit memory consistency synchronization capture read for c.tls12ExplicitNonce - record := <-c.s2cInsert + var record *tlsmirror.TLSRecord + select { + case <-c.ctx.Done(): + return + case record = <-c.s2cInsert: + // implicit memory consistency synchronization capture read for c.tls12ExplicitNonce + } // memory consistency synchronization for value c.tls12ExplicitNonce is required!!! if *c.tls12ExplicitNonce { if record.RecordType == mirrorcommon.TLSRecord_RecordType_application_data || @@ -307,6 +323,11 @@ func (c *conn) s2cWorker() { newError("failed to write S2C message").Base(err).AtWarning().WriteToLog() return } + if record.RecordType == mirrorcommon.TLSRecord_RecordType_alert { + c.done() + newError("alert sent, ending copy").AtWarning().WriteToLog() + return + } } }() explicitNonceSessionAndChangeCipherSpecWasLastMessage := false