Skip to content

Commit 0cfeed2

Browse files
bonzinigitster
authored andcommitted
make git-shell paranoid about closed stdin/stdout/stderr
It is in general unsafe to start a program 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(). Fixing this in git was considered to be overkill, so this patch works around it only for git-shell. The fix is simply to open all the "low" descriptors to /dev/null in main. Signed-off-by: Paolo Bonzini <[email protected]> Acked-by: Stephen R. van den Berg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 29f2815 commit 0cfeed2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

shell.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ int main(int argc, char **argv)
5656
{
5757
char *prog;
5858
struct commands *cmd;
59+
int devnull_fd;
60+
61+
/*
62+
* Always open file descriptors 0/1/2 to avoid clobbering files
63+
* in die(). It also avoids not messing up when the pipes are
64+
* dup'ed onto stdin/stdout/stderr in the child processes we spawn.
65+
*/
66+
devnull_fd = open("/dev/null", O_RDWR);
67+
while (devnull_fd >= 0 && devnull_fd <= 2)
68+
devnull_fd = dup(devnull_fd);
69+
if (devnull_fd == -1)
70+
die("opening /dev/null failed (%s)", strerror(errno));
71+
close (devnull_fd);
5972

6073
/*
6174
* Special hack to pretend to be a CVS server

0 commit comments

Comments
 (0)