kentpeacock / rpms / openssh

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