contrib/credential: avoid fixed-size buffer in libsecret
commit64f1e658e935bea6c9afdc4fa8be1d3ad6740355
authorTaylor Blau <me@ttaylorr.com>
Mon, 1 May 2023 15:54:03 +0000 (1 11:54 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 May 2023 16:27:02 +0000 (1 09:27 -0700)
treef93db9257c69947b0320e7acb48a2c416444868a
parentde2fb99006575d55f878f9a4d02726cb598c361d
contrib/credential: avoid fixed-size buffer in libsecret

The libsecret credential helper reads the newline-delimited
protocol stream one line at a time by repeatedly calling fgets() into a
fixed-size buffer, and is thus affected by the vulnerability described
in the previous commit.

To mitigate this attack, avoid using a fixed-size buffer, and instead
rely on getline() to allocate a buffer as large as necessary to fit the
entire content of the line, preventing any protocol injection.

In most parts of Git we don't assume that every platform has getline().
But libsecret is primarily used on Linux, where we do already assume it
(using a knob in config.mak.uname). POSIX also added getline() in 2008,
so we'd expect other recent Unix-like operating systems to have it
(e.g., FreeBSD also does).

Note that the buffer was already allocated on the heap in this case, but
we'll swap `g_free()` for `free()`, since it will now be allocated by
the system `getline()`, rather than glib's `g_malloc()`.

Tested-by: Jeff King <peff@peff.net>
Co-authored-by: Jeff King <peff@peff.net>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/credential/libsecret/git-credential-libsecret.c