jonathancammack / rpms / openssh

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