git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Han-Wen Nienhuys <hanwen@google.com>
Cc: sunshine@sunshineco.com, jrn@google.com, git@vger.kernel.org
Subject: Re* [PATCH v7 1/1] sideband: highlight keywords in remote sideband output
Date: Fri, 17 Aug 2018 11:44:59 -0700	[thread overview]
Message-ID: <xmqq8t54bzo4.fsf_-_@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <xmqqd0ugc07x.fsf@gitster-ct.c.googlers.com> (Junio C. Hamano's message of "Fri, 17 Aug 2018 11:33:06 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> 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);
 }
 
 

  reply	other threads:[~2018-08-17 18:45 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-07 12:51 [PATCH v7 0/1] sideband: highlight keywords in remote sideband output Han-Wen Nienhuys
2018-08-07 12:51 ` [PATCH v7 1/1] " Han-Wen Nienhuys
2018-08-17 18:33   ` Junio C Hamano
2018-08-17 18:44     ` Junio C Hamano [this message]
2018-08-18  6:09       ` Re* " Jonathan Nieder
2018-08-18 14:40         ` Junio C Hamano
2018-08-18 16:02           ` Junio C Hamano
2018-08-18 16:16             ` Junio C Hamano
2018-08-18 23:22               ` Jeff King
2018-08-20 14:21                 ` Junio C Hamano
2018-08-20 12:21               ` Han-Wen Nienhuys
2018-08-20 12:21                 ` Han-Wen Nienhuys
2018-08-20 14:32                 ` Junio C Hamano
2018-08-18  6:35       ` Jonathan Nieder
2018-08-18 16:06         ` Junio C Hamano
2018-08-07 21:01 ` [PATCH v7 0/1] " Junio C Hamano
2018-08-08 13:12   ` Han-Wen Nienhuys

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=xmqq8t54bzo4.fsf_-_@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=hanwen@google.com \
    --cc=jrn@google.com \
    --cc=sunshine@sunshineco.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).