changeset: 6870:b319ec2dc93a user: Kevin McCarthy <ke...@8t8.us> date: Sun Nov 20 16:19:17 2016 -0800 link: http://dev.mutt.org/hg/mutt/rev/b319ec2dc93a
Don't close stderr when opening a tunnel. (closes #3726) Instead of closing stderr, redirect it to /dev/null in tunnel_socket_open(). Otherwise a program can accidentally open a file into handle 2 and then unknowingly use that when trying to print to stderr. Thanks to lotheac for the original patch, which I just modified slightly. changeset: 6871:c770d2fa615b user: Kevin McCarthy <ke...@8t8.us> date: Sun Nov 20 16:19:18 2016 -0800 link: http://dev.mutt.org/hg/mutt/rev/c770d2fa615b Minor resource and error logic cleanup in tunnel_socket_open() Free the conn->sockdata on failure. conn->fd is not set until the bottom, and before it is set, conn->conn_close() will not be called. Close the pin pipe if the pout pipe fails. Call mutt_perror first on a fork failure. Calling after the close() may cause errno to be changed on a close failure. diffs (61 lines): diff -r 695243ba6374 -r c770d2fa615b mutt_tunnel.c --- a/mutt_tunnel.c Sat Nov 19 19:35:07 2016 -0800 +++ b/mutt_tunnel.c Sun Nov 20 16:19:18 2016 -0800 @@ -65,6 +65,7 @@ int pid; int rc; int pin[2], pout[2]; + int devnull; tunnel = (TUNNEL_DATA*) safe_malloc (sizeof (TUNNEL_DATA)); conn->sockdata = tunnel; @@ -74,11 +75,15 @@ if ((rc = pipe (pin)) == -1) { mutt_perror ("pipe"); + FREE (&conn->sockdata); return -1; } if ((rc = pipe (pout)) == -1) { mutt_perror ("pipe"); + close (pin[0]); + close (pin[1]); + FREE (&conn->sockdata); return -1; } @@ -86,13 +91,17 @@ if ((pid = fork ()) == 0) { mutt_unblock_signals_system (0); - if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2 (pin[1], STDOUT_FILENO) < 0) + devnull = open ("/dev/null", O_RDWR); + if (devnull < 0 || + dup2 (pout[0], STDIN_FILENO) < 0 || + dup2 (pin[1], STDOUT_FILENO) < 0 || + dup2 (devnull, STDERR_FILENO) < 0) _exit (127); close (pin[0]); close (pin[1]); close (pout[0]); close (pout[1]); - close (STDERR_FILENO); + close (devnull); /* Don't let the subprocess think it can use the controlling tty */ setsid (); @@ -104,11 +113,12 @@ if (pid == -1) { + mutt_perror ("fork"); close (pin[0]); close (pin[1]); close (pout[0]); close (pout[1]); - mutt_perror ("fork"); + FREE (&conn->sockdata); return -1; } if (close (pin[1]) < 0 || close (pout[0]) < 0)