[PATCH] erofs-utils: mkfs: fix uninitialized z_erofs_mt_ctrl.mutex

Isaiah Derose-Wilson <[email protected]>
Newsgroups org.ozlabs.lists.linux-erofs
Message-ID <CAMTkE+Te5f7v9MV4B6sBnHdC5XLcJtPQiS29PhFhVhqHtr+uJQ@mail.gmail.com>
z_erofs_mt_ctrl.mutex guards the multi-threaded compression idle
work-item list but is never initialized: z_erofs_mt_global_init()
initializes g_ictx.mutex and g_ictx.cond, not this one.

A zero-filled pthread_mutex_t happens to be a valid initialized mutex
on glibc, so Linux builds are unaffected by accident.  On macOS a
zeroed mutex is invalid: pthread_mutex_lock() fails with EINVAL and,
since the return value is not checked, the idle-list critical sections
in z_erofs_mt_compress() and erofs_mt_write_compressed_file() run with
no mutual exclusion at all.  The torn list can hand the same work item
to two owners, which corrupts the workqueue and crashes mkfs.erofs
non-deterministically (NULL work dequeued in worker_thread(), SIGABRT,
or corrupted output).  --workers=1 does not help since the race is
between the producer and the reaper thread.

Initialize the mutex statically.

Fixes: 830b27bc23342 ("erofs-utils: mkfs: introduce inner-file
multi-threaded compression")
Closes: https://github.com/erofs/erofs-utils/issues/53
Signed-off-by: Isaiah DeRose-Wilson <[email protected]>
---
 lib/compress.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/compress.c b/lib/compress.c
index 48a18b9..9262afa 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -110,7 +110,9 @@ static struct {
  struct erofs_workqueue wq;
  struct erofs_compress_work *idle;
  pthread_mutex_t mutex;
-} z_erofs_mt_ctrl;
+} z_erofs_mt_ctrl = {
+ .mutex = PTHREAD_MUTEX_INITIALIZER,
+};

 struct z_erofs_compress_fslot {
  struct list_head pending;
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.