🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #3228

[PATCH v2 1/5] object-file: check size of inflated loose object against its header

#3228
From: Elia Pinto
Date: Tue, 17 Nov 2026 11:38
133 lines
15621 bytes
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.

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.

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.

Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 object-file.c                | 15 +++++++++++----
 t/lib-loose.sh               |  5 ++++-
 t/t1060-object-corruption.sh | 28 ++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 5 deletions(-)

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;
 	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));
 		FREE_AND_NULL(buf);
 	} else if (stream->avail_in) {
diff --git a/t/lib-loose.sh b/t/lib-loose.sh
index 3613631eaf..807fca6f05 100644
--- a/t/lib-loose.sh
+++ b/t/lib-loose.sh
@@ -5,9 +5,12 @@
 #
 #   oid=$(echo foo | loose_obj .git/objects blob)
 #
+# If $3 is given, it is written as the size in the object header instead
+# of the real size of the contents.
+#
 loose_obj () {
 	cat >tmp_loose.content &&
-	size=$(wc -c <tmp_loose.content) &&
+	size=${3:-$(wc -c <tmp_loose.content)} &&
 	{
 		# Do not quote $size here; we want the shell
 		# to strip whitespace that "wc" adds on some platforms.
diff --git a/t/t1060-object-corruption.sh b/t/t1060-object-corruption.sh
index d2ef468b45..bd3d1adf5d 100755
--- a/t/t1060-object-corruption.sh
+++ b/t/t1060-object-corruption.sh
@@ -3,6 +3,7 @@
 test_description='see how we handle various forms of corruption'
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-loose.sh
 
 # convert "1234abcd" to ".git/objects/12/34abcd"
 obj_to_file() {
@@ -163,4 +164,31 @@ test_expect_success 'corrupted loose commit can be read from alternate' '
 	test_grep "inflate: data stream error" err
 '
 
+test_expect_success 'setup loose objects with bogus header size' '
+	git init bogus-size &&
+	(
+		cd bogus-size &&
+		printf "short" |
+		loose_obj .git/objects commit 200 >../oid-too-big &&
+		printf "much longer" |
+		loose_obj .git/objects commit 3 >../oid-too-small
+	)
+'
+
+test_expect_success 'reading loose object shorter than its header fails' '
+	test_must_fail git -C bogus-size cat-file commit $(cat oid-too-big) 2>err &&
+	test_grep "corrupt loose object" err
+'
+
+test_expect_success 'reading loose object longer than its header fails' '
+	test_must_fail git -C bogus-size cat-file commit $(cat oid-too-small) 2>err &&
+	test_grep "corrupt loose object" err
+'
+
+test_expect_success 'fsck reports loose objects with bogus header size' '
+	test_must_fail git -C bogus-size fsck 2>err &&
+	test_grep "corrupt loose object .$(cat oid-too-big)" err &&
+	test_grep "corrupt loose object .$(cat oid-too-small)" err
+'
+
 test_done
-- 
2.53.0
Technical Details
Message-ID: <edb5f34498e91beb0480d9b6ac417602580b3016.1794914210.git.gitter.spiros@gmail.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!git+bounces-75598-linux-kernel-git=dewy.ru
References: <cover.1793355390.git.gitter.spiros@gmail.com> <cover.1794914210.git.gitter.spiros@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!git+bounces-75598-linux-kernel-git=dewy.ru
From: gitter.spiros@gmail.com (Elia Pinto)
Newsgroups: linux.kernel.git
Subject: [PATCH v2 1/5] object-file: check size of inflated loose object against its header
Date: Tue, 17 Nov 2026 12:38:54 +0100
Organization: Usenet Network
Message-ID: <edb5f34498e91beb0480d9b6ac417602580b3016.1794914210.git.gitter.spiros@gmail.com>
References: <cover.1793355390.git.gitter.spiros@gmail.com> <cover.1794914210.git.gitter.spiros@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2317053"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:B06nRH0+gIcD4irPG0Rl5uyaVf4= sha256:GMpTzQk3mn9MDFf3bzPPFXpLyTLNYzLlVCkZ9M2WYLA=
	sha1:7U8fIes7j+QNehyggZgDFcUrK+E= sha256:sARzK306gTiumGQkhCSzVqRE0qXfF3xYC/SqbGU3Zqs=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5918A114F27; Tue, 17 Nov 2026 14:39:31 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id YRPTRvyAJSQL;
	Tue, 17 Nov 2026 14:39:27 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-75598-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 6B331114F1B; Tue, 17 Nov 2026 14:39:18 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id A075037289; Tue, 17 Nov 2026 11:39:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794915550;
	b=0+9Xne67syvhIpMJb2yKMBTWl7EDGDYH3Gvz67/JNu34hMNKKEtHkQlc/vgCCZmKqkp5
	 ggv/aJT1+D2tAPaReMeuOxECBfS3MHAh9jkuKASkmjAsBP0xAdxmGEzZPrxJuDS91Lj/Z
	 eBdydDNZejciaZwcT7DCu3SX3QAeLzJcBIz0G7nGfnnm/j3DdvL/BK8dMmu5XAPkcvcS2
	 JhJk78u4esb4AmwXfWD+DTsEGrsVlGlAKSiRBt6eDKcReXmlJD/Mp6JhbxWiEPbHyPauJ
	 HGpnm73/xnpq6Mu78MxXGVl9TytEVGobrnNIxksku+dMMBMY16tHGiz3s/s5o0A4Msw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794915550;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=iuGBUCmyzh9YkT9pFfoI7u10VW9CKEwghhVYPcXfQKA=;
	b=ChLW4OvfvKj+t8W5pLxw4tvyjRA7HdINj/AfDAt3Ei69epAp5DnDpuX48ArVD285zTMr
	 1tftsvnO/Mm5pnVDqyM6+u23eWYhxKqLnv0CY7DA1eNQTMHSiRZ+rUXGIzKmQ90efGr8W
	 sBgovfSAiu7+2Rx6fnlYaakLJ+jsq7bV9E7Mk52ouwr7eYStYkiVzWtvcchkAU4HHLy0C
	 2g8HU4mNDQXqYAdfEWxynAaXnhkVUCOkhdwtwnaqVOEzlJyVlvQ1zsHD8LtSx+p/gXV4r
	 AKUPhNhlQDVAmitcBHPn1UUl+HuVfS1eUCzpkg/6f/R7boYfd8owuw8Eq3I1TgIbIjA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JDOREiiz;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 14984606A334;
	Tue, 17 Nov 2026 11:39:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JDOREiiz
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.42
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794915549;
	b=STbGOD1BAW+IpgN9klwkgAaf5eUtFFQPjfxbuNu6y+wThsHTqipG4yedTNM9cLaxk+II
	 Oivficu3VweNjmBl7ffYW7U6KRqyCnRSmBbrnFIIXqfDRowWZ58OXZpx8FMgqv/qSo+WJ
	 jxljYU5hDYBjTRXTnZwlMc+4siwP6izxaZ2BS7jd+PrOcaRWcfdUD1aoW65oY8Ho9diwN
	 nIS1bi2sjLff4wWpr3gnIJrznd+Su0VyO6s6BaE9XT9XOCsh6f4QWDXlsUpg+DkT99UUZ
	 X5hYLi6Z1IeKl3YiWbbMXFurP/k3dyKj9+8+JRfLm9Djq98i8xoSL2NegllcxUU7euw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794915549;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=iuGBUCmyzh9YkT9pFfoI7u10VW9CKEwghhVYPcXfQKA=;
	b=F+6IdHLE6NxeLlB4m4YKfDCK/9jqcQQc7CUW+3f6J3x9IlNNEaEwu/VJiGzzAVaC/rgJ
	 EIrXFNHudZWxoPHqZ9eZp1E7oaDu0PLYuLAnwRhO9elqJlfEqDSgeM/6oq7RB+iMjOa/F
	 GNpKg15j024EiFptx9gi02G5DN4aeh6KdVBBWU5uI4qreQpUJ3DktexC/hqZDi1QCWqRW
	 LMgiry+NBBWyhhYYuGNiYD2i++rnVKKxTIDQV4eTLNk+V5HpywuFSygkrqzeu7KKy6bI6
	 dMxeMWUtXowLAmv61tkdsg39ucfAUr0jb6X0aFUZTJnUHGG6ychW17HRXM6UPgZa4Xg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=JDOREiiz;
	arc=none smtp.remote-ip=209.85.128.42
X-Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB07D606A881
	for <git@vger.kernel.org>; Tue, 17 Nov 2026 11:39:08 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DB07D606A881
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4a140e7405dso46706375e9.3
        for <git@vger.kernel.org>; Tue, 17 Nov 2026 03:39:08 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1794915547; x=1795520347; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=iuGBUCmyzh9YkT9pFfoI7u10VW9CKEwghhVYPcXfQKA=;
        b=JDOREiizFgXwtIKWkaMFNvu/GBB58zGJnxB+vRd6sj93ZciyRSGjB1ny1MbzhkDiOF
         3tII5z5Xt6CqNzfkyfMLn5/DIJEvYtgY3/nZpYdAyUglo05XhpO65LfktYO5am4xjvF0
         2RImuLL40Dd+hk9atPsSiG8mhiZsivC3Wc4gL82b3xRxG3jYTfJBMnR7Gxhzmqlnh8OF
         mJJH8n/wy82/Riawxa9r+fLZi1YlfRB5Ca/7hHQKy9km/4wepYBnel0aXCPz+mBcvgV6
         19Up2yZ6RBUvvmPtnYH44MoUTQjskZW0XwWtADWATnDwPxVFcvHBSduDkP36NP8Zw6GU
         0OvQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1794915547; x=1795520347;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=iuGBUCmyzh9YkT9pFfoI7u10VW9CKEwghhVYPcXfQKA=;
        b=o9iwzeNlR+7lr/l47DXnyMSY3vQEcdJSTa3O3vIg3Rcut8hd+RS03yciD9ThoD9KDS
         lc+6bxailmLuP58FtaD8Dk7x/oxAQHmbGzs4y9k+dTm3g4hn0DCBuG0BrZZJKCSgnbwx
         hynhkUb3gGfZ65myYyxeWaEIRsWtSxsZ/nOq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f17b1804b1-4bb60bd55eamr234484985e9.6.1794915547124;
        Tue, 17 Nov 2026 03:39:07 -0800 (PST)
X-Received: from ubuntu26lts1.example.com ([151.31.213.44])
        by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb78420edasm93555925e9.9.2026.11.17.03.39.06
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Tue, 17 Nov 2026 03:39:06 -0800 (PST)
X-Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Elia Pinto <gitter.spiros@gmail.com>,
	Ævar Arnfjörð Bjarmason <avarab@gmail.com>,
	Patrick Steinhardt <ps@pks.im>,
	Jeff King <peff@peff.net>,
	Junio C Hamano <gitster@pobox.com>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <cover.1794914210.git.gitter.spiros@gmail.com>
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list