44fc972
diff --git a/monitor_wrap.c b/monitor_wrap.c
44fc972
index 89a1762..fe98e08 100644
44fc972
--- a/monitor_wrap.c
44fc972
+++ b/monitor_wrap.c
44fc972
@@ -1251,4 +1251,48 @@ mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
44fc972
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
44fc972
 	buffer_free(&m);
44fc972
 }
44fc972
+
44fc972
+int mm_forward_audit_messages(int fdin)
44fc972
+{
44fc972
+	u_char buf[4];
44fc972
+	u_int blen, msg_len;
44fc972
+	Buffer m;
44fc972
+	int ret = 0;
44fc972
+
44fc972
+	debug3("%s: entering", __func__);
44fc972
+	buffer_init(&m);
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);
44fc972
+		buffer_clear(&m);
44fc972
+		buffer_append_space(&m, msg_len);
44fc972
+		if (atomicio(read, fdin, buffer_ptr(&m), msg_len) != msg_len) {
44fc972
+			error("%s: Failed to read the the buffer conent from the child", __func__);
44fc972
+			ret = -1;
44fc972
+			break;
44fc972
+		}
44fc972
+		if (atomicio(vwrite, pmonitor->m_recvfd, buf, blen) != blen || 
44fc972
+		    atomicio(vwrite, pmonitor->m_recvfd, buffer_ptr(&m), msg_len) != msg_len) {
44fc972
+			error("%s: Failed to write the messag to the monitor", __func__);
44fc972
+			ret = -1;
44fc972
+			break;
44fc972
+		}
44fc972
+	} while (1);
44fc972
+	buffer_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 */
44fc972
diff --git a/monitor_wrap.h b/monitor_wrap.h
44fc972
index e73134e..fbfe395 100644
44fc972
--- a/monitor_wrap.h
44fc972
+++ b/monitor_wrap.h
44fc972
@@ -86,6 +86,8 @@ void mm_audit_unsupported_body(int);
44fc972
 void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
44fc972
 void mm_audit_session_key_free_body(int, pid_t, uid_t);
44fc972
 void mm_audit_destroy_sensitive_data(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;
44fc972
diff --git a/session.c b/session.c
44fc972
index 8949fd1..9afb764 100644
44fc972
--- a/session.c
44fc972
+++ b/session.c
44fc972
@@ -159,6 +159,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;
44fc972
@@ -875,6 +879,8 @@ do_exec(Session *s, const char *command)
44fc972
 	}
44fc972
 	if (s->command != NULL && s->ptyfd == -1)
44fc972
 		s->command_handle = PRIVSEP(audit_run_command(s->command));
44fc972
+	if (pipe(paudit) < 0)
44fc972
+		fatal("pipe: %s", strerror(errno));
44fc972
 #endif
44fc972
 	if (s->ttyfd != -1)
44fc972
 		ret = do_exec_pty(s, command);
44fc972
@@ -890,6 +896,20 @@ do_exec(Session *s, const char *command)
44fc972
 	 */
44fc972
 	buffer_clear(&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
 
44fc972
@@ -1707,12 +1727,28 @@ do_child(Session *s, const char *command)
44fc972
 	struct passwd *pw = s->pw;
44fc972
 	int r = 0;
44fc972
 
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
+
44fc972
 	/* remove hostkey from the child's memory */
44fc972
-	destroy_sensitive_data(1);
44fc972
-	/* Don't audit this - both us and the parent would be talking to the
44fc972
-	   monitor over a single socket, with no synchronization. */
44fc972
+	destroy_sensitive_data(use_privsep);
44fc972
+	/*
44fc972
+	 * We can audit this, because wer 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
+	 */
44fc972
 	packet_destroy_all(0, 1);
44fc972
 
44fc972
+#ifdef SSH_AUDIT_EVENTS
44fc972
+	/* Notify parent that we are done */
44fc972
+	close(pparent);
44fc972
+#endif
44fc972
+
44fc972
 	/* Force a password change */
44fc972
 	if (s->authctxt->force_pwchange) {
44fc972
 		do_setusercontext(pw);