vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/monitor_wrap.c.audit-race openssh-7.4p1/monitor_wrap.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/monitor_wrap.c.audit-race	2016-12-23 16:35:52.694685771 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/monitor_wrap.c	2016-12-23 16:35:52.697685772 +0100
Jakub Jelen 6cf9b8
@@ -1107,4 +1107,48 @@ mm_audit_destroy_sensitive_data(const ch
Jakub Jelen 44fc97
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Jakub Jelen 44fc97
 	buffer_free(&m);
Jakub Jelen 44fc97
 }
Jakub Jelen 44fc97
+
Jakub Jelen 44fc97
+int mm_forward_audit_messages(int fdin)
Jakub Jelen 44fc97
+{
Jakub Jelen 44fc97
+	u_char buf[4];
Jakub Jelen 44fc97
+	u_int blen, msg_len;
Jakub Jelen 44fc97
+	Buffer m;
Jakub Jelen 44fc97
+	int ret = 0;
Jakub Jelen 44fc97
+
Jakub Jelen 44fc97
+	debug3("%s: entering", __func__);
Jakub Jelen 44fc97
+	buffer_init(&m);
Jakub Jelen 44fc97
+	do {
Jakub Jelen 44fc97
+		blen = atomicio(read, fdin, buf, sizeof(buf));
Jakub Jelen 44fc97
+		if (blen == 0) /* closed pipe */
Jakub Jelen 44fc97
+			break;
Jakub Jelen 44fc97
+		if (blen != sizeof(buf)) {
Jakub Jelen 44fc97
+			error("%s: Failed to read the buffer from child", __func__);
Jakub Jelen 44fc97
+			ret = -1;
Jakub Jelen 44fc97
+			break;
Jakub Jelen 44fc97
+		}
Jakub Jelen 44fc97
+
Jakub Jelen 44fc97
+		msg_len = get_u32(buf);
Jakub Jelen 44fc97
+		if (msg_len > 256 * 1024)
Jakub Jelen 44fc97
+			fatal("%s: read: bad msg_len %d", __func__, msg_len);
Jakub Jelen 44fc97
+		buffer_clear(&m);
Jakub Jelen 44fc97
+		buffer_append_space(&m, msg_len);
Jakub Jelen 44fc97
+		if (atomicio(read, fdin, buffer_ptr(&m), msg_len) != msg_len) {
Jakub Jelen 38869a
+			error("%s: Failed to read the the buffer content from the child", __func__);
Jakub Jelen 44fc97
+			ret = -1;
Jakub Jelen 44fc97
+			break;
Jakub Jelen 44fc97
+		}
Jakub Jelen 44fc97
+		if (atomicio(vwrite, pmonitor->m_recvfd, buf, blen) != blen || 
Jakub Jelen 44fc97
+		    atomicio(vwrite, pmonitor->m_recvfd, buffer_ptr(&m), msg_len) != msg_len) {
Jakub Jelen 38869a
+			error("%s: Failed to write the message to the monitor", __func__);
Jakub Jelen 44fc97
+			ret = -1;
Jakub Jelen 44fc97
+			break;
Jakub Jelen 44fc97
+		}
Jakub Jelen 44fc97
+	} while (1);
Jakub Jelen 44fc97
+	buffer_free(&m);
Jakub Jelen 44fc97
+	return ret;
Jakub Jelen 44fc97
+}
Jakub Jelen 44fc97
+void mm_set_monitor_pipe(int fd)
Jakub Jelen 44fc97
+{
Jakub Jelen 44fc97
+	pmonitor->m_recvfd = fd;
Jakub Jelen 44fc97
+}
Jakub Jelen 44fc97
 #endif /* SSH_AUDIT_EVENTS */
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/monitor_wrap.h.audit-race openssh-7.4p1/monitor_wrap.h
Jakub Jelen 6cf9b8
--- openssh-7.4p1/monitor_wrap.h.audit-race	2016-12-23 16:35:52.694685771 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/monitor_wrap.h	2016-12-23 16:35:52.698685772 +0100
Jakub Jelen 6cf9b8
@@ -83,6 +83,8 @@ void mm_audit_unsupported_body(int);
Jakub Jelen 44fc97
 void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
Jakub Jelen 44fc97
 void mm_audit_session_key_free_body(int, pid_t, uid_t);
Jakub Jelen 44fc97
 void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jakub Jelen 44fc97
+int mm_forward_audit_messages(int);
Jakub Jelen 44fc97
+void mm_set_monitor_pipe(int);
Jakub Jelen 44fc97
 #endif
Jakub Jelen 44fc97
 
Jakub Jelen 44fc97
 struct Session;
Jakub Jelen 6cf9b8
diff -up openssh-7.4p1/session.c.audit-race openssh-7.4p1/session.c
Jakub Jelen 6cf9b8
--- openssh-7.4p1/session.c.audit-race	2016-12-23 16:35:52.695685771 +0100
Jakub Jelen 6cf9b8
+++ openssh-7.4p1/session.c	2016-12-23 16:37:26.339730596 +0100
Jakub Jelen 6cf9b8
@@ -162,6 +162,10 @@ static Session *sessions = NULL;
Jakub Jelen 44fc97
 login_cap_t *lc;
Jakub Jelen 44fc97
 #endif
Jakub Jelen 44fc97
 
Jakub Jelen 44fc97
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 44fc97
+int paudit[2];
Jakub Jelen 44fc97
+#endif
Jakub Jelen 44fc97
+
Jakub Jelen 44fc97
 static int is_child = 0;
Jakub Jelen 13073f
 static int in_chroot = 0;
Jakub Jelen 44fc97
 static int have_dev_log = 1;
Jakub Jelen 6cf9b8
@@ -289,6 +293,8 @@ xauth_valid_string(const char *s)
Jakub Jelen 6cf9b8
 	return 1;
Jakub Jelen 38869a
 }
Jakub Jelen 38869a
 
Jakub Jelen 38869a
+void child_destory_sensitive_data();
Jakub Jelen 38869a
+
Jakub Jelen 38869a
 #define USE_PIPES 1
Jakub Jelen 38869a
 /*
Jakub Jelen 38869a
  * This is called to fork and execute a command when we have no tty.  This
Jakub Jelen 6cf9b8
@@ -424,6 +430,8 @@ do_exec_no_pty(Session *s, const char *c
Jakub Jelen 3cd489
 		close(err[0]);
Jakub Jelen 38869a
 #endif
Jakub Jelen 38869a
 
Jakub Jelen 38869a
+		child_destory_sensitive_data();
Jakub Jelen 38869a
+
Jakub Jelen 38869a
 		/* Do processing for the child (exec command etc). */
Jakub Jelen 5b55d0
 		do_child(ssh, s, command);
Jakub Jelen 38869a
 		/* NOTREACHED */
Jakub Jelen 6cf9b8
@@ -547,6 +555,9 @@ do_exec_pty(Session *s, const char *comm
Jakub Jelen 38869a
 		/* Close the extra descriptor for the pseudo tty. */
Jakub Jelen 38869a
 		close(ttyfd);
Jakub Jelen 38869a
 
Jakub Jelen 38869a
+		/* Do this early, so we will not block large MOTDs */
Jakub Jelen 38869a
+		child_destory_sensitive_data();
Jakub Jelen 38869a
+
Jakub Jelen 38869a
 		/* record login, etc. similar to login(1) */
Jakub Jelen 3cd489
 #ifndef HAVE_OSF_SIA
Jakub Jelen 3cd489
 		do_login(ssh, s, command);
Jakub Jelen 6cf9b8
@@ -717,6 +728,8 @@ do_exec(Session *s, const char *command)
Jakub Jelen 44fc97
 	}
Jakub Jelen 44fc97
 	if (s->command != NULL && s->ptyfd == -1)
Jakub Jelen 44fc97
 		s->command_handle = PRIVSEP(audit_run_command(s->command));
Jakub Jelen 44fc97
+	if (pipe(paudit) < 0)
Jakub Jelen 44fc97
+		fatal("pipe: %s", strerror(errno));
Jakub Jelen 44fc97
 #endif
Jakub Jelen 44fc97
 	if (s->ttyfd != -1)
Jakub Jelen 5b55d0
 		ret = do_exec_pty(ssh, s, command);
Jakub Jelen 6cf9b8
@@ -732,6 +745,20 @@ do_exec(Session *s, const char *command)
Jakub Jelen 44fc97
 	 */
Jakub Jelen 44fc97
 	buffer_clear(&loginmsg);
Jakub Jelen 44fc97
 
Jakub Jelen 44fc97
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 44fc97
+	close(paudit[1]);
Jakub Jelen 44fc97
+	if (use_privsep && ret == 0) {
Jakub Jelen 44fc97
+		/*
Jakub Jelen 44fc97
+		 * Read the audit messages from forked child and send them
Jakub Jelen 44fc97
+		 * back to monitor. We don't want to communicate directly,
Jakub Jelen 44fc97
+		 * because the messages might get mixed up.
Jakub Jelen 44fc97
+		 * Continue after the pipe gets closed (all messages sent).
Jakub Jelen 44fc97
+		 */
Jakub Jelen 44fc97
+		ret = mm_forward_audit_messages(paudit[0]);
Jakub Jelen 44fc97
+	}
Jakub Jelen 44fc97
+	close(paudit[0]);
Jakub Jelen 44fc97
+#endif /* SSH_AUDIT_EVENTS */
Jakub Jelen 44fc97
+
Jakub Jelen 44fc97
 	return ret;
Jakub Jelen 44fc97
 }
Jakub Jelen 44fc97
 
Jakub Jelen 5b55d0
@@ -1538,6 +1565,34 @@ child_close_fds(void)
Jakub Jelen 38869a
 	endpwent();
Jakub Jelen 38869a
 }
Jakub Jelen 44fc97
 
Jakub Jelen 38869a
+void
Jakub Jelen 38869a
+child_destory_sensitive_data()
Jakub Jelen 38869a
+{
Jakub Jelen 44fc97
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 44fc97
+	int pparent = paudit[1];
Jakub Jelen 44fc97
+	close(paudit[0]);
Jakub Jelen 44fc97
+	/* Hack the monitor pipe to avoid race condition with parent */
Jakub Jelen 44fc97
+	if (use_privsep)
Jakub Jelen 44fc97
+		mm_set_monitor_pipe(pparent);
Jakub Jelen 44fc97
+#endif
Jakub Jelen 44fc97
+
Jakub Jelen 38869a
+	/* remove hostkey from the child's memory */
Jakub Jelen 44fc97
+	destroy_sensitive_data(use_privsep);
Jakub Jelen 44fc97
+	/*
Jakub Jelen 38869a
+	 * We can audit this, because we hacked the pipe to direct the
Jakub Jelen 44fc97
+	 * messages over postauth child. But this message requires answer
Jakub Jelen 44fc97
+	 * which we can't do using one-way pipe.
Jakub Jelen 44fc97
+	 */
Jakub Jelen 38869a
+	packet_destroy_all(0, 1);
Jakub Jelen 5b55d0
+	/* XXX this will clean the rest but should not audit anymore */
Jakub Jelen 5b55d0
+	/* packet_clear_keys(); */
Jakub Jelen 38869a
+
Jakub Jelen 44fc97
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 44fc97
+	/* Notify parent that we are done */
Jakub Jelen 44fc97
+	close(pparent);
Jakub Jelen 44fc97
+#endif
Jakub Jelen 38869a
+}
Jakub Jelen 38869a
+
Jakub Jelen 38869a
 /*
Jakub Jelen 38869a
  * Performs common processing for the child, such as setting up the
Jakub Jelen 38869a
  * environment, closing extra file descriptors, setting the user and group
Jakub Jelen 5b55d0
@@ -1554,13 +1608,6 @@ do_child(Session *s, const char *command
Jakub Jelen 38869a
 	struct passwd *pw = s->pw;
Jakub Jelen 38869a
 	int r = 0;
Jakub Jelen 38869a
 
Jakub Jelen 38869a
-	/* remove hostkey from the child's memory */
Jakub Jelen 38869a
-	destroy_sensitive_data(1);
Jakub Jelen 5b55d0
-	packet_clear_keys();
Jakub Jelen 38869a
-	/* Don't audit this - both us and the parent would be talking to the
Jakub Jelen 38869a
-	   monitor over a single socket, with no synchronization. */
Jakub Jelen 38869a
-	packet_destroy_all(0, 1);
Jakub Jelen 38869a
-
Jakub Jelen 44fc97
 	/* Force a password change */
Jakub Jelen 44fc97
 	if (s->authctxt->force_pwchange) {
Jakub Jelen 44fc97
 		do_setusercontext(pw);