Skip to content

Integer Overflow Causing HTTP1 Connections to Fail in Kestrel #41756

@nerddtvg

Description

@nerddtvg

When Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions is configured with MaxRequestHeadersTotalSize as int.MaxValue, the assignment in Http1Connection.cs causes an integer overflow prior to conversion to long. This overflowed value is passed through to the header parser which uses the negative value as a string length parameter. This causes 500 errors and connections to fail.

_remainingRequestHeadersBytesAllowed = ServerOptions.Limits.MaxRequestHeadersTotalSize + 2;

services.Configure<Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions>(options =>
{
  options.Limits.MaxRequestHeadersTotalSize = int.MaxValue;
});
_remainingRequestHeadersBytesAllowed = ServerOptions.Limits.MaxRequestHeadersTotalSize + 2;

Type int at its max value + 2 is -2147483647 after being set:

image

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Connection processing ended abnormally.
      System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'length')
         at System.ThrowHelper.ThrowStartOrEndArgumentValidationException(Int64 start)
         at System.Buffers.ReadOnlySequence`1.Slice(SequencePosition start, Int64 length)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.<TakeMessageHeaders>g__TrimAndTakeMessageHeaders|41_0(SequenceReader`1& reader, Boolean trailers)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeMessageHeaders(SequenceReader`1& reader, Boolean trailers)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(SequenceReader`1& reader)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
Microsoft.AspNetCore.Server.Kestrel: Warning: Connection processing ended abnormally.

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'length')
   at System.ThrowHelper.ThrowStartOrEndArgumentValidationException(Int64 start)
   at System.Buffers.ReadOnlySequence`1.Slice(SequencePosition start, Int64 length)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.<TakeMessageHeaders>g__TrimAndTakeMessageHeaders|41_0(SequenceReader`1& reader, Boolean trailers)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeMessageHeaders(SequenceReader`1& reader, Boolean trailers)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(SequenceReader`1& reader)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)

Metadata

Metadata

Assignees

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions