<feed xmlns='http://www.w3.org/2005/Atom'>
<title>cgit/cache.c, branch ch/git-2-54</title>
<subtitle>A hyperfast web frontend for git repositories written in C.</subtitle>
<id>https://git.zx2c4.com/cgit/atom/cache.c?h=ch%2Fgit-2-54</id>
<link rel='self' href='https://git.zx2c4.com/cgit/atom/cache.c?h=ch%2Fgit-2-54'/>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/'/>
<updated>2026-04-13T17:04:38Z</updated>
<entry>
<title>cache: truncate lock file before filling</title>
<updated>2026-04-13T17:04:38Z</updated>
<author>
<name>Jason A. Donenfeld</name>
<email>Jason@zx2c4.com</email>
</author>
<published>2026-04-13T17:02:31Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=829eb0711305e8946fa2f4a1c57c43354f35e208'/>
<id>urn:sha1:829eb0711305e8946fa2f4a1c57c43354f35e208</id>
<content type='text'>
lock_slot() opens the lock file with O_RDWR|O_CREAT but without
O_TRUNC. If a previous cgit process was killed between lock_slot() and
unlock_slot() (e.g. by a CGI timeout or OOM), the stale lock file
remains on disk with the old content, and the kernel releases the fcntl
lock.

The next process to claim the same cache slot then opens this stale lock
file, acquires the fcntl lock, writes its key and generated content on
top of the old bytes. If the new response is shorter than what was
previously in the file, trailing bytes from the old response survive
beyond the end of the new content. fstat() in fill_slot() reports the
total file size (including the stale tail), and print_slot() faithfully
sends all of it -- producing a response that is the correct page
followed by a fragment of whatever previously occupied that lock file.

Fix this by truncating the lock file after acquiring the lock and before
writing the new key.

Signed-off-by: Jason A. Donenfeld &lt;Jason@zx2c4.com&gt;
</content>
</entry>
<entry>
<title>cache: tolerate short writes in print_slot</title>
<updated>2022-12-19T15:13:58Z</updated>
<author>
<name>Hristo Venev</name>
<email>hristo@venev.name</email>
</author>
<published>2022-05-07T17:07:00Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=852cb3b0e267dd2ddfd2eeef6275435098c606e7'/>
<id>urn:sha1:852cb3b0e267dd2ddfd2eeef6275435098c606e7</id>
<content type='text'>
sendfile() can return after a short read/write, so we may need to call
it more than once. As suggested in the manual page, we fall back to
read/write if sendfile fails with EINVAL or ENOSYS.

On the read/write path, use write_in_full which deals with short writes.

Signed-off-by: Hristo Venev &lt;hristo@venev.name&gt;
Signed-off-by: Jason A. Donenfeld &lt;Jason@zx2c4.com&gt;
</content>
</entry>
<entry>
<title>git: update to v2.30.0</title>
<updated>2020-12-29T11:29:42Z</updated>
<author>
<name>Christian Hesse</name>
<email>mail@eworm.de</email>
</author>
<published>2020-12-28T22:27:13Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=cef27b670a66c9840bb6120260864e4b3a701dc2'/>
<id>urn:sha1:cef27b670a66c9840bb6120260864e4b3a701dc2</id>
<content type='text'>
Update to git version v2.30.0, this requires changes for these
upstream commits:

* 88894aaeeae92e8cb41143cc2e045f50289dc790
  blame: simplify 'setup_scoreboard' interface

* 1fbfdf556f2abc708183caca53ae4e2881b46ae2
  banned.h: mark non-reentrant gmtime, etc as banned

Signed-off-by: Christian Hesse &lt;mail@eworm.de&gt;
</content>
</entry>
<entry>
<title>cache: close race window when unlocking slots</title>
<updated>2018-06-27T16:13:03Z</updated>
<author>
<name>John Keeping</name>
<email>john@keeping.me.uk</email>
</author>
<published>2018-06-20T05:29:14Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=b31e99887b17f513289fb11227b2484504e85b6c'/>
<id>urn:sha1:b31e99887b17f513289fb11227b2484504e85b6c</id>
<content type='text'>
We use POSIX advisory record locks to control access to cache slots, but
these have an unhelpful behaviour in that they are released when any
file descriptor referencing the file is closed by this process.

Mostly this is okay, since we know we won't be opening the lock file
anywhere else, but there is one place that it does matter: when we
restore stdout we dup2() over a file descriptor referring to the file,
thus closing that descriptor.

Since we restore stdout before unlocking the slot, this creates a window
during which the slot content can be overwritten.  The fix is reasonably
straightforward: simply restore stdout after unlocking the slot, but the
diff is a bit bigger because this requires us to move the temporary
stdout FD into struct cache_slot.

Signed-off-by: John Keeping &lt;john@keeping.me.uk&gt;
Reviewed-by: Christian Hesse &lt;mail@eworm.de&gt;
</content>
</entry>
<entry>
<title>global: spelling fixes</title>
<updated>2017-10-15T16:44:55Z</updated>
<author>
<name>Ville Skyttä</name>
<email>ville.skytta@iki.fi</email>
</author>
<published>2017-10-14T19:05:51Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=67d0f870506e3bc3703ae3cb2cb00e19691ce967'/>
<id>urn:sha1:67d0f870506e3bc3703ae3cb2cb00e19691ce967</id>
<content type='text'>
Signed-off-by: Ville Skyttä &lt;ville.skytta@iki.fi&gt;
</content>
</entry>
<entry>
<title>cache: flush stdio before restoring FDs</title>
<updated>2017-10-03T18:19:34Z</updated>
<author>
<name>John Keeping</name>
<email>john@keeping.me.uk</email>
</author>
<published>2017-04-24T18:38:34Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=3b485cc5422f800d142c7023295e82c0a1c10b19'/>
<id>urn:sha1:3b485cc5422f800d142c7023295e82c0a1c10b19</id>
<content type='text'>
As described in commit 2efb59e (ui-patch: Flush stdout after outputting
data, 2014-06-11), we need to ensure that stdout is flushed before
restoring the file descriptor when writing to the cache.  It turns out
that it's not just ui-patch that is affected by this but also raw diff
which writes to stdout internally.

Let's avoid risking more places doing this by ensuring that stdout is
flushed after writing in fill_slot().

Signed-off-by: John Keeping &lt;john@keeping.me.uk&gt;
</content>
</entry>
<entry>
<title>cache: don't check for match with no key</title>
<updated>2016-01-17T16:05:39Z</updated>
<author>
<name>John Keeping</name>
<email>john@keeping.me.uk</email>
</author>
<published>2016-01-16T11:03:07Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=33bc949a1e927e14479568518bd92e70998e25f8'/>
<id>urn:sha1:33bc949a1e927e14479568518bd92e70998e25f8</id>
<content type='text'>
We call open_slot() from cache_ls() without a key since we simply want
to read the path out of the header.  Should the file happen to contain
an empty key then we end up calling memcmp() with NULL and a non-zero
length.  Fix this by assigning slot-&gt;match only if a key is set, which
is always will be in the code paths where we use slot-&gt;match.

Coverity-id: 13807
Signed-off-by: John Keeping &lt;john@keeping.me.uk&gt;
</content>
</entry>
<entry>
<title>cache: use size_t for string lengths</title>
<updated>2016-01-17T16:05:19Z</updated>
<author>
<name>John Keeping</name>
<email>john@keeping.me.uk</email>
</author>
<published>2016-01-16T11:03:06Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=3fbfced7401cfcbb8006a9a6ce4add6b37a41a55'/>
<id>urn:sha1:3fbfced7401cfcbb8006a9a6ce4add6b37a41a55</id>
<content type='text'>
Avoid integer truncation on 64-bit systems.

Coverity-id: 13864
Signed-off-by: John Keeping &lt;john@keeping.me.uk&gt;
</content>
</entry>
<entry>
<title>cache: fix resource leak: close file handle before return</title>
<updated>2015-10-10T19:41:04Z</updated>
<author>
<name>Christian Hesse</name>
<email>mail@eworm.de</email>
</author>
<published>2015-10-10T14:56:28Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=76dc7a3371e487fdc9de7b3b4c991fe370598f0e'/>
<id>urn:sha1:76dc7a3371e487fdc9de7b3b4c991fe370598f0e</id>
<content type='text'>
Coverity-id: 13910
Signed-off-by: Christian Hesse &lt;mail@eworm.de&gt;
</content>
</entry>
<entry>
<title>cache.c: fix header order</title>
<updated>2015-08-13T13:37:42Z</updated>
<author>
<name>John Keeping</name>
<email>john@keeping.me.uk</email>
</author>
<published>2015-08-13T11:14:17Z</published>
<link rel='alternate' type='text/html' href='https://git.zx2c4.com/cgit/commit/?id=43620cf6aa62decaf319d00c28297e3b87a4da78'/>
<id>urn:sha1:43620cf6aa62decaf319d00c28297e3b87a4da78</id>
<content type='text'>
git-compat-util.h may define values that affect how system headers are
interpreted, so move sys/sendfile.h after cgit.h (which includes
git-compat-util.h).

Signed-off-by: John Keeping &lt;john@keeping.me.uk&gt;
</content>
</entry>
</feed>
