Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 1d26e56

Browse files
committed
Protect from length overflows
1 parent ea823bf commit 1d26e56

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,25 @@ public int GetLength(MemoryPoolIterator2 end)
514514
var block = _block;
515515
var index = _index;
516516
var length = 0;
517-
while (true)
517+
518+
checked
518519
{
519-
if (block == end._block)
520-
{
521-
return length + end._index - index;
522-
}
523-
else if (block.Next == null)
520+
while (true)
524521
{
525-
throw new InvalidOperationException("end did not follow iterator");
526-
}
527-
else
528-
{
529-
length += block.End - index;
530-
block = block.Next;
531-
index = block.Start;
522+
if (block == end._block)
523+
{
524+
return length + end._index - index;
525+
}
526+
else if (block.Next == null)
527+
{
528+
throw new InvalidOperationException("end did not follow iterator");
529+
}
530+
else
531+
{
532+
length += block.End - index;
533+
block = block.Next;
534+
index = block.Start;
535+
}
532536
}
533537
}
534538
}

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public unsafe static string GetAsciiString(this MemoryPoolIterator2 start, Memor
8181
{
8282
return null;
8383
}
84+
else if (length > int.MaxValue - 12)
85+
{
86+
// protect unrolled loop from using negative values in extremis
87+
throw new ArgumentOutOfRangeException(nameof(end));
88+
}
8489

8590
// Bytes out of the range of ascii are treated as "opaque data"
8691
// and kept in string as a char value that casts to same input byte value

0 commit comments

Comments
 (0)