From nobody Wed Jul 02 18:28:13 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bXT0s4vh3z60mHS; Wed, 02 Jul 2025 18:28:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bXT0s3HRXz3Hhx; Wed, 02 Jul 2025 18:28:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751480893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KHBaHR64nuXKbZzp7G/IrmYqMjuftqttobecWJmxXOU=; b=N7XN8yG8wOv5stVR+bViUdaf7ZvrbPE88QL8VQAAso9nfMhjaJKRihVAYbY91bOFr3GFlg BshdFlmG00PGihsv7qF7NPTK7ZJbnAfNYIYzNzRJLPRGDF4bghJp1SB37/0rFmg9KWciFo 0Ce/gI91EfnISCq7YSSun9GppgTp522+oyXMHKkkBX6LT4WOb6/9erfrQHHYZir49alJN/ qZ00SATetpe7IXHJU+O3TljVtXsrFcLGqboPIt5sh3PHETwBkulIKy5tZerTUSqHRByl5u k1X1HZNM3B9aQT59oxEo9S679rcI5Lu73YUw2xt6OXAVmKZgCB+odrZ1ZtfaBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751480893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KHBaHR64nuXKbZzp7G/IrmYqMjuftqttobecWJmxXOU=; b=phs2MmaiDaA0OjcPEU8AXENqHE4rvAbEfkUvSVI6elPuXAbtu5ifeEBg3K0WAdg/cK44g5 FuAK3H7pIfWooW14tI2GBEGeKnUEub+e0iHCnCC8aHcz6INHqZR70E96w/R0QvcuqKsi9g c5JdprFCcx1vvpvy6nqzPBOhZMFpAXW5Scm05Zs3tfsJBj/tq9GTDt7Em6sDfjf7LM3CrH JhDkAaJnVwEHoUQku6MZCmjm9gyIl2UTSZ46VohbV70yJPAvkBLO4tJKfMCjxXw5y/bGks 4+QZoD6Mvbz0BM0FLMPa9Y1DxoZYHtkzrWkQetmJ66eDo1/AlgN2O89xCTMYpA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751480893; a=rsa-sha256; cv=none; b=uYVX4LZvwf9OaxbT7YIQk168eShtt/7CCetx12YrMrQ6laUW5FUdX6zOdRPaptaNZGudxr IJnAiYXBDKo3MgPQZnDpD9SlYx7CoX+sm0RBAazoqp4TCRy1ZYOVujHFH4kaalts8pwxI6 uK0TkrNkiW7usq0whZfpoodY3bWYXTqotBftdx5154UVJRZtIdAflj0IwAUgKOfgZsMaKD qI4p6Pw3/fvaKa+LokuACv9zD3jDUe6pXhNTKflcj20zmkE4h3qSTbnLtihYeiCjPoKwV2 dUjg5r7boK9tE41P5L2N+JKCbCCupdaHULsnhYoCDv3xtDa6WrALzSojuyLS3w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bXT0s2HKBzWTf; Wed, 02 Jul 2025 18:28:13 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 562ISDgc057455; Wed, 2 Jul 2025 18:28:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 562ISDUs057452; Wed, 2 Jul 2025 18:28:13 GMT (envelope-from git) Date: Wed, 2 Jul 2025 18:28:13 GMT Message-Id: <202507021828.562ISDUs057452@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Gordon Tetlow Subject: git: 49b07b94662b - releng/14.2 - Fix Use-after-free in multi-threaded xz decoder. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gordon X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.2 X-Git-Reftype: branch X-Git-Commit: 49b07b94662b92edb6d8de1e997fa805ac8d2b7c Auto-Submitted: auto-generated The branch releng/14.2 has been updated by gordon: URL: https://cgit.FreeBSD.org/src/commit/?id=49b07b94662b92edb6d8de1e997fa805ac8d2b7c commit 49b07b94662b92edb6d8de1e997fa805ac8d2b7c Author: Gordon Tetlow AuthorDate: 2025-07-02 05:56:35 +0000 Commit: Gordon Tetlow CommitDate: 2025-07-02 05:56:35 +0000 Fix Use-after-free in multi-threaded xz decoder. Approved by: so Security: FreeBSD-SA-25:06.xz Security: CVE-2025-31115 --- contrib/xz/src/liblzma/common/stream_decoder_mt.c | 104 +++++++++++----------- 1 file changed, 51 insertions(+), 53 deletions(-) diff --git a/contrib/xz/src/liblzma/common/stream_decoder_mt.c b/contrib/xz/src/liblzma/common/stream_decoder_mt.c index 76212b46da32..6bbbe53b5e32 100644 --- a/contrib/xz/src/liblzma/common/stream_decoder_mt.c +++ b/contrib/xz/src/liblzma/common/stream_decoder_mt.c @@ -24,15 +24,10 @@ typedef enum { THR_IDLE, /// Decoding is in progress. - /// Main thread may change this to THR_STOP or THR_EXIT. + /// Main thread may change this to THR_IDLE or THR_EXIT. /// The worker thread may change this to THR_IDLE. THR_RUN, - /// The main thread wants the thread to stop whatever it was doing - /// but not exit. Main thread may change this to THR_EXIT. - /// The worker thread may change this to THR_IDLE. - THR_STOP, - /// The main thread wants the thread to exit. THR_EXIT, @@ -347,27 +342,6 @@ worker_enable_partial_update(void *thr_ptr) } -/// Things do to at THR_STOP or when finishing a Block. -/// This is called with thr->mutex locked. -static void -worker_stop(struct worker_thread *thr) -{ - // Update memory usage counters. - thr->coder->mem_in_use -= thr->in_size; - thr->in_size = 0; // thr->in was freed above. - - thr->coder->mem_in_use -= thr->mem_filters; - thr->coder->mem_cached += thr->mem_filters; - - // Put this thread to the stack of free threads. - thr->next = thr->coder->threads_free; - thr->coder->threads_free = thr; - - mythread_cond_signal(&thr->coder->cond); - return; -} - - static MYTHREAD_RET_TYPE worker_decoder(void *thr_ptr) { @@ -398,17 +372,6 @@ next_loop_unlocked: return MYTHREAD_RET_VALUE; } - if (thr->state == THR_STOP) { - thr->state = THR_IDLE; - mythread_mutex_unlock(&thr->mutex); - - mythread_sync(thr->coder->mutex) { - worker_stop(thr); - } - - goto next_loop_lock; - } - assert(thr->state == THR_RUN); // Update progress info for get_progress(). @@ -473,8 +436,7 @@ next_loop_unlocked: } // Either we finished successfully (LZMA_STREAM_END) or an error - // occurred. Both cases are handled almost identically. The error - // case requires updating thr->coder->thread_error. + // occurred. // // The sizes are in the Block Header and the Block decoder // checks that they match, thus we know these: @@ -482,16 +444,30 @@ next_loop_unlocked: assert(ret != LZMA_STREAM_END || thr->out_pos == thr->block_options.uncompressed_size); - // Free the input buffer. Don't update in_size as we need - // it later to update thr->coder->mem_in_use. - lzma_free(thr->in, thr->allocator); - thr->in = NULL; - mythread_sync(thr->mutex) { + // Block decoder ensures this, but do a sanity check anyway + // because thr->in_filled < thr->in_size means that the main + // thread is still writing to thr->in. + if (ret == LZMA_STREAM_END && thr->in_filled != thr->in_size) { + assert(0); + ret = LZMA_PROG_ERROR; + } + if (thr->state != THR_EXIT) thr->state = THR_IDLE; } + // Free the input buffer. Don't update in_size as we need + // it later to update thr->coder->mem_in_use. + // + // This step is skipped if an error occurred because the main thread + // might still be writing to thr->in. The memory will be freed after + // threads_end() sets thr->state = THR_EXIT. + if (ret == LZMA_STREAM_END) { + lzma_free(thr->in, thr->allocator); + thr->in = NULL; + } + mythread_sync(thr->coder->mutex) { // Move our progress info to the main thread. thr->coder->progress_in += thr->in_pos; @@ -511,7 +487,20 @@ next_loop_unlocked: && thr->coder->thread_error == LZMA_OK) thr->coder->thread_error = ret; - worker_stop(thr); + // Return the worker thread to the stack of available + // threads only if no errors occurred. + if (ret == LZMA_STREAM_END) { + // Update memory usage counters. + thr->coder->mem_in_use -= thr->in_size; + thr->coder->mem_in_use -= thr->mem_filters; + thr->coder->mem_cached += thr->mem_filters; + + // Put this thread to the stack of free threads. + thr->next = thr->coder->threads_free; + thr->coder->threads_free = thr; + } + + mythread_cond_signal(&thr->coder->cond); } goto next_loop_lock; @@ -545,17 +534,22 @@ threads_end(struct lzma_stream_coder *coder, const lzma_allocator *allocator) } +/// Tell worker threads to stop without doing any cleaning up. +/// The clean up will be done when threads_exit() is called; +/// it's not possible to reuse the threads after threads_stop(). +/// +/// This is called before returning an unrecoverable error code +/// to the application. It would be waste of processor time +/// to keep the threads running in such a situation. static void threads_stop(struct lzma_stream_coder *coder) { for (uint32_t i = 0; i < coder->threads_initialized; ++i) { + // The threads that are in the THR_RUN state will stop + // when they check the state the next time. There's no + // need to signal coder->threads[i].cond. mythread_sync(coder->threads[i].mutex) { - // The state must be changed conditionally because - // THR_IDLE -> THR_STOP is not a valid state change. - if (coder->threads[i].state != THR_IDLE) { - coder->threads[i].state = THR_STOP; - mythread_cond_signal(&coder->threads[i].cond); - } + coder->threads[i].state = THR_IDLE; } } @@ -1562,6 +1556,10 @@ stream_decode_mt(void *coder_ptr, const lzma_allocator *allocator, } // Return if the input didn't contain the whole Block. + // + // NOTE: When we updated coder->thr->in_filled a few lines + // above, the worker thread might by now have finished its + // work and returned itself back to the stack of free threads. if (coder->thr->in_filled < coder->thr->in_size) { assert(*in_pos == in_size); return LZMA_OK; @@ -1949,7 +1947,7 @@ stream_decoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator, // accounting from scratch, too. Changes in filter and block sizes may // affect number of threads. // - // FIXME? Reusing should be easy but unlike the single-threaded + // Reusing threads doesn't seem worth it. Unlike the single-threaded // decoder, with some types of input file combinations reusing // could leave quite a lot of memory allocated but unused (first // file could allocate a lot, the next files could use fewer