|
| 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.Runtime.Serialization; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace DotnetFuzzing.Fuzzers |
| 10 | +{ |
| 11 | + internal sealed class NrbfDecoderFuzzer : IFuzzer |
| 12 | + { |
| 13 | + public string[] TargetAssemblies { get; } = ["System.Formats.Nrbf"]; |
| 14 | + |
| 15 | + public string[] TargetCoreLibPrefixes => []; |
| 16 | + |
| 17 | + public string Dictionary => "nrbfdecoder.dict"; |
| 18 | + |
| 19 | + public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| 20 | + { |
| 21 | + using PooledBoundedMemory<byte> inputPoisonedAfter = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.After); |
| 22 | + using PooledBoundedMemory<byte> inputPoisonedBefore = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.Before); |
| 23 | + using MemoryStream streamAfter = new MemoryStream(inputPoisonedAfter.Memory.ToArray()); |
| 24 | + using MemoryStream streamBefore = new MemoryStream(inputPoisonedBefore.Memory.ToArray()); |
| 25 | + |
| 26 | + Test(inputPoisonedAfter.Span, streamAfter); |
| 27 | + Test(inputPoisonedBefore.Span, streamBefore); |
| 28 | + } |
| 29 | + |
| 30 | + private static void Test(Span<byte> testSpan, MemoryStream stream) |
| 31 | + { |
| 32 | + if (NrbfDecoder.StartsWithPayloadHeader(testSpan)) |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + SerializationRecord record = NrbfDecoder.Decode(stream, out IReadOnlyDictionary<SerializationRecordId, SerializationRecord> recordMap); |
| 37 | + switch (record.RecordType) |
| 38 | + { |
| 39 | + case SerializationRecordType.ArraySingleObject: |
| 40 | + SZArrayRecord<object?> arrayObj = (SZArrayRecord<object?>)record; |
| 41 | + object?[] objArray = arrayObj.GetArray(); |
| 42 | + Assert.Equal(arrayObj.Length, objArray.Length); |
| 43 | + Assert.Equal(1, arrayObj.Rank); |
| 44 | + break; |
| 45 | + case SerializationRecordType.ArraySingleString: |
| 46 | + SZArrayRecord<string?> arrayString = (SZArrayRecord<string?>)record; |
| 47 | + string?[] array = arrayString.GetArray(); |
| 48 | + Assert.Equal(arrayString.Length, array.Length); |
| 49 | + Assert.Equal(1, arrayString.Rank); |
| 50 | + Assert.Equal(true, arrayString.TypeNameMatches(typeof(string[]))); |
| 51 | + break; |
| 52 | + case SerializationRecordType.ArraySinglePrimitive: |
| 53 | + case SerializationRecordType.BinaryArray: |
| 54 | + ArrayRecord arrayBinary = (ArrayRecord)record; |
| 55 | + Assert.NotNull(arrayBinary.TypeName); |
| 56 | + break; |
| 57 | + case SerializationRecordType.BinaryObjectString: |
| 58 | + _ = ((PrimitiveTypeRecord<string>)record).Value; |
| 59 | + break; |
| 60 | + case SerializationRecordType.ClassWithId: |
| 61 | + case SerializationRecordType.ClassWithMembersAndTypes: |
| 62 | + case SerializationRecordType.SystemClassWithMembersAndTypes: |
| 63 | + ClassRecord classRecord = (ClassRecord)record; |
| 64 | + Assert.NotNull(classRecord.TypeName); |
| 65 | + |
| 66 | + foreach (string name in classRecord.MemberNames) |
| 67 | + { |
| 68 | + Assert.Equal(true, classRecord.HasMember(name)); |
| 69 | + } |
| 70 | + break; |
| 71 | + case SerializationRecordType.MemberPrimitiveTyped: |
| 72 | + PrimitiveTypeRecord primitiveType = (PrimitiveTypeRecord)record; |
| 73 | + Assert.NotNull(primitiveType.Value); |
| 74 | + break; |
| 75 | + case SerializationRecordType.MemberReference: |
| 76 | + Assert.NotNull(record.TypeName); |
| 77 | + break; |
| 78 | + case SerializationRecordType.BinaryLibrary: |
| 79 | + Assert.Equal(false, record.Id.Equals(default)); |
| 80 | + break; |
| 81 | + case SerializationRecordType.ObjectNull: |
| 82 | + case SerializationRecordType.ObjectNullMultiple: |
| 83 | + case SerializationRecordType.ObjectNullMultiple256: |
| 84 | + Assert.Equal(default, record.Id); |
| 85 | + break; |
| 86 | + case SerializationRecordType.MessageEnd: |
| 87 | + case SerializationRecordType.SerializedStreamHeader: |
| 88 | + // case SerializationRecordType.ClassWithMembers: will cause NotSupportedException |
| 89 | + // case SerializationRecordType.SystemClassWithMembers: will cause NotSupportedException |
| 90 | + default: |
| 91 | + throw new Exception("Unexpected RecordType"); |
| 92 | + } |
| 93 | + } |
| 94 | + catch (SerializationException) { /* Reading from the stream encountered invalid NRBF data.*/ } |
| 95 | + catch (NotSupportedException) { /* Reading from the stream encountered unsupported records */ } |
| 96 | + catch (DecoderFallbackException) { /* Reading from the stream encountered an invalid UTF8 sequence. */ } |
| 97 | + catch (EndOfStreamException) { /* The end of the stream was reached before reading SerializationRecordType.MessageEnd record. */ } |
| 98 | + catch (IOException) { /* An I/O error occurred. */ } |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + try |
| 103 | + { |
| 104 | + NrbfDecoder.Decode(stream); |
| 105 | + throw new Exception("Decoding supposed to fail!"); |
| 106 | + } |
| 107 | + catch (SerializationException) { /* Everything has to start with a header */ } |
| 108 | + catch (NotSupportedException) { /* Reading from the stream encountered unsupported records */ } |
| 109 | + catch (EndOfStreamException) { /* The end of the stream was reached before reading SerializationRecordType.MessageEnd record. */ } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments