From b473b916337a0a95a177e24703ee6ba584d9ae3b Mon Sep 17 00:00:00 2001 From: Brian May Date: Thu, 1 Nov 2018 09:27:50 +1100 Subject: [PATCH] Close stdin, stdout, and stderr when using syslog or forking to daemon (#283) * Close stdin, stdout, and stderr when using syslog or forking to daemon Fixes #139 * Ensure we close devnull after use --- sshuttle/cmdline.py | 2 ++ sshuttle/ssyslog.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sshuttle/cmdline.py b/sshuttle/cmdline.py index d952288..c454956 100644 --- a/sshuttle/cmdline.py +++ b/sshuttle/cmdline.py @@ -59,6 +59,8 @@ def main(): ipport_v6 = "auto" if not opt.disable_ipv6 else None if opt.syslog: ssyslog.start_syslog() + ssyslog.close_stdin() + ssyslog.stdout_to_syslog() ssyslog.stderr_to_syslog() return_code = client.main(ipport_v6, ipport_v4, opt.ssh_cmd, diff --git a/sshuttle/ssyslog.py b/sshuttle/ssyslog.py index 023a4c8..630c00e 100644 --- a/sshuttle/ssyslog.py +++ b/sshuttle/ssyslog.py @@ -8,12 +8,24 @@ _p = None def start_syslog(): global _p - _p = ssubprocess.Popen(['logger', - '-p', 'daemon.notice', - '-t', 'sshuttle'], stdin=ssubprocess.PIPE) + with open(os.devnull, 'w') as devnull: + _p = ssubprocess.Popen( + ['logger', '-p', 'daemon.notice', '-t', 'sshuttle'], + stdin=ssubprocess.PIPE, + stdout=devnull, + stderr=devnull + ) + + +def close_stdin(): + sys.stdin.close() + + +def stdout_to_syslog(): + sys.stdout.flush() + os.dup2(_p.stdin.fileno(), sys.stdout.fileno()) def stderr_to_syslog(): - sys.stdout.flush() sys.stderr.flush() - os.dup2(_p.stdin.fileno(), 2) + os.dup2(_p.stdin.fileno(), sys.stderr.fileno())