jonathancammack / rpms / openssh

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