<feed xmlns='http://www.w3.org/2005/Atom'>
<title>cgit, branch lh/grep</title>
<subtitle>A hyperfast web frontend for git repositories written in C.</subtitle>
<id>https://git.zx2c4.com/cgit/atom/?h=lh%2Fgrep</id>
<link rel='self' href='https://git.zx2c4.com/cgit/atom/?h=lh%2Fgrep'/>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/'/>
<updated>2012-10-16T21:57:00Z</updated>
<entry>
<title>Grep the source, Luke</title>
<updated>2012-10-16T21:57:00Z</updated>
<author>
<name>Lars Hjemli</name>
<email>hjemli@gmail.com</email>
</author>
<published>2011-06-22T08:49:57Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=c028b604ef666d64a8aa9ebdfcf69675cd5d13fb'/>
<id>urn:sha1:c028b604ef666d64a8aa9ebdfcf69675cd5d13fb</id>
<content type='text'>
This is a PoC for grepping the blobs in the current tree.

Signed-off-by: Lars Hjemli &lt;larsh@hjemli.net&gt;
</content>
</entry>
<entry>
<title>ui-repolist: Rename section-sort to repository-sort.</title>
<updated>2012-10-16T21:32:53Z</updated>
<author>
<name>Jason A. Donenfeld</name>
<email>Jason@zx2c4.com</email>
</author>
<published>2012-10-16T21:32:40Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=fdfb6a6d802d6b91061296eda9848d5819760d01'/>
<id>urn:sha1:fdfb6a6d802d6b91061296eda9848d5819760d01</id>
<content type='text'>
Signed-off-by: Jason A. Donenfeld &lt;Jason@zx2c4.com&gt;
</content>
</entry>
<entry>
<title>repo_config: do not let globals override repo settings</title>
<updated>2012-10-16T21:00:50Z</updated>
<author>
<name>Tobias Bieniek</name>
<email>Tobias.Bieniek@gmx.de</email>
</author>
<published>2012-04-18T10:32:13Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=8f40be229cf8ac7ecbf5e03e11098bad8e82e7e5'/>
<id>urn:sha1:8f40be229cf8ac7ecbf5e03e11098bad8e82e7e5</id>
<content type='text'>
This makes it possible to activate the enable_commit_graph,
enable_log_filecount, and enable_log_linecount for individual
repositories, even if the global setting is "0" (default).

The commit that introduced the broken behavior was e189344, and the
commit message of that makes it clear that this wasn't the intended
behavior.

Signed-off-by: Jason A. Donenfeld &lt;Jason@zx2c4.com&gt;
</content>
</entry>
<entry>
<title>README: times, they are a-changin</title>
<updated>2012-10-09T11:21:30Z</updated>
<author>
<name>Jason A. Donenfeld</name>
<email>Jason@zx2c4.com</email>
</author>
<published>2012-10-09T11:21:30Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=a9a6a205608c83125841f8f0b2aca07476cd2113'/>
<id>urn:sha1:a9a6a205608c83125841f8f0b2aca07476cd2113</id>
<content type='text'>
</content>
</entry>
<entry>
<title>syntax-highlight: when the file has no extension, assume text</title>
<updated>2012-10-09T11:19:12Z</updated>
<author>
<name>Ferry Huberts</name>
<email>ferry.huberts@pelagic.nl</email>
</author>
<published>2012-10-09T11:10:48Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=d14faf4424ae06ced696c15f205c85fe55b8e6e7'/>
<id>urn:sha1:d14faf4424ae06ced696c15f205c85fe55b8e6e7</id>
<content type='text'>
There are 2 situations:
1- empty extension: assuming text is better than highlight
   producing no output because of a missing argument.
2- no extension at all: assuming text is better than setting
   the extension to the filename, which is what now happens.

Signed-off-by: Ferry Huberts &lt;ferry.huberts@pelagic.nl&gt;
</content>
</entry>
<entry>
<title>ui-repolist: do not use agefile if it's date could not be parsed</title>
<updated>2012-10-09T11:14:21Z</updated>
<author>
<name>Ferry Huberts</name>
<email>ferry.huberts@pelagic.nl</email>
</author>
<published>2012-10-09T11:09:58Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=e52a5226b83dc5232adb7d9076cae3ad4b123df4'/>
<id>urn:sha1:e52a5226b83dc5232adb7d9076cae3ad4b123df4</id>
<content type='text'>
Signed-off-by: Ferry Huberts &lt;ferry.huberts@pelagic.nl&gt;
</content>
</entry>
<entry>
<title>Revert "filters/syntax-highlighting.sh: work around highlight --force bug"</title>
<updated>2012-10-09T11:12:09Z</updated>
<author>
<name>Ferry Huberts</name>
<email>ferry.huberts@pelagic.nl</email>
</author>
<published>2012-10-09T11:08:30Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=2ad9063cb53b55b1456c7d2ed08e6fb99d32ab78'/>
<id>urn:sha1:2ad9063cb53b55b1456c7d2ed08e6fb99d32ab78</id>
<content type='text'>
This reverts commit f50be7fda0a7ab57009169dd5905fcbab8eb5166.

An update with the latest highlight landed in EPEL. This new version
doesn't have the --force bug, so the workaround can now be removed.

Signed-off-by: Ferry Huberts &lt;ferry.huberts@pelagic.nl&gt;
</content>
</entry>
<entry>
<title>Makefile: add tag target to generate ctags</title>
<updated>2012-10-08T21:40:23Z</updated>
<author>
<name>Jamie Couture</name>
<email>jamie.couture@gmail.com</email>
</author>
<published>2012-10-08T17:12:17Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=6419c9b57c33a8a26d2775f01629f899afb61026'/>
<id>urn:sha1:6419c9b57c33a8a26d2775f01629f899afb61026</id>
<content type='text'>
Signed-off-by: Jamie Couture &lt;jamie.couture@gmail.com&gt;
</content>
</entry>
<entry>
<title>ui-repolist: Bold the currently viewed page.</title>
<updated>2012-10-08T21:29:29Z</updated>
<author>
<name>Jamie Couture</name>
<email>jamie.couture@gmail.com</email>
</author>
<published>2012-10-08T16:49:34Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=4675b91211fe3119a0def539521ac759b3fb57d0'/>
<id>urn:sha1:4675b91211fe3119a0def539521ac759b3fb57d0</id>
<content type='text'>
Signed-off-by: Jamie Couture &lt;jamie.couture@gmail.com&gt;
</content>
</entry>
<entry>
<title>do not write outside heap buffer</title>
<updated>2012-10-02T02:03:47Z</updated>
<author>
<name>Jim Meyering</name>
<email>meyering@redhat.com</email>
</author>
<published>2012-04-23T20:06:35Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=61d4147ea2d58c9d129a084be13ccec6ae18b4d5'/>
<id>urn:sha1:61d4147ea2d58c9d129a084be13ccec6ae18b4d5</id>
<content type='text'>
* parsing.c (substr): Handle tail &lt; head.

This started when I noticed some cgit segfaults on savannah.gnu.org.
Finding the offending URL/commit and then constructing a stand-alone
reproducer were far more time-consuming than writing the actual patch.

The problem arises with a commit like this, in which the user name
part of the "Author" field is empty:

    $ git log -1
    commit 6f3f41d73393278f3ede68a2cb1e7a2a23fa3421
    Author: &lt;T at h.or&gt;
    Date:   Mon Apr 23 22:29:16 2012 +0200

Here's what happens:

(this is due to buf=malloc(0); strncpy (buf, head, -1);
 where "head" may point to plenty of attacker-specified non-NUL bytes,
 so we can overwrite a zero-length heap buffer with arbitrary data)

 Invalid write of size 1
    at 0x4A09361: strncpy (mc_replace_strmem.c:463)
    by 0x408977: substr (parsing.c:61)
    by 0x4089EF: parse_user (parsing.c:73)
    by 0x408D10: cgit_parse_commit (parsing.c:153)
    by 0x40A540: cgit_mk_refinfo (shared.c:171)
    by 0x40A581: cgit_refs_cb (shared.c:181)
    by 0x43DEB3: do_for_each_ref (refs.c:690)
    by 0x41075E: cgit_print_branches (ui-refs.c:191)
    by 0x416EF2: cgit_print_summary (ui-summary.c:56)
    by 0x40780A: summary_fn (cmd.c:120)
    by 0x40667A: process_request (cgit.c:544)
    by 0x404078: cache_process (cache.c:322)
  Address 0x4c718d0 is 0 bytes after a block of size 0 alloc'd
    at 0x4A0884D: malloc (vg_replace_malloc.c:263)
    by 0x455C85: xmalloc (wrapper.c:35)
    by 0x40894C: substr (parsing.c:60)
    by 0x4089EF: parse_user (parsing.c:73)
    by 0x408D10: cgit_parse_commit (parsing.c:153)
    by 0x40A540: cgit_mk_refinfo (shared.c:171)
    by 0x40A581: cgit_refs_cb (shared.c:181)
    by 0x43DEB3: do_for_each_ref (refs.c:690)
    by 0x41075E: cgit_print_branches (ui-refs.c:191)
    by 0x416EF2: cgit_print_summary (ui-summary.c:56)
    by 0x40780A: summary_fn (cmd.c:120)
    by 0x40667A: process_request (cgit.c:544)

 Invalid write of size 1
    at 0x4A09400: strncpy (mc_replace_strmem.c:463)
    by 0x408977: substr (parsing.c:61)
    by 0x4089EF: parse_user (parsing.c:73)
    by 0x408D10: cgit_parse_commit (parsing.c:153)
    by 0x40A540: cgit_mk_refinfo (shared.c:171)
    by 0x40A581: cgit_refs_cb (shared.c:181)
    by 0x43DEB3: do_for_each_ref (refs.c:690)
    by 0x41075E: cgit_print_branches (ui-refs.c:191)
    by 0x416EF2: cgit_print_summary (ui-summary.c:56)
    by 0x40780A: summary_fn (cmd.c:120)
    by 0x40667A: process_request (cgit.c:544)
    by 0x404078: cache_process (cache.c:322)
  Address 0x4c7192b is not stack'd, malloc'd or (recently) free'd

 Invalid write of size 1
    at 0x4A0940E: strncpy (mc_replace_strmem.c:463)
    by 0x408977: substr (parsing.c:61)
    by 0x4089EF: parse_user (parsing.c:73)
    by 0x408D10: cgit_parse_commit (parsing.c:153)
    by 0x40A540: cgit_mk_refinfo (shared.c:171)
    by 0x40A581: cgit_refs_cb (shared.c:181)
    by 0x43DEB3: do_for_each_ref (refs.c:690)
    by 0x41075E: cgit_print_branches (ui-refs.c:191)
    by 0x416EF2: cgit_print_summary (ui-summary.c:56)
    by 0x40780A: summary_fn (cmd.c:120)
    by 0x40667A: process_request (cgit.c:544)
    by 0x404078: cache_process (cache.c:322)
  Address 0x4c7192d is not stack'd, malloc'd or (recently) free'd

 Process terminating with default action of signal 11 (SIGSEGV)
  Access not within mapped region at address 0x502F000
    at 0x4A09400: strncpy (mc_replace_strmem.c:463)
    by 0x408977: substr (parsing.c:61)
    by 0x4089EF: parse_user (parsing.c:73)
    by 0x408D10: cgit_parse_commit (parsing.c:153)
    by 0x40A540: cgit_mk_refinfo (shared.c:171)
    by 0x40A581: cgit_refs_cb (shared.c:181)
    by 0x43DEB3: do_for_each_ref (refs.c:690)
    by 0x41075E: cgit_print_branches (ui-refs.c:191)
    by 0x416EF2: cgit_print_summary (ui-summary.c:56)
    by 0x40780A: summary_fn (cmd.c:120)
    by 0x40667A: process_request (cgit.c:544)
    by 0x404078: cache_process (cache.c:322)

This happens when tail - head == -1 here:
(parsing.c)

  char *substr(const char *head, const char *tail)
  {
          char *buf;

          buf = xmalloc(tail - head + 1);
          strncpy(buf, head, tail - head);
          buf[tail - head] = '\0';
          return buf;
  }

  char *parse_user(char *t, char **name, char **email, unsigned long *date)
  {
          char *p = t;
          int mode = 1;

          while (p &amp;&amp; *p) {
                  if (mode == 1 &amp;&amp; *p == '&lt;') {
                          *name = substr(t, p - 1);
                          t = p;
                          mode++;
                  } else if (mode == 1 &amp;&amp; *p == '\n') {

The fix is to handle the case of (tail &lt; head) before calling xmalloc,
thus avoiding passing an invalid value to xmalloc.

And here's the reproducer:
It was tricky to reproduce, because git prohibits use of an empty "name"
in a commit ID.  To construct the offending commit, I had to resort to
using "git hash-object".

git init -q foo &amp;&amp;
( cd foo &amp;&amp;
  echo a &gt; j &amp;&amp; git add . &amp;&amp; git ci -q --author='au &lt;T at h.or&gt;' -m. . &amp;&amp;
  h=$(git cat-file commit HEAD|sed 's/au //' \
    |git hash-object -t commit -w --stdin) &amp;&amp;
  git co -q -b test $h &amp;&amp;
  git br -q -D master &amp;&amp;
  git br -q -m test master)
git clone -q --bare foo foo.git

cat &lt;&lt;EOF &gt; in
repo.url=foo.git
repo.path=foo.git
EOF
CGIT_CONFIG=in QUERY_STRING=url=foo.git valgrind ./cgit

The valgrind output is what you see above.

AFAICS, this is not exploitable thanks (ironically) to the use of strncpy.
Since that -1 translates to SIZE_MAX and this is strncpy, not only does it
copy whatever is in "head" (up to first NUL), but it also writes
SIZE_MAX - strlen(head) NUL bytes into the destination buffer, and that
latter is guaranteed to evoke a segfault.  Since cgit is single-threaded,
AFAICS, there is no way that the buffer clobbering can be turned into
an exploit.
</content>
</entry>
</feed>
