kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
943807
diff -up openssh-8.6p1/log.c.log-in-chroot openssh-8.6p1/log.c
943807
--- openssh-8.6p1/log.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/log.c	2021-05-06 11:32:25.179006811 +0200
943807
@@ -194,6 +194,11 @@ void
943807
 log_init(const char *av0, LogLevel level, SyslogFacility facility,
943807
     int on_stderr)
943807
 {
943807
+	log_init_handler(av0, level, facility, on_stderr, 1);
943807
+}
943807
+
943807
+void
943807
+log_init_handler(const char *av0, LogLevel level, SyslogFacility facility, int on_stderr, int reset_handler) {
943807
 #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT)
943807
 	struct syslog_data sdata = SYSLOG_DATA_INIT;
943807
 #endif
943807
@@ -206,8 +211,10 @@ log_init(const char *av0, LogLevel level
943807
 		exit(1);
943807
 	}
943807
 
943807
-	log_handler = NULL;
943807
-	log_handler_ctx = NULL;
943807
+	if (reset_handler) {
943807
+		log_handler = NULL;
943807
+		log_handler_ctx = NULL;
943807
+	}
943807
 
943807
 	log_on_stderr = on_stderr;
943807
 	if (on_stderr)
943807
diff -up openssh-8.6p1/log.h.log-in-chroot openssh-8.6p1/log.h
943807
--- openssh-8.6p1/log.h.log-in-chroot	2021-05-06 11:32:25.179006811 +0200
943807
+++ openssh-8.6p1/log.h	2021-05-06 11:34:22.349925757 +0200
943807
@@ -52,6 +52,7 @@ typedef enum {
943807
 typedef void (log_handler_fn)(LogLevel, int, const char *, void *);
943807
 
943807
 void     log_init(const char *, LogLevel, SyslogFacility, int);
943807
+void     log_init_handler(const char *, LogLevel, SyslogFacility, int, int);
943807
 LogLevel log_level_get(void);
943807
 int      log_change_level(LogLevel);
943807
 int      log_is_on_stderr(void);
943807
diff -up openssh-8.6p1/monitor.c.log-in-chroot openssh-8.6p1/monitor.c
943807
--- openssh-8.6p1/monitor.c.log-in-chroot	2021-05-06 11:32:25.153006607 +0200
943807
+++ openssh-8.6p1/monitor.c	2021-05-06 11:33:37.671575348 +0200
943807
@@ -297,6 +297,8 @@ monitor_child_preauth(struct ssh *ssh, s
943807
 		close(pmonitor->m_log_sendfd);
943807
 	pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
943807
 
943807
+	pmonitor->m_state = "preauth";
943807
+
943807
 	authctxt = (Authctxt *)ssh->authctxt;
943807
 	memset(authctxt, 0, sizeof(*authctxt));
943807
 	ssh->authctxt = authctxt;
943807
@@ -408,6 +410,8 @@ monitor_child_postauth(struct ssh *ssh,
943807
 	close(pmonitor->m_recvfd);
943807
 	pmonitor->m_recvfd = -1;
943807
 
943807
+	pmonitor->m_state = "postauth";
943807
+
943807
 	monitor_set_child_handler(pmonitor->m_pid);
943807
 	ssh_signal(SIGHUP, &monitor_child_handler);
943807
 	ssh_signal(SIGTERM, &monitor_child_handler);
943807
@@ -480,7 +484,7 @@ monitor_read_log(struct monitor *pmonito
943807
 	/* Log it */
943807
 	if (log_level_name(level) == NULL)
943807
 		fatal_f("invalid log level %u (corrupted message?)", level);
943807
-	sshlogdirect(level, forced, "%s [preauth]", msg);
943807
+	sshlogdirect(level, forced, "%s [%s]", msg, pmonitor->m_state);
943807
 
943807
 	sshbuf_free(logmsg);
943807
 	free(msg);
943807
@@ -1868,13 +1872,28 @@ monitor_init(void)
943807
 	mon = xcalloc(1, sizeof(*mon));
943807
 	monitor_openfds(mon, 1);
943807
 
943807
+	mon->m_state = "";
943807
+
943807
 	return mon;
943807
 }
943807
 
943807
 void
943807
-monitor_reinit(struct monitor *mon)
943807
+monitor_reinit(struct monitor *mon, const char *chroot_dir)
943807
 {
943807
-	monitor_openfds(mon, 0);
943807
+	struct stat dev_log_stat;
943807
+	char *dev_log_path;
943807
+	int do_logfds = 0;
943807
+
943807
+	if (chroot_dir != NULL) {
943807
+		xasprintf(&dev_log_path, "%s/dev/log", chroot_dir);
943807
+
943807
+		if (stat(dev_log_path, &dev_log_stat) != 0) {
943807
+			debug_f("/dev/log doesn't exist in %s chroot - will try to log via monitor using [postauth] suffix", chroot_dir);
943807
+			do_logfds = 1;
943807
+		}
943807
+		free(dev_log_path);
943807
+	}
943807
+	monitor_openfds(mon, do_logfds);
943807
 }
943807
 
943807
 #ifdef GSSAPI
943807
diff -up openssh-8.6p1/monitor.h.log-in-chroot openssh-8.6p1/monitor.h
943807
--- openssh-8.6p1/monitor.h.log-in-chroot	2021-05-06 11:32:25.153006607 +0200
943807
+++ openssh-8.6p1/monitor.h	2021-05-06 11:32:25.180006819 +0200
943807
@@ -80,10 +80,11 @@ struct monitor {
943807
 	int			 m_log_sendfd;
943807
 	struct kex		**m_pkex;
943807
 	pid_t			 m_pid;
943807
+	char		*m_state;
943807
 };
943807
 
943807
 struct monitor *monitor_init(void);
943807
-void monitor_reinit(struct monitor *);
943807
+void monitor_reinit(struct monitor *, const char *);
943807
 
943807
 struct Authctxt;
943807
 void monitor_child_preauth(struct ssh *, struct monitor *);
943807
diff -up openssh-8.6p1/session.c.log-in-chroot openssh-8.6p1/session.c
943807
--- openssh-8.6p1/session.c.log-in-chroot	2021-05-06 11:32:25.166006709 +0200
943807
+++ openssh-8.6p1/session.c	2021-05-06 11:32:25.181006827 +0200
943807
@@ -160,6 +160,7 @@ login_cap_t *lc;
943807
 
943807
 static int is_child = 0;
943807
 static int in_chroot = 0;
943807
+static int have_dev_log = 1;
943807
 
943807
 /* File containing userauth info, if ExposeAuthInfo set */
943807
 static char *auth_info_file = NULL;
943807
@@ -661,6 +662,7 @@ do_exec(struct ssh *ssh, Session *s, con
943807
 	int ret;
943807
 	const char *forced = NULL, *tty = NULL;
943807
 	char session_type[1024];
943807
+	struct stat dev_log_stat;
943807
 
943807
 	if (options.adm_forced_command) {
943807
 		original_command = command;
943807
@@ -720,6 +722,10 @@ do_exec(struct ssh *ssh, Session *s, con
943807
 			tty += 5;
943807
 	}
943807
 
943807
+	if (lstat("/dev/log", &dev_log_stat) != 0) {
943807
+		have_dev_log = 0;
943807
+	}
943807
+
943807
 	verbose("Starting session: %s%s%s for %s from %.200s port %d id %d",
943807
 	    session_type,
943807
 	    tty == NULL ? "" : " on ",
943807
@@ -1524,14 +1530,6 @@ child_close_fds(struct ssh *ssh)
943807
 
943807
 	/* Stop directing logs to a high-numbered fd before we close it */
943807
 	log_redirect_stderr_to(NULL);
943807
-
943807
-	/*
943807
-	 * Close any extra open file descriptors so that we don't have them
943807
-	 * hanging around in clients.  Note that we want to do this after
943807
-	 * initgroups, because at least on Solaris 2.3 it leaves file
943807
-	 * descriptors open.
943807
-	 */
943807
-	closefrom(STDERR_FILENO + 1);
943807
 }
943807
 
943807
 /*
943807
@@ -1665,8 +1663,6 @@ do_child(struct ssh *ssh, Session *s, co
943807
 			exit(1);
943807
 	}
943807
 
943807
-	closefrom(STDERR_FILENO + 1);
943807
-
943807
 	do_rc_files(ssh, s, shell);
943807
 
943807
 	/* restore SIGPIPE for child */
943807
@@ -1691,9 +1687,17 @@ do_child(struct ssh *ssh, Session *s, co
943807
 		argv[i] = NULL;
943807
 		optind = optreset = 1;
943807
 		__progname = argv[0];
943807
-		exit(sftp_server_main(i, argv, s->pw));
943807
+		exit(sftp_server_main(i, argv, s->pw, have_dev_log));
943807
 	}
943807
 
943807
+	/*
943807
+	 * Close any extra open file descriptors so that we don't have them
943807
+	 * hanging around in clients.  Note that we want to do this after
943807
+	 * initgroups, because at least on Solaris 2.3 it leaves file
943807
+	 * descriptors open.
943807
+	 */
943807
+	closefrom(STDERR_FILENO + 1);
943807
+
943807
 	fflush(NULL);
943807
 
943807
 	/* Get the last component of the shell name. */
943807
diff -up openssh-8.6p1/sftp.h.log-in-chroot openssh-8.6p1/sftp.h
943807
--- openssh-8.6p1/sftp.h.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/sftp.h	2021-05-06 11:32:25.181006827 +0200
943807
@@ -97,5 +97,5 @@
943807
 
943807
 struct passwd;
943807
 
943807
-int	sftp_server_main(int, char **, struct passwd *);
943807
+int	sftp_server_main(int, char **, struct passwd *, int);
943807
 void	sftp_server_cleanup_exit(int) __attribute__((noreturn));
943807
diff -up openssh-8.6p1/sftp-server.c.log-in-chroot openssh-8.6p1/sftp-server.c
943807
--- openssh-8.6p1/sftp-server.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/sftp-server.c	2021-05-06 11:32:25.181006827 +0200
943807
@@ -1644,7 +1644,7 @@ sftp_server_usage(void)
943807
 }
943807
 
943807
 int
943807
-sftp_server_main(int argc, char **argv, struct passwd *user_pw)
943807
+sftp_server_main(int argc, char **argv, struct passwd *user_pw, int reset_handler)
943807
 {
943807
 	fd_set *rset, *wset;
943807
 	int i, r, in, out, max, ch, skipargs = 0, log_stderr = 0;
943807
@@ -1657,7 +1657,7 @@ sftp_server_main(int argc, char **argv,
943807
 	extern char *__progname;
943807
 
943807
 	__progname = ssh_get_progname(argv[0]);
943807
-	log_init(__progname, log_level, log_facility, log_stderr);
943807
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
943807
 
943807
 	pw = pwcopy(user_pw);
943807
 
943807
@@ -1730,7 +1730,7 @@ sftp_server_main(int argc, char **argv,
943807
 		}
943807
 	}
943807
 
943807
-	log_init(__progname, log_level, log_facility, log_stderr);
943807
+	log_init_handler(__progname, log_level, log_facility, log_stderr, reset_handler);
943807
 
943807
 	/*
943807
 	 * On platforms where we can, avoid making /proc/self/{mem,maps}
943807
diff -up openssh-8.6p1/sftp-server-main.c.log-in-chroot openssh-8.6p1/sftp-server-main.c
943807
--- openssh-8.6p1/sftp-server-main.c.log-in-chroot	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/sftp-server-main.c	2021-05-06 11:32:25.181006827 +0200
943807
@@ -50,5 +50,5 @@ main(int argc, char **argv)
943807
 		return 1;
943807
 	}
943807
 
943807
-	return (sftp_server_main(argc, argv, user_pw));
943807
+	return (sftp_server_main(argc, argv, user_pw, 0));
943807
 }
943807
diff -up openssh-8.6p1/sshd.c.log-in-chroot openssh-8.6p1/sshd.c
943807
--- openssh-8.6p1/sshd.c.log-in-chroot	2021-05-06 11:32:25.177006795 +0200
943807
+++ openssh-8.6p1/sshd.c	2021-05-06 11:32:25.182006834 +0200
943807
@@ -559,7 +559,7 @@ privsep_postauth(struct ssh *ssh, Authct
943807
 	}
943807
 
943807
 	/* New socket pair */
943807
-	monitor_reinit(pmonitor);
943807
+	monitor_reinit(pmonitor, options.chroot_directory);
943807
 
943807
 	pmonitor->m_pid = fork();
943807
 	if (pmonitor->m_pid == -1)
943807
@@ -578,6 +578,11 @@ privsep_postauth(struct ssh *ssh, Authct
943807
 
943807
 	close(pmonitor->m_sendfd);
943807
 	pmonitor->m_sendfd = -1;
943807
+	close(pmonitor->m_log_recvfd);
943807
+	pmonitor->m_log_recvfd = -1;
943807
+
943807
+	if (pmonitor->m_log_sendfd != -1)
943807
+		set_log_handler(mm_log_handler, pmonitor);
943807
 
943807
 	/* Demote the private keys to public keys. */
943807
 	demote_sensitive_data();