Repository navigation
Always Encrypted: Validate attestation component lengths before allocation - #5162
Conversation
…ation Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Security-sensitive HGS framing changes have only synthetic coverage, leaving live HGS/Azure compatibility and .NET Framework test execution unverified and requiring human review or enclave-enabled integration results.
No open issues 🎉
What changed in this PR
Hardens Always Encrypted attestation parsing across HGS, Azure and None providers by validating component lengths before allocation or copying, without changing public APIs.
Changes:
- Adds shared, overflow-safe buffer checks and validates nested HGS report sizes.
- Reuses the shared Diffie-Hellman parser in the None provider.
- Adds malformed-response, allocation and valid-response tests, plus documentation.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/AttestationTestData.cs | Adds response builders and allocation assertions. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/NoneAttestationEnclaveProviderTests.cs | Tests invalid responses and sync/async session creation and caching. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/EnclaveReportPackageTests.cs | Tests nested report bounds and signed-byte preservation. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/EnclaveHelpersTests.cs | Tests slice boundaries and rejection before allocation. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/EnclaveDiffieHellmanInfoTests.cs | Tests key-exchange lengths and exact boundaries. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AzureAttestationInfoTests.cs | Tests Azure response framing and component preservation. |
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AttestationInfoTests.cs | Tests HGS response framing and component preservation. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/VirtualSecureModeEnclaveProvider.cs | Adds shared bounds checks and HGS report validation. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/NoneAttestationEnclaveProvider.cs | Validates the header and reuses shared key-exchange parsing. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AzureAttestationBasedEnclaveProvider.cs | Validates Azure component lengths before copying. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AlwaysEncryptedEnclaveProviderUtils.cs | Validates combined key and signature lengths before allocation. |
| doc/always-encrypted-async.md | Documents bounds checking across sync and async paths. |
Review effort: Balanced
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5162 +/- ##
==========================================
+ Coverage 71.17% 73.51% +2.34%
==========================================
Files 315 315
Lines 46140 71467 +25327
==========================================
+ Hits 32841 52542 +19701
- Misses 13299 18925 +5626
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:
|
saurabh500
left a comment
There was a problem hiding this comment.
Reviewed the bounds validation across HGS, Azure, and None attestation response parsing. No actionable findings; current PR checks pass.
…ation (#5162) Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> [6.1] Applied None-provider validation to the synchronous parser (no async provider path on 6.1); removed async-only tests and the unshipped doc change; included the EnclaveDiffieHellmanInfo.Size precedence fix from #4346 so valid responses satisfy the existing offset assertion. (cherry picked from commit 0eeb306)
…ation (#5162) Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> [7.0] Applied None-provider validation to the synchronous parser (no async provider path on 7.0); removed async-only tests and the unshipped doc change; included the EnclaveDiffieHellmanInfo.Size precedence fix from #4346. (cherry picked from commit 0eeb306)
…ation (#5162) Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> [7.1] Applied None-provider validation to the synchronous parser (no async provider path on 7.1); removed async-only tests and the unshipped doc change. (cherry picked from commit 0eeb306)
…lengths before allocation (#5175) * Cherry-pick of #5162 requires manual resolution To resolve, run: git cherry-pick 0eeb306 * Always Encrypted: Validate attestation component lengths before allocation (#5162) Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> [7.0] Applied None-provider validation to the synchronous parser (no async provider path on 7.0); removed async-only tests and the unshipped doc change; included the EnclaveDiffieHellmanInfo.Size precedence fix from #4346. (cherry picked from commit 0eeb306) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Paul Medynski <31868385+paulmedynski@users.noreply.github.com>
…ation (#5162) (#5173) Check attestation response component lengths, offsets and arithmetic against the enclosing buffer before allocating or copying in the shared helpers and the HGS, Azure and None providers. HGS enclave report packages must also be self-consistent. The None provider now reuses the shared Diffie-Hellman parser. Valid responses keep the same cryptographic verification and session caching. Co-authored-by: Paul Medynski <31868385+paulmedynski@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Description
Adds defensive bounds checking to enclave attestation response parsing. Component lengths in HGS, Azure and None attestation responses were previously used to allocate and copy buffers before being validated against the enclosing response.
EnclaveHelpers.ValidateBufferRangeand uses it inTakeBytesAndAdvance, so every component copy checks its offset and length, using overflow-safe arithmetic.EnclaveDiffieHellmanInfo, HGSAttestationInfoandAzureAttestationInforeject negative lengths and validate the combined component lengths against the remaining buffer before allocating.EnclaveReportPackagevalidates its header and requires the package size, signed statement size and report size to be self-consistent.EnclaveDiffieHellmanInfoparser instead of its own copy of that logic.ArgumentExceptionmessages (Data_InvalidOffsetLength,VerifyEnclaveReportFormatFailed); no new resources.doc/always-encrypted-async.md.Compatibility
EnclaveReportPackage.GetSizeInPayload()already returned the declared report size.ArgumentException,IndexOutOfRangeExceptionorOverflowException, sometimes after a large allocation. They now fail before allocating, withArgumentException(Data_InvalidOffsetLengthorVerifyEnclaveReportFormatFailed). Azure attestation errors are still wrapped bySQL.AttestationFailed, though the inner message text may differ. HGS and None errors surface asArgumentException, as several malformed inputs already did.PackageSizemust equal the header, signed statement and signature sizes combined, and the report must fit inside the signed statement. Conforming servers already meet this. A server that sent an inconsistent header was relying on unvalidated parsing, and its attestation will now fail. Existing VBS attestation tests pass unchanged.Issues
None.
Testing
69 new unit test cases in
EnclaveHelpersTests,EnclaveDiffieHellmanInfoTests,EnclaveReportPackageTests,AttestationInfoTests,AzureAttestationInfoTestsandNoneAttestationEnclaveProviderTests, with shared builders inUtilities/AttestationTestData.cs. They cover truncated, negative, oversized and boundary lengths, plus valid responses. Before the fix, 17 of 45 initial cases failed, and five regressions observed allocations of roughly 1 MiB.dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net10.0 --filter '(FullyQualifiedName~EnclaveHelpersTests|FullyQualifiedName~EnclaveDiffieHellmanInfoTests|FullyQualifiedName~EnclaveReportPackageTests|FullyQualifiedName~NoneAttestationEnclaveProviderTests|FullyQualifiedName~AzureAttestationInfoTests|FullyQualifiedName~AttestationInfoTests|FullyQualifiedName~VirtualSecureModeEnclaveProviderTest|FullyQualifiedName~AzureAttestationEnclaveProviderShould|FullyQualifiedName~EnclaveDelegateAsyncTests|FullyQualifiedName~SqlColumnEncryptionEnclaveProviderAsyncShould|FullyQualifiedName~SqlCommandEnclaveAsyncTests|FullyQualifiedName~EnclaveSessionCacheTest)&category!=failing&category!=flaky&category!=interactive'— passed (234/234, 0 skipped), including existing signature-binding, async and session-cache tests.dotnet build build.proj -t:BuildSqlClient -p:Configuration=Debug— passed, 0 warnings.dotnet build src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net462— passed, 0 warnings.Not run locally:
net462test execution and live HGS/Azure attestation with an enclave-enabled SQL Server.Guidelines
Please review the contribution guidelines before submitting a pull request: