Skip to content

Commit 9b81fae

Browse files
pks-tdscho
authored andcommitted
builtin/blame: fix out-of-bounds read with excessive --abbrev
In 6411a0a (builtin/blame: fix type of `length` variable when emitting object ID, 2024-12-06) we have fixed the type of the `length` variable. In order to avoid a cast from `size_t` to `int` in the call to printf(3p) with the "%.*s" formatter we have converted the code to instead use fwrite(3p), which accepts the length as a `size_t`. It was reported though that this makes us read over the end of the OID array when the provided `--abbrev=` length exceeds the length of the object ID. This is because fwrite(3p) of course doesn't stop when it sees a NUL byte, where as printf(3p) does. Fix the bug by reverting back to printf(3p) and culling the provided length to keep it from overflowing when cast to an `int`. Note that when calling `git blame --abbrev=<N>` with an `N` that is larger than the maximal OID hex size, there is a subtle side effect that makes it behave _differently_ than specifying said maximal hex size: When the command outputs boundary, unblamable or ignored commits' OIDs, those outputs are prefixed with characters indicating this, and the `abbrev` value is used to align the information that comes after the OID, clipping it as needed. Specifying a "too large" abbrev value here tells Git that yes, we want the full OIDs and don't you worry about alignment. Thanks to SHA-256 being _larger_ than the default SHA-1-based OIDs, and thanks to clipping at `GIT_MAX_HEXSZ`, this change of behavior can only be observed when running the test in SHA-256 mode. Nevertheless, this means that we cannot cull at `GIT_MAX_HEXSZ` but at a slightly larger threshold value. This patch is a slightly modified version of the one sent as https://lore.kernel.org/git/20250109-b4-pks-blame-truncate-hash-length-v1-1-9ad4bb09e059@pks.im to the Git mailing list. Reported-by: Johannes Schindelin <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 783fac4 commit 9b81fae

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Diff for: builtin/blame.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
476476
size_t length = (opt & OUTPUT_LONG_OBJECT_NAME) ?
477477
the_hash_algo->hexsz : (size_t) abbrev;
478478

479+
/*
480+
* Leave enough space for ^, * and ? indicators (boundary,
481+
* unblamable, ignored).
482+
*/
483+
if (length > GIT_MAX_HEXSZ + 3)
484+
length = GIT_MAX_HEXSZ + 3;
485+
479486
if (opt & OUTPUT_COLOR_LINE) {
480487
if (cnt > 0) {
481488
color = repeated_meta_color;
@@ -505,7 +512,7 @@ static void emit_other(struct blame_scoreboard *sb, struct blame_entry *ent, int
505512
length--;
506513
putchar('?');
507514
}
508-
fwrite(hex, 1, length, stdout);
515+
printf("%.*s", (int)length, hex);
509516
if (opt & OUTPUT_ANNOTATE_COMPAT) {
510517
const char *name;
511518
if (opt & OUTPUT_SHOW_EMAIL)

Diff for: t/t8002-blame.sh

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ test_expect_success '--no-abbrev works like --abbrev with full length' '
126126
check_abbrev $hexsz --no-abbrev
127127
'
128128

129+
test_expect_success 'blame --abbrev gets truncated' '
130+
check_abbrev 9000 --abbrev=9000 HEAD..
131+
'
132+
129133
test_expect_success '--exclude-promisor-objects does not BUG-crash' '
130134
test_must_fail git blame --exclude-promisor-objects one
131135
'

0 commit comments

Comments
 (0)