kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
b58e57
diff -up openssh-7.4p1/monitor_wrap.c.audit-race openssh-7.4p1/monitor_wrap.c
b58e57
--- openssh-7.4p1/monitor_wrap.c.audit-race	2017-02-09 14:07:56.870994116 +0100
b58e57
+++ openssh-7.4p1/monitor_wrap.c	2017-02-09 14:07:56.874994112 +0100
b58e57
@@ -1107,4 +1107,48 @@ mm_audit_destroy_sensitive_data(const ch
b58e57
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
b58e57
 	buffer_free(&m);
b58e57
 }
b58e57
+
b58e57
+int mm_forward_audit_messages(int fdin)
b58e57
+{
b58e57
+	u_char buf[4];
b58e57
+	u_int blen, msg_len;
b58e57
+	Buffer m;
b58e57
+	int ret = 0;
b58e57
+
b58e57
+	debug3("%s: entering", __func__);
b58e57
+	buffer_init(&m);
b58e57
+	do {
b58e57
+		blen = atomicio(read, fdin, buf, sizeof(buf));
b58e57
+		if (blen == 0) /* closed pipe */
b58e57
+			break;
b58e57
+		if (blen != sizeof(buf)) {
b58e57
+			error("%s: Failed to read the buffer from child", __func__);
b58e57
+			ret = -1;
b58e57
+			break;
b58e57
+		}
b58e57
+
b58e57
+		msg_len = get_u32(buf);
b58e57
+		if (msg_len > 256 * 1024)
b58e57
+			fatal("%s: read: bad msg_len %d", __func__, msg_len);
b58e57
+		buffer_clear(&m);
b58e57
+		buffer_append_space(&m, msg_len);
b58e57
+		if (atomicio(read, fdin, buffer_ptr(&m), msg_len) != msg_len) {
b58e57
+			error("%s: Failed to read the the buffer content from the child", __func__);
b58e57
+			ret = -1;
b58e57
+			break;
b58e57
+		}
b58e57
+		if (atomicio(vwrite, pmonitor->m_recvfd, buf, blen) != blen || 
b58e57
+		    atomicio(vwrite, pmonitor->m_recvfd, buffer_ptr(&m), msg_len) != msg_len) {
b58e57
+			error("%s: Failed to write the message to the monitor", __func__);
b58e57
+			ret = -1;
b58e57
+			break;
b58e57
+		}
b58e57
+	} while (1);
b58e57
+	buffer_free(&m);
b58e57
+	return ret;
b58e57
+}
b58e57
+void mm_set_monitor_pipe(int fd)
b58e57
+{
b58e57
+	pmonitor->m_recvfd = fd;
b58e57
+}
b58e57
 #endif /* SSH_AUDIT_EVENTS */
b58e57
diff -up openssh-7.4p1/monitor_wrap.h.audit-race openssh-7.4p1/monitor_wrap.h
b58e57
--- openssh-7.4p1/monitor_wrap.h.audit-race	2017-02-09 14:07:56.870994116 +0100
b58e57
+++ openssh-7.4p1/monitor_wrap.h	2017-02-09 14:07:56.874994112 +0100
b58e57
@@ -80,6 +80,8 @@ void mm_audit_unsupported_body(int);
b58e57
 void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
b58e57
 void mm_audit_session_key_free_body(int, pid_t, uid_t);
b58e57
 void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
b58e57
+int mm_forward_audit_messages(int);
b58e57
+void mm_set_monitor_pipe(int);
b58e57
 #endif
b58e57
 
b58e57
 struct Session;
b58e57
diff -up openssh-7.4p1/session.c.audit-race openssh-7.4p1/session.c
b58e57
--- openssh-7.4p1/session.c.audit-race	2017-02-09 14:07:56.871994115 +0100
b58e57
+++ openssh-7.4p1/session.c	2017-02-09 14:09:44.710893783 +0100
b58e57
@@ -162,6 +162,10 @@ static Session *sessions = NULL;
b58e57
 login_cap_t *lc;
b58e57
 #endif
b58e57
 
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+int paudit[2];
b58e57
+#endif
b58e57
+
b58e57
 static int is_child = 0;
b58e57
 static int in_chroot = 0;
b58e57
 static int have_dev_log = 1;
b58e57
@@ -289,6 +293,8 @@ xauth_valid_string(const char *s)
b58e57
 	return 1;
b58e57
 }
b58e57
 
b58e57
+void child_destory_sensitive_data();
b58e57
+
b58e57
 #define USE_PIPES 1
b58e57
 /*
b58e57
  * This is called to fork and execute a command when we have no tty.  This
b58e57
@@ -424,6 +430,8 @@ do_exec_no_pty(Session *s, const char *c
b58e57
 		cray_init_job(s->pw); /* set up cray jid and tmpdir */
b58e57
 #endif
b58e57
 
b58e57
+		child_destory_sensitive_data();
b58e57
+
b58e57
 		/* Do processing for the child (exec command etc). */
b58e57
 		do_child(s, command);
b58e57
 		/* NOTREACHED */
b58e57
@@ -547,6 +555,9 @@ do_exec_pty(Session *s, const char *comm
b58e57
 		/* Close the extra descriptor for the pseudo tty. */
b58e57
 		close(ttyfd);
b58e57
 
b58e57
+		/* Do this early, so we will not block large MOTDs */
b58e57
+		child_destory_sensitive_data();
b58e57
+
b58e57
 		/* record login, etc. similar to login(1) */
b58e57
 #ifdef _UNICOS
b58e57
 		cray_init_job(s->pw); /* set up cray jid and tmpdir */
b58e57
@@ -717,6 +728,8 @@ do_exec(Session *s, const char *command)
b58e57
 	}
b58e57
 	if (s->command != NULL && s->ptyfd == -1)
b58e57
 		s->command_handle = PRIVSEP(audit_run_command(s->command));
b58e57
+	if (pipe(paudit) < 0)
b58e57
+		fatal("pipe: %s", strerror(errno));
b58e57
 #endif
b58e57
 	if (s->ttyfd != -1)
b58e57
 		ret = do_exec_pty(s, command);
b58e57
@@ -732,6 +745,20 @@ do_exec(Session *s, const char *command)
b58e57
 	 */
b58e57
 	buffer_clear(&loginmsg);
b58e57
 
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	close(paudit[1]);
b58e57
+	if (use_privsep && ret == 0) {
b58e57
+		/*
b58e57
+		 * Read the audit messages from forked child and send them
b58e57
+		 * back to monitor. We don't want to communicate directly,
b58e57
+		 * because the messages might get mixed up.
b58e57
+		 * Continue after the pipe gets closed (all messages sent).
b58e57
+		 */
b58e57
+		ret = mm_forward_audit_messages(paudit[0]);
b58e57
+	}
b58e57
+	close(paudit[0]);
b58e57
+#endif /* SSH_AUDIT_EVENTS */
b58e57
+
b58e57
 	return ret;
b58e57
 }
b58e57
 
b58e57
@@ -1542,6 +1569,33 @@ child_close_fds(void)
b58e57
 	endpwent();
b58e57
 }
b58e57
 
b58e57
+void
b58e57
+child_destory_sensitive_data()
b58e57
+{
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	int pparent = paudit[1];
b58e57
+	close(paudit[0]);
b58e57
+	/* Hack the monitor pipe to avoid race condition with parent */
b58e57
+	if (use_privsep)
b58e57
+		mm_set_monitor_pipe(pparent);
b58e57
+#endif
b58e57
+
b58e57
+	/* remove hostkey from the child's memory */
b58e57
+	destroy_sensitive_data(use_privsep);
b58e57
+	/*
b58e57
+	 * We can audit this, because we hacked the pipe to direct the
b58e57
+	 * messages over postauth child. But this message requires answer
b58e57
+	 * which we can't do using one-way pipe.
b58e57
+	 */
b58e57
+	packet_destroy_all(0, 1);
b58e57
+
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	/* Notify parent that we are done */
b58e57
+	close(pparent);
b58e57
+#endif
b58e57
+
b58e57
+}
b58e57
+
b58e57
 /*
b58e57
  * Performs common processing for the child, such as setting up the
b58e57
  * environment, closing extra file descriptors, setting the user and group
b58e57
@@ -1558,12 +1612,6 @@ do_child(Session *s, const char *command
b58e57
 	struct passwd *pw = s->pw;
b58e57
 	int r = 0;
b58e57
 
b58e57
-	/* remove hostkey from the child's memory */
b58e57
-	destroy_sensitive_data(1);
b58e57
-	/* Don't audit this - both us and the parent would be talking to the
b58e57
-	   monitor over a single socket, with no synchronization. */
b58e57
-	packet_destroy_all(0, 1);
b58e57
-
b58e57
 	/* Force a password change */
b58e57
 	if (s->authctxt->force_pwchange) {
b58e57
 		do_setusercontext(pw);