Skip to content

fix(storage): avoid use-after-free in AsyncWriterConnectionImpl - #16729

Open
v-pratap wants to merge 4 commits into
googleapis:mainfrom
v-pratap:fix-async-writer-connection-uaf
Open

v-pratap wants to merge 4 commits into
googleapis:mainfrom
v-pratap:fix-async-writer-connection-uaf

Conversation

@v-pratap

@v-pratap v-pratap commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a use-after-free in AsyncWriterConnectionImpl when an AsyncWriter (such as a buffered or appendable/resumed writer) is destroyed while asynchronous streaming RPC operations are still in flight.

Root Cause

With buffered (MakeWriterConnectionBuffered) and appendable/resumed (MakeWriterConnectionResumed) uploads, Write() returns a ready future immediately when the resend buffer is below the high-water mark while the underlying AsyncWriterConnectionImpl::Write() is still in flight. Previously:

  1. AsyncWriterConnectionImpl captured raw this in all async streaming RPC continuations (Write, Finalize, Flush, Close, OnFlush, OnClose, OnFinalUpload).
  2. ~AsyncWriterConnectionImpl() called Finish() immediately even when an operation was still in flight, violating AsyncStreamingReadWriteRpc::Finish()'s precondition and freeing AsyncWriterConnectionImpl before pending callbacks executed.

When the in-flight RPC later completed, the continuation dereferenced freed AsyncWriterConnectionImpl members (heap-use-after-free under ASan).

Changes

  • Move AsyncWriterConnectionImpl's state and async callbacks into std::shared_ptr<AsyncWriterConnectionImpl::State> inheriting from std::enable_shared_from_this<State>, preserving std::unique_ptr / stack ownership of AsyncWriterConnectionImpl.
  • Replace all raw this captures in async continuations (Write, Finalize, Flush, Close, ConsumeLoop, Finish) with shared_from_this().
  • Track in-flight operations (in_flight_) and shutdown_ under mu_: ~AsyncWriterConnectionImpl() cancels the stream and defers impl_->Finish() until all in-flight operations complete, extending State (and impl_) lifetime until impl_->Finish() completes.
  • Add unit tests covering destruction with in-flight operations:
    • writer_connection_impl_test.cc: Write (succeeds/fails), Flush (write phase and read phase), Finalize, and Close.
    • writer_connection_buffered_test.cc: WriterConnectionBufferedDestroyTest.PendingWriteOutlivesConnection (WriteSucceeds / WriteFails).
    • writer_connection_resumed_test.cc: WriterConnectionResumedDestroyTest.PendingWriteOutlivesConnection (WriteSucceeds / WriteFails).

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Dec 1, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors AsyncWriterConnectionImpl to use an internal State class managed by a std::shared_ptr. This design change successfully prevents potential use-after-free bugs when a writer is destroyed while asynchronous operations (such as writes, flushes, or closes) are still in flight. Corresponding unit tests have been added to verify this behavior. Regarding the code quality, there are several violations of the repository style guide concerning the use of auto. Specifically, auto is used where it obscures domain objects, protobuf messages, or primitive types. It is recommended to replace these with explicit type declarations to improve readability and maintainability.

Move AsyncWriterConnectionImpl's async state into a shared_ptr<State>
inheriting from std::enable_shared_from_this, and capture shared_from_this()
in all streaming RPC continuations instead of raw this.

Also track in-flight operations so ~AsyncWriterConnectionImpl() cancels the
stream and defers impl_->Finish() until all in-flight operations complete,
avoiding Finish()-while-op-pending contract violations and keeping State and
the underlying StreamingRpc alive until Finish() completes.
@v-pratap
v-pratap force-pushed the fix-async-writer-connection-uaf branch from f55ee38 to 43790ea Compare December 1, 2026 07:02
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.11111% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.84%. Comparing base (4dfc41b) to head (43790ea).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...d/storage/internal/async/writer_connection_impl.cc 91.11% 27 Missing ⚠️
...rage/internal/async/writer_connection_impl_test.cc 97.98% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16729      +/-   ##
==========================================
+ Coverage   92.83%   92.84%   +0.01%     
==========================================
  Files        2279     2278       -1     
  Lines      229442   230551    +1109     
==========================================
+ Hits       213009   214062    +1053     
- Misses      16433    16489      +56     

☔ 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.

@v-pratap
v-pratap marked this pull request as ready for review December 1, 2026 08:04
@v-pratap
v-pratap requested review from a team as code owners December 1, 2026 08:04

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant