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

[PATCH] be paranoid about closed stdin/stdout/stderr

From
Paolo Bonzini <bonzini@gnu.org>
Date
Aug 26, 2008, 06:48 UTC
Message-ID
<E1KXsL9-0004ef-Co@fencepost.gnu.org>
In-Reply-To
<7vbpzgb94q.fsf@gitster.siamese.dyndns.org>

It is in general unsafe to start git with one or more of file descriptors 0/1/2 closed. Karl Chen for example noticed that stat_command does this in order to rename a pipe file descriptor to 0:

    dup2(from, 0);
    close(from);
... but if stdin was closed (for example) from == 0, so that
    dup2(0, 0);
    close(0);

just ends up closing the pipe. Another extremely rare but nasty problem would occur if an "important" file ends up in file descriptor 2, and is corrupted by a call to die().

This patch fixes these problems by opening all of the "low" descriptors to /dev/null in main.

Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
---
 git.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/git.c b/git.c
index 89e4645..be227b2 100644
--- a/git.c
+++ b/git.c
@@ -420,6 +420,19 @@ int main(int argc, const char **argv)
 	const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
 	char *slash = (char *)cmd + strlen(cmd);
 	int done_alias = 0;
+	int devnull_fd;
+
+	/*
+	 * Always open file descriptors 0/1/2 to avoid clobbering files
+	 * in die().  It also avoids not messing up when the pipes are
+	 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
+	 */
+	devnull_fd = open("/dev/null", O_RDWR);
+	while (devnull_fd >= 0 && devnull_fd <= 2)
+		devnull_fd = dup(devnull_fd);
+	if (devnull_fd == -1)
+		die("opening /dev/null failed (%s)", strerror(errno));
+	close (devnull_fd);
 
 	/*
 	 * Take the basename of argv[0] as the command
-- 
1.5.5
Previous: Paolo BonziniNext: Johannes Sixt
Message 12 of 37 in “Fix start_command() pipe bug when stdin is closed.”
  1. Fix start_command() pipe bug when stdin is closed.Karl Chen, Aug 25, 2008
  2. Johannes SixtAug 25, 2008
  3. Paolo BonziniAug 25, 2008
  4. fix start_command() bug when stdin is closedPaolo Bonzini, Aug 25, 2008
  5. Johannes SixtAug 25, 2008
  6. fix start_command() bug when stdin is closedPaolo Bonzini, Aug 25, 2008
  7. Karl ChenAug 25, 2008
  8. Junio C HamanoAug 26, 2008
  9. Junio C HamanoAug 26, 2008
  10. Johannes SixtAug 26, 2008
  11. Paolo BonziniAug 26, 2008
  12. be paranoid about closed stdin/stdout/stderrPaolo Bonzini, Aug 26, 2008
  13. Johannes SixtAug 26, 2008
  14. Stephen R. van den BergAug 26, 2008
  15. Avery PennarunAug 27, 2008
  16. Stephen R. van den BergAug 27, 2008
  17. Paolo BonziniAug 27, 2008
  18. make git-shell paranoid about closed stdin/stdout/stderrPaolo Bonzini, Aug 27, 2008
  19. Stephen R. van den BergAug 27, 2008
  20. Junio C HamanoAug 27, 2008
  21. Paolo BonziniAug 28, 2008
  22. Stephen R. van den BergAug 28, 2008
  23. Avery PennarunAug 27, 2008
  24. Nick AndrewAug 28, 2008
  25. Stephen R. van den BergAug 28, 2008
  26. Junio C HamanoAug 26, 2008
  27. Paolo BonziniAug 26, 2008
  28. Junio C HamanoAug 26, 2008
  29. Junio C HamanoAug 26, 2008
  30. Stephen R. van den BergAug 26, 2008
  31. Karl ChenAug 27, 2008
  32. Paolo BonziniAug 27, 2008
  33. Stephen R. van den BergAug 27, 2008
  34. Johannes SixtAug 27, 2008
  35. Paolo BonziniAug 27, 2008
  36. Nick AndrewAug 27, 2008
  37. Karl ChenAug 25, 2008

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.