vishalmishra434 / rpms / openssh

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