Article View: linux.kernel.git
Article #1903[PATCH 1/6] object-file: check size of inflated loose object against its header
From: Elia Pinto
Date: Fri, 30 Oct 2026 10:28
Date: Fri, 30 Oct 2026 10:28
133 lines
15531 bytes
15531 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 c049bf13f7..6d39138511 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:
<6c2efb87fbb95d3d24ac8e4ef34b586c2b25980e.1793355390.git.gitter.spiros@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73872-linux-kernel-git=dewy.ruReferences:
<cover.1793355390.git.gitter.spiros@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73872-linux-kernel-git=dewy.ru
From: gitter.spiros@gmail.com (Elia Pinto)
Newsgroups: linux.kernel.git
Subject: [PATCH 1/6] object-file: check size of inflated loose object against its header
Date: Fri, 30 Oct 2026 11:28:34 +0100
Organization: Usenet Network
Message-ID: <6c2efb87fbb95d3d24ac8e4ef34b586c2b25980e.1793355390.git.gitter.spiros@gmail.com>
References: <cover.1793355390.git.gitter.spiros@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2213693"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:/FACadIWnDWDna+AqIWYaFfs3s0= sha256:moG8sD2x02kH03qVnlvNnHcsJlkGK2WNGgSSUgynmV0=
sha1:thIqBe5qDeuQY4C+e2KttHHD6sU= sha256:hx/u84dVFAIWxvhJJbyqe9eRQpNxzetjVXUprGzEoJI=
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 3D0E9125C92; Fri, 30 Oct 2026 13:39:16 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.947
X-Spam-Status: No, score=-1.947 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.1, 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.25,
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 01Ypc_VM1XRI;
Fri, 30 Oct 2026 13:39:14 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-73872-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 sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
by mail.dewy.ru (Postfix) with ESMTPS id 7C1A8125C8F; Fri, 30 Oct 2026 13:39:04 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sin.lore.kernel.org (Postfix) with ESMTP id D95B42BA81; Fri, 30 Oct 2026 10:29:49 +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=1793356129;
b=uQnKetNuVjXPgpgaV8AQu9cGAmOEDB4p4WQjDqeAiy5a9DvkoIL0tH9gcKlMEq4wYqTS
vUbJ/57bsELvi8Py2z8993pFe2GnpDEKBxywWQwQbpGSdTzEcYylWp4gjWAHgVS1/yXPa
NEgjoBKoL8p6rctWV3Om0Qa6FzTNAMg6sWUWtPebbYZx8EWKNplFNOP8nwJ9t31QB60e3
oU2WJijqXnJUJ04xiJywxotMyBph3WVq/PV+tpM/goXlJeDvO2kpaG6GBYao1qBt2BNaF
5ZFDYB7Mrx1uuAltScJ/12XSvU0sJtKQY8sMLdWg+omTIq3iyN0x+JDc/CqQyvElmEA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793356129;
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=YfdX/o6CwbfM/kj1CTQ2Cv7cDqpw0N6PZX5sOl9jbVY=;
b=EfBQamuXlEXmImgr5HJcbHYqBITxULn0o4GGNLdhPPECA29gvF3xgnuIwev1n6n6LfAU
/c7FcVc32w56p8qZ7Weeale1Im0H98hp+KodIvUXrirdC7+XVmIB2l0+bRVbloQ7HtCl0
ku6gyWTkcNetvJSIj7o2fGXKToQewhRTGL+ffcsSFLl1g6FcwCCxp8cnhtYpi73I8l9LK
oqz2EXAm78/Jo5Go2itURIwcaniUuH1S5cBPfMGb9EsGDTzs/fFW9e6hR97LRG3SjV9I9
43H5mq/Chx5Eu2jkD0kSZKLRs4B8/XZ/62ylpWd7dHZ0yCLUj1QKMZDCa08QSASjBmQ==
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=FgIYuViw;
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 048CB60A6F4F;
Fri, 30 Oct 2026 10:28:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=FgIYuViw
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.53
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793356128;
b=27akGapBFdzuhKsgsIomwdR+prv4mZUhj1oecBz/n/7H3MCLmIIVJGpKjzxvWo6hgXmT
kCqvqgYeFI0afWXETS3CQ8ZSEwCZYkpmsFKaId95f0Cwt6iVNxkm3rHzy3L2pqvVBMW/J
XHtga+uI3O28ORtWipZgLCSn7Bv8H98hGY8BQYWLVW1Lg68w9ibL+iFsvG6hTfTcpr9RT
fU6Z+8bOJlpOQGGIw7KO3haere6SMPbj2uV8X0M+ueO/XjD+k5Qn3IzufpUnYMjSj87IL
HhDpUYNRKeKzNixr07dl2upWC19tprIvr6XFL2N8bSFIOgCcO1x8DYCEqoq7/0D7A3w==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793356128;
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=YfdX/o6CwbfM/kj1CTQ2Cv7cDqpw0N6PZX5sOl9jbVY=;
b=U5FYDShjhQaqk2rSpKJToVLBW02m/Wo4jZkx6jjxOzzwe/pkOumK+OABbrKumnkO1Y5u
EyjTbBdAyJAGk1Jj8I/Q7QYAAZmV0LJZQVATDt2ocogsDsVk++IRkZOwKGGcriQGJSkYM
NHmOl/kvF2o1ZRooq8vi/WI0djgX4qiF7UQl48T+XPN/glHSn9jkH/jpkRh7+VKvK9AXO
1PEn4DB9MtDrPZFtUcOPm1bYQoQiIbOqTYy2UI0FSqsyz6DUEBwgpXT8/TxLIb0psKMml
/cyxFjlXll2RdFnzGA1IbNy32iBPZ4uyixHk8W7SIlG9Qm+Pi1y10TQOByKeW2o/LAA==
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=FgIYuViw;
arc=none smtp.remote-ip=209.85.128.53
X-Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53])
(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 C712F60A6F43
for <git@vger.kernel.org>; Fri, 30 Oct 2026 10:28:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C712F60A6F43
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-f53.google.com with SMTP id 5b1f17b1804b1-4a1b07c3c5fso4493285e9.1
for <git@vger.kernel.org>; Fri, 30 Oct 2026 03:28:47 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1793356126; x=1793960926; 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=YfdX/o6CwbfM/kj1CTQ2Cv7cDqpw0N6PZX5sOl9jbVY=;
b=FgIYuViwHoxIpd2OMQs9rJ3qujv0D6FuOvK2uPIV7NrNYncukQ1oURQ5CdztzyWqC4
nhpGshAWkDEaCvTKLbtfNvZ+Cawg6xI5EjqZ+RTxLQu4jPa3mK2G5cmmetPyKqy3ZRwk
ur2xNml9TLCvqQnJpLjVBDQ75BWjkd/CmpgK/zXDczKyzD9uqaLJMYtdqwBVMYJteEJC
6/A3xwaEp2WZXFDejmAdV+rVwY9cPCtaE0go6ZNZkblMrwv/ryDmUSJpmztZqGcRwcrg
a/+A7wZx1YDm6UpD/qNVYHcOL8jbiXoLON7VKsNtZ7uOItrXUGVQYJJ5MXzRVJJf4Hur
1/IQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793356126; x=1793960926;
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=YfdX/o6CwbfM/kj1CTQ2Cv7cDqpw0N6PZX5sOl9jbVY=;
b=ohfsUA744nByH6zmjC88aEdF8h7hv7333wRjisIdX8aKycb4JbdGK0q9Wn4UxHXOHy
kAB2cO0x/B8NpdpEaGJ4J8Mc9QgcB/LgN0XA7dv1FZdFS9y20HjKdyCfkpO7jUEZbVIM
gNHEgGsjusBoddo1XyoK6ux3skowvmOYPOZLhNsV5L9vKvGWeHFQwW57k5KdKfJqWirN
SMvW6wgV6k97SLvOESKaH+lJ3/OlcHwQ0Yuxwp1mmLyuCx3phDETVWceD8Fq5AFGAdXF
enIhiGGVf7jJC0n84Y5gEYXiWVsrhFPOdgnQz4JaP267zBGVci9uB6c/jGHNBpVfETEu
eplA==
X-Gm-Message-State: AFq9FYKpefW4MLy9BzZunl11OILsRBG1mFC2a3xuaHKKU+5zZLPKbzjY
yZubBbG4A/plfLWugow6+dgS5AoPflmQSj8MfnSmqEY1SUCz7lWkFi7nQE3cNQ==
X-Gm-Gg: AYBFou1QrCUqzY1hLbRjrbPbw8oG0zasrhGkK+WuDxiFAh/L7MKnpDsmPdqiLRzr3CD
Go1q47EVyoj2LPbv0gVrDrtGpiZsrFS8G5e4yJRvP6LwYFIF2Y+j4gBRCeGEkQ9goRxIws6Zx0G
DQZ8dUXJQl4DCcJk4QZFoW5/nbnCTKurFKLIMQjlFop9kB92NvHpQYu0PRsAdsCqAn/eZKotqxg
RXupjKjpZVdxdeWqtLrV7wIezKfPLfQcyqMbwMEDZMqyTbyErAeXwTYu8fUi41YxFXDdAWDAkdS
MR9ZR5MTLTQhL5NzX5ZWi8M94ptzjAauogSn+6mgAHZvV3+4plGOj8y/mJOh7yhlXT7D297aQ3n
reK2vYWROUKyU8iV1cpA4YFt4PdCP9QkLEjXcHz9Lnw+Qd0bDJd0BK4NXjBp6fN0IwjwRvkQxA9
CoX1PYPf0mBJBSt0OOxNXHRIqoQc8uCg+Zp0IxUXFEBOWSK6f4rAnsmRHbPvwIFZilzSHRaBsj6
ORwg6aq66l9hJPkSXA+RL+C+O7it22oNX41CUKTSUFTLFOMlaAHMiKkL2hQonBHcFOBBq/L5mr7
pew4SqXosbJW6l+HOHtQPRe9FTxr85w0XOfDi4G3mQ2rSAo4Xw==
X-Received: by 2002:a05:600c:354a:b0:4a3:5a55:f454 with SMTP id 5b1f17b1804b1-4ac17e10ac3mr38502525e9.19.1793356126139;
Fri, 30 Oct 2026 03:28:46 -0700 (PDT)
X-Received: from ubuntu26lts1.example.com ([205.147.30.18])
by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e71b964desm2449445f8f.46.2026.10.30.03.28.45
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 30 Oct 2026 03:28:45 -0700 (PDT)
X-Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Elia Pinto <gitter.spiros@gmail.com>,
Jeff King <peff@peff.net>,
Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>,
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <cover.1793355390.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