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