e2fsck: fix a self-deadlock that hangs fsck on corrupt images
cover e2fsck: fix a self-deadlock that hangs fsck on corrupt images
e2fsck can hang forever on a corrupt image. The hang is a self-deadlock in libext2fs: flush_cached_blocks() releases CACHE_MTX around the write_error callback, re-acquires it, and then jumps to a label above the loop whose first statement acquires it again. The mutex is not recursive, so the first flush that reports a write error through a registered handler blocks the thread that already holds the lock.
Patch 1 removes the redundant acquisition. Patch 2 adds a regression test.
This matters beyond the fuzzer that found it: the deadlocked process sits at 0% CPU and does not respond to SIGTERM, so an init script waiting on fsck waits forever, and read-only checking (-fn) is enough to reach it.
Patch 2 departs from the usual f_* shape twice, both times because the bug is a hang rather than a wrong answer: the e2fsck run is wrapped in timeout(1), or a failure would stop the suite indefinitely rather than fail, and the transcript is not compared, because it is a thousand lines of repeated write errors that say nothing about this bug. Happy to drop the test or shape it differently if you would rather not have either of those in the f_* tests.
Matthias Goergens (2):
libext2fs: fix self-deadlock in flush_cached_blocks() write-error
retry
tests: add f_cache_mtx_deadlock for the flush_cached_blocks() retry
lock
lib/ext2fs/unix_io.c | 2 +- tests/f_cache_mtx_deadlock/expect | 2 ++ tests/f_cache_mtx_deadlock/image.gz | Bin 0 -> 695 bytes tests/f_cache_mtx_deadlock/name | 1 + tests/f_cache_mtx_deadlock/script | 44 ++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/f_cache_mtx_deadlock/expect create mode 100644 tests/f_cache_mtx_deadlock/image.gz create mode 100644 tests/f_cache_mtx_deadlock/name create mode 100644 tests/f_cache_mtx_deadlock/script
base-commit: 8fd79523d051d5ea881237f27eb1c664486e0078
Signature · 1 line
2.55.0
PATCH 1/2 libext2fs: fix self-deadlock in flush_cached_blocks() write-error retry
flush_cached_blocks() drops CACHE_MTX before invoking the channel's write_error handler, then re-acquires it and jumps to the retry label. The retry label is above the while loop, whose first statement acquires CACHE_MTX again:
retry:
while (errors_found) {
if ((flags & FLUSH_NOLOCK) == 0)
mutex_lock(data, CACHE_MTX); <- second
...
mutex_unlock(data, CACHE_MTX);
(channel->write_error)(...);
...
mutex_lock(data, CACHE_MTX); <- first
goto retry;The mutex is created with pthread_mutex_init(..., NULL), so it is not recursive and the second acquisition blocks the thread that already holds it. The first flush that has to report a write error through a registered handler deadlocks, every time.
Found by fuzzing e2fsck with corrupt images. An image whose group descriptors force bitmap relocation makes e2fsck -fn fail hundreds of writes on the read-only device; the aborted run reaches fatal_error(), whose io_channel_flush() enters this loop. e2fsck then sleeps forever at 0% CPU and does not respond to SIGTERM, so an init script waiting on fsck waits indefinitely. Read-only checking is enough to reach it.
unfixed: killed after 35s, no progress
fixed: exits 12 after 13ms
Drop the redundant acquisition; the loop head takes the lock.
--- lib/ext2fs/unix_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ext2fs/unix_io.c +1 -1
@@ -738,7 +738,7 @@ retry: retval2);if (err_buf)ext2fs_free_mem (&err_buf); -mutex_lock (data,CACHE_MTX ); +/* the loop head re-acquires CACHE_MTX */ goto retry; }else cache->write_err =0 ;
Signature · 1 line
2.55.0
Re: [PATCH e2fsprogs 1/2] libext2fs: fix self-deadlock in flush_cached_blocks() write-error retry
Quoted 43 lines · Matthias Goergens
flush_cached_blocks()dropsCACHE_MTXbefore invoking the channel'swrite_errorhandler, then re-acquires it and jumps to the retry label. The retry label is above the while loop, whose first statement acquiresCACHE_MTXagain:retry:
while (errors_found) { if ((flags & FLUSH_NOLOCK) == 0) mutex_lock(data, CACHE_MTX); <- second ... mutex_unlock(data, CACHE_MTX); (channel->write_error)(...); ... mutex_lock(data, CACHE_MTX); <- first goto retry;The mutex is created with
pthread_mutex_init(..., NULL), so it is not recursive and the second acquisition blocks the thread that already holds it. The first flush that has to report a write error through a registered handler deadlocks, every time.Found by fuzzing e2fsck with corrupt images. An image whose group descriptors force bitmap relocation makes e2fsck -fn fail hundreds of writes on the read-only device; the aborted run reaches
fatal_error(), whoseio_channel_flush()enters this loop. e2fsck then sleeps forever at 0% CPU and does not respond to SIGTERM, so an init script waiting on fsck waits indefinitely. Read-only checking is enough to reach it.unfixed: killed after 35s, no progress
fixed: exits 12 after 13msDrop the redundant acquisition; the loop head takes the lock.
Signed-off-by Matthias Goergens <[email protected]>--- lib/ext2fs/unix_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/ext2fs/unix_io.c +0
-0
lib/ext2fs/unix_io.c retry: · line 741
Quoted 5 lines
lib/ext2fs/unix_io.c +1
-1@@ -738,7 +738,7 @@ retry: retval2);if (err_buf)ext2fs_free_mem (&err_buf); -mutex_lock (data,CACHE_MTX ); +/* the loop head re-acquires CACHE_MTX */
Yep, looks correct.
--D
Quoted 7 lines
lib/ext2fs/unix_io.c +0
-0goto retry; }else cache->write_err =0 ;Signature · 1 line
2.55.0
PATCH 2/2 tests: add f_cache_mtx_deadlock for the flush_cached_blocks() retry lock
In reply to Matthias GoergensRegression test for the previous patch. The image has group descriptors that force bitmap and inode table relocation, so e2fsck -fn fails its writes on the read-only test file and reaches the write-error retry loop in flush_cached_blocks() with a handler registered.
Two departures from the usual f_* shape, both because the bug is a hang rather than a wrong answer.
The e2fsck run is wrapped in timeout(1). A deadlocked e2fsck ignores SIGTERM, so without a bound this test does not fail against an affected build, it stops the suite indefinitely. The probe-then-use form is taken from tests/r_corrupt_fs/script, with -k so the SIGTERM escalates to SIGKILL.
The transcript is not compared. It is about a thousand lines of repeated "Error writing block N" that say nothing about this bug and would need updating whenever those messages change. The test records the exit status and that -n left the image unmodified, which is what it is actually asserting. On an affected build the diff is
-Exit status is 12
+Exit status is 137
--- tests/f_cache_mtx_deadlock/expect | 2 ++ tests/f_cache_mtx_deadlock/image.gz | Bin 0 -> 695 bytes tests/f_cache_mtx_deadlock/name | 1 + tests/f_cache_mtx_deadlock/script | 44 ++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 tests/f_cache_mtx_deadlock/expect create mode 100644 tests/f_cache_mtx_deadlock/image.gz create mode 100644 tests/f_cache_mtx_deadlock/name create mode 100644 tests/f_cache_mtx_deadlock/script
tests/f_cache_mtx_deadlock/expect new file +2 -0
@@ -0,0 +1,2 @@ +Exit status is 12 +crc did not change.
tests/f_cache_mtx_deadlock/image.gz new file binary +0 -0
tests/f_cache_mtx_deadlock/name new file +1 -0
@@ -0,0 +1 @@ +e2fsck self-deadlocks flushing the write-error cache (regression test for libext2fs CACHE_MTX retry double-lock)
tests/f_cache_mtx_deadlock/script new file +44 -0
44 lines not on this page · raw message
Signature · 1 line
2.55.0