Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2c217a3
First integration of Resolver sources
rzikm Oct 4, 2024
704e33c
Allow mocking
rzikm Oct 4, 2024
aff6d17
Fix dispose
rzikm Oct 4, 2024
15db8a1
Fix tests
rzikm Oct 4, 2024
6bda235
Actually run ResolvConf tests
rzikm Oct 4, 2024
6fc61e0
Add retry functionality
rzikm Oct 8, 2024
2b6d91e
Add logging
rzikm Oct 10, 2024
2137954
Fix Microsoft.Extensions.ServiceDiscovery.Yarp.Tests
rzikm Oct 10, 2024
c204b01
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Nov 26, 2024
2fc7de4
Activity WIP
rzikm Nov 26, 2024
9b4de29
Fix reading CNAME with pointers in domain name segments
rzikm Nov 28, 2024
3ad5a75
Reenable telemetry
rzikm Nov 28, 2024
40f8f9b
Last changes to telemetry
rzikm Nov 28, 2024
e385605
Use strong RNG for Transaction ID generation
rzikm Jan 15, 2025
3cf0a9d
Check against too long domain name
rzikm Feb 3, 2025
6016e41
Disallow pointer to pointer.
rzikm Feb 3, 2025
f8c189d
Code review feedback (easy fixes)
rzikm Feb 3, 2025
d929889
More code review feedback
rzikm Feb 6, 2025
00e41d7
Fix increment
rzikm Feb 6, 2025
7267fb7
Detect CNAME loops
rzikm Feb 7, 2025
c1b76d3
Handle empty Tcp fallback responses and lack of TCP failover support …
rzikm Feb 7, 2025
86490cc
Dispose result.Response when overwritten by another result.
rzikm Feb 7, 2025
92b2905
Move DnsMessageHeader parsing to DnsPrimitives
rzikm Feb 11, 2025
fdf1904
Rework and add tests to retries and failover
rzikm Feb 12, 2025
61814e7
Test failover after exhausting attempts on one server
rzikm Feb 13, 2025
82a8108
Streamline options, remove unsupported options from ResolverOptions
rzikm Feb 13, 2025
fa627fd
Better handling of malformed responses
rzikm Feb 13, 2025
4ec79ca
Code review feedback
rzikm Mar 25, 2025
123375d
More feedback
rzikm Mar 27, 2025
6311616
Guarantee linear parsing of CNAME chains
rzikm Mar 27, 2025
c57a8c5
Fix decoding compressed CNAME in TCP fallback
rzikm Mar 27, 2025
83ca958
More code coverage
rzikm May 12, 2025
03e6a99
Correctly handle IDN names
rzikm May 15, 2025
46418fe
Minor fixes
rzikm May 22, 2025
4075e4f
Add Fuzzing tests for DNS resolver
rzikm May 23, 2025
56cd169
Improve fuzzing of EncodedDomainName
rzikm May 23, 2025
a19e8a2
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Jun 10, 2025
5d5edae
Remove commented out code
rzikm Jun 10, 2025
3c387e9
Fix build
rzikm Jun 10, 2025
c6e3d63
Fix Yarp service discovery tests
rzikm Jun 10, 2025
e66229a
Lazy TCP socket allocation in LoopbackTests
rzikm Jun 12, 2025
da018f7
LoopbackTestBaseLogging
rzikm Jun 12, 2025
2697412
fixup! LoopbackTestBaseLogging
rzikm Jun 12, 2025
214cc9b
Downgrade SharpFuzz
rzikm Jun 17, 2025
0d891cb
Fix buffer use after returning to pool
rzikm Jun 18, 2025
46cf262
Merge branch 'main' into managed-resolver
rzikm Jun 18, 2025
35a5809
Fix build
rzikm Jun 18, 2025
fcf18cf
Merge branch 'main' into managed-resolver
rzikm Jun 25, 2025
df785d1
Remove DnsClient package ref
rzikm Jun 25, 2025
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
Prev Previous commit
Next Next commit
Disallow pointer to pointer.
  • Loading branch information
rzikm committed Feb 3, 2025
commit 6016e41982dcb99b906ac2de638d320ee9db76d1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Extensions.ServiceDiscovery.Dns.Resolver;

internal static class DnsPrimitives
{
// Maximum length of a domain name in ASCII (excluding trailing dot)
internal const int MaxDomainNameLength = 253;

internal static bool TryWriteQName(Span<byte> destination, string name, out int written)
Expand Down Expand Up @@ -59,7 +60,7 @@ internal static bool TryWriteQName(Span<byte> destination, string name, out int
return true;
}

private static bool TryReadQNameCore(StringBuilder sb, ReadOnlySpan<byte> messageBuffer, int offset, out int bytesRead)
private static bool TryReadQNameCore(StringBuilder sb, ReadOnlySpan<byte> messageBuffer, int offset, out int bytesRead, bool canStartWithPointer = true)
{
//
// domain name can be either
Expand All @@ -72,10 +73,12 @@ private static bool TryReadQNameCore(StringBuilder sb, ReadOnlySpan<byte> messag
//
// It is not specified by the RFC if pointers must be backwards only,
// the code below prohibits forward (and self) pointers to avoid
// infinite loops
// infinite loops. It also allows pointers only to point to a
// label, not to another pointer.
//

bytesRead = 0;
bool allowPointer = canStartWithPointer;

if (offset < 0 || offset >= messageBuffer.Length)
{
Expand All @@ -98,52 +101,51 @@ private static bool TryReadQNameCore(StringBuilder sb, ReadOnlySpan<byte> messag
return true;
}

if (currentOffset + 1 + length < messageBuffer.Length)
if (currentOffset + 1 + length >= messageBuffer.Length)
{
// read next label/segment
if (sb.Length > 0)
{
sb.Append('.');
}

sb.Append(Encoding.ASCII.GetString(messageBuffer.Slice(currentOffset + 1, length)));

if (sb.Length > MaxDomainNameLength)
{
// domain name is too long
return false;
}

currentOffset += 1 + length;
bytesRead += 1 + length;
// too many labels or truncated data
break;
}
else

// read next label/segment
if (sb.Length > 0)
{
// truncated data
break;
sb.Append('.');
}

sb.Append(Encoding.ASCII.GetString(messageBuffer.Slice(currentOffset + 1, length)));

if (sb.Length > MaxDomainNameLength)
{
// domain name is too long
return false;
}

currentOffset += 1 + length;
bytesRead += 1 + length;

// we read a label, they can be followed by pointer.
allowPointer = true;
}
else if ((length & 0xC0) == 0xC0)
{
// pointer, together with next byte gives the offset of the true label
if (currentOffset + 1 < messageBuffer.Length)
{
bytesRead += 2;
int pointer = ((length & 0x3F) << 8) | messageBuffer[currentOffset + 1];

// we prohibit self-references and forward pointers to avoid
// infinite loops, we do this by truncating the
// messageBuffer at the offset where we started reading the
// name. We also ignore the bytesRead from the recursive
// call, as we are only interested on how many bytes we read
// from the initial start of the name.
return TryReadQNameCore(sb, messageBuffer.Slice(0, offset), pointer, out int _);
}
else
if (!allowPointer || currentOffset + 1 >= messageBuffer.Length)
{
// truncated data
// pointer to pointer or truncated data
break;
}

bytesRead += 2;
int pointer = ((length & 0x3F) << 8) | messageBuffer[currentOffset + 1];

// we prohibit self-references and forward pointers to avoid
// infinite loops, we do this by truncating the
// messageBuffer at the offset where we started reading the
// name. We also ignore the bytesRead from the recursive
// call, as we are only interested on how many bytes we read
// from the initial start of the name.
return TryReadQNameCore(sb, messageBuffer.Slice(0, offset), pointer, out int _, false);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void TryReadQName_PointerTruncated_Fails()
public void TryReadQName_ForwardPointer_Fails()
{
// www->[ptr to example.com], [7B padding], example.com.
Span<byte> data = "\x03www\x00\0x000cpaddingexample\x0003com\x00"u8.ToArray();
Span<byte> data = "\x03www\x00\x000dpadding\x0007example\x0003com\x00"u8.ToArray();
data[4] = 0xc0;

Assert.False(DnsPrimitives.TryReadQName(data, 0, out _, out _));
Expand All @@ -107,17 +107,29 @@ public void TryReadQName_ForwardPointer_Fails()
public void TryReadQName_PointerToSelf_Fails()
{
// www->[ptr to www->...]
Span<byte> data = "\x0003www\x00c0"u8.ToArray();
Span<byte> data = "\x0003www\0\0"u8.ToArray();
data[4] = 0xc0;

Assert.False(DnsPrimitives.TryReadQName(data, 0, out _, out _));
}

[Fact]
public void TryReadQName_PointerToPointer_Fails()
{
// com, example[->com], example2[->[->com]]
Span<byte> data = "\x0003com\0\x0007example\0\0\x0008example2\0\0"u8.ToArray();
data[13] = 0xc0;
data[14] = 0x00; // -> com
data[24] = 0xc0;
data[25] = 13; // -> -> com

Assert.False(DnsPrimitives.TryReadQName(data, 15, out _, out _));
}

[Fact]
public void TryReadQName_ReservedBits()
{
Span<byte> data = "\x0003www\x00c0"u8.ToArray();
data[4] = 0xc0;
data[0] = 0x40;

Assert.False(DnsPrimitives.TryReadQName(data, 0, out _, out _));
Expand Down