Make sure to have file descriptors 0-2 open before doing further setup.

Otherwise strange invocations could trick us into passing the event
descriptor as, for example, stdin to the server.
See PR 46463 for details.
This commit is contained in:
martin 2012-05-31 19:14:56 +00:00
parent 968a666e92
commit c05e3b1f3a
1 changed files with 18 additions and 1 deletions

View File

@ -1,4 +1,4 @@
/* $Id: tmux.c,v 1.1.1.2 2011/08/17 18:40:05 jmmv Exp $ */
/* $Id: tmux.c,v 1.2 2012/05/31 19:14:56 martin Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -213,6 +213,22 @@ shell_exec(const char *shell, const char *shellcmd)
fatal("execl failed");
}
static void
init_std_fds(void)
{
int fd;
/*
* Make sure the standard file descriptors are populated, so we
* don't end up forwarding (for example) the event descriptor
* instead of stdin to the server.
*/
while ((fd = open("/dev/null", O_RDWR)) <= 2)
;
close(fd);
}
int
main(int argc, char **argv)
{
@ -283,6 +299,7 @@ main(int argc, char **argv)
if (shell_cmd != NULL && argc != 0)
usage();
init_std_fds();
log_open_tty(debug_level);
if (!(flags & IDENTIFY_UTF8)) {