Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit cb29dfd

Browse files
committed
Merge branch 'tr/protect-low-3-fds'
When "git" is spawned in such a way that any of the low 3 file descriptors is closed, our first open() may yield file descriptor 2, and writing error message to it would screw things up in a big way. * tr/protect-low-3-fds: git: ensure 0/1/2 are open in main() daemon/shell: refactor redirection of 0/1/2 from /dev/null
2 parents 5701c3d + a11c396 commit cb29dfd

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ extern int path_inside_repo(const char *prefix, const char *path);
425425
extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
426426
extern int init_db(const char *template_dir, unsigned int flags);
427427

428+
extern void sanitize_stdfds(void);
429+
428430
#define alloc_nr(x) (((x)+16)*3/2)
429431

430432
/*

daemon.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,18 +1047,6 @@ static int service_loop(struct socketlist *socklist)
10471047
}
10481048
}
10491049

1050-
/* if any standard file descriptor is missing open it to /dev/null */
1051-
static void sanitize_stdfds(void)
1052-
{
1053-
int fd = open("/dev/null", O_RDWR, 0);
1054-
while (fd != -1 && fd < 2)
1055-
fd = dup(fd);
1056-
if (fd == -1)
1057-
die_errno("open /dev/null or dup failed");
1058-
if (fd > 2)
1059-
close(fd);
1060-
}
1061-
10621050
#ifdef NO_POSIX_GOODIES
10631051

10641052
struct credentials;

git.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,13 @@ int main(int argc, char **av)
525525
if (!cmd)
526526
cmd = "git-help";
527527

528+
/*
529+
* Always open file descriptors 0/1/2 to avoid clobbering files
530+
* in die(). It also avoids messing up when the pipes are dup'ed
531+
* onto stdin/stdout/stderr in the child processes we spawn.
532+
*/
533+
sanitize_stdfds();
534+
528535
git_setup_gettext();
529536

530537
/*

setup.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,3 +908,15 @@ const char *resolve_gitdir(const char *suspect)
908908
return suspect;
909909
return read_gitfile(suspect);
910910
}
911+
912+
/* if any standard file descriptor is missing open it to /dev/null */
913+
void sanitize_stdfds(void)
914+
{
915+
int fd = open("/dev/null", O_RDWR, 0);
916+
while (fd != -1 && fd < 2)
917+
fd = dup(fd);
918+
if (fd == -1)
919+
die_errno("open /dev/null or dup failed");
920+
if (fd > 2)
921+
close(fd);
922+
}

shell.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ int main(int argc, char **argv)
147147
char *prog;
148148
const char **user_argv;
149149
struct commands *cmd;
150-
int devnull_fd;
151150
int count;
152151

153152
git_setup_gettext();
@@ -156,15 +155,10 @@ int main(int argc, char **argv)
156155

157156
/*
158157
* Always open file descriptors 0/1/2 to avoid clobbering files
159-
* in die(). It also avoids not messing up when the pipes are
160-
* dup'ed onto stdin/stdout/stderr in the child processes we spawn.
158+
* in die(). It also avoids messing up when the pipes are dup'ed
159+
* onto stdin/stdout/stderr in the child processes we spawn.
161160
*/
162-
devnull_fd = open("/dev/null", O_RDWR);
163-
while (devnull_fd >= 0 && devnull_fd <= 2)
164-
devnull_fd = dup(devnull_fd);
165-
if (devnull_fd == -1)
166-
die_errno("opening /dev/null failed");
167-
close (devnull_fd);
161+
sanitize_stdfds();
168162

169163
/*
170164
* Special hack to pretend to be a CVS server

0 commit comments

Comments
 (0)