All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Kristofer Karlsson via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Elia Pinto <gitter.spiros@gmail.com>,
	Elijah Newren <newren@gmail.com>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Harald Nordgren <haraldnordgren@gmail.com>,
	Kristofer Karlsson <krka@spotify.com>
Subject: Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int
Date: Tue, 1 Dec 2026 16:11:33 +0100	[thread overview]
Message-ID: <aw7jpRvNW53QBXtF@pks.im> (raw)
In-Reply-To: <pull.2342.git.1796049772272.gitgitgadget@gmail.com>

On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:
> From: Kristofer Karlsson <krka@spotify.com>
> 
> The EXPENSIVE tests 13-15 in t1600 have been failing in CI on all
> 64-bit platforms since 53d5cfc3b8 (tree-walk: use size_t for the size
> of tree buffers, 2026-10-30) was merged:
> 
>   -fatal: index entry path is too long (4294967298 bytes)
>   +fatal: malformed mode in tree entry
> 
> The tests were added in 89048b48fb (t1600-index: add expensive tests
> for over-long tree paths, 2026-10-16).  They use a tree with a single
> entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores
> that length in the "int" pathlen of struct name_entry, where it wraps
> to -2.  update_tree_entry() then uses it to find the next entry, ends
> up in the middle of the name, and fails to parse it.

Things like this in central data structures make me wonder whether we
should be explicit with types. That is, do we really want to have a
different maximum tree entry length depending on the platform that
you're on?

I myself don't think that's sensible, so I wonder whether we should
convert this to e.g. an `uint32_t` (or an `int32_t` in case we need
negative values). But that's of course outside of the scope of this
patch series.

> diff --git a/tree-walk.c b/tree-walk.c
> index aaac1fd48d..f4b4d3fed7 100644
> --- a/tree-walk.c
> +++ b/tree-walk.c
> @@ -17,7 +17,7 @@
>  static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)
>  {
>  	const char *path;
> -	unsigned int len;
> +	size_t len;
>  	uint16_t mode;
>  	const unsigned hashsz = desc->algo->rawsz;
>  
> @@ -36,6 +36,11 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t siz
>  		return -1;
>  	}
>  	len = strlen(path) + 1;

Makes sense to change the type of `len`, as we assign the result of
`strlen()` to it.

> +	if (len - 1 > INT_MAX) {
> +		strbuf_addf(err, _("too long filename in tree entry (%"PRIuMAX" bytes)"),
> +			    (uintmax_t)(len - 1));
> +		return -1;
> +	}

So restricting the length makes sense to me. But as mentioned above, do
we maybe want to check against `INT32_MAX`? I don't think we support any
platforms (or whether those even exist) with where int is 16 bits, but I
think it does make a ton of sense to behave the same on all platforms.

We can then still adapt `struct name_entry::len` at a later point in
time.

Ideally, we'd also have documentation around this somewhere to spell out
that tree entries may at most be INT32_MAX.

Thanks!

Patrick

  reply	other threads:[~2026-12-01 15:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 14:42 [PATCH] tree-walk: reject tree entries whose name length overflows an int Kristofer Karlsson via GitGitGadget
2026-12-01 15:11 ` Patrick Steinhardt [this message]
2026-12-01 15:58   ` Kristofer Karlsson

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=aw7jpRvNW53QBXtF@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitter.spiros@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=krka@spotify.com \
    --cc=newren@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.