Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Elia Pinto <gitter.spiros@gmail.com>
Cc: git@vger.kernel.org,
	"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Patrick Steinhardt" <ps@pks.im>, "Jeff King" <peff@peff.net>
Subject: Re: [PATCH v2 1/5] object-file: check size of inflated loose object against its header
Date: Tue, 17 Nov 2026 11:33:20 -0800	[thread overview]
Message-ID: <xmqqbj7ndzi7.fsf@gitster.g> (raw)
In-Reply-To: <edb5f34498e91beb0480d9b6ac417602580b3016.1794914210.git.gitter.spiros@gmail.com> (Elia Pinto's message of "Tue, 17 Nov 2026 12:38:54 +0100")

Elia Pinto <gitter.spiros@gmail.com> writes:

> unpack_loose_rest() allocates a buffer of the size given in the object
> header and inflates the rest of the stream into it, but it never checks
> that the stream actually produced that many bytes. If the compressed
> data ends early we return success with the tail of the buffer never
> written, and callers like odb_read_object() hand it out as object
> content. For example, "git cat-file commit" on a loose object whose
> header says "commit 200" but which contains only 44 bytes prints 156
> bytes of uninitialized heap memory.

Interesting.

We only inflate to read up to the first MAX_HEADER_LEN bytes, and
that is sufficient to read "<type> <len>\0".  unpack_loose_rest()
first prepares a buffer that is <len> bytes big, copies the
remainder of the header buffer to it, and then goes into a loop to
fill the remainder of the buffer with the remainder of the zlib
stream inflated.  The condition to terminate the loop is for
git_inflate() to return something other than Z_OK, and we are
protected from the case where the deflated payload inflates beyond
the prepared buffer (i.e., the malicious or broken object file claims
it is smaller than the inflated data, attempting to cause a buffer
overrun) because we would stop the loop with Z_BUF_ERROR, which
signals that we prepared a buffer that is too small to receive the
payload.  But git_inflate() may return Z_OK without filling all of
the buffer we prepared (i.e., the malicious or broken object file
claims with <len> that it is larger than it actually is), and we did
not check for this condition.

> The opposite case slips through, too: when the whole object fits into
> the buffer filled by unpack_loose_header(), we copy only "size" bytes
> out of it, the stream has already ended, and the extra data is silently
> dropped.

In other words, when the object file is inflated into a buffer that
is MAX_HEADER_LEN bytes long, "<type> <len>\0" may be 10 bytes long;
MAX_HEADER_LEN is 32 bytes, so there can be up to 22 bytes of
payload already inflated in the same header buffer.  If "<len>" says
that the payload is only 10 bytes, even though it actually inflates
to 15 bytes, we happily copy the 10 bytes to the final buffer,
ignoring the remaining 5 bytes without even noticing.

> The packfile reader in unpack_compressed_entry() and index-pack already
> compare total_out with the expected size. Do the same here and report
> the object as corrupt if they differ. Today fsck notices the first case
> only by accident, because it hashes the uninitialized bytes and gets a
> mismatch.

;-)

> Teach loose_obj() in lib-loose.sh to write a bogus size, so that the
> tests can create such objects.

"lib-loose.sh" -> "t/lib-loose.sh" for clarity.

Nicely spotted.

> diff --git a/object-file.c b/object-file.c
> index e04ce0bc11..5d03ae94c7 100644
> --- a/object-file.c
> +++ b/object-file.c
> @@ -212,14 +212,14 @@ void *unpack_loose_rest(git_zstream *stream,
>  			void *buffer, unsigned long size,
>  			const struct object_id *oid)
>  {
> -	size_t bytes = strlen(buffer) + 1, n;
> +	size_t hdrlen = strlen(buffer) + 1, bytes, n;

Earlier, "bytes" here was initialized so that buffer[bytes] points
at the first byte beyond the terminating NUL in "<type> <len>\0".
The parse_loose_header() function, which our caller has called to
find "size", guarantees that the terminating NUL exists, so it is
safe to call strlen().

Now we have a dedicated variable hdrlen that gives the length of the
header, including the NUL at its end.

>  	unsigned char *buf = xmallocz(size);

We allocate <len> bytes (in size) for buf[] for the payload.

>  	int status = Z_OK;
>  
> -	n = stream->total_out - bytes;
> +	n = stream->total_out - hdrlen;
>  	if (n > size)
>  		n = size;

The arithmetic is the same here as before.  n gives us the number of
bytes from the real payload we already have when we inflated the
header.  If that is larger than size (i.e., <len>, which the object
header claimed to be the size of the payload), we limit n to that
size.

> -	memcpy(buf, (char *) buffer + bytes, n);
> +	memcpy(buf, (char *) buffer + hdrlen, n);

And we copy the first part of the payload data out of the tail of
the header buffer to the beginning of buf[].

>  	bytes = n;

Here, as before, bytes keeps track of the number of bytes of the
real payload added to buf[].

>  	if (bytes <= size) {
>  		/*
> @@ -244,7 +244,14 @@ void *unpack_loose_rest(git_zstream *stream,
>  		}
>  	}

Before this point, we have an "if" block that reads more data from
the zlib stream after the initial part read by unpack_loose_header()
into buf[] until it fills up or until the zlib stream finishes,
whichever comes first.

Thanks to the condition of the "if" block covering the bytes==size
case, we would have made sure we called git_inflate(Z_FINISH) even
when we had all the payload bytes inflated into buffer[] by the
caller's call to unpack_loose_header().  So we can rely on status
being what we got from git_inflate().  And we are not going to bust
the end of buf[] because stream->avail_out keeps track of the
remaining space in buf[].

> -	if (status != Z_STREAM_END) {
> +	/*
> +	 * Make sure that the stream produced exactly as many bytes as the
> +	 * header claims. Otherwise we would hand back a buffer whose tail
> +	 * was never written (if the stream is short), or silently drop data
> +	 * (if the stream is long but ended before we noticed).
> +	 */
> +	if (status != Z_STREAM_END ||
> +	    stream->total_out - hdrlen != size) {

So a successful inflation of the payload should give us Z_STREAM_END
and total_out being the same as the sum of the payload <len> (aka
size) and the header length (aka hdrlen).  I might have written the
latter as

	stream->total_out != hdrlen + size

but that does not go beyond personal preference, I guess.  What I am
saying is that I see this patch is doing exactly the right thing.

Looking good.  Thanks.

  reply	other threads:[~2026-11-17 19:33 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 10:28 [PATCH 0/6] loose object size checks and a few size truncations Elia Pinto
2026-10-30 10:28 ` [PATCH 1/6] object-file: check size of inflated loose object against its header Elia Pinto
2026-10-30 10:28 ` [PATCH 2/6] object-file: use size_t for loose object sizes Elia Pinto
2026-10-30 10:28 ` [PATCH 3/6] odb/source-loose: check object size when streaming loose objects Elia Pinto
2026-10-30 10:28 ` [PATCH 4/6] cat-file: use size_t for the length passed to batch_write() Elia Pinto
2026-10-30 10:28 ` [PATCH 5/6] notes: avoid truncating the size of concatenated notes Elia Pinto
2026-10-30 10:28 ` [PATCH 6/6] diff: use size_t in count_trailing_blank() Elia Pinto
2026-11-17 11:38 ` [PATCH v2 0/5] loose object size checks and a few size truncations Elia Pinto
2026-11-17 11:38   ` [PATCH v2 1/5] object-file: check size of inflated loose object against its header Elia Pinto
2026-11-17 19:33     ` Junio C Hamano [this message]
2026-11-17 11:38   ` [PATCH v2 2/5] object-file: use size_t for loose object sizes Elia Pinto
2026-11-17 22:34     ` Junio C Hamano
2026-11-17 11:38   ` [PATCH v2 3/5] odb/source-loose: check object size when streaming loose objects Elia Pinto
2026-11-17 22:55     ` Junio C Hamano
2026-11-17 11:38   ` [PATCH v2 4/5] cat-file: use size_t for the length passed to batch_write() Elia Pinto
2026-11-17 22:59     ` Junio C Hamano
2026-11-17 11:38   ` [PATCH v2 5/5] notes: avoid truncating the size of concatenated notes Elia Pinto
2026-11-17 23:01     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqbj7ndzi7.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitter.spiros@gmail.com \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox