Skip to content

Fix unbound read in Azure::readBigAt - #119054

Merged
arsenmuk merged 8 commits into
masterfrom
arsenmuk/azure-fix-unbound-read
Sep 14, 2026
Merged

arsenmuk merged 8 commits into
masterfrom
arsenmuk/azure-fix-unbound-read

Conversation

@arsenmuk

@arsenmuk arsenmuk commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Problem

The reported length of a response from blob storage was trusted when copying data, which could result in a heap buffer overflow.

Fix

The read is now limited by the local buffer capacity. Test coverage is added.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Fix a possible heap buffer overflow on malformed responses from blob storage.


Workflow [PR]
Sync PR [sync-upstream/pr/119054]

Version info

  • Merged into: 26.9.1.1439-master (included in 26.9 and later)
  • Backported to: 26.8.22.13, 26.7.28.6, 26.3.47.4

@arsenmuk
arsenmuk marked this pull request as ready for review September 9, 2026 15:09
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a515da0]

Summary: ✅


AI Review

Summary

This PR fixes ReadBufferFromAzureBlobStorage::readBigAt so it no longer trusts the response-reported body length when copying into caller memory, returns the accumulated byte count on short bodies, and makes lazy BlobClient creation safe for the documented supportsReadAt() / readBigAt() handshake. I did not find a remaining correctness or safety issue in the current patch, and the added regression tests cover the overflow and truncated-body cases the PR claims to fix.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit a515da0.

Metric Baseline Current Δ
Lines 89.00% 88.90% -0.10%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 94/100 (94.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 9, 2026
@scanhex12 scanhex12 self-assigned this Sep 9, 2026
@arsenmuk arsenmuk changed the title Fix unbound read Fix unbound read in Azure::readBigAt Sep 9, 2026
…g a full read

`readBigAt` returned the requested size even when the response bodies kept
ending prematurely and the retry loop ran out of attempts, so callers such as
`Parquet::Prefetcher::readSync` could not detect the truncation. Return the
accumulated byte count, like the S3 and HTTP implementations.
@clickhouse-gh clickhouse-gh Bot added the comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a515da026 with master c5c3c4d5d (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

13 translation units recompiled, 44 s compile time in total, 13 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +2.7 s (+6%) in total before that adjustment.

Job report

… hit a null client

`readBigAt` dereferenced `blob_client` unconditionally, while the client was
only created lazily by `initialize` or `tryGetFileSize`, so a caller following
the documented `supportsReadAt` handshake could hit a null pointer. Route every
user through `getBlobClient`, which creates the client under `std::call_once`:
`readBigAt` is `const` and may be called from multiple threads in parallel, and
creating the client is cheap (it only composes the blob URL, no request is
made). Also make the regression test exercise the documented handshake instead
of forcing a `getFileSize` call.
The existing test only served responses at least as long as requested, so it
exercised the overlong-body clamp but left the short-count return unproved: a
regression to returning the requested size after a truncated Azure response
would still have passed. Parameterize the mock transport by advertised vs.
served size and add a case whose body ends early, asserting `readBigAt` returns
the accumulated byte count and leaves the unread tail untouched.
@arsenmuk
arsenmuk added this pull request to the merge queue Sep 14, 2026
Merged via the queue into master with commit 0d04f2b Sep 14, 2026
344 of 345 checks passed
@arsenmuk
arsenmuk deleted the arsenmuk/azure-fix-unbound-read branch September 14, 2026 02:32
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 14, 2026
alexey-milovidov added a commit that referenced this pull request Sep 14, 2026
…-buffer-overflows

Master merged #119054, an independent fix of
the same `readBigAt` overflow in `ReadBufferFromAzureBlobStorage`. Resolution:

- `ReadBufferFromAzureBlobStorage.cpp`: take master's thread-safe lazy `getBlobClient`
  (created under `call_once`) everywhere, which supersedes this branch's call-local blob
  client in `readBigAt`; keep this branch's `checkReturnedRange` / `checkReturnedETag`,
  `copyFromAzureBodyStream`, the null body stream guard and the short-read warning; keep
  master's `chassert(bytes_copied <= n)`.
- `gtest_azure_read_buffer.cpp` (add/add): keep this branch's file and port master's two
  tests `AzureReadBigAt.DoesNotTrustResponseLength` and
  `AzureReadBigAt.ReturnsAccumulatedCountOnTruncatedResponse` onto its `makeFreshBuffer`
  harness.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
groeneai pushed a commit to groeneai/ClickHouse that referenced this pull request Oct 4, 2026
…the response length

`ReadBufferFromAzureBlobStorage::initialize` derived `total_size` from
`BodyStream::Length`, which is the `Content-Length` chosen by the remote endpoint.
With `read_until_position = 100`, `offset = 0` and a 64-byte reading buffer, an
endpoint that answers the 100-byte ranged request with `Content-Length = 128` made
`nextImpl` hand out 64 bytes twice and only trip the right-bound check on the
following call, so 28 bytes from outside the requested range had already escaped into
the caller. With a reading buffer larger than the range, all 28 escape in a single call.

Derive the bound of the current download from `read_until_position`, which is set
locally by the caller, and let the length of the response only shorten it. The size
of the blob from the same response is not used for this: it comes from the same
untrusted place, and an endpoint that answers a ranged request with `200 OK` would
make it truncate a legitimate read.

`src/Disks/tests/gtest_azure_read_buffer.cpp` gains regressions that drive `nextImpl`
through `setReadUntilPosition` against a fake Azure HTTP transport whose response is
longer than the requested range, with a reading buffer both smaller and larger than
the range. Without the change they fail with
`Attempt to read beyond right offset (128 > 99)`.

This is one of the independent parts of
ClickHouse#115706, which was reverted in
ClickHouse#120419. The positioned read path,
`readBigAt`, was fixed separately in
ClickHouse#119054.
alexey-milovidov added a commit that referenced this pull request Oct 4, 2026
…nly the core bound fix

The original PR #120910 builds on Azure
reader changes that are not in 26.3 (#119054,
#120955, #120963,
`s3_validate_etag_on_read`, `LazyReadFromObjectStorageSource`, the `gtest_azure_read_buffer`
fake transport). Resolved by taking the 26.3 tree for every touched path and porting only the
fix itself to `ReadBufferFromAzureBlobStorage`:

- the bound of the current download (`total_size`) is the locally set `read_until_position`,
  and the `Content-Length` of the response can only shorten it;
- `setReadUntilPosition` discards the bytes buffered under the previous, wider bound and
  rewinds `offset`, so a tightened bound takes effect immediately.

Not backported: `If-Match` pinning with `azure_validate_etag_on_read` and the
`AZURE_OBJECT_CHANGED_DURING_READ` error code, the `std::optional` right bound, the
`file_size` constructor argument, and the tests that depend on those (the gtest file does
not exist in 26.3, `05259_lazy_materialization_azure_etag_pinned`, the integration test).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 4, 2026
Resolve the conflicts of the cherry-pick of #120910 to 26.7. The release
branch lacks several prerequisites of the original change, so only its own
part is kept:
- `checkReturnedRange`, the premature-end-of-response handling and the lazy
  `getBlobClient` are not on 26.7; the `ETag` pinning and the local right
  bound are applied to the existing `blob_client` code instead.
- Lazy materialization from object storage is not on 26.7, so the changes to
  `LazyReadFromObjectStorageSource`, `canUseLazyMaterialization` and the test
  `05259_lazy_materialization_azure_etag_pinned` are dropped.
- `gtest_azure_read_buffer.cpp` is not on 26.7 (it came with #119054, which is
  not backported), so it is not added here.
- The new setting `azure_validate_etag_on_read` is recorded in the "26.8"
  block of `SettingsChangesHistory.cpp`, as other backported settings are.
alexey-milovidov added a commit that referenced this pull request Oct 4, 2026
…o 26.8

The cherry-pick of #120910 conflicted because
26.8 lacks the prerequisite Azure read fixes that landed on `master` first:
#119054 (lazy blob client, `readBigAt` fix, and
the creation of `gtest_azure_read_buffer.cpp`),
#120955 (`checkReturnedRange`) and
#120963 (reopen on a premature end of a bounded read).

Resolution:
- `ReadBufferFromAzureBlobStorage` is the 26.8 version with the changes of #120910 applied on
  top: `read_until_position` becomes `std::optional<size_t>`, the current download is bounded by
  `getTotalSizeOfCurrentDownload`, `setReadUntilPosition` drops the bytes buffered past a tighter
  bound, and every `Download` is pinned with `If-Match` to `expected_etag`, with the `ETag` of the
  response checked and `412` reported as `AZURE_OBJECT_CHANGED_DURING_READ`. The `file_size`
  argument is reported by `tryGetFileSize`.
- `AzureObjectStorage::readObject` uses `connection_params.getContainer` as in 26.8.
- `AZURE_OBJECT_CHANGED_DURING_READ` keeps the code 1021 from `master`; `END` is raised to it.
- `azure_validate_etag_on_read` is declared in the 26.8 style, with its history entry in the
  `26.10` section of `SettingsChangesHistory.cpp`.
- `gtest_azure_read_buffer.cpp` is added with the tests of #120910 only. The tests that belong to
  the prerequisites (`AzureReadBigAt.DoesNotTrustResponseLength`,
  `AzureReadBigAt.ReturnsAccumulatedCountOnTruncatedResponse`,
  `AzureReadUntilPosition.ShortRangeResponse`) are dropped, the `AzureObjectStorage` constructor
  takes the 26.8 `auth_method` argument, and `AzureReadBigAt.RejectsReplacedBlobThroughIfMatch`
  calls `getFileSize` first, because in 26.8 that is what creates the blob client for `readBigAt`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@arsenmuk arsenmuk added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 6, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 6, 2026
arsenmuk added a commit that referenced this pull request Oct 9, 2026
Preserve the 26.8 generation checks while using the lazy blob client. Reuse the existing Azure mock transport and add both positioned-read regression tests alongside the sequential-read tests.

Related: #119054

Related: #124266
robot-clickhouse added a commit that referenced this pull request Oct 9, 2026
Cherry pick #119054 to 26.8: Fix unbound read in Azure::readBigAt
arsenmuk added a commit that referenced this pull request Oct 9, 2026
Keep the branch sequential-read and remote-size code while applying the lazy blob client, bounded positioned read, and both regression tests from the original change.

Related: #119054

Related: #124264
arsenmuk added a commit that referenced this pull request Oct 9, 2026
Keep the branch ETag checks while routing downloads through the lazy client. Include both positioned-read regression tests from the original change.

Related: #119054

Related: #124265
robot-ch-test-poll added a commit that referenced this pull request Oct 9, 2026
Cherry pick #119054 to 26.3: Fix unbound read in Azure::readBigAt
robot-ch-test-poll added a commit that referenced this pull request Oct 9, 2026
Cherry pick #119054 to 26.7: Fix unbound read in Azure::readBigAt
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 9, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 9, 2026
Backport #119054 to 26.8: Fix unbound read in Azure::readBigAt
alexey-milovidov added a commit that referenced this pull request Oct 9, 2026
Backport #119054 to 26.3: Fix unbound read in Azure::readBigAt
PedroTadim added a commit that referenced this pull request Oct 12, 2026
Backport #119054 to 26.7: Fix unbound read in Azure::readBigAt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants