Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent concurrent access to IO buffers on macOS #61723

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ protected override void Dispose(bool disposing)
SafeSslHandle sslContext = _sslContext;
if (null != sslContext)
{
_inputBuffer.Dispose();
_outputBuffer.Dispose();
lock (_sslContext)
{
_inputBuffer.Dispose();
_outputBuffer.Dispose();
}
sslContext.Dispose();
}
}
Expand All @@ -178,18 +181,21 @@ private static unsafe int WriteToConnection(IntPtr connection, byte* data, void*
// but if we were to pool the buffers we would have a potential use-after-free issue.
try
{
ulong length = (ulong)*dataLength;
Debug.Assert(length <= int.MaxValue);
lock (context)
{
ulong length = (ulong)*dataLength;
Debug.Assert(length <= int.MaxValue);

int toWrite = (int)length;
var inputBuffer = new ReadOnlySpan<byte>(data, toWrite);
int toWrite = (int)length;
var inputBuffer = new ReadOnlySpan<byte>(data, toWrite);

context._outputBuffer.EnsureAvailableSpace(toWrite);
inputBuffer.CopyTo(context._outputBuffer.AvailableSpan);
context._outputBuffer.Commit(toWrite);
// Since we can enqueue everything, no need to re-assign *dataLength.
context._outputBuffer.EnsureAvailableSpace(toWrite);
inputBuffer.CopyTo(context._outputBuffer.AvailableSpan);
context._outputBuffer.Commit(toWrite);
// Since we can enqueue everything, no need to re-assign *dataLength.

return OSStatus_noErr;
return OSStatus_noErr;
}
}
catch (Exception e)
{
Expand All @@ -207,29 +213,32 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void

try
{
ulong toRead = (ulong)*dataLength;

if (toRead == 0)
lock (context)
{
return OSStatus_noErr;
}
ulong toRead = (ulong)*dataLength;

uint transferred = 0;
if (toRead == 0)
{
return OSStatus_noErr;
}

if (context._inputBuffer.ActiveLength == 0)
{
*dataLength = (void*)0;
return OSStatus_errSSLWouldBlock;
}
uint transferred = 0;

int limit = Math.Min((int)toRead, context._inputBuffer.ActiveLength);
if (context._inputBuffer.ActiveLength == 0)
{
*dataLength = (void*)0;
return OSStatus_errSSLWouldBlock;
}

int limit = Math.Min((int)toRead, context._inputBuffer.ActiveLength);

context._inputBuffer.ActiveSpan.Slice(0, limit).CopyTo(new Span<byte>(data, limit));
context._inputBuffer.Discard(limit);
transferred = (uint)limit;
context._inputBuffer.ActiveSpan.Slice(0, limit).CopyTo(new Span<byte>(data, limit));
context._inputBuffer.Discard(limit);
transferred = (uint)limit;

*dataLength = (void*)transferred;
return OSStatus_noErr;
*dataLength = (void*)transferred;
return OSStatus_noErr;
}
}
catch (Exception e)
{
Expand All @@ -241,24 +250,30 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void

internal void Write(ReadOnlySpan<byte> buf)
{
_inputBuffer.EnsureAvailableSpace(buf.Length);
buf.CopyTo(_inputBuffer.AvailableSpan);
_inputBuffer.Commit(buf.Length);
lock (_sslContext)
{
_inputBuffer.EnsureAvailableSpace(buf.Length);
buf.CopyTo(_inputBuffer.AvailableSpan);
_inputBuffer.Commit(buf.Length);
}
}

internal int BytesReadyForConnection => _outputBuffer.ActiveLength;

internal byte[]? ReadPendingWrites()
{
if (_outputBuffer.ActiveLength == 0)
lock (_sslContext)
{
return null;
}
if (_outputBuffer.ActiveLength == 0)
{
return null;
}

byte[] buffer = _outputBuffer.ActiveSpan.ToArray();
_outputBuffer.Discard(_outputBuffer.ActiveLength);
byte[] buffer = _outputBuffer.ActiveSpan.ToArray();
_outputBuffer.Discard(_outputBuffer.ActiveLength);

return buffer;
return buffer;
}
}

internal int ReadPendingWrites(byte[] buf, int offset, int count)
Expand All @@ -268,12 +283,15 @@ internal int ReadPendingWrites(byte[] buf, int offset, int count)
Debug.Assert(count >= 0);
Debug.Assert(count <= buf.Length - offset);

int limit = Math.Min(count, _outputBuffer.ActiveLength);
lock (_sslContext)
{
int limit = Math.Min(count, _outputBuffer.ActiveLength);

_outputBuffer.ActiveSpan.Slice(0, limit).CopyTo(new Span<byte>(buf, offset, limit));
_outputBuffer.Discard(limit);
_outputBuffer.ActiveSpan.Slice(0, limit).CopyTo(new Span<byte>(buf, offset, limit));
_outputBuffer.Discard(limit);

return limit;
return limit;
}
}

private static readonly SslProtocols[] s_orderedSslProtocols = new SslProtocols[5]
Expand Down