jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

Blame SOURCES/openssh-7.1p2-audit-race-condition.patch

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