vishalmishra434 / rpms / openssh

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