Commit bacd4676 for xz

commit bacd4676214c80d79fa4e53bc0057c82350ae811
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Wed Dec 2 15:52:11 2026 +0200

    liblzma: Fix a data corruption issue in the LZMA2 encoder

    There is an off-by-a-few-bytes mistake. If triggered in a debug build,
    an assertion will fail. Non-debug builds will produce invalid LZMA2 data,
    but recovery might be possible[*]. If the dictionary size is smaller than
    64 KiB and some extra conditions are met, triggering the bug can result
    in memory reads a few bytes before the beginning of an array. If it
    doesn't crash, those bytes are leaked to the output stream and the file
    won't be recoverable.

    The bug was discovered by analysing the code. There are no reports
    of observed data corruption. As of writing, I haven't managed to
    trigger the bug without modifying the buggy condition to be even more
    incorrect, so I don't have a test for this bug.

    The bug is specific to liblzma; it wasn't inherited from LZMA SDK.

    Make LZMA2 encoder produce two uncompressed chunks when needed. This
    might be needed later anyway if the encoder is revised some day.
    The LZMA2 encoder in XZ for Java has done it similarly since the
    beginning (2011).

    Don't fix the incorrect condition because it would change the encoder
    output with most non-small files. While there's nothing wrong that
    different encoder versions produce different output, there unfortunately
    are use cases where people want to reproduce the exact compressed output,
    and it would be bad if only a known-to-be-buggy encoder could do that.
    It's best to not change the output solely to fix this particular issue
    except for the rare cases where the old code produced broken output.

    [*] If one knows that one has hit this specific bug, data recovery might
        be possible by modifying LZMA2 decoder to add 64 KiB to the sizes of
        very tiny uncompressed chunks. This can be done by adding the
        following at the end of "case SEQ_COMPRESSED_1:" in lzma2_decoder.c
        (this might even need 11 instead of 2):

            if (coder->sequence == SEQ_COPY && coder->compressed_size <= 2)
                coder->compressed_size += 65536;

        Note that this will create a new error if LZMA2 stream correctly ends
        in a tiny uncompressed chunk; it's only a hack for data recovery.

    Reported-by: Vladimir Kukharenko (GenProof)

diff --git a/src/liblzma/lz/lz_encoder.h b/src/liblzma/lz/lz_encoder.h
index eb197c6b..25672a4d 100644
--- a/src/liblzma/lz/lz_encoder.h
+++ b/src/liblzma/lz/lz_encoder.h
@@ -301,15 +301,15 @@ mf_skip(lzma_mf *mf, uint32_t amount)
 /// to out[]. This is needed by LZMA2 to encode uncompressed chunks.
 static inline void
 mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
-		size_t *left)
+		size_t back, size_t *left)
 {
 	const size_t out_avail = out_size - *out_pos;
 	const size_t copy_size = my_min(out_avail, *left);

 	assert(mf->read_ahead == 0);
-	assert(mf->read_pos >= *left);
+	assert(mf->read_pos >= back + *left);

-	memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
+	memcpy(out + *out_pos, mf->buffer + mf->read_pos - back - *left,
 			copy_size);

 	*out_pos += copy_size;
diff --git a/src/liblzma/lzma/lzma2_encoder.c b/src/liblzma/lzma/lzma2_encoder.c
index 71cfd9b4..18fa4508 100644
--- a/src/liblzma/lzma/lzma2_encoder.c
+++ b/src/liblzma/lzma/lzma2_encoder.c
@@ -40,6 +40,9 @@ typedef struct {

 	/// Compressed size of a chunk (excluding headers); this is also used
 	/// to indicate the end of buf[] in SEQ_LZMA_COPY.
+	///
+	/// If encoding uncompressed chunks, in SEQ_UNCOMPRESSED_COPY this
+	/// is the size of the second chunk (excluding the header).
 	size_t compressed_size;

 	/// Read position in buf[]
@@ -207,6 +210,20 @@ lzma2_encode(void *coder_ptr, lzma_mf *restrict mf,
 			assert(coder->uncompressed_size
 					<= LZMA2_UNCOMPRESSED_MAX);
 			mf->read_ahead = 0;
+
+			// If two chunks are needed, store the size of
+			// the second chunk in coder->compressed_size.
+			if (coder->uncompressed_size > LZMA2_CHUNK_MAX) {
+				coder->compressed_size
+						= coder->uncompressed_size
+						- LZMA2_CHUNK_MAX;
+				assert(coder->compressed_size
+						< LZMA2_CHUNK_MAX);
+				coder->uncompressed_size = LZMA2_CHUNK_MAX;
+			} else {
+				coder->compressed_size = 0;
+			}
+
 			lzma2_header_uncompressed(coder);
 			coder->need_state_reset = true;
 			coder->sequence = SEQ_UNCOMPRESSED_HEADER;
@@ -247,11 +264,25 @@ lzma2_encode(void *coder_ptr, lzma_mf *restrict mf,
 	case SEQ_UNCOMPRESSED_COPY:
 		// Copy the uncompressed data as is from the dictionary
 		// to the output buffer.
-		mf_read(mf, out, out_pos, out_size, &coder->uncompressed_size);
+		//
+		// NOTE: Here coder->compressed_size is the size of the second
+		// uncompressed chunk or zero.
+		mf_read(mf, out, out_pos, out_size, coder->compressed_size,
+				&coder->uncompressed_size);
 		if (coder->uncompressed_size != 0)
 			return LZMA_OK;

-		coder->sequence = SEQ_INIT;
+		if (coder->compressed_size > 0) {
+			// Create a second uncompressed chunk whose size is
+			// in coder->compressed_size.
+			coder->uncompressed_size = coder->compressed_size;
+			coder->compressed_size = 0;
+			lzma2_header_uncompressed(coder);
+			coder->sequence = SEQ_UNCOMPRESSED_HEADER;
+		} else {
+			coder->sequence = SEQ_INIT;
+		}
+
 		break;
 	}

@@ -342,11 +373,15 @@ lzma2_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator,
 	// Make sure that we will always have enough history available in
 	// case we need to use uncompressed chunks. They are used when the
 	// compressed size of a chunk is not smaller than the uncompressed
-	// size, so we need to have at least LZMA2_COMPRESSED_MAX bytes
-	// history available.
-	if (lz_options->before_size + lz_options->dict_size < LZMA2_CHUNK_MAX)
-		lz_options->before_size
-				= LZMA2_CHUNK_MAX - lz_options->dict_size;
+	// size. The encoder may read ahead at most lz_options->after_size
+	// bytes, so we need LZMA2_CHUNK_MAX + lz_options->after_size bytes
+	// of history in the worst case (move_window() might be called in
+	// the middle of the encoding of an uncompressed chunk).
+	if (lz_options->before_size + lz_options->dict_size
+			< LZMA2_CHUNK_MAX + lz_options->after_size)
+		lz_options->before_size = LZMA2_CHUNK_MAX
+				+ lz_options->after_size
+				- lz_options->dict_size;

 	return LZMA_OK;
 }