Bug #80501
closedheap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())
0%
Description
Tested by running >200 runs of bin/unittest_bluefs --gtest_filter='BlueFS_wal.wal_v2_simulate_crash
And about 1 in 200 gives a real crash. So it looks like a crash due to
a race condition.
Testing with an ASAN compiled versions:
export ASAN_OPTIONS=detect_stack_use_after_return=1:abort_on_error=1:halt_on_error=1 bin/unittest_bluefs --gtest_filter='BlueFS_wal.wal_v2_simulate_crash
Actually reports trouble in paradise:
==54046==ERROR: AddressSanitizer: heap-use-after-free on address 0x5130000061a8 at pc 0x000000a5ea69 bp 0x7fffdf9fa3d0 sp 0x7fffdf9fa3c8
fsync() only submits aios via _flush_bdev(); it does not call
_drain_writer()/aio_wait() and therefore does not guarantee that
submitted aios have completed. close_writer() correctly drains
before destroying the FileWriter, but a FileWriter destructed
directly (e.g. process crash, or BlueFS_wal.wal_v2_simulate_crash's
deliberate \`delete writer\` without close_writer(), which is a
realistic simulation of an actual crash) skips this drain.
This leaves a race: KernelDevice::_aio_thread() can still be
processing a completion (get_return_value() / try_aio_wake()) on an
aio_t that ~FileWriter()'s IOContext teardown has already freed via
the pending_aios/running_aios list destruction, producing a
heap-use-after-free. Confirmed via AddressSanitizer with both an
allocation-site and free-site stack trace pointing at
KernelDevice::aio_write() vs. IOContext::~IOContext() from
FileWriter::~FileWriter() called from
BlueFS_wal.wal_v2_simulate_crash. Also independently reproduced (via
gdb core, mutex_debug_impl::try_lock_impl() -> EINVAL on a corrupted
mutex) on two separate FreeBSD hosts.
Fix: call IOContext::aio_wait() on each iocv[] before deleting it in
~FileWriter(), mirroring what _drain_writer() already does for the
close_writer() path. On the normal close_writer() path this is a
no-op (num_running is already 0), so there is no performance impact;
confirmed via unittest_bluefs/unittest_bluefs_ex timing before and
after (no measurable difference) and 1000 iterations of
wal_v2_simulate_crash under ASan with no further failures (previously
failed within ~124 iterations).
This like it is not FreeBSD-specific -- it's a generic lifecycle race in
BlueFS/KernelDevice -- FreeBSD's stricter libthr pthread
implementation and ASan's instrumentation simply surface it more
reliably than default Linux glibc/pthread timing.
Files
IF Updated by Igor Fedotov 3 months ago
- Status changed from New to Fix Under Review
- Backport set to umbrella, tentacle, squid
- Pull request ID set to 71766
IF Updated by Igor Fedotov 3 months ago
FYI: @Ming Lei - this has already got a PR, see https://github.com/ceph/ceph/pull/71766
UB Updated by Upkeep Bot about 1 month ago
- Copied to Backport #82619: umbrella: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer()) added
UB Updated by Upkeep Bot about 1 month ago
- Copied to Backport #82620: tentacle: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer()) added
UB Updated by Upkeep Bot about 1 month ago
- Copied to Backport #82621: squid: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer()) added
UB Updated by Upkeep Bot about 1 month ago
- Status changed from Fix Under Review to Backporting
- Merge Commit set to 73e4f37dc7b475a5f139a9ac228d962d681c7e7b
- Fixed In set to v22.0.0-375-g73e4f37dc7
- Upkeep Timestamp set to 2026-11-03T14:20:55+00:00