| | | 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.Buffers; |
| | | 5 | | using System.Formats.Nrbf; |
| | | 6 | | using System.Numerics; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.Serialization; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace DotnetFuzzing.Fuzzers |
| | | 12 | | { |
| | | 13 | | internal sealed class NrbfDecoderFuzzer : IFuzzer |
| | | 14 | | { |
| | 0 | 15 | | public string[] TargetAssemblies { get; } = ["System.Formats.Nrbf"]; |
| | | 16 | | |
| | 0 | 17 | | public string[] TargetCoreLibPrefixes => []; |
| | | 18 | | |
| | 0 | 19 | | public string Corpus => "nrbf"; |
| | | 20 | | |
| | | 21 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 22 | | { |
| | 0 | 23 | | Test(bytes, PoisonPagePlacement.Before); |
| | 0 | 24 | | Test(bytes, PoisonPagePlacement.After); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | private static void Test(ReadOnlySpan<byte> bytes, PoisonPagePlacement poisonPagePlacement) |
| | 0 | 28 | | { |
| | 0 | 29 | | using PooledBoundedMemory<byte> inputPoisoned = PooledBoundedMemory<byte>.Rent(bytes, poisonPagePlacement); |
| | | 30 | | |
| | 0 | 31 | | using MemoryStream seekableStream = new(inputPoisoned.Memory.ToArray()); |
| | 0 | 32 | | Test(inputPoisoned.Span, seekableStream); |
| | | 33 | | |
| | | 34 | | // NrbfDecoder has few code paths dedicated to non-seekable streams, let's test them as well. |
| | 0 | 35 | | using NonSeekableStream nonSeekableStream = new(inputPoisoned.Memory.ToArray()); |
| | 0 | 36 | | Test(inputPoisoned.Span, nonSeekableStream); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static void Test(Span<byte> testSpan, Stream stream) |
| | 0 | 40 | | { |
| | 0 | 41 | | if (NrbfDecoder.StartsWithPayloadHeader(testSpan)) |
| | 0 | 42 | | { |
| | 0 | 43 | | HashSet<SerializationRecordId> visited = new(); |
| | 0 | 44 | | Queue<SerializationRecord> queue = new(); |
| | | 45 | | try |
| | 0 | 46 | | { |
| | 0 | 47 | | SerializationRecord record = NrbfDecoder.Decode(stream, out IReadOnlyDictionary<SerializationRecordI |
| | | 48 | | |
| | 0 | 49 | | Assert.Equal(true, recordMap.ContainsKey(record.Id)); // make sure the loop below includes it |
| | 0 | 50 | | foreach (SerializationRecord fromMap in recordMap.Values) |
| | 0 | 51 | | { |
| | 0 | 52 | | visited.Add(fromMap.Id); |
| | 0 | 53 | | queue.Enqueue(fromMap); |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | 0 | 56 | | catch (SerializationException) { /* Reading from the stream encountered invalid NRBF data.*/ } |
| | 0 | 57 | | catch (NotSupportedException) { /* Reading from the stream encountered unsupported records */ } |
| | 0 | 58 | | catch (DecoderFallbackException) { /* Reading from the stream encountered an invalid UTF8 sequence. */ } |
| | 0 | 59 | | catch (EndOfStreamException) { /* The end of the stream was reached before reading SerializationRecordTy |
| | 0 | 60 | | catch (IOException) { /* An I/O error occurred. */ } |
| | | 61 | | |
| | | 62 | | // Lets consume it outside of the try/catch block to not swallow any exceptions by accident. |
| | 0 | 63 | | Consume(visited, queue); |
| | 0 | 64 | | } |
| | | 65 | | else |
| | 0 | 66 | | { |
| | | 67 | | try |
| | 0 | 68 | | { |
| | 0 | 69 | | NrbfDecoder.Decode(stream); |
| | 0 | 70 | | throw new Exception("Decoding supposed to fail!"); |
| | | 71 | | } |
| | 0 | 72 | | catch (SerializationException) { /* Everything has to start with a header */ } |
| | 0 | 73 | | catch (NotSupportedException) { /* Reading from the stream encountered unsupported records */ } |
| | 0 | 74 | | catch (EndOfStreamException) { /* The end of the stream was reached before reading SerializationRecordTy |
| | 0 | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static void Consume(HashSet<SerializationRecordId> visited, Queue<SerializationRecord> queue) |
| | 0 | 79 | | { |
| | 0 | 80 | | while (queue.Count > 0) |
| | 0 | 81 | | { |
| | 0 | 82 | | SerializationRecord serializationRecord = queue.Dequeue(); |
| | | 83 | | |
| | 0 | 84 | | if (serializationRecord is PrimitiveTypeRecord primitiveTypeRecord) |
| | 0 | 85 | | { |
| | 0 | 86 | | ConsumePrimitiveValue(primitiveTypeRecord.Value); |
| | 0 | 87 | | } |
| | 0 | 88 | | else if (serializationRecord is ClassRecord classRecord) |
| | 0 | 89 | | { |
| | 0 | 90 | | foreach (string memberName in classRecord.MemberNames) |
| | 0 | 91 | | { |
| | 0 | 92 | | ConsumePrimitiveValue(memberName); |
| | | 93 | | |
| | 0 | 94 | | Assert.Equal(true, classRecord.HasMember(memberName)); |
| | | 95 | | |
| | | 96 | | object? rawValue; |
| | | 97 | | |
| | | 98 | | try |
| | 0 | 99 | | { |
| | 0 | 100 | | rawValue = classRecord.GetRawValue(memberName); |
| | 0 | 101 | | } |
| | 0 | 102 | | catch (SerializationException ex) when (ex.Message == "Invalid member reference.") |
| | 0 | 103 | | { |
| | | 104 | | // It was a reference to a non-existing record, just continue. |
| | 0 | 105 | | continue; |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | if (rawValue is not null) |
| | 0 | 109 | | { |
| | 0 | 110 | | if (rawValue is SerializationRecord nestedRecord) |
| | 0 | 111 | | { |
| | 0 | 112 | | TryEnqueue(nestedRecord); |
| | 0 | 113 | | } |
| | | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | ConsumePrimitiveValue(rawValue); |
| | 0 | 117 | | } |
| | 0 | 118 | | } |
| | 0 | 119 | | } |
| | 0 | 120 | | } |
| | 0 | 121 | | else if (serializationRecord is ArrayRecord arrayRecord) |
| | 0 | 122 | | { |
| | | 123 | | Type? type; |
| | | 124 | | |
| | | 125 | | try |
| | 0 | 126 | | { |
| | | 127 | | // THIS IS VERY BAD IDEA FOR ANY KIND OF PRODUCT CODE!! |
| | | 128 | | // IT'S USED ONLY FOR THE PURPOSE OF TESTING, DO NOT COPY IT. |
| | 0 | 129 | | type = Type.GetType(arrayRecord.TypeName.AssemblyQualifiedName, throwOnError: false); |
| | 0 | 130 | | if (type is null) |
| | 0 | 131 | | { |
| | 0 | 132 | | continue; |
| | | 133 | | } |
| | 0 | 134 | | } |
| | 0 | 135 | | catch (Exception) // throwOnError passed to GetType does not prevent from all kinds of exceptions |
| | 0 | 136 | | { |
| | | 137 | | // It was some type made up by the Fuzzer. |
| | | 138 | | // Since it's currently impossible to get the array without providing the type, |
| | | 139 | | // we just bail here (in the future we may add an enumerator to ArrayRecord). |
| | 0 | 140 | | continue; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | Array? array; |
| | | 144 | | try |
| | 0 | 145 | | { |
| | 0 | 146 | | array = arrayRecord.GetArray(type); |
| | 0 | 147 | | } |
| | 0 | 148 | | catch (SerializationException ex) when (ex.Message == "Invalid member reference.") |
| | 0 | 149 | | { |
| | | 150 | | // It contained a reference to a non-existing record, just continue. |
| | 0 | 151 | | continue; |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | ReadOnlySpan<int> lengths = arrayRecord.Lengths; |
| | 0 | 155 | | long totalElementsCount = 1; |
| | 0 | 156 | | for (int i = 0; i < arrayRecord.Rank; i++) |
| | 0 | 157 | | { |
| | 0 | 158 | | Assert.Equal(lengths[i], array.GetLength(i)); |
| | 0 | 159 | | totalElementsCount *= lengths[i]; |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | // This array contains indices that are used to get values of multi-dimensional array. |
| | | 163 | | // At the beginning, all values are set to 0, so we start from the first element. |
| | 0 | 164 | | int[] indices = new int[arrayRecord.Rank]; |
| | | 165 | | |
| | 0 | 166 | | long flatIndex = 0; |
| | 0 | 167 | | int maxIndex = 1_000_000; // to prevent long loops timing out the fuzzer |
| | 0 | 168 | | for (; flatIndex < Math.Min(totalElementsCount, maxIndex); flatIndex++) |
| | 0 | 169 | | { |
| | 0 | 170 | | object? rawValue = array.GetValue(indices); |
| | 0 | 171 | | if (rawValue is not null) |
| | 0 | 172 | | { |
| | 0 | 173 | | if (rawValue is SerializationRecord record) |
| | 0 | 174 | | { |
| | 0 | 175 | | TryEnqueue(record); |
| | 0 | 176 | | } |
| | | 177 | | else |
| | 0 | 178 | | { |
| | 0 | 179 | | ConsumePrimitiveValue(rawValue); |
| | 0 | 180 | | } |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | // The loop below is responsible for incrementing the multi-dimensional indices. |
| | | 184 | | // It finds the dimension and then performs an increment. |
| | 0 | 185 | | int dimension = indices.Length - 1; |
| | 0 | 186 | | while (dimension >= 0) |
| | 0 | 187 | | { |
| | 0 | 188 | | indices[dimension]++; |
| | 0 | 189 | | if (indices[dimension] < lengths[dimension]) |
| | 0 | 190 | | { |
| | 0 | 191 | | break; |
| | | 192 | | } |
| | 0 | 193 | | indices[dimension] = 0; |
| | 0 | 194 | | dimension--; |
| | 0 | 195 | | } |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | // We track the flat index to ensure that we have enumerated over all elements. |
| | 0 | 199 | | Assert.Equal(Math.Min(totalElementsCount, maxIndex), flatIndex); |
| | 0 | 200 | | } |
| | | 201 | | else |
| | 0 | 202 | | { |
| | | 203 | | // The map may currently contain it (it may change in the future) |
| | 0 | 204 | | Assert.Equal(SerializationRecordType.BinaryLibrary, serializationRecord.RecordType); |
| | 0 | 205 | | } |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | void TryEnqueue(SerializationRecord record) |
| | 0 | 209 | | { |
| | 0 | 210 | | if (visited.Add(record.Id)) // avoid unbounded recursion |
| | 0 | 211 | | { |
| | 0 | 212 | | queue.Enqueue(record); |
| | 0 | 213 | | } |
| | 0 | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 218 | | private static void ConsumePrimitiveValue(object value) |
| | 0 | 219 | | { |
| | 0 | 220 | | if (value is string text) |
| | 0 | 221 | | Assert.Equal(text, text.ToString()); // we want to touch all elements to see if memory is not corrupted |
| | 0 | 222 | | else if (value is bool boolean) |
| | 0 | 223 | | Assert.Equal(true, Unsafe.BitCast<bool, byte>(boolean) is 1 or 0); // other values are illegal!! |
| | 0 | 224 | | else if (value is sbyte @sbyte) |
| | 0 | 225 | | TestNumber(@sbyte); |
| | 0 | 226 | | else if (value is byte @byte) |
| | 0 | 227 | | TestNumber(@byte); |
| | 0 | 228 | | else if (value is char character) |
| | 0 | 229 | | TestNumber(character); |
| | 0 | 230 | | else if (value is short @short) |
| | 0 | 231 | | TestNumber(@short); |
| | 0 | 232 | | else if (value is ushort @ushort) |
| | 0 | 233 | | TestNumber(@ushort); |
| | 0 | 234 | | else if (value is int integer) |
| | 0 | 235 | | TestNumber(integer); |
| | 0 | 236 | | else if (value is uint @uint) |
| | 0 | 237 | | TestNumber(@uint); |
| | 0 | 238 | | else if (value is long @long) |
| | 0 | 239 | | TestNumber(@long); |
| | 0 | 240 | | else if (value is ulong @ulong) |
| | 0 | 241 | | TestNumber(@ulong); |
| | 0 | 242 | | else if (value is float @float) |
| | 0 | 243 | | { |
| | 0 | 244 | | if (!float.IsNaN(@float) && !float.IsInfinity(@float)) |
| | 0 | 245 | | { |
| | 0 | 246 | | TestNumber(@float); |
| | 0 | 247 | | } |
| | 0 | 248 | | } |
| | 0 | 249 | | else if (value is double @double) |
| | 0 | 250 | | { |
| | 0 | 251 | | if (!double.IsNaN(@double) && !double.IsInfinity(@double)) |
| | 0 | 252 | | { |
| | 0 | 253 | | TestNumber(@double); |
| | 0 | 254 | | } |
| | 0 | 255 | | } |
| | 0 | 256 | | else if (value is decimal @decimal) |
| | 0 | 257 | | TestNumber(@decimal); |
| | 0 | 258 | | else if (value is nint @nint) |
| | 0 | 259 | | TestNumber(@nint); |
| | 0 | 260 | | else if (value is nuint @nuint) |
| | 0 | 261 | | TestNumber(@nuint); |
| | 0 | 262 | | else if (value is DateTime datetime) |
| | 0 | 263 | | Assert.Equal(true, datetime >= DateTime.MinValue && datetime <= DateTime.MaxValue); |
| | 0 | 264 | | else if (value is TimeSpan timeSpan) |
| | 0 | 265 | | Assert.Equal(true, timeSpan >= TimeSpan.MinValue && timeSpan <= TimeSpan.MaxValue); |
| | | 266 | | else |
| | 0 | 267 | | throw new InvalidOperationException(); |
| | | 268 | | |
| | | 269 | | static void TestNumber<T>(T value) where T : IComparable<T>, IMinMaxValue<T> |
| | 0 | 270 | | { |
| | 0 | 271 | | if (value.CompareTo(T.MinValue) < 0) |
| | 0 | 272 | | { |
| | 0 | 273 | | throw new Exception($"Expected {value} to be more or equal {T.MinValue}, {value.CompareTo(T.MinValue |
| | | 274 | | } |
| | 0 | 275 | | if (value.CompareTo(T.MaxValue) > 0) |
| | 0 | 276 | | { |
| | 0 | 277 | | throw new Exception($"Expected {value} to be less or equal {T.MaxValue}, {value.CompareTo(T.MaxValue |
| | | 278 | | } |
| | 0 | 279 | | } |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | private sealed class NonSeekableStream : MemoryStream |
| | | 283 | | { |
| | 0 | 284 | | public NonSeekableStream(byte[] buffer) : base(buffer) { } |
| | 0 | 285 | | public override bool CanSeek => false; |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |