Project

General

Profile

Actions

Bug #80501

closed
WW WW

heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())

Bug #80501: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())

Added by Willem Jan Withagen 3 months ago. Updated about 1 month ago.

Status:
Backporting
Priority:
Normal
Target version:
-
Due date:
% Done:

0%

Source:
Backport:
umbrella, tentacle, squid
Regression:
No
Severity:
2 - major
Reviewed:
Affected Versions:
ceph-qa-suite:
upgrade/quincy-x
Pull request ID:
Tags (freeform):
Fixed In:
v22.0.0-375-g73e4f37dc7
Released In:
Upkeep Timestamp:
2026-11-03T14:20:55+00:00

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

asan-trace.txt (13.7 KB) asan-trace.txt Willem Jan Withagen, 09/13/2026 10:39 PM

Related issues 3 (3 open — 0 closed)

Copied to bluestore - Backport #82619: umbrella: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())NewWillem Jan Withagen

Actions
Copied to bluestore - Backport #82620: tentacle: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())NewWillem Jan Withagen

Actions
Copied to bluestore - Backport #82621: squid: heap-use-after-free in BlueFS::FileWriter destructor on abrupt destruction (skipped close_writer())NewWillem Jan Withagen

Actions

ML Updated by Ming Lei 3 months ago Actions #1

  • Assignee set to Ming Lei

I will take a look at the issue first.

IF Updated by Igor Fedotov 3 months ago Actions #2

  • Status changed from New to Fix Under Review
  • Backport set to umbrella, tentacle, squid
  • Pull request ID set to 71766

ML Updated by Ming Lei 3 months ago Actions #4

  • Assignee changed from Ming Lei to Willem Jan Withagen

OK, reassign to Williem, given he is working on fix.

Thanks,

UB Updated by Upkeep Bot about 1 month ago Actions #5

  • 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 Actions #6

  • 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 Actions #7

  • 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 Actions #8

  • 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
Actions

Also available in: PDF Atom