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