git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] name-hash: fix buffer overrun
@ 2017-03-31 17:32 git
  2017-03-31 17:32 ` git
  0 siblings, 1 reply; 11+ messages in thread
From: git @ 2017-03-31 17:32 UTC (permalink / raw)
  To: git; +Cc: gitster, peff, kewill, Jeff Hostetler

From: Jeff Hostetler <jeffhost@microsoft.com>

Fix buffer overrun in handle_range_dir() when the final entry
in the index was the only file in the last directory, such as
"a/b/foo.txt". The look ahead (k_start + 1) was invalid since
(k_start + 1) == k_end.

This bug was introduced by Jeff in "jh/memihash-opt" which was
recently merged into master.

Kevin Willford (1):
  name-hash: fix buffer overrun

 name-hash.c                             |  4 +++-
 t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 t/t3008-ls-files-lazy-init-name-hash.sh

-- 
2.9.3


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] name-hash: fix buffer overrun
  2017-03-31 17:32 [PATCH] name-hash: fix buffer overrun git
@ 2017-03-31 17:32 ` git
  2017-03-31 19:35   ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: git @ 2017-03-31 17:32 UTC (permalink / raw)
  To: git
  Cc: gitster, peff, kewill, Kevin Willford, Johannes Schindelin,
	Jeff Hostetler

From: Kevin Willford <kewillf@microsoft.com>

Add check for the end of the entries for the thread partition.
Add test for lazy init name hash with specific directory structure

The lazy init hash name was causing a buffer overflow when the last
entry in the index was multiple folder deep with parent folders that
did not have any files in them.

This adds a test for the boundary condition of the thread partitions
with the folder structure that was triggering the buffer overflow.

The fix was to check if it is the last entry for the thread partition
in the handle_range_dir and not try to use the next entry in the cache.

Signed-off-by: Kevin Willford <kewillf@microsoft.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>

---
 name-hash.c                             |  4 +++-
 t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 t/t3008-ls-files-lazy-init-name-hash.sh

diff --git a/name-hash.c b/name-hash.c
index cac313c..39309ef 100644
--- a/name-hash.c
+++ b/name-hash.c
@@ -342,7 +342,9 @@ static int handle_range_dir(
 	 * Scan forward in the index array for index entries having the same
 	 * path prefix (that are also in this directory).
 	 */
-	if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
+	if (k_start + 1 >= k_end)
+		k = k_end;
+	else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
 		k = k_start + 1;
 	else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
 		k = k_end;
diff --git a/t/t3008-ls-files-lazy-init-name-hash.sh b/t/t3008-ls-files-lazy-init-name-hash.sh
new file mode 100644
index 0000000..e46a11b
--- /dev/null
+++ b/t/t3008-ls-files-lazy-init-name-hash.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+test_description='Test the lazy init name hash with various folder structures'
+
+. ./test-lib.sh
+
+test_expect_success 'no buffer overflow in lazy_init_name_hash' '
+	(
+	    test_seq 2000 | sed "s/^/a_/"
+	    echo b/b/b
+	    test_seq 2000 | sed "s/^/c_/"
+	    test_seq 50 | sed "s/^/d_/" | tr "\n" "/"; echo d
+	) |
+	sed "s/^/100644 $EMPTY_BLOB\t/" |
+	git update-index --index-info &&
+	test-lazy-init-name-hash -m
+'
+
+test_done
-- 
2.9.3


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-03-31 17:32 ` git
@ 2017-03-31 19:35   ` Junio C Hamano
  2017-03-31 21:18     ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2017-03-31 19:35 UTC (permalink / raw)
  To: git; +Cc: git, peff, kewill, Kevin Willford, Johannes Schindelin,
	Jeff Hostetler

git@jeffhostetler.com writes:

> From: Kevin Willford <kewillf@microsoft.com>
>
> Add check for the end of the entries for the thread partition.
> Add test for lazy init name hash with specific directory structure
>
> The lazy init hash name was causing a buffer overflow when the last
> entry in the index was multiple folder deep with parent folders that
> did not have any files in them.
>
> This adds a test for the boundary condition of the thread partitions
> with the folder structure that was triggering the buffer overflow.
>
> The fix was to check if it is the last entry for the thread partition
> in the handle_range_dir and not try to use the next entry in the cache.
>
> Signed-off-by: Kevin Willford <kewillf@microsoft.com>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
>
> ---

Will queue with ...

>  name-hash.c                             |  4 +++-
>  t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
>  2 files changed, 22 insertions(+), 1 deletion(-)
>  create mode 100644 t/t3008-ls-files-lazy-init-name-hash.sh

... this thing fixed by "chmod +x" (otherwise the tests won't start).

Thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-03-31 19:35   ` Junio C Hamano
@ 2017-03-31 21:18     ` Junio C Hamano
  2017-03-31 23:18       ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2017-03-31 21:18 UTC (permalink / raw)
  To: git; +Cc: git, peff, kewillf, Kevin Willford, Johannes Schindelin,
	Jeff Hostetler

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

> Will queue with ...
>
>>  name-hash.c                             |  4 +++-
>>  t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
>>  2 files changed, 22 insertions(+), 1 deletion(-)
>>  create mode 100644 t/t3008-ls-files-lazy-init-name-hash.sh
>
> ... this thing fixed by "chmod +x" (otherwise the tests won't start).
>
> Thanks.

Also, https://travis-ci.org/git/git/jobs/217303927 seems to say that
MacOS is not happy with this change.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-03-31 21:18     ` Junio C Hamano
@ 2017-03-31 23:18       ` Junio C Hamano
  2017-04-01  4:12         ` Junio C Hamano
  2017-04-01  4:14         ` [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts Junio C Hamano
  0 siblings, 2 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-03-31 23:18 UTC (permalink / raw)
  To: Jeff Hostetler
  Cc: Git Mailing List, Jeff King, Kevin Willford, Johannes Schindelin,
	Jeff Hostetler

On Fri, Mar 31, 2017 at 2:18 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Will queue with ...
>>
>>>  name-hash.c                             |  4 +++-
>>>  t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
>>>  2 files changed, 22 insertions(+), 1 deletion(-)
>>>  create mode 100644 t/t3008-ls-files-lazy-init-name-hash.sh
>>
>> ... this thing fixed by "chmod +x" (otherwise the tests won't start).
>>
>> Thanks.
>
> Also, https://travis-ci.org/git/git/jobs/217303927 seems to say that
> MacOS is not happy with this change.

Ah, of course. Avoid GNUism to spell HT as "\t" in a sed script.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-03-31 23:18       ` Junio C Hamano
@ 2017-04-01  4:12         ` Junio C Hamano
  2017-04-01 12:12           ` Johannes Schindelin
  2017-04-01  4:14         ` [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts Junio C Hamano
  1 sibling, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2017-04-01  4:12 UTC (permalink / raw)
  To: Jeff Hostetler
  Cc: Git Mailing List, Jeff King, Kevin Willford, Johannes Schindelin,
	Jeff Hostetler

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

> Ah, of course. Avoid GNUism to spell HT as "\t" in a sed script.

Here is what I replaced the original patch with.  Let's see how well
it fares with Travis tonight.

-- >8 --
From: Kevin Willford <kewillf@microsoft.com>
Date: Fri, 31 Mar 2017 17:32:14 +0000
Subject: [PATCH] name-hash: fix buffer overrun

Add check for the end of the entries for the thread partition.
Add test for lazy init name hash with specific directory structure

The lazy init hash name was causing a buffer overflow when the last
entry in the index was multiple folder deep with parent folders that
did not have any files in them.

This adds a test for the boundary condition of the thread partitions
with the folder structure that was triggering the buffer overflow.

The fix was to check if it is the last entry for the thread partition
in the handle_range_dir and not try to use the next entry in the cache.

Signed-off-by: Kevin Willford <kewillf@microsoft.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 name-hash.c                             |  4 +++-
 t/t3008-ls-files-lazy-init-name-hash.sh | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100755 t/t3008-ls-files-lazy-init-name-hash.sh

diff --git a/name-hash.c b/name-hash.c
index cac313c78d..39309efb7f 100644
--- a/name-hash.c
+++ b/name-hash.c
@@ -342,7 +342,9 @@ static int handle_range_dir(
 	 * Scan forward in the index array for index entries having the same
 	 * path prefix (that are also in this directory).
 	 */
-	if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
+	if (k_start + 1 >= k_end)
+		k = k_end;
+	else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
 		k = k_start + 1;
 	else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
 		k = k_end;
diff --git a/t/t3008-ls-files-lazy-init-name-hash.sh b/t/t3008-ls-files-lazy-init-name-hash.sh
new file mode 100755
index 0000000000..971975bff4
--- /dev/null
+++ b/t/t3008-ls-files-lazy-init-name-hash.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+test_description='Test the lazy init name hash with various folder structures'
+
+. ./test-lib.sh
+
+test_expect_success 'no buffer overflow in lazy_init_name_hash' '
+	(
+	    test_seq 2000 | sed "s/^/a_/"
+	    echo b/b/b
+	    test_seq 2000 | sed "s/^/c_/"
+	    test_seq 50 | sed "s/^/d_/" | tr "\n" "/"; echo d
+	) |
+	sed -e "s/^/100644 $EMPTY_BLOB	/" |
+	git update-index --index-info &&
+	test-lazy-init-name-hash -m
+'
+
+test_done
-- 
2.12.2-752-g2215051a9e


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts
  2017-03-31 23:18       ` Junio C Hamano
  2017-04-01  4:12         ` Junio C Hamano
@ 2017-04-01  4:14         ` Junio C Hamano
  2017-04-02  9:58           ` Jakub Narębski
  1 sibling, 1 reply; 11+ messages in thread
From: Junio C Hamano @ 2017-04-01  4:14 UTC (permalink / raw)
  To: git

Just like we did in 0d1d6e50 ("t/t7003: replace \t with literal tab
in sed expression", 2010-08-12), avoid writing "\t" for HT in sed
scripts, which is not portable.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This should hopefully kill the last instances of the same issue
   to make sure people do not copy-and-paste this unportable
   construct.

 contrib/git-resurrect.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/git-resurrect.sh b/contrib/git-resurrect.sh
index c364dda696..3b78ffd079 100755
--- a/contrib/git-resurrect.sh
+++ b/contrib/git-resurrect.sh
@@ -24,13 +24,13 @@ n,dry-run            don't recreate the branch"
 . git-sh-setup
 
 search_reflog () {
-        sed -ne 's~^\([^ ]*\) .*\tcheckout: moving from '"$1"' .*~\1~p' \
+	sed -ne 's~^\([^ ]*\) .*	checkout: moving from '"$1"' .*~\1~p' \
                 < "$GIT_DIR"/logs/HEAD
 }
 
 search_reflog_merges () {
 	git rev-parse $(
-		sed -ne 's~^[^ ]* \([^ ]*\) .*\tmerge '"$1"':.*~\1^2~p' \
+		sed -ne 's~^[^ ]* \([^ ]*\) .*	merge '"$1"':.*~\1^2~p' \
 			< "$GIT_DIR"/logs/HEAD
 	)
 }
-- 
2.12.2-752-g2215051a9e


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-04-01  4:12         ` Junio C Hamano
@ 2017-04-01 12:12           ` Johannes Schindelin
  2017-04-01 18:16             ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Johannes Schindelin @ 2017-04-01 12:12 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Jeff Hostetler, Git Mailing List, Jeff King, Kevin Willford,
	Jeff Hostetler

Hi Junio,

On Fri, 31 Mar 2017, Junio C Hamano wrote:

> Junio C Hamano <gitster@pobox.com> writes:
> 
> > Ah, of course. Avoid GNUism to spell HT as "\t" in a sed script.

Sorry about that, I suggested this snippet to Kevin, it is my fault for
not remembering BSD sed's idiosynchracies.

Ciao,
Johannes

P.S.: enjoy your time off!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] name-hash: fix buffer overrun
  2017-04-01 12:12           ` Johannes Schindelin
@ 2017-04-01 18:16             ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-04-01 18:16 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Jeff Hostetler, Git Mailing List, Jeff King, Kevin Willford,
	Jeff Hostetler

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> On Fri, 31 Mar 2017, Junio C Hamano wrote:
>
>> Junio C Hamano <gitster@pobox.com> writes:
>> 
>> > Ah, of course. Avoid GNUism to spell HT as "\t" in a sed script.
>
> Sorry about that, I suggested this snippet to Kevin, it is my fault for
> not remembering BSD sed's idiosynchracies.

Heh, don't fret about that.  We all make mistakes and that is why we
review on the list so that patches get exposure to more sets of
eyes.  We may be interested to learn from our common mistakes, but
for that purpose, "whose fault is it?" is far less interesting than
"how it came about?", i.e. what made that mistake common and if/how
we can help people avoid it (removing cuttable-and-pastable bad
examples is one way to do so).

Thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts
  2017-04-01  4:14         ` [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts Junio C Hamano
@ 2017-04-02  9:58           ` Jakub Narębski
  2017-04-02 16:46             ` Junio C Hamano
  0 siblings, 1 reply; 11+ messages in thread
From: Jakub Narębski @ 2017-04-02  9:58 UTC (permalink / raw)
  To: Junio C Hamano, git; +Cc: Jeff Hostetler

W dniu 01.04.2017 o 06:14, Junio C Hamano pisze:

> Just like we did in 0d1d6e50 ("t/t7003: replace \t with literal tab
> in sed expression", 2010-08-12), avoid writing "\t" for HT in sed
> scripts, which is not portable.

I guess it is not possible to use HT variable (similar to LF that we
have in t/test-lib.sh, and which is defined by some scripts) set to
literal tab

  HT="	"

wouldn't work there, is it?

Using this variable would make literal tab character more visible.
-- 
Jakub Narębski


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts
  2017-04-02  9:58           ` Jakub Narębski
@ 2017-04-02 16:46             ` Junio C Hamano
  0 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2017-04-02 16:46 UTC (permalink / raw)
  To: Jakub Narębski; +Cc: git, Jeff Hostetler

Jakub Narębski <jnareb@gmail.com> writes:

> W dniu 01.04.2017 o 06:14, Junio C Hamano pisze:
>
>> Just like we did in 0d1d6e50 ("t/t7003: replace \t with literal tab
>> in sed expression", 2010-08-12), avoid writing "\t" for HT in sed
>> scripts, which is not portable.
>
> I guess it is not possible to use HT variable (similar to LF that we
> have in t/test-lib.sh, and which is defined by some scripts) set to
> literal tab
>
>   HT="	"
>
> wouldn't work there, is it?
>
> Using this variable would make literal tab character more visible.

Patches welcome.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-04-02 16:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-31 17:32 [PATCH] name-hash: fix buffer overrun git
2017-03-31 17:32 ` git
2017-03-31 19:35   ` Junio C Hamano
2017-03-31 21:18     ` Junio C Hamano
2017-03-31 23:18       ` Junio C Hamano
2017-04-01  4:12         ` Junio C Hamano
2017-04-01 12:12           ` Johannes Schindelin
2017-04-01 18:16             ` Junio C Hamano
2017-04-01  4:14         ` [PATCH] contrib/git-resurrect.sh: do not write \t for HT in sed scripts Junio C Hamano
2017-04-02  9:58           ` Jakub Narębski
2017-04-02 16:46             ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).