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