Skip to content

Always Encrypted: Validate attestation component lengths before allocation - #5162

Merged
paulmedynski merged 1 commit into
mainfrom
dev/paul/hardening/attestation-lengths
Dec 3, 2026
Merged

paulmedynski merged 1 commit into
mainfrom
dev/paul/hardening/attestation-lengths

Conversation

@paulmedynski

@paulmedynski paulmedynski commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • Adds EnclaveHelpers.ValidateBufferRange and uses it in TakeBytesAndAdvance, so every component copy checks its offset and length, using overflow-safe arithmetic.
  • EnclaveDiffieHellmanInfo, HGS AttestationInfo and AzureAttestationInfo reject negative lengths and validate the combined component lengths against the remaining buffer before allocating.
  • HGS EnclaveReportPackage validates its header and requires the package size, signed statement size and report size to be self-consistent.
  • The None provider reuses the shared EnclaveDiffieHellmanInfo parser instead of its own copy of that logic.
  • Malformed responses fail with the existing ArgumentException messages (Data_InvalidOffsetLength, VerifyEnclaveReportFormatFailed); no new resources.
  • Documented in doc/always-encrypted-async.md.

Compatibility

  • Valid responses: no public API changes. Valid responses parse to the same values, with the same cryptographic verification and enclave session caching. Offsets past the HGS enclave report are unchanged, because EnclaveReportPackage.GetSizeInPayload() already returned the declared report size.
  • Malformed responses: these used to fail with whatever the copy or conversion threw, for example ArgumentException, IndexOutOfRangeException or OverflowException, sometimes after a large allocation. They now fail before allocating, with ArgumentException (Data_InvalidOffsetLength or VerifyEnclaveReportFormatFailed). Azure attestation errors are still wrapped by SQL.AttestationFailed, though the inner message text may differ. HGS and None errors surface as ArgumentException, as several malformed inputs already did.
  • Stricter HGS checks: the enclave report package header's PackageSize must 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.
  • Backports: the affected code is in the shared source on 6.1, 7.0, 7.1 and 8.0. No compatibility switch is needed.

Issues

None.

Testing

69 new unit test cases in EnclaveHelpersTests, EnclaveDiffieHellmanInfoTests, EnclaveReportPackageTests, AttestationInfoTests, AzureAttestationInfoTests and NoneAttestationEnclaveProviderTests, with shared builders in Utilities/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: net462 test execution and live HGS/Azure attestation with an enclave-enabled SQL Server.

Guidelines

Please review the contribution guidelines before submitting a pull request:

…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>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 13:50
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Dec 1, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@paulmedynski paulmedynski added Hotfix 6.1.9 PRs targeting main that should be backported to release/6.1 for 6.1.9. Hotfix 7.0.5 PRs targeting main that should be backported to release/7.0 for 7.0.5 Hotfix 7.1.3 PRs targeting main that should be backported to release/7.1 for 7.1.3. Hotfix 8.0.1 PRs that must be backported to release/8.0 labels Dec 1, 2026
@paulmedynski paulmedynski moved this from To triage to In progress in SqlClient Board Dec 1, 2026
@paulmedynski paulmedynski added this to the 8.1.0-preview1 milestone Dec 1, 2026
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.51%. Comparing base (b3eefc2) to head (ef8bb53).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...Data/SqlClient/VirtualSecureModeEnclaveProvider.cs 93.10% 2 Missing ⚠️
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     
Flag Coverage Δ
CI-SqlClient ?
sqlclient-pr 73.51% <95.83%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@paulmedynski
paulmedynski marked this pull request as ready for review December 1, 2026 15:17
@paulmedynski
paulmedynski requested a review from a team as a code owner December 1, 2026 15:17
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board Dec 1, 2026
@paulmedynski
paulmedynski enabled auto-merge (squash) December 2, 2026 12:20

@saurabh500 saurabh500 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the bounds validation across HGS, Azure, and None attestation response parsing. No actionable findings; current PR checks pass.

@paulmedynski
paulmedynski merged commit 0eeb306 into main Dec 3, 2026
60 of 61 checks passed
@paulmedynski
paulmedynski deleted the dev/paul/hardening/attestation-lengths branch December 3, 2026 07:01
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Dec 3, 2026
@paulmedynski paulmedynski added Hotfix 6.1.9 PRs targeting main that should be backported to release/6.1 for 6.1.9. Hotfix 8.0.1 PRs that must be backported to release/8.0 Hotfix 7.0.5 PRs targeting main that should be backported to release/7.0 for 7.0.5 and removed Hotfix 6.1.9 PRs targeting main that should be backported to release/6.1 for 6.1.9. Hotfix 8.0.1 PRs that must be backported to release/8.0 Hotfix 7.0.5 PRs targeting main that should be backported to release/7.0 for 7.0.5 Hotfix 7.1.3 PRs targeting main that should be backported to release/7.1 for 7.1.3. labels Dec 3, 2026
@paulmedynski paulmedynski added the Hotfix 7.1.3 PRs targeting main that should be backported to release/7.1 for 7.1.3. label Dec 3, 2026
paulmedynski added a commit that referenced this pull request Dec 3, 2026
…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)
paulmedynski added a commit that referenced this pull request Dec 3, 2026
…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)
paulmedynski added a commit that referenced this pull request Dec 3, 2026
…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)
paulmedynski added a commit that referenced this pull request Dec 4, 2026
…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>
paulmedynski added a commit that referenced this pull request Dec 4, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hotfix 6.1.9 PRs targeting main that should be backported to release/6.1 for 6.1.9. Hotfix 7.0.5 PRs targeting main that should be backported to release/7.0 for 7.0.5 Hotfix 7.1.3 PRs targeting main that should be backported to release/7.1 for 7.1.3. Hotfix 8.0.1 PRs that must be backported to release/8.0

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants