Git development
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
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>,
	"Jeff King" <peff@peff.net>, "Junio C Hamano" <gitster@pobox.com>
Subject: Re: [PATCH v2 1/5] object-file: check size of inflated loose object against its header
Date: Wed, 18 Nov 2026 13:21:30 +0100	[thread overview]
Message-ID: <av2YSgkd2fUNRlNM@pks.im> (raw)
In-Reply-To: <edb5f34498e91beb0480d9b6ac417602580b3016.1794914210.git.gitter.spiros@gmail.com>

On Tue, Nov 17, 2026 at 12:38:54PM +0100, Elia Pinto wrote:
> 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.

That's certainly fishy.

> 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.

Hum, this part I don't quite understand. Do you mean to say that the
header declares "commit 200", but it actually carries 400 bytes, so we
only copy the first 200 and ignore the remainder? If so, why would the
stream have already ended in that case?

> 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.

Makes sense -- the object should yield exactly as many bytes as its
header declares, and everything else is a corrupted object. We could of
course try to paper over such corruption, but I very much doubt that
anything good comes out of that.

> 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;

Not a problem that you introduce, but it's quite fishy that we use
strlen(3p) here, as the input buffer is known-bounded. I assume that
doesn't really matter as we've probably already verified the header
before we call this function. But regardless of that I think we should
use `memchr(buffer, '\0', size)` instead.

Maybe that could be done in a separate patch.

>  	unsigned char *buf = xmallocz(size);
>  	int status = Z_OK;
>  
> -	n = stream->total_out - bytes;
> +	n = stream->total_out - hdrlen;
>  	if (n > size)
>  		n = size;
> -	memcpy(buf, (char *) buffer + bytes, n);
> +	memcpy(buf, (char *) buffer + hdrlen, n);
>  	bytes = n;
>  	if (bytes <= size) {
>  		/*
> @@ -244,7 +244,14 @@ void *unpack_loose_rest(git_zstream *stream,
>  		}
>  	}
>  
> -	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) {
>  		error(_("corrupt loose object '%s'"), oid_to_hex(oid));

Would it make sense to be more specific here in the case where the
expected size doesn't match the actual size and print an error message
saying exactly that?

Patrick

  parent reply	other threads:[~2026-11-18 12:21 UTC|newest]

Thread overview: 20+ 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
2026-11-18 12:21     ` Patrick Steinhardt [this message]
2026-11-18 18:17       ` Junio C Hamano
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=av2YSgkd2fUNRlNM@pks.im \
    --to=ps@pks.im \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=gitter.spiros@gmail.com \
    --cc=peff@peff.net \
    /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