Repository navigation
Fix unbound read in Azure::readBigAt - #119054
Conversation
|
Workflow [PR], commit [a515da0] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit a515da0.
Changed lines: Changed C/C++ lines covered: 94/100 (94.00%) · Uncovered code |
…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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units13 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. |
… 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.
…-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]>
…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.
…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]>
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.
…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]>
Cherry pick #119054 to 26.8: Fix unbound read in Azure::readBigAt
Cherry pick #119054 to 26.3: Fix unbound read in Azure::readBigAt
Cherry pick #119054 to 26.7: Fix unbound read in Azure::readBigAt
Backport #119054 to 26.8: Fix unbound read in Azure::readBigAt
Backport #119054 to 26.3: Fix unbound read in Azure::readBigAt
Backport #119054 to 26.7: Fix unbound read in Azure::readBigAt
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):
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
26.9.1.1439-master(included in26.9and later)26.8.22.13,26.7.28.6,26.3.47.4