]> git.ipfire.org Git - thirdparty/git.git/commit - attr.c
attr: fix overflow when upserting attribute with overly long name
authorPatrick Steinhardt <ps@pks.im>
Thu, 1 Dec 2022 14:45:15 +0000 (15:45 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Dec 2022 06:14:16 +0000 (15:14 +0900)
commiteb22e7dfa23da6bd9aed9bd1dad69e1e8e167d24
treebbb190465cae31b5131bf98ef2cafb9d621545e8
parentabd4d67ab0f84fff703fa14d9eebfd287b42daeb
attr: fix overflow when upserting attribute with overly long name

The function `git_attr_internal()` is called to upsert attributes into
the global map. And while all callers pass a `size_t`, the function
itself accepts an `int` as the attribute name's length. This can lead to
an integer overflow in case the attribute name is longer than `INT_MAX`.

Now this overflow seems harmless as the first thing we do is to call
`attr_name_valid()`, and that function only succeeds in case all chars
in the range of `namelen` match a certain small set of chars. We thus
can't do an out-of-bounds read as NUL is not part of that set and all
strings passed to this function are NUL-terminated. And furthermore, we
wouldn't ever read past the current attribute name anyway due to the
same reason. And if validation fails we will return early.

On the other hand it feels fragile to rely on this behaviour, even more
so given that we pass `namelen` to `FLEX_ALLOC_MEM()`. So let's instead
just do the correct thing here and accept a `size_t` as line length.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
attr.c