jonathancammack / rpms / openssh

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