Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

It's safer to close stdin/out/err by replacing their fd with /dev/null.

Otherwise the next opened fds are 0/1/2, and any code writing to
stdout/stderr might break things by writing to an unintentional fd.

Signed-off-by: Timo Sirainen <tss@iki.fi>
  • Loading branch information...
commit 9cade23616351c02cf001690901979f6aa8d5736 1 parent b245ed0
@sirainen sirainen authored keithw committed
Showing with 15 additions and 3 deletions.
  1. +15 −3 src/frontend/mosh-server.cc
View
18 src/frontend/mosh-server.cc
@@ -37,6 +37,7 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
@@ -391,9 +392,20 @@ int run_server( const char *desired_ip, const char *desired_port,
/* close file descriptors */
if ( !verbose ) {
/* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */
- fclose( stdin );
- fclose( stdout );
- fclose( stderr );
+ int nullfd;
+
+ nullfd = open( "/dev/null", O_RDWR );
+ if ( nullfd == -1 ) {
+ perror( "dup2" );
+ exit( 1 );
+ }
+
+ if ( dup2 ( nullfd, STDIN_FILENO ) < 0 ||
+ dup2 ( nullfd, STDOUT_FILENO ) < 0 ||
+ dup2 ( nullfd, STDERR_FILENO ) < 0 ) {
+ perror( "dup2" );
+ exit( 1 );
+ }
}
char utmp_entry[ 64 ] = { 0 };
Please sign in to comment.
Something went wrong with that request. Please try again.