git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 17, 2018, 18:44 UTC
Message-ID
<xmqq8t54bzo4.fsf_-_@gitster-ct.c.googlers.com>
In-Reply-To
<xmqqd0ugc07x.fsf@gitster-ct.c.googlers.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 16 quoted lines
> This loop can run out of bytes in src in search of non-space before
> n gets to zero or negative, and when that happens ...
>
>> +	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>> +		struct keyword_entry *p = keywords + i;
>> +		int len = strlen(p->keyword);
>> +		/*
>> +		 * Match case insensitively, so we colorize output from existing
>> +		 * servers regardless of the case that they use for their
>> +		 * messages. We only highlight the word precisely, so
>> +		 * "successful" stays uncolored.
>> +		 */
>> +		if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {
>
> ... these access src[] beyond the end of what the caller intended to
> show us, and also ...

Actually, leaving when !n before this loop is insufficient. src[] may have 2 bytes "in" remaining, and we may be trying to see if it begins with "info", for example, and using strncasecmp() with len==4 would of course read beyond the end of src[].

-- >8 --
Subject: sideband: do not read beyond the end of input

The caller of maybe_colorize_sideband() gives a counted buffer <src,n>, but the callee checked *src as if it were a NUL terminated buffer. If src[] had all isspace() bytes in it, we would have made n negative, and then (1) called number of strncasecmp() to see if the remaining bytes in src[] matched keywords, reading beyond the end of the array, and/or (2) called strbuf_add() with negative count, most likely triggering the "you want to use way too much memory" error due to unsigned integer overflow.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 sideband.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/sideband.c b/sideband.c
index 1c6bb0e25b..372039247f 100644
--- a/sideband.c
+++ b/sideband.c
@@ -75,7 +75,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		return;
 	}
 
-	while (isspace(*src)) {
+	while (0 < n && isspace(*src)) {
 		strbuf_addch(dest, *src);
 		src++;
 		n--;
@@ -84,6 +84,9 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
 		struct keyword_entry *p = keywords + i;
 		int len = strlen(p->keyword);
+
+		if (n <= len)
+			continue;
 		/*
 		 * Match case insensitively, so we colorize output from existing
 		 * servers regardless of the case that they use for their
@@ -100,8 +103,8 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
 		}
 	}
 
-	strbuf_add(dest, src, n);
-
+	if (0 < n)
+		strbuf_add(dest, src, n);
 }
 
 
Previous: Junio C HamanoNext: Jonathan Nieder
Message 4 of 17 in “sideband: highlight keywords in remote sideband output”
  1. 0/1 sideband: highlight keywords in remote sideband outputHan-Wen Nienhuys, Aug 7, 2018
  2. 1/1 sideband: highlight keywords in remote sideband outputHan-Wen Nienhuys, Aug 7, 2018
  3. Junio C HamanoAug 17, 2018
  4. Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband outputJunio C Hamano, Aug 17, 2018
  5. Jonathan NiederAug 18, 2018
  6. Junio C HamanoAug 18, 2018
  7. Junio C HamanoAug 18, 2018
  8. Junio C HamanoAug 18, 2018
  9. Jeff KingAug 18, 2018
  10. Junio C HamanoAug 20, 2018
  11. Han-Wen NienhuysAug 20, 2018
  12. Han-Wen NienhuysAug 20, 2018
  13. Junio C HamanoAug 20, 2018
  14. Jonathan NiederAug 18, 2018
  15. Junio C HamanoAug 18, 2018
  16. Junio C HamanoAug 7, 2018
  17. Han-Wen NienhuysAug 8, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.