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

[PATCH 1/7] config: avoid "write_in_full(fd, buf, len) < len" pattern

From
Jeff King <peff@peff.net>
Date
Sep 13, 2017, 17:11 UTC
Message-ID
<20170913171104.yu7ags4aq2zdwz6r@sigill.intra.peff.net>
In-Reply-To
<20170913170807.cyx7rrpoyhaauvol@sigill.intra.peff.net>

The return type of write_in_full() is a signed ssize_t, because we may return "-1" on failure (even if we succeeded in writing some bytes). But "len" itself may be an unsigned type (the function takes a size_t, but of course we may have something else in the calling function). So while it seems like:

  if (write_in_full(fd, buf, len) < len)
	die_errno("write error");

would notice an error, it won't if "len" is unsigned. The compiler sees a signed/unsigned comparison and promotes the signed value, resulting in (size_t)-1, the highest possible size_t (or again, whatever type the caller has). This cannot possibly be smaller than "len", and so the conditional can never trigger.

I scoured the code base for cases of this, but it turns out that these two in git_config_set_multivar_in_file_gently() are the only ones. This case is actually quite interesting: we don't have a size_t, but rather use the subtraction of two pointers. Which you might think would be a signed ptrdiff_t, but clearly both gcc and clang treat it as unsigned (possibly because the conditional just above guarantees that the result is greater than zero).

We can avoid the whole question by just checking for a negative return value directly, as write_in_full() will never return any value except -1 or the full count.

There's no addition to the test suite here, since you need to convince write() to fail in order to see the problem. The simplest reproduction recipe I came up with is to trigger ENOSPC (this only works on Linux, obviously):

  # make a limited-size filesystem
  dd if=/dev/zero of=small.disk bs=1M count=1
  mke2fs small.disk
  mkdir mnt
  sudo mount -o loop small.disk mnt
  cd mnt
  sudo chown $USER:$USER .
  # make a config file with some content
  git config --file=config one.key value
  git config --file=config two.key value
  # now fill up the disk
  dd if=/dev/zero of=fill
  # and try to delete a key, which requires copying the rest
  # of the file to config.lock, and will fail on write()
  git config --file=config --unset two.key

That final command should (and does after this patch) produce an error message due to the failed write, and leave the file intact. With the current code it silently ignores the failure and renames config.lock into place, leaving you with a totally empty config file!

Reported-by: demerphq <demerphq@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
---
 config.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/config.c b/config.c
index d0d8ce823a..eee4ac0355 100644
--- a/config.c
+++ b/config.c
@@ -2608,8 +2608,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
 			/* write the first part of the config */
 			if (copy_end > copy_begin) {
 				if (write_in_full(fd, contents + copy_begin,
-						  copy_end - copy_begin) <
-				    copy_end - copy_begin)
+						  copy_end - copy_begin) < 0)
 					goto write_err_out;
 				if (new_line &&
 				    write_str_in_full(fd, "\n") != 1)
@@ -2631,8 +2630,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
 		/* write the rest of the config */
 		if (copy_begin < contents_sz)
 			if (write_in_full(fd, contents + copy_begin,
-					  contents_sz - copy_begin) <
-			    contents_sz - copy_begin)
+					  contents_sz - copy_begin) < 0)
 				goto write_err_out;
 
 		munmap(contents, contents_sz);
-- 
2.14.1.874.ge7b2e05270
Previous: Jeff KingNext: Jonathan Nieder
Message 11 of 46 in “Bug: git branch --unset-upstream command can nuke config when disk is full.”
  1. demerphqSep 13, 2017
  2. Jeff KingSep 13, 2017
  3. demerphqSep 13, 2017
  4. Jeff KingSep 13, 2017
  5. demerphqSep 13, 2017
  6. Jeff KingSep 13, 2017
  7. demerphqSep 13, 2017
  8. Jeff KingSep 13, 2017
  9. demerphqSep 13, 2017
  10. 0/7 config.c may fail to notice some write() failuresJeff King, Sep 13, 2017
  11. 1/7 config: avoid "write_in_full(fd, buf, len) < len" patternJeff King, Sep 13, 2017
  12. Jonathan NiederSep 13, 2017
  13. Jeff KingSep 13, 2017
  14. Jonathan NiederSep 13, 2017
  15. Jeff KingSep 13, 2017
  16. config: avoid "write_in_full(fd, buf, len) < len" patternJeff King, Sep 13, 2017
  17. Jonathan NiederSep 13, 2017
  18. Jeff KingSep 13, 2017
  19. Jonathan NiederSep 13, 2017
  20. Jonathan NiederSep 13, 2017
  21. Ramsay JonesSep 13, 2017
  22. Ramsay JonesSep 13, 2017
  23. Jeff KingSep 15, 2017
  24. Ramsay JonesSep 15, 2017
  25. Junio C HamanoSep 13, 2017
  26. 2/7 get-tar-commit-id: check write_in_full() return against 0Jeff King, Sep 13, 2017
  27. Jonathan NiederSep 13, 2017
  28. Jeff KingSep 13, 2017
  29. Jeff KingSep 13, 2017
  30. Jonathan NiederSep 13, 2017
  31. Jeff KingSep 15, 2017
  32. 3/7 avoid "write_in_full(fd, buf, len) != len" patternJeff King, Sep 13, 2017
  33. Jonathan NiederSep 13, 2017
  34. Jeff KingSep 15, 2017
  35. 4/7 convert less-trivial versions of "write_in_full() != len"Jeff King, Sep 13, 2017
  36. Jonathan NiederSep 13, 2017
  37. 5/7 pkt-line: check write_in_full() errors against "< 0"Jeff King, Sep 13, 2017
  38. Jonathan NiederSep 13, 2017
  39. 6/7 notes-merge: use ssize_t for write_in_full() return valueJeff King, Sep 13, 2017
  40. Jonathan NiederSep 13, 2017
  41. Jeff KingSep 15, 2017
  42. 7/7 config: flip return value of store_write_*()Jeff King, Sep 13, 2017
  43. Jonathan NiederSep 13, 2017
  44. Jeff KingSep 15, 2017
  45. 8/7 read_pack_header: handle signed/unsigned comparison in read resultJeff King, Sep 13, 2017
  46. Jonathan NiederSep 13, 2017

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.