[7.0.2 Cherry-pick] Add TDS token data length bounds checks (#4340)#4358
[7.0.2 Cherry-pick] Add TDS token data length bounds checks (#4340)#4358github-actions[bot] wants to merge 2 commits into
Conversation
aae16d0 to
cae627e
Compare
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Cherry-picks the TDS token-length bounds checking work onto release/7.0 to harden the client against malicious servers sending spoofed lengths that could otherwise trigger unbounded byte[] allocations.
Changes:
- Adds new protocol-reasonable maximums in
TdsEnumsand applies new length bounds checks in multiple TDS parsing sites. - Introduces a
SQL.ParsingErrorLength(..., uint)helper overload to report oversized lengths consistently. - Extends the simulated TDS server test infrastructure and adds new unit tests to validate the new bounds checks.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/TdsTokenBoundsTests.cs | Adds simulated-server tests for multiple new bounds-check sites. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/FeatureExtensionNegotiationTests.cs | Adds clarification comment for serialized test collection usage. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/FeatureExtAckBoundsTests.cs | Adds bounds tests for FeatureExtAck token data length. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionRoutingTestsAzure.cs | Whitespace/comment updates. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionRoutingTests.cs | Whitespace/comment updates. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionReadOnlyRoutingTests.cs | Whitespace/comment updates. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs | Whitespace/comment updates. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/SimulatedServerTests/ConnectionEnhancedRoutingTests.cs | Comment update about test collection serialization. |
| src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTdsServer.cs | Adds OnSQLBatchCompleted hook to enable post-login token injection in tests. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs | Adds bounds checks in parser paths to prevent unsafe allocations from spoofed lengths. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs | Adds new max-length constants used by parser bounds checks. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs | Adds ParsingErrorLength(..., uint) helper overload. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Connection/SqlConnectionInternal.cs | Adds bounds check preventing out-of-range copy in SRECOVERY parsing. |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## release/7.0 #4358 +/- ##
===============================================
- Coverage 73.11% 65.79% -7.33%
===============================================
Files 280 275 -5
Lines 43026 65883 +22857
===============================================
+ Hits 31457 43345 +11888
- Misses 11569 22538 +10969
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Cherry-pick of #4340 to
release/7.0This is a cherry-pick of #4340 (commit b6c0569) onto the
release/7.0branch for the 7.0.2 hotfix.Summary
Adds bounds checks on data lengths read from the TDS wire to prevent unbounded
byte[]allocation from a malicious server.All checks throw
SQL.ParsingErrorLength(ParsingErrorState.CorruptedTdsStream, ...)when a spoofed length exceeds protocol-reasonable maximums.Changes
TdsEnums.csMaxTokenDataLength(1 MB),MaxPromoteTransactionLength(64 KB),MaxDateTimeLength(10)SqlUtil.csParsingErrorLength()helperTdsParser.csSqlConnectionInternal.csloginTimeoutintoTdsParserfor connection-level timeout propagationGenericTdsServer.csFeatureExtAckDataOverride+FeatureExtAckFeatureIdOverrideproperties for test injectionTdsTokenBoundsTests.csFeatureExtAckBoundsTests.csConnection*Tests.csCherry-pick resolution notes
The cherry-pick required manual conflict resolution in
TdsParser.csdue to differences betweenmainandrelease/7.0:mainbut still present onrelease/7.0(fixed as part of this cherry-pick)SQLVECTORcase used an older two-line allocation pattern (byte[] b = new byte[length]+out b) which was updated to the inlineout byte[] bform consistent with the bounds-check patternAll substantive logic changes from the original PR are present and verified.