| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers.Binary; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Net.WebSockets; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace DotnetFuzzing.Fuzzers; |
| | | 11 | | |
| | | 12 | | internal sealed class WebSocketFuzzer : IFuzzer |
| | | 13 | | { |
| | 0 | 14 | | public string[] TargetAssemblies => ["System.Net.WebSockets"]; |
| | 0 | 15 | | public string[] TargetCoreLibPrefixes => []; |
| | | 16 | | |
| | 0 | 17 | | private readonly byte[] _inputBuffer = new byte[4096]; // Default max fuzzer input length |
| | 0 | 18 | | private readonly ReadOnlyMemory<byte>[] _segments = new ReadOnlyMemory<byte>[4096]; |
| | 0 | 19 | | private readonly byte[] _receiveBuffer = new byte[ushort.MaxValue + 1]; |
| | 0 | 20 | | private readonly byte[] _clientOperationsBuffer = new byte[ushort.MaxValue + 1]; |
| | | 21 | | |
| | | 22 | | [Flags] |
| | | 23 | | private enum Options : byte |
| | | 24 | | { |
| | | 25 | | None = 0, |
| | | 26 | | IsServer = 1 << 0, |
| | | 27 | | EnableCompression = 1 << 1, |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 31 | | { |
| | 0 | 32 | | if (bytes.Length < 5) |
| | 0 | 33 | | { |
| | 0 | 34 | | return; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | Options options = (Options)bytes[0]; |
| | 0 | 38 | | bytes = bytes.Slice(1); |
| | | 39 | | |
| | 0 | 40 | | int receiveBufferLength = 1 + BinaryPrimitives.ReadUInt16BigEndian(bytes); |
| | 0 | 41 | | bytes = bytes.Slice(2); |
| | | 42 | | |
| | 0 | 43 | | int clientOperationsBufferLength = BinaryPrimitives.ReadUInt16BigEndian(bytes); |
| | 0 | 44 | | bytes = bytes.Slice(2); |
| | | 45 | | |
| | 0 | 46 | | if (bytes.Length < clientOperationsBufferLength) |
| | 0 | 47 | | { |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | bytes.Slice(0, clientOperationsBufferLength).CopyTo(_clientOperationsBuffer); |
| | 0 | 52 | | bytes = bytes.Slice(clientOperationsBufferLength); |
| | 0 | 53 | | ReadOnlyMemory<byte> clientOperations = _clientOperationsBuffer.AsMemory(0, clientOperationsBufferLength); |
| | | 54 | | |
| | 0 | 55 | | if (!TryCreateSegments(bytes, out Memory<ReadOnlyMemory<byte>> segments)) |
| | 0 | 56 | | { |
| | 0 | 57 | | return; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | var stream = new TrickleStream(segments); |
| | | 61 | | |
| | 0 | 62 | | using WebSocket webSocket = WebSocket.CreateFromStream(stream, new WebSocketCreationOptions |
| | 0 | 63 | | { |
| | 0 | 64 | | KeepAliveInterval = Timeout.InfiniteTimeSpan, |
| | 0 | 65 | | KeepAliveTimeout = Timeout.InfiniteTimeSpan, |
| | 0 | 66 | | IsServer = options.HasFlag(Options.IsServer), |
| | 0 | 67 | | DangerousDeflateOptions = options.HasFlag(Options.EnableCompression) ? new WebSocketDeflateOptions() : null |
| | 0 | 68 | | }); |
| | | 69 | | |
| | 0 | 70 | | TestWebSocket(webSocket, stream, _receiveBuffer.AsMemory(0, receiveBufferLength), clientOperations).GetAwaiter() |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private async Task TestWebSocket(WebSocket webSocket, TrickleStream stream, Memory<byte> receiveBuffer, ReadOnlyMemo |
| | 0 | 74 | | { |
| | 0 | 75 | | using CancellationTokenSource cts = new(); |
| | 0 | 76 | | cts.CancelAfter(TimeSpan.FromSeconds(30)); |
| | | 77 | | |
| | | 78 | | try |
| | 0 | 79 | | { |
| | 0 | 80 | | while (true) // We should always exit from reading EOF |
| | 0 | 81 | | { |
| | 0 | 82 | | ValueWebSocketReceiveResult result = await webSocket.ReceiveAsync(ReadBool() ? Memory<byte>.Empty : rece |
| | | 83 | | |
| | 0 | 84 | | if (ReadBool()) |
| | 0 | 85 | | { |
| | 0 | 86 | | await webSocket.CloseAsync(WebSocketCloseStatus.PolicyViolation, ReadBool() ? "Close description" : |
| | 0 | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if (ReadBool()) |
| | 0 | 90 | | { |
| | 0 | 91 | | await webSocket.CloseOutputAsync(WebSocketCloseStatus.PolicyViolation, ReadBool() ? "Close output de |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (ReadBool()) |
| | 0 | 95 | | { |
| | 0 | 96 | | await webSocket.SendAsync( |
| | 0 | 97 | | buffer: receiveBuffer.Slice(0, Math.Min(receiveBuffer.Length, ReadByte())), |
| | 0 | 98 | | messageType: ReadBool() ? WebSocketMessageType.Text : WebSocketMessageType.Binary, |
| | 0 | 99 | | endOfMessage: ReadBool(), |
| | 0 | 100 | | cancellationToken: GetCancellationToken()); |
| | 0 | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | if (ReadBool()) |
| | 0 | 104 | | { |
| | 0 | 105 | | webSocket.Dispose(); |
| | 0 | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | } |
| | 0 | 109 | | catch (WebSocketException) |
| | 0 | 110 | | { |
| | | 111 | | // Expected |
| | 0 | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | CancellationToken GetCancellationToken() => ReadBool() ? cts.Token : CancellationToken.None; |
| | | 115 | | |
| | 0 | 116 | | bool ReadBool() => ReadByte() != 0; |
| | | 117 | | |
| | | 118 | | byte ReadByte() |
| | 0 | 119 | | { |
| | 0 | 120 | | if (clientOperations.IsEmpty) return 0; |
| | 0 | 121 | | byte value = clientOperations.Span[0]; |
| | 0 | 122 | | clientOperations = clientOperations.Slice(1); |
| | 0 | 123 | | return value; |
| | 0 | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | private bool TryCreateSegments(ReadOnlySpan<byte> bytes, out Memory<ReadOnlyMemory<byte>> segments) |
| | 0 | 128 | | { |
| | 0 | 129 | | bytes.CopyTo(_inputBuffer); |
| | 0 | 130 | | ReadOnlyMemory<byte> buffer = _inputBuffer.AsMemory(0, bytes.Length); |
| | | 131 | | |
| | 0 | 132 | | int segmentCount = 0; |
| | 0 | 133 | | while (!buffer.IsEmpty) |
| | 0 | 134 | | { |
| | 0 | 135 | | int length = buffer.Span[0]; |
| | 0 | 136 | | buffer = buffer.Slice(1); |
| | | 137 | | |
| | 0 | 138 | | if (length > buffer.Length) |
| | 0 | 139 | | { |
| | 0 | 140 | | segments = default; |
| | 0 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | _segments[segmentCount++] = buffer.Slice(0, length); |
| | 0 | 145 | | buffer = buffer.Slice(length); |
| | 0 | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | segments = _segments.AsMemory(0, segmentCount); |
| | 0 | 149 | | return true; |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | // Allows controlling how the read data is chunked between Read calls. |
| | | 153 | | // All writes are ignored. |
| | | 154 | | private sealed class TrickleStream : Stream |
| | | 155 | | { |
| | | 156 | | private Memory<ReadOnlyMemory<byte>> _segments; |
| | | 157 | | |
| | 0 | 158 | | public TrickleStream(Memory<ReadOnlyMemory<byte>> segments) |
| | 0 | 159 | | { |
| | 0 | 160 | | _segments = segments; |
| | 0 | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | public override bool CanRead => true; |
| | 0 | 164 | | public override bool CanWrite => true; |
| | 0 | 165 | | public override bool CanSeek => false; |
| | | 166 | | |
| | 0 | 167 | | public override int Read(byte[] buffer, int offset, int count) => throw new NotImplementedException("Expected on |
| | | 168 | | |
| | | 169 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) = |
| | 0 | 170 | | ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| | | 171 | | |
| | | 172 | | public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| | 0 | 173 | | { |
| | 0 | 174 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 175 | | |
| | 0 | 176 | | if (buffer.IsEmpty || _segments.IsEmpty) |
| | 0 | 177 | | { |
| | 0 | 178 | | return new ValueTask<int>(0); |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | ReadOnlyMemory<byte> segment = _segments.Span[0]; |
| | 0 | 182 | | int toCopy = Math.Min(buffer.Length, segment.Length); |
| | | 183 | | |
| | 0 | 184 | | segment.Span.Slice(0, toCopy).CopyTo(buffer.Span); |
| | | 185 | | |
| | 0 | 186 | | if (toCopy == segment.Length) |
| | 0 | 187 | | { |
| | 0 | 188 | | _segments = _segments.Slice(1); |
| | 0 | 189 | | } |
| | | 190 | | else |
| | 0 | 191 | | { |
| | 0 | 192 | | _segments.Span[0] = segment.Slice(toCopy); |
| | 0 | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | return new ValueTask<int>(toCopy); |
| | 0 | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | public override void Flush() { } |
| | 0 | 199 | | public override void Write(byte[] buffer, int offset, int count) { } |
| | 0 | 200 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => Ta |
| | 0 | 201 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| | | 202 | | |
| | 0 | 203 | | public override long Length => throw new NotImplementedException(); |
| | 0 | 204 | | public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedExcep |
| | 0 | 205 | | public override void SetLength(long value) => throw new NotImplementedException(); |
| | 0 | 206 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); |
| | | 207 | | } |
| | | 208 | | } |