vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
b58e57
diff -up openssh-7.4p1/audit-bsm.c.audit openssh-7.4p1/audit-bsm.c
b58e57
--- openssh-7.4p1/audit-bsm.c.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/audit-bsm.c	2016-12-23 18:54:54.433080419 +0100
b58e57
@@ -373,10 +373,23 @@ audit_connection_from(const char *host,
b58e57
 #endif
b58e57
 }
b58e57
 
b58e57
-void
b58e57
+int
b58e57
 audit_run_command(const char *command)
b58e57
 {
b58e57
 	/* not implemented */
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_end_command(int handle, const char *command)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_count_session_open(void)
b58e57
+{
b58e57
+	/* not necessary */
b58e57
 }
b58e57
 
b58e57
 void
b58e57
@@ -391,6 +404,12 @@ audit_session_close(struct logininfo *li
b58e57
 	/* not implemented */
b58e57
 }
b58e57
 
b58e57
+int
b58e57
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
 void
b58e57
 audit_event(ssh_audit_event_t event)
b58e57
 {
b58e57
@@ -452,4 +471,40 @@ audit_event(ssh_audit_event_t event)
b58e57
 		debug("%s: unhandled event %d", __func__, event);
b58e57
 	}
b58e57
 }
b58e57
+
b58e57
+void
b58e57
+audit_unsupported_body(int what)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_destroy_sensitive_data(const char *fp)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_generate_ephemeral_server_key(const char *fp)
b58e57
+{
b58e57
+	/* not implemented */
b58e57
+}
b58e57
 #endif /* BSM */
b58e57
diff -up openssh-7.4p1/audit.c.audit openssh-7.4p1/audit.c
b58e57
--- openssh-7.4p1/audit.c.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/audit.c	2016-12-23 18:54:54.433080419 +0100
b58e57
@@ -26,6 +26,7 @@
b58e57
 
b58e57
 #include <stdarg.h>
b58e57
 #include <string.h>
b58e57
+#include <unistd.h>
b58e57
 
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
 
b58e57
@@ -34,6 +35,11 @@
b58e57
 #include "key.h"
b58e57
 #include "hostfile.h"
b58e57
 #include "auth.h"
b58e57
+#include "ssh-gss.h"
b58e57
+#include "monitor_wrap.h"
b58e57
+#include "xmalloc.h"
b58e57
+#include "misc.h"
b58e57
+#include "servconf.h"
b58e57
 
b58e57
 /*
b58e57
  * Care must be taken when using this since it WILL NOT be initialized when
b58e57
@@ -41,6 +47,7 @@
b58e57
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
b58e57
  */
b58e57
 extern Authctxt *the_authctxt;
b58e57
+extern ServerOptions options;
b58e57
 
b58e57
 /* Maybe add the audit class to struct Authmethod? */
b58e57
 ssh_audit_event_t
b58e57
@@ -69,13 +76,10 @@ audit_classify_auth(const char *method)
b58e57
 const char *
b58e57
 audit_username(void)
b58e57
 {
b58e57
-	static const char unknownuser[] = "(unknown user)";
b58e57
-	static const char invaliduser[] = "(invalid user)";
b58e57
+	static const char unknownuser[] = "(unknown)";
b58e57
 
b58e57
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
b58e57
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
b58e57
 		return (unknownuser);
b58e57
-	if (!the_authctxt->valid)
b58e57
-		return (invaliduser);
b58e57
 	return (the_authctxt->user);
b58e57
 }
b58e57
 
b58e57
@@ -109,6 +113,40 @@ audit_event_lookup(ssh_audit_event_t ev)
b58e57
 	return(event_lookup[i].name);
b58e57
 }
b58e57
 
b58e57
+void
b58e57
+audit_key(int host_user, int *rv, const Key *key)
b58e57
+{
b58e57
+	char *fp;
b58e57
+	const char *crypto_name;
b58e57
+
b58e57
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
b58e57
+	if (key->type == KEY_RSA1)
b58e57
+		crypto_name = "ssh-rsa1";
b58e57
+	else
b58e57
+		crypto_name = key_ssh_name(key);
b58e57
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
b58e57
+		*rv = 0;
b58e57
+	free(fp);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_unsupported(int what)
b58e57
+{
b58e57
+	PRIVSEP(audit_unsupported_body(what));
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
b58e57
+{
b58e57
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_session_key_free(int ctos)
b58e57
+{
b58e57
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
b58e57
+}
b58e57
+
b58e57
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
b58e57
 /*
b58e57
  * Null implementations of audit functions.
b58e57
@@ -138,6 +176,17 @@ audit_event(ssh_audit_event_t event)
b58e57
 }
b58e57
 
b58e57
 /*
b58e57
+ * Called when a child process has called, or will soon call,
b58e57
+ * audit_session_open.
b58e57
+ */
b58e57
+void
b58e57
+audit_count_session_open(void)
b58e57
+{
b58e57
+	debug("audit count session open euid %d user %s", geteuid(),
b58e57
+	      audit_username());
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
  * Called when a user session is started.  Argument is the tty allocated to
b58e57
  * the session, or NULL if no tty was allocated.
b58e57
  *
b58e57
@@ -172,13 +221,91 @@ audit_session_close(struct logininfo *li
b58e57
 /*
b58e57
  * This will be called when a user runs a non-interactive command.  Note that
b58e57
  * it may be called multiple times for a single connection since SSH2 allows
b58e57
- * multiple sessions within a single connection.
b58e57
+ * multiple sessions within a single connection.  Returns a "handle" for
b58e57
+ * audit_end_command.
b58e57
  */
b58e57
-void
b58e57
+int
b58e57
 audit_run_command(const char *command)
b58e57
 {
b58e57
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
b58e57
 	    audit_username(), command);
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called when the non-interactive command finishes.  Note that
b58e57
+ * it may be called multiple times for a single connection since SSH2 allows
b58e57
+ * multiple sessions within a single connection.  "handle" should come from
b58e57
+ * the corresponding audit_run_command.
b58e57
+ */
b58e57
+void
b58e57
+audit_end_command(int handle, const char *command)
b58e57
+{
b58e57
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
b58e57
+	    audit_username(), command);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
b58e57
+ *
b58e57
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
b58e57
+ */
b58e57
+int
b58e57
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
b58e57
+{
b58e57
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s, result %d",
b58e57
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
b58e57
+		fp, rv);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called when the protocol negotiation fails.
b58e57
+ */
b58e57
+void
b58e57
+audit_unsupported_body(int what)
b58e57
+{
b58e57
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called on succesfull protocol negotiation.
b58e57
+ */
b58e57
+void
b58e57
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
b58e57
+	       uid_t uid)
b58e57
+{
b58e57
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
b58e57
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
b58e57
+	        (unsigned)uid);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called on succesfull session key discard
b58e57
+ */
b58e57
+void
b58e57
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
b58e57
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called on destroy private part of the server key
b58e57
+ */
b58e57
+void
b58e57
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
b58e57
+		geteuid(), fp, (long)pid, (unsigned)uid);
b58e57
+}
b58e57
+
b58e57
+/*
b58e57
+ * This will be called on generation of the ephemeral server key
b58e57
+ */
b58e57
+void
b58e57
+audit_generate_ephemeral_server_key(const char *)
b58e57
+{
b58e57
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
b58e57
 }
b58e57
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
b58e57
 #endif /* SSH_AUDIT_EVENTS */
b58e57
diff -up openssh-7.4p1/audit.h.audit openssh-7.4p1/audit.h
b58e57
--- openssh-7.4p1/audit.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/audit.h	2016-12-23 18:54:54.433080419 +0100
b58e57
@@ -26,6 +26,7 @@
b58e57
 # define _SSH_AUDIT_H
b58e57
 
b58e57
 #include "loginrec.h"
b58e57
+#include "key.h"
b58e57
 
b58e57
 enum ssh_audit_event_type {
b58e57
 	SSH_LOGIN_EXCEED_MAXTRIES,
b58e57
@@ -43,13 +44,33 @@ enum ssh_audit_event_type {
b58e57
 	SSH_CONNECTION_ABANDON,	/* closed without completing auth */
b58e57
 	SSH_AUDIT_UNKNOWN
b58e57
 };
b58e57
+
b58e57
+enum ssh_audit_kex {
b58e57
+	SSH_AUDIT_UNSUPPORTED_CIPHER,
b58e57
+	SSH_AUDIT_UNSUPPORTED_MAC,
b58e57
+	SSH_AUDIT_UNSUPPORTED_COMPRESSION
b58e57
+};
b58e57
 typedef enum ssh_audit_event_type ssh_audit_event_t;
b58e57
 
b58e57
+int	listening_for_clients(void);
b58e57
+
b58e57
 void	audit_connection_from(const char *, int);
b58e57
 void	audit_event(ssh_audit_event_t);
b58e57
+void	audit_count_session_open(void);
b58e57
 void	audit_session_open(struct logininfo *);
b58e57
 void	audit_session_close(struct logininfo *);
b58e57
-void	audit_run_command(const char *);
b58e57
+int	audit_run_command(const char *);
b58e57
+void 	audit_end_command(int, const char *);
b58e57
 ssh_audit_event_t audit_classify_auth(const char *);
b58e57
+int	audit_keyusage(int, const char *, unsigned, char *, int);
b58e57
+void	audit_key(int, int *, const Key *);
b58e57
+void	audit_unsupported(int);
b58e57
+void	audit_kex(int, char *, char *, char *, char *);
b58e57
+void	audit_unsupported_body(int);
b58e57
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
b58e57
+void	audit_session_key_free(int ctos);
b58e57
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
b58e57
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
b58e57
+void	audit_generate_ephemeral_server_key(const char *);
b58e57
 
b58e57
 #endif /* _SSH_AUDIT_H */
b58e57
diff -up openssh-7.4p1/audit-linux.c.audit openssh-7.4p1/audit-linux.c
b58e57
--- openssh-7.4p1/audit-linux.c.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/audit-linux.c	2016-12-23 18:54:54.434080419 +0100
b58e57
@@ -33,25 +33,38 @@
b58e57
 
b58e57
 #include "log.h"
b58e57
 #include "audit.h"
b58e57
+#include "key.h"
b58e57
+#include "hostfile.h"
b58e57
+#include "auth.h"
b58e57
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
b58e57
+#include "servconf.h"
b58e57
 #include "canohost.h"
b58e57
 #include "packet.h"
b58e57
-
b58e57
+#include "cipher.h"
b58e57
+#include "channels.h"
b58e57
+#include "session.h"
b58e57
+
b58e57
+#define AUDIT_LOG_SIZE 256
b58e57
+
b58e57
+extern ServerOptions options;
b58e57
+extern Authctxt *the_authctxt;
b58e57
+extern u_int utmp_len;
b58e57
 const char *audit_username(void);
b58e57
 
b58e57
-int
b58e57
-linux_audit_record_event(int uid, const char *username, const char *hostname,
b58e57
-    const char *ip, const char *ttyn, int success)
b58e57
+static void
b58e57
+linux_audit_user_logxxx(int uid, const char *username, const char *hostname,
b58e57
+    const char *ip, const char *ttyn, int success, int event)
b58e57
 {
b58e57
 	int audit_fd, rc, saved_errno;
b58e57
 
b58e57
 	if ((audit_fd = audit_open()) < 0) {
b58e57
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
b58e57
 		    errno == EAFNOSUPPORT)
b58e57
-			return 1; /* No audit support in kernel */
b58e57
+			return; /* No audit support in kernel */
b58e57
 		else
b58e57
-			return 0; /* Must prevent login */
b58e57
+			goto fatal_report; /* Must prevent login */
b58e57
 	}
b58e57
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
b58e57
+	rc = audit_log_acct_message(audit_fd, event,
b58e57
 	    NULL, "login", username ? username : "(unknown)",
b58e57
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
b58e57
 	saved_errno = errno;
b58e57
@@ -65,9 +78,97 @@ linux_audit_record_event(int uid, const
b58e57
 		rc = 0;
b58e57
 	errno = saved_errno;
b58e57
 
b58e57
-	return rc >= 0;
b58e57
+	if (rc < 0) {
b58e57
+fatal_report:
b58e57
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
b58e57
+	}
b58e57
+}
b58e57
+
b58e57
+static void
b58e57
+linux_audit_user_auth(int uid, const char *username,
b58e57
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
b58e57
+{
b58e57
+	int audit_fd, rc, saved_errno;
b58e57
+	static const char *event_name[] = {
b58e57
+		"maxtries exceeded",
b58e57
+		"root denied",
b58e57
+		"success",
b58e57
+		"none",
b58e57
+		"password",
b58e57
+		"challenge-response",
b58e57
+		"pubkey",
b58e57
+		"hostbased",
b58e57
+		"gssapi",
b58e57
+		"invalid user",
b58e57
+		"nologin",
b58e57
+		"connection closed",
b58e57
+		"connection abandoned",
b58e57
+		"unknown"
b58e57
+	};
b58e57
+
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
b58e57
+		    errno == EAFNOSUPPORT)
b58e57
+			return; /* No audit support in kernel */
b58e57
+		else
b58e57
+			goto fatal_report; /* Must prevent login */
b58e57
+	}
b58e57
+
b58e57
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
b58e57
+		event = SSH_AUDIT_UNKNOWN;
b58e57
+
b58e57
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
b58e57
+	    NULL, event_name[event], username ? username : "(unknown)",
b58e57
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
b58e57
+	saved_errno = errno;
b58e57
+	close(audit_fd);
b58e57
+	/*
b58e57
+	 * Do not report error if the error is EPERM and sshd is run as non
b58e57
+	 * root user.
b58e57
+	 */
b58e57
+	if ((rc == -EPERM) && (geteuid() != 0))
b58e57
+		rc = 0;
b58e57
+	errno = saved_errno;
b58e57
+	if (rc < 0) {
b58e57
+fatal_report:
b58e57
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
b58e57
+	}
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
b58e57
+{
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	int audit_fd, rc, saved_errno;
b58e57
+
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
b58e57
+					 errno == EAFNOSUPPORT)
b58e57
+			return 1; /* No audit support in kernel */
b58e57
+		else
b58e57
+			return 0; /* Must prevent login */
b58e57
+	}
b58e57
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", ssh_remote_port(active_state));
b58e57
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
b58e57
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
b58e57
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
b58e57
+		goto out;
b58e57
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s rport=%d",
b58e57
+			type, bits, fp, ssh_remote_port(active_state));
b58e57
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
b58e57
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(active_state), NULL, rv);
b58e57
+out:
b58e57
+	saved_errno = errno;
b58e57
+	audit_close(audit_fd);
b58e57
+	errno = saved_errno;
b58e57
+	/* do not report error if the error is EPERM and sshd is run as non root user */
b58e57
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
b58e57
 }
b58e57
 
b58e57
+static int user_login_count = 0;
b58e57
+
b58e57
 /* Below is the sshd audit API code */
b58e57
 
b58e57
 void
b58e57
@@ -76,24 +177,51 @@ audit_connection_from(const char *host,
b58e57
 	/* not implemented */
b58e57
 }
b58e57
 
b58e57
-void
b58e57
+int
b58e57
 audit_run_command(const char *command)
b58e57
 {
b58e57
-	/* not implemented */
b58e57
+	if (!user_login_count++)
b58e57
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
b58e57
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
b58e57
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
b58e57
+	    NULL, "ssh", 1, AUDIT_USER_START);
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_end_command(int handle, const char *command)
b58e57
+{
b58e57
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
b58e57
+	    NULL, "ssh", 1, AUDIT_USER_END);
b58e57
+	if (user_login_count && !--user_login_count)
b58e57
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, session_get_remote_name_or_ip(active_state, utmp_len, options.use_dns),
b58e57
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_count_session_open(void)
b58e57
+{
b58e57
+	user_login_count++;
b58e57
 }
b58e57
 
b58e57
 void
b58e57
 audit_session_open(struct logininfo *li)
b58e57
 {
b58e57
-	if (linux_audit_record_event(li->uid, NULL, li->hostname, NULL,
b58e57
-	    li->line, 1) == 0)
b58e57
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
b58e57
+	if (!user_login_count++)
b58e57
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
b58e57
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
b58e57
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
b58e57
+	    NULL, li->line, 1, AUDIT_USER_START);
b58e57
 }
b58e57
 
b58e57
 void
b58e57
 audit_session_close(struct logininfo *li)
b58e57
 {
b58e57
-	/* not implemented */
b58e57
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
b58e57
+	    NULL, li->line, 1, AUDIT_USER_END);
b58e57
+	if (user_login_count && !--user_login_count)
b58e57
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
b58e57
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
b58e57
 }
b58e57
 
b58e57
 void
b58e57
@@ -103,24 +231,180 @@ audit_event(ssh_audit_event_t event)
b58e57
 
b58e57
 	switch(event) {
b58e57
 	case SSH_AUTH_SUCCESS:
b58e57
-	case SSH_CONNECTION_CLOSE:
b58e57
+		linux_audit_user_auth(-1, audit_username(), NULL,
b58e57
+			ssh_remote_ipaddr(ssh), "ssh", 1, event);
b58e57
+		break;
b58e57
+
b58e57
 	case SSH_NOLOGIN:
b58e57
-	case SSH_LOGIN_EXCEED_MAXTRIES:
b58e57
 	case SSH_LOGIN_ROOT_DENIED:
b58e57
+		linux_audit_user_auth(-1, audit_username(), NULL,
b58e57
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
b58e57
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
b58e57
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
b58e57
 		break;
b58e57
+	case SSH_LOGIN_EXCEED_MAXTRIES:
b58e57
 	case SSH_AUTH_FAIL_NONE:
b58e57
 	case SSH_AUTH_FAIL_PASSWD:
b58e57
 	case SSH_AUTH_FAIL_KBDINT:
b58e57
 	case SSH_AUTH_FAIL_PUBKEY:
b58e57
 	case SSH_AUTH_FAIL_HOSTBASED:
b58e57
 	case SSH_AUTH_FAIL_GSSAPI:
b58e57
+		linux_audit_user_auth(-1, audit_username(), NULL,
b58e57
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
b58e57
+		break;
b58e57
+
b58e57
+	case SSH_CONNECTION_CLOSE:
b58e57
+		if (user_login_count) {
b58e57
+			while (user_login_count--)
b58e57
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
b58e57
+				    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
b58e57
+				    NULL, "ssh", 1, AUDIT_USER_END);
b58e57
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
b58e57
+			    session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
b58e57
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
b58e57
+		}
b58e57
+		break;
b58e57
+
b58e57
+	case SSH_CONNECTION_ABANDON:
b58e57
 	case SSH_INVALID_USER:
b58e57
-		linux_audit_record_event(-1, audit_username(), NULL,
b58e57
-		    ssh_remote_ipaddr(ssh), "sshd", 0);
b58e57
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
b58e57
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
b58e57
 		break;
b58e57
 	default:
b58e57
 		debug("%s: unhandled event %d", __func__, event);
b58e57
 		break;
b58e57
 	}
b58e57
 }
b58e57
+
b58e57
+void
b58e57
+audit_unsupported_body(int what)
b58e57
+{
b58e57
+#ifdef AUDIT_CRYPTO_SESSION
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	const static char *name[] = { "cipher", "mac", "comp" };
b58e57
+	char *s;
b58e57
+	int audit_fd;
b58e57
+
b58e57
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
b58e57
+		name[what], ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())),
b58e57
+		ssh_local_port(active_state));
b58e57
+	free(s);
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0)
b58e57
+		/* no problem, the next instruction will be fatal() */
b58e57
+		return;
b58e57
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
b58e57
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 0);
b58e57
+	audit_close(audit_fd);
b58e57
+#endif
b58e57
+}
b58e57
+
b58e57
+const static char *direction[] = { "from-server", "from-client", "both" };
b58e57
+
b58e57
+void
b58e57
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
b58e57
+	       uid_t uid)
b58e57
+{
b58e57
+#ifdef AUDIT_CRYPTO_SESSION
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	int audit_fd, audit_ok;
b58e57
+	const struct sshcipher *cipher = cipher_by_name(enc);
b58e57
+	char *s;
b58e57
+
b58e57
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
b58e57
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
b58e57
+		(intmax_t)pid, (intmax_t)uid,
b58e57
+		ssh_remote_port(active_state), (s = get_local_ipaddr(packet_get_connection_in())), ssh_local_port(active_state));
b58e57
+	free(s);
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
b58e57
+					 errno == EAFNOSUPPORT)
b58e57
+			return; /* No audit support in kernel */
b58e57
+		else
b58e57
+			fatal("cannot open audit"); /* Must prevent login */
b58e57
+	}
b58e57
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
b58e57
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
b58e57
+	audit_close(audit_fd);
b58e57
+	/* do not abort if the error is EPERM and sshd is run as non root user */
b58e57
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
b58e57
+		fatal("cannot write into audit"); /* Must prevent login */
b58e57
+#endif
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	int audit_fd, audit_ok;
b58e57
+	char *s;
b58e57
+
b58e57
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
b58e57
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
b58e57
+		 ssh_remote_port(active_state),
b58e57
+		 (s = get_local_ipaddr(packet_get_connection_in())),
b58e57
+		 ssh_local_port(active_state));
b58e57
+	free(s);
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
b58e57
+					 errno != EAFNOSUPPORT)
b58e57
+			error("cannot open audit");
b58e57
+		return;
b58e57
+	}
b58e57
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
b58e57
+			buf, NULL, ssh_remote_ipaddr(active_state), NULL, 1);
b58e57
+	audit_close(audit_fd);
b58e57
+	/* do not abort if the error is EPERM and sshd is run as non root user */
b58e57
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
b58e57
+		error("cannot write into audit");
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	int audit_fd, audit_ok;
b58e57
+
b58e57
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
b58e57
+		fp, (intmax_t)pid, (intmax_t)uid);
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
b58e57
+					 errno != EAFNOSUPPORT)
b58e57
+			error("cannot open audit");
b58e57
+		return;
b58e57
+	}
b58e57
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
b58e57
+			buf, NULL,
b58e57
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(active_state),
b58e57
+			NULL, 1);
b58e57
+	audit_close(audit_fd);
b58e57
+	/* do not abort if the error is EPERM and sshd is run as non root user */
b58e57
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
b58e57
+		error("cannot write into audit");
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_generate_ephemeral_server_key(const char *fp)
b58e57
+{
b58e57
+	char buf[AUDIT_LOG_SIZE];
b58e57
+	int audit_fd, audit_ok;
b58e57
+
b58e57
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
b58e57
+	audit_fd = audit_open();
b58e57
+	if (audit_fd < 0) {
b58e57
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
b58e57
+					 errno != EAFNOSUPPORT)
b58e57
+			error("cannot open audit");
b58e57
+		return;
b58e57
+	}
b58e57
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
b58e57
+			buf, NULL, 0, NULL, 1);
b58e57
+	audit_close(audit_fd);
b58e57
+	/* do not abort if the error is EPERM and sshd is run as non root user */
b58e57
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
b58e57
+		error("cannot write into audit");
b58e57
+}
b58e57
 #endif /* USE_LINUX_AUDIT */
b58e57
diff -up openssh-7.4p1/auditstub.c.audit openssh-7.4p1/auditstub.c
b58e57
--- openssh-7.4p1/auditstub.c.audit	2016-12-23 18:54:54.434080419 +0100
b58e57
+++ openssh-7.4p1/auditstub.c	2016-12-23 18:54:54.434080419 +0100
b58e57
@@ -0,0 +1,50 @@
b58e57
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
b58e57
+
b58e57
+/*
b58e57
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
b58e57
+ * Use is subject to license terms.
b58e57
+ *
b58e57
+ * Redistribution and use in source and binary forms, with or without
b58e57
+ * modification, are permitted provided that the following conditions
b58e57
+ * are met:
b58e57
+ * 1. Redistributions of source code must retain the above copyright
b58e57
+ *    notice, this list of conditions and the following disclaimer.
b58e57
+ * 2. Redistributions in binary form must reproduce the above copyright
b58e57
+ *    notice, this list of conditions and the following disclaimer in the
b58e57
+ *    documentation and/or other materials provided with the distribution.
b58e57
+ *
b58e57
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
b58e57
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b58e57
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
b58e57
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
b58e57
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
b58e57
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
b58e57
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
b58e57
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
b58e57
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
b58e57
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
b58e57
+ *
b58e57
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
b58e57
+ */
b58e57
+
b58e57
+#include <sys/types.h>
b58e57
+
b58e57
+void
b58e57
+audit_unsupported(int n)
b58e57
+{
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
b58e57
+{
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_session_key_free(int ctos)
b58e57
+{
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+}
b58e57
diff -up openssh-7.4p1/auth2.c.audit openssh-7.4p1/auth2.c
b58e57
--- openssh-7.4p1/auth2.c.audit	2016-12-23 18:54:54.422080416 +0100
b58e57
+++ openssh-7.4p1/auth2.c	2016-12-23 18:54:54.434080419 +0100
b58e57
@@ -249,9 +249,6 @@ input_userauth_request(int type, u_int32
b58e57
 		} else {
b58e57
 			logit("input_userauth_request: invalid user %s", user);
b58e57
 			authctxt->pw = fakepw();
b58e57
-#ifdef SSH_AUDIT_EVENTS
b58e57
-			PRIVSEP(audit_event(SSH_INVALID_USER));
b58e57
-#endif
b58e57
 		}
b58e57
 #ifdef USE_PAM
b58e57
 		if (options.use_pam)
b58e57
diff -up openssh-7.4p1/auth2-hostbased.c.audit openssh-7.4p1/auth2-hostbased.c
b58e57
--- openssh-7.4p1/auth2-hostbased.c.audit	2016-12-23 18:54:54.422080416 +0100
b58e57
+++ openssh-7.4p1/auth2-hostbased.c	2016-12-23 18:54:54.434080419 +0100
b58e57
@@ -148,7 +148,7 @@ userauth_hostbased(Authctxt *authctxt)
b58e57
 	/* test for allowed key and correct signature */
b58e57
 	authenticated = 0;
b58e57
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
b58e57
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
b58e57
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
b58e57
 			buffer_len(&b))) == 1) {
b58e57
 		authenticated = 1;
b58e57
 		authctxt->last_details = pubkey;
b58e57
@@ -169,6 +169,18 @@ done:
b58e57
 	return authenticated;
b58e57
 }
b58e57
 
b58e57
+int
b58e57
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
b58e57
+{
b58e57
+	int rv;
b58e57
+
b58e57
+	rv = key_verify(key, sig, slen, data, datalen);
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	audit_key(0, &rv, key);
b58e57
+#endif
b58e57
+	return rv;
b58e57
+}
b58e57
+
b58e57
 /* return 1 if given hostkey is allowed */
b58e57
 int
b58e57
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
b58e57
diff -up openssh-7.4p1/auth2-pubkey.c.audit openssh-7.4p1/auth2-pubkey.c
b58e57
--- openssh-7.4p1/auth2-pubkey.c.audit	2016-12-23 18:54:54.423080416 +0100
b58e57
+++ openssh-7.4p1/auth2-pubkey.c	2016-12-23 18:54:54.435080419 +0100
b58e57
@@ -183,7 +183,7 @@ userauth_pubkey(Authctxt *authctxt)
b58e57
 		/* test for correct signature */
b58e57
 		authenticated = 0;
b58e57
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
b58e57
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
b58e57
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
b58e57
 		    buffer_len(&b))) == 1) {
b58e57
 			authenticated = 1;
b58e57
 			authctxt->last_details = pubkey;
b58e57
@@ -252,6 +252,18 @@ pubkey_auth_info(Authctxt *authctxt, con
b58e57
 	free(extra);
b58e57
 }
b58e57
 
b58e57
+int
b58e57
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
b58e57
+{
b58e57
+	int rv;
b58e57
+
b58e57
+	rv = key_verify(key, sig, slen, data, datalen);
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	audit_key(1, &rv, key);
b58e57
+#endif
b58e57
+	return rv;
b58e57
+}
b58e57
+
b58e57
 /*
b58e57
  * Splits 's' into an argument vector. Handles quoted string and basic
b58e57
  * escape characters (\\, \", \'). Caller must free the argument vector
b58e57
diff -up openssh-7.4p1/auth.c.audit openssh-7.4p1/auth.c
b58e57
--- openssh-7.4p1/auth.c.audit	2016-12-23 18:54:54.373080404 +0100
b58e57
+++ openssh-7.4p1/auth.c	2016-12-23 18:54:54.435080419 +0100
b58e57
@@ -666,9 +666,6 @@ getpwnamallow(const char *user)
b58e57
 		record_failed_login(user,
b58e57
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
b58e57
 #endif
b58e57
-#ifdef SSH_AUDIT_EVENTS
b58e57
-		audit_event(SSH_INVALID_USER);
b58e57
-#endif /* SSH_AUDIT_EVENTS */
b58e57
 		return (NULL);
b58e57
 	}
b58e57
 	if (!allowed_user(pw))
b58e57
diff -up openssh-7.4p1/auth.h.audit openssh-7.4p1/auth.h
b58e57
--- openssh-7.4p1/auth.h.audit	2016-12-23 18:54:54.423080416 +0100
b58e57
+++ openssh-7.4p1/auth.h	2016-12-23 18:54:54.435080419 +0100
b58e57
@@ -185,6 +185,7 @@ struct passwd * getpwnamallow(const char
b58e57
 
b58e57
 char	*expand_authorized_keys(const char *, struct passwd *pw);
b58e57
 char	*authorized_principals_file(struct passwd *);
b58e57
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
b58e57
 
b58e57
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
b58e57
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
b58e57
@@ -204,6 +205,7 @@ Key	*get_hostkey_private_by_type(int, in
b58e57
 int	 get_hostkey_index(Key *, int, struct ssh *);
b58e57
 int	 sshd_hostkey_sign(Key *, Key *, u_char **, size_t *,
b58e57
 	     const u_char *, size_t, const char *, u_int);
b58e57
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
b58e57
 
b58e57
 /* debug messages during authentication */
b58e57
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
b58e57
diff -up openssh-7.4p1/cipher.c.audit openssh-7.4p1/cipher.c
b58e57
--- openssh-7.4p1/cipher.c.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/cipher.c	2016-12-23 18:54:54.435080419 +0100
b58e57
@@ -66,26 +66,6 @@ struct sshcipher_ctx {
b58e57
 	const struct sshcipher *cipher;
b58e57
 };
b58e57
 
b58e57
-struct sshcipher {
b58e57
-	char	*name;
b58e57
-	int	number;		/* for ssh1 only */
b58e57
-	u_int	block_size;
b58e57
-	u_int	key_len;
b58e57
-	u_int	iv_len;		/* defaults to block_size */
b58e57
-	u_int	auth_len;
b58e57
-	u_int	discard_len;
b58e57
-	u_int	flags;
b58e57
-#define CFLAG_CBC		(1<<0)
b58e57
-#define CFLAG_CHACHAPOLY	(1<<1)
b58e57
-#define CFLAG_AESCTR		(1<<2)
b58e57
-#define CFLAG_NONE		(1<<3)
b58e57
-#ifdef WITH_OPENSSL
b58e57
-	const EVP_CIPHER	*(*evptype)(void);
b58e57
-#else
b58e57
-	void	*ignored;
b58e57
-#endif
b58e57
-};
b58e57
-
b58e57
 static const struct sshcipher ciphers[] = {
b58e57
 #ifdef WITH_SSH1
b58e57
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
b58e57
diff -up openssh-7.4p1/cipher.h.audit openssh-7.4p1/cipher.h
b58e57
--- openssh-7.4p1/cipher.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/cipher.h	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -62,7 +62,25 @@
b58e57
 #define CIPHER_ENCRYPT		1
b58e57
 #define CIPHER_DECRYPT		0
b58e57
 
b58e57
-struct sshcipher;
b58e57
+struct sshcipher { /* from cipher.c */
b58e57
+	char	*name;
b58e57
+	int	number;		/* for ssh1 only */
b58e57
+	u_int	block_size;
b58e57
+	u_int	key_len;
b58e57
+	u_int	iv_len;		/* defaults to block_size */
b58e57
+	u_int	auth_len;
b58e57
+	u_int	discard_len;
b58e57
+	u_int	flags;
b58e57
+#define CFLAG_CBC		(1<<0)
b58e57
+#define CFLAG_CHACHAPOLY	(1<<1)
b58e57
+#define CFLAG_AESCTR		(1<<2)
b58e57
+#define CFLAG_NONE		(1<<3)
b58e57
+#ifdef WITH_OPENSSL
b58e57
+	const EVP_CIPHER	*(*evptype)(void);
b58e57
+#else
b58e57
+	void	*ignored;
b58e57
+#endif
b58e57
+};
b58e57
 struct sshcipher_ctx;
b58e57
 
b58e57
 u_int	 cipher_mask_ssh1(int);
b58e57
diff -up openssh-7.4p1/kex.c.audit openssh-7.4p1/kex.c
b58e57
--- openssh-7.4p1/kex.c.audit	2016-12-23 18:54:54.410080413 +0100
b58e57
+++ openssh-7.4p1/kex.c	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -54,6 +54,7 @@
b58e57
 #include "ssherr.h"
b58e57
 #include "sshbuf.h"
b58e57
 #include "digest.h"
b58e57
+#include "audit.h"
b58e57
 
b58e57
 #ifdef GSSAPI
b58e57
 #include "ssh-gss.h"
b58e57
@@ -683,8 +684,12 @@ choose_enc(struct sshenc *enc, char *cli
b58e57
 {
b58e57
 	char *name = match_list(client, server, NULL);
b58e57
 
b58e57
-	if (name == NULL)
b58e57
+	if (name == NULL) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_CIPHER);
b58e57
+#endif
b58e57
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
b58e57
+	}
b58e57
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
b58e57
 		return SSH_ERR_INTERNAL_ERROR;
b58e57
 	enc->name = name;
b58e57
@@ -702,8 +707,12 @@ choose_mac(struct ssh *ssh, struct sshma
b58e57
 {
b58e57
 	char *name = match_list(client, server, NULL);
b58e57
 
b58e57
-	if (name == NULL)
b58e57
+	if (name == NULL) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_MAC);
b58e57
+#endif
b58e57
 		return SSH_ERR_NO_MAC_ALG_MATCH;
b58e57
+	}
b58e57
 	if (mac_setup(mac, name) < 0)
b58e57
 		return SSH_ERR_INTERNAL_ERROR;
b58e57
 	/* truncate the key */
b58e57
@@ -720,8 +729,12 @@ choose_comp(struct sshcomp *comp, char *
b58e57
 {
b58e57
 	char *name = match_list(client, server, NULL);
b58e57
 
b58e57
-	if (name == NULL)
b58e57
+	if (name == NULL) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+		audit_unsupported(SSH_AUDIT_UNSUPPORTED_COMPRESSION);
b58e57
+#endif
b58e57
 		return SSH_ERR_NO_COMPRESS_ALG_MATCH;
b58e57
+	}
b58e57
 	if (strcmp(name, "zlib@openssh.com") == 0) {
b58e57
 		comp->type = COMP_DELAYED;
b58e57
 	} else if (strcmp(name, "zlib") == 0) {
b58e57
@@ -890,6 +903,10 @@ kex_choose_conf(struct ssh *ssh)
b58e57
 		dh_need = MAXIMUM(dh_need, newkeys->enc.block_size);
b58e57
 		dh_need = MAXIMUM(dh_need, newkeys->enc.iv_len);
b58e57
 		dh_need = MAXIMUM(dh_need, newkeys->mac.key_len);
b58e57
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
b58e57
+#endif
b58e57
 	}
b58e57
 	/* XXX need runden? */
b58e57
 	kex->we_need = need;
b58e57
@@ -1064,3 +1081,33 @@ dump_digest(char *msg, u_char *digest, i
b58e57
 	sshbuf_dump_data(digest, len, stderr);
b58e57
 }
b58e57
 #endif
b58e57
+
b58e57
+static void
b58e57
+enc_destroy(struct sshenc *enc)
b58e57
+{
b58e57
+	if (enc == NULL)
b58e57
+		return;
b58e57
+
b58e57
+	if (enc->key) {
b58e57
+		memset(enc->key, 0, enc->key_len);
b58e57
+		free(enc->key);
b58e57
+	}
b58e57
+
b58e57
+	if (enc->iv) {
b58e57
+		memset(enc->iv,  0, enc->iv_len);
b58e57
+		free(enc->iv);
b58e57
+	}
b58e57
+
b58e57
+	memset(enc, 0, sizeof(*enc));
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+newkeys_destroy(struct newkeys *newkeys)
b58e57
+{
b58e57
+	if (newkeys == NULL)
b58e57
+		return;
b58e57
+
b58e57
+	enc_destroy(&newkeys->enc);
b58e57
+	mac_destroy(&newkeys->mac);
b58e57
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
b58e57
+}
b58e57
diff -up openssh-7.4p1/kex.h.audit openssh-7.4p1/kex.h
b58e57
--- openssh-7.4p1/kex.h.audit	2016-12-23 18:54:54.410080413 +0100
b58e57
+++ openssh-7.4p1/kex.h	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -213,6 +213,8 @@ int	 kexgss_client(struct ssh *);
b58e57
 int	 kexgss_server(struct ssh *);
b58e57
 #endif
b58e57
 
b58e57
+void	newkeys_destroy(struct newkeys *newkeys);
b58e57
+
b58e57
 int	 kex_dh_hash(int, const char *, const char *,
b58e57
     const u_char *, size_t, const u_char *, size_t, const u_char *, size_t,
b58e57
     const BIGNUM *, const BIGNUM *, const BIGNUM *, u_char *, size_t *);
b58e57
diff -up openssh-7.4p1/key.h.audit openssh-7.4p1/key.h
b58e57
--- openssh-7.4p1/key.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/key.h	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -50,6 +50,7 @@ typedef struct sshkey Key;
b58e57
 #define key_ecdsa_bits_to_nid	sshkey_ecdsa_bits_to_nid
b58e57
 #define key_ecdsa_key_to_nid	sshkey_ecdsa_key_to_nid
b58e57
 #define key_is_cert		sshkey_is_cert
b58e57
+#define key_is_private		sshkey_is_private
b58e57
 #define key_type_plain		sshkey_type_plain
b58e57
 #define key_curve_name_to_nid	sshkey_curve_name_to_nid
b58e57
 #define key_curve_nid_to_bits	sshkey_curve_nid_to_bits
b58e57
diff -up openssh-7.4p1/mac.c.audit openssh-7.4p1/mac.c
b58e57
--- openssh-7.4p1/mac.c.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/mac.c	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -249,6 +249,20 @@ mac_clear(struct sshmac *mac)
b58e57
 	mac->umac_ctx = NULL;
b58e57
 }
b58e57
 
b58e57
+void
b58e57
+mac_destroy(struct sshmac *mac)
b58e57
+{
b58e57
+	if (mac == NULL)
b58e57
+		return;
b58e57
+
b58e57
+	if (mac->key) {
b58e57
+		memset(mac->key, 0, mac->key_len);
b58e57
+		free(mac->key);
b58e57
+	}
b58e57
+
b58e57
+	memset(mac, 0, sizeof(*mac));
b58e57
+}
b58e57
+
b58e57
 /* XXX copied from ciphers_valid */
b58e57
 #define	MAC_SEP	","
b58e57
 int
b58e57
diff -up openssh-7.4p1/mac.h.audit openssh-7.4p1/mac.h
b58e57
--- openssh-7.4p1/mac.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/mac.h	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -49,5 +49,6 @@ int	 mac_compute(struct sshmac *, u_int3
b58e57
 int	 mac_check(struct sshmac *, u_int32_t, const u_char *, size_t,
b58e57
     const u_char *, size_t);
b58e57
 void	 mac_clear(struct sshmac *);
b58e57
+void	 mac_destroy(struct sshmac *);
b58e57
 
b58e57
 #endif /* SSHMAC_H */
b58e57
diff -up openssh-7.4p1/Makefile.in.audit openssh-7.4p1/Makefile.in
b58e57
--- openssh-7.4p1/Makefile.in.audit	2016-12-23 18:54:54.375080404 +0100
b58e57
+++ openssh-7.4p1/Makefile.in	2016-12-23 18:54:54.436080419 +0100
b58e57
@@ -100,7 +100,7 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
b58e57
 	kex.o kexdh.o kexgex.o kexecdh.o kexc25519.o \
b58e57
 	kexdhc.o kexgexc.o kexecdhc.o kexc25519c.o \
b58e57
 	kexdhs.o kexgexs.o kexecdhs.o kexc25519s.o \
b58e57
-	platform-pledge.o platform-tracing.o
b58e57
+	platform-pledge.o platform-tracing.o auditstub.o
b58e57
 
b58e57
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
b58e57
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o
b58e57
diff -up openssh-7.4p1/monitor.c.audit openssh-7.4p1/monitor.c
b58e57
--- openssh-7.4p1/monitor.c.audit	2016-12-23 18:54:54.423080416 +0100
b58e57
+++ openssh-7.4p1/monitor.c	2016-12-23 18:54:54.437080420 +0100
b58e57
@@ -102,6 +102,7 @@
b58e57
 #include "compat.h"
b58e57
 #include "ssh2.h"
b58e57
 #include "authfd.h"
b58e57
+#include "audit.h"
b58e57
 #include "match.h"
b58e57
 #include "ssherr.h"
b58e57
 
b58e57
@@ -117,6 +118,8 @@ extern Buffer auth_debug;
b58e57
 extern int auth_debug_init;
b58e57
 extern Buffer loginmsg;
b58e57
 
b58e57
+extern void destroy_sensitive_data(int);
b58e57
+
b58e57
 /* State exported from the child */
b58e57
 static struct sshbuf *child_state;
b58e57
 
b58e57
@@ -167,6 +170,11 @@ int mm_answer_gss_updatecreds(int, Buffe
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
 int mm_answer_audit_event(int, Buffer *);
b58e57
 int mm_answer_audit_command(int, Buffer *);
b58e57
+int mm_answer_audit_end_command(int, Buffer *);
b58e57
+int mm_answer_audit_unsupported_body(int, Buffer *);
b58e57
+int mm_answer_audit_kex_body(int, Buffer *);
b58e57
+int mm_answer_audit_session_key_free_body(int, Buffer *);
b58e57
+int mm_answer_audit_server_key_free(int, Buffer *);
b58e57
 #endif
b58e57
 
b58e57
 static int monitor_read_log(struct monitor *);
b58e57
@@ -222,6 +230,10 @@ struct mon_table mon_dispatch_proto20[]
b58e57
 #endif
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
b58e57
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
b58e57
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
b58e57
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
b58e57
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
b58e57
 #endif
b58e57
 #ifdef BSD_AUTH
b58e57
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
b58e57
@@ -260,6 +272,11 @@ struct mon_table mon_dispatch_postauth20
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
b58e57
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
b58e57
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
b58e57
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
b58e57
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
b58e57
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
b58e57
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
b58e57
 #endif
b58e57
     {0, 0, NULL}
b58e57
 };
b58e57
@@ -1396,9 +1413,11 @@ mm_answer_keyverify(int sock, Buffer *m)
b58e57
 	Key *key;
b58e57
 	u_char *signature, *data, *blob;
b58e57
 	u_int signaturelen, datalen, bloblen;
b58e57
+	int type = 0;
b58e57
 	int verified = 0;
b58e57
 	int valid_data = 0;
b58e57
 
b58e57
+	type = buffer_get_int(m);
b58e57
 	blob = buffer_get_string(m, &bloblen);
b58e57
 	signature = buffer_get_string(m, &signaturelen);
b58e57
 	data = buffer_get_string(m, &datalen);
b58e57
@@ -1406,6 +1425,8 @@ mm_answer_keyverify(int sock, Buffer *m)
b58e57
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
b58e57
 	  !monitor_allowed_key(blob, bloblen))
b58e57
 		fatal("%s: bad key, not previously allowed", __func__);
b58e57
+	if (type != key_blobtype)
b58e57
+		fatal("%s: bad key type", __func__);
b58e57
 
b58e57
 	key = key_from_blob(blob, bloblen);
b58e57
 	if (key == NULL)
b58e57
@@ -1426,7 +1447,17 @@ mm_answer_keyverify(int sock, Buffer *m)
b58e57
 	if (!valid_data)
b58e57
 		fatal("%s: bad signature data blob", __func__);
b58e57
 
b58e57
-	verified = key_verify(key, signature, signaturelen, data, datalen);
b58e57
+	switch (key_blobtype) {
b58e57
+	case MM_USERKEY:
b58e57
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
b58e57
+		break;
b58e57
+	case MM_HOSTKEY:
b58e57
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
b58e57
+		break;
b58e57
+	default:
b58e57
+		verified = 0;
b58e57
+		break;
b58e57
+	}
b58e57
 	debug3("%s: key %p signature %s",
b58e57
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
b58e57
 
b58e57
@@ -1489,6 +1520,12 @@ mm_session_close(Session *s)
b58e57
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
b58e57
 		session_pty_cleanup2(s);
b58e57
 	}
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	if (s->command != NULL) {
b58e57
+		debug3("%s: command %d", __func__, s->command_handle);
b58e57
+		session_end_command2(s);
b58e57
+	}
b58e57
+#endif
b58e57
 	session_unused(s->self);
b58e57
 }
b58e57
 
b58e57
@@ -1591,6 +1628,8 @@ mm_answer_term(int sock, Buffer *req)
b58e57
 		sshpam_cleanup();
b58e57
 #endif
b58e57
 
b58e57
+	destroy_sensitive_data(0);
b58e57
+
b58e57
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
b58e57
 		if (errno != EINTR)
b58e57
 			exit(1);
b58e57
@@ -1633,11 +1672,45 @@ mm_answer_audit_command(int socket, Buff
b58e57
 {
b58e57
 	u_int len;
b58e57
 	char *cmd;
b58e57
+	Session *s;
b58e57
 
b58e57
 	debug3("%s entering", __func__);
b58e57
 	cmd = buffer_get_string(m, &len;;
b58e57
+
b58e57
 	/* sanity check command, if so how? */
b58e57
-	audit_run_command(cmd);
b58e57
+	s = session_new();
b58e57
+	if (s == NULL)
b58e57
+		fatal("%s: error allocating a session", __func__);
b58e57
+	s->command = cmd;
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	s->command_handle = audit_run_command(cmd);
b58e57
+#endif
b58e57
+
b58e57
+	buffer_clear(m);
b58e57
+	buffer_put_int(m, s->self);
b58e57
+
b58e57
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
b58e57
+
b58e57
+	return (0);
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+mm_answer_audit_end_command(int socket, Buffer *m)
b58e57
+{
b58e57
+	int handle;
b58e57
+	u_int len;
b58e57
+	char *cmd;
b58e57
+	Session *s;
b58e57
+
b58e57
+	debug3("%s entering", __func__);
b58e57
+	handle = buffer_get_int(m);
b58e57
+	cmd = buffer_get_string(m, &len;;
b58e57
+
b58e57
+	s = session_by_id(handle);
b58e57
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
b58e57
+	    strcmp(s->command, cmd) != 0)
b58e57
+		fatal("%s: invalid handle", __func__);
b58e57
+	mm_session_close(s);
b58e57
 	free(cmd);
b58e57
 	return (0);
b58e57
 }
b58e57
@@ -1690,6 +1763,7 @@ monitor_apply_keystate(struct monitor *p
b58e57
 void
b58e57
 mm_get_keystate(struct monitor *pmonitor)
b58e57
 {
b58e57
+	Buffer m;
b58e57
 	debug3("%s: Waiting for new keys", __func__);
b58e57
 
b58e57
 	if ((child_state = sshbuf_new()) == NULL)
b58e57
@@ -1697,6 +1771,21 @@ mm_get_keystate(struct monitor *pmonitor
b58e57
 	mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
b58e57
 	    child_state);
b58e57
 	debug3("%s: GOT new keys", __func__);
b58e57
+
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	if (compat20) {
b58e57
+		buffer_init(&m);
b58e57
+		mm_request_receive_expect(pmonitor->m_sendfd,
b58e57
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
b58e57
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
b58e57
+		buffer_free(&m);
b58e57
+	}
b58e57
+#endif
b58e57
+
b58e57
+	/* Drain any buffered messages from the child */
b58e57
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
b58e57
+		;
b58e57
+
b58e57
 }
b58e57
 
b58e57
 
b58e57
@@ -1953,3 +2042,86 @@ mm_answer_gss_updatecreds(int socket, Bu
b58e57
 
b58e57
 #endif /* GSSAPI */
b58e57
 
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+int
b58e57
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
b58e57
+{
b58e57
+	int what;
b58e57
+
b58e57
+	what = buffer_get_int(m);
b58e57
+
b58e57
+	audit_unsupported_body(what);
b58e57
+
b58e57
+	buffer_clear(m);
b58e57
+
b58e57
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+mm_answer_audit_kex_body(int sock, Buffer *m)
b58e57
+{
b58e57
+	int ctos, len;
b58e57
+	char *cipher, *mac, *compress, *pfs;
b58e57
+	pid_t pid;
b58e57
+	uid_t uid;
b58e57
+
b58e57
+	ctos = buffer_get_int(m);
b58e57
+	cipher = buffer_get_string(m, &len;;
b58e57
+	mac = buffer_get_string(m, &len;;
b58e57
+	compress = buffer_get_string(m, &len;;
b58e57
+	pfs = buffer_get_string(m, &len;;
b58e57
+	pid = buffer_get_int64(m);
b58e57
+	uid = buffer_get_int64(m);
b58e57
+
b58e57
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
b58e57
+
b58e57
+	free(cipher);
b58e57
+	free(mac);
b58e57
+	free(compress);
b58e57
+	free(pfs);
b58e57
+	buffer_clear(m);
b58e57
+
b58e57
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
b58e57
+{
b58e57
+	int ctos;
b58e57
+	pid_t pid;
b58e57
+	uid_t uid;
b58e57
+
b58e57
+	ctos = buffer_get_int(m);
b58e57
+	pid = buffer_get_int64(m);
b58e57
+	uid = buffer_get_int64(m);
b58e57
+
b58e57
+	audit_session_key_free_body(ctos, pid, uid);
b58e57
+
b58e57
+	buffer_clear(m);
b58e57
+
b58e57
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
b58e57
+	return 0;
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+mm_answer_audit_server_key_free(int sock, Buffer *m)
b58e57
+{
b58e57
+	int len;
b58e57
+	char *fp;
b58e57
+	pid_t pid;
b58e57
+	uid_t uid;
b58e57
+
b58e57
+	fp = buffer_get_string(m, &len;;
b58e57
+	pid = buffer_get_int64(m);
b58e57
+	uid = buffer_get_int64(m);
b58e57
+
b58e57
+	audit_destroy_sensitive_data(fp, pid, uid);
b58e57
+
b58e57
+	free(fp);
b58e57
+	buffer_clear(m);
b58e57
+
b58e57
+	return 0;
b58e57
+}
b58e57
+#endif /* SSH_AUDIT_EVENTS */
b58e57
diff -up openssh-7.4p1/monitor.h.audit openssh-7.4p1/monitor.h
b58e57
--- openssh-7.4p1/monitor.h.audit	2016-12-23 18:54:54.393080409 +0100
b58e57
+++ openssh-7.4p1/monitor.h	2016-12-23 18:54:54.437080420 +0100
b58e57
@@ -69,7 +69,13 @@ enum monitor_reqtype {
b58e57
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
b58e57
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
b58e57
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
b58e57
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
b58e57
+	MONITOR_REQ_AUDIT_EVENT = 112,
b58e57
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
b58e57
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
b58e57
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
b58e57
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
b58e57
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
b58e57
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124
b58e57
 
b58e57
 };
b58e57
 
b58e57
diff -up openssh-7.4p1/monitor_wrap.c.audit openssh-7.4p1/monitor_wrap.c
b58e57
--- openssh-7.4p1/monitor_wrap.c.audit	2016-12-23 18:54:54.376080405 +0100
b58e57
+++ openssh-7.4p1/monitor_wrap.c	2016-12-23 18:54:54.437080420 +0100
b58e57
@@ -453,7 +453,7 @@ mm_key_allowed(enum mm_keytype type, con
b58e57
  */
b58e57
 
b58e57
 int
b58e57
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
b58e57
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
b58e57
 {
b58e57
 	Buffer m;
b58e57
 	u_char *blob;
b58e57
@@ -467,6 +467,7 @@ mm_key_verify(Key *key, u_char *sig, u_i
b58e57
 		return (0);
b58e57
 
b58e57
 	buffer_init(&m);
b58e57
+	buffer_put_int(&m, type);
b58e57
 	buffer_put_string(&m, blob, len);
b58e57
 	buffer_put_string(&m, sig, siglen);
b58e57
 	buffer_put_string(&m, data, datalen);
b58e57
@@ -484,6 +485,18 @@ mm_key_verify(Key *key, u_char *sig, u_i
b58e57
 	return (verified);
b58e57
 }
b58e57
 
b58e57
+int
b58e57
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
b58e57
+{
b58e57
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
b58e57
+}
b58e57
+
b58e57
+int
b58e57
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
b58e57
+{
b58e57
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
b58e57
+}
b58e57
+
b58e57
 void
b58e57
 mm_send_keystate(struct monitor *monitor)
b58e57
 {
b58e57
@@ -861,10 +874,11 @@ mm_audit_event(ssh_audit_event_t event)
b58e57
 	buffer_free(&m);
b58e57
 }
b58e57
 
b58e57
-void
b58e57
+int
b58e57
 mm_audit_run_command(const char *command)
b58e57
 {
b58e57
 	Buffer m;
b58e57
+	int handle;
b58e57
 
b58e57
 	debug3("%s entering command %s", __func__, command);
b58e57
 
b58e57
@@ -872,6 +886,26 @@ mm_audit_run_command(const char *command
b58e57
 	buffer_put_cstring(&m, command);
b58e57
 
b58e57
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
b58e57
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
b58e57
+
b58e57
+	handle = buffer_get_int(&m);
b58e57
+	buffer_free(&m);
b58e57
+
b58e57
+	return (handle);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+mm_audit_end_command(int handle, const char *command)
b58e57
+{
b58e57
+	Buffer m;
b58e57
+
b58e57
+	debug3("%s entering command %s", __func__, command);
b58e57
+
b58e57
+	buffer_init(&m);
b58e57
+	buffer_put_int(&m, handle);
b58e57
+	buffer_put_cstring(&m, command);
b58e57
+
b58e57
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
b58e57
 	buffer_free(&m);
b58e57
 }
b58e57
 #endif /* SSH_AUDIT_EVENTS */
b58e57
@@ -1007,3 +1041,70 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
b58e57
 
b58e57
 #endif /* GSSAPI */
b58e57
 
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+void
b58e57
+mm_audit_unsupported_body(int what)
b58e57
+{
b58e57
+	Buffer m;
b58e57
+
b58e57
+	buffer_init(&m);
b58e57
+	buffer_put_int(&m, what);
b58e57
+
b58e57
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
b58e57
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
b58e57
+				  &m);
b58e57
+
b58e57
+	buffer_free(&m);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
b58e57
+		  uid_t uid)
b58e57
+{
b58e57
+	Buffer m;
b58e57
+
b58e57
+	buffer_init(&m);
b58e57
+	buffer_put_int(&m, ctos);
b58e57
+	buffer_put_cstring(&m, cipher);
b58e57
+	buffer_put_cstring(&m, (mac ? mac : "<implicit>"));
b58e57
+	buffer_put_cstring(&m, compress);
b58e57
+	buffer_put_cstring(&m, fps);
b58e57
+	buffer_put_int64(&m, pid);
b58e57
+	buffer_put_int64(&m, uid);
b58e57
+
b58e57
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
b58e57
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
b58e57
+				  &m);
b58e57
+
b58e57
+	buffer_free(&m);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	Buffer m;
b58e57
+
b58e57
+	buffer_init(&m);
b58e57
+	buffer_put_int(&m, ctos);
b58e57
+	buffer_put_int64(&m, pid);
b58e57
+	buffer_put_int64(&m, uid);
b58e57
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
b58e57
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
b58e57
+				  &m);
b58e57
+	buffer_free(&m);
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
b58e57
+{
b58e57
+	Buffer m;
b58e57
+
b58e57
+	buffer_init(&m);
b58e57
+	buffer_put_cstring(&m, fp);
b58e57
+	buffer_put_int64(&m, pid);
b58e57
+	buffer_put_int64(&m, uid);
b58e57
+
b58e57
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
b58e57
+	buffer_free(&m);
b58e57
+}
b58e57
+#endif /* SSH_AUDIT_EVENTS */
b58e57
diff -up openssh-7.4p1/monitor_wrap.h.audit openssh-7.4p1/monitor_wrap.h
b58e57
--- openssh-7.4p1/monitor_wrap.h.audit	2016-12-23 18:54:54.376080405 +0100
b58e57
+++ openssh-7.4p1/monitor_wrap.h	2016-12-23 18:54:54.437080420 +0100
b58e57
@@ -52,7 +52,8 @@ int mm_key_allowed(enum mm_keytype, cons
b58e57
 int mm_user_key_allowed(struct passwd *, Key *, int);
b58e57
 int mm_hostbased_key_allowed(struct passwd *, const char *,
b58e57
     const char *, Key *);
b58e57
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
b58e57
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
b58e57
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
b58e57
 
b58e57
 #ifdef GSSAPI
b58e57
 OM_uint32 mm_ssh_gssapi_server_ctx(Gssctxt **, gss_OID);
b58e57
@@ -76,7 +77,12 @@ void mm_sshpam_free_ctx(void *);
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
 #include "audit.h"
b58e57
 void mm_audit_event(ssh_audit_event_t);
b58e57
-void mm_audit_run_command(const char *);
b58e57
+int mm_audit_run_command(const char *);
b58e57
+void mm_audit_end_command(int, const char *);
b58e57
+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
 #endif
b58e57
 
b58e57
 struct Session;
b58e57
diff -up openssh-7.4p1/packet.c.audit openssh-7.4p1/packet.c
b58e57
--- openssh-7.4p1/packet.c.audit	2016-12-23 18:54:54.318080390 +0100
b58e57
+++ openssh-7.4p1/packet.c	2016-12-23 18:54:54.438080420 +0100
b58e57
@@ -67,6 +67,7 @@
b58e57
 #include "key.h"	/* typedefs XXX */
b58e57
 
b58e57
 #include "xmalloc.h"
b58e57
+#include "audit.h"
b58e57
 #include "crc32.h"
b58e57
 #include "deattack.h"
b58e57
 #include "compat.h"
b58e57
@@ -494,6 +495,13 @@ ssh_packet_get_connection_out(struct ssh
b58e57
 	return ssh->state->connection_out;
b58e57
 }
b58e57
 
b58e57
+static int
b58e57
+packet_state_has_keys (const struct session_state *state)
b58e57
+{
b58e57
+	return state != NULL &&
b58e57
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
b58e57
+}
b58e57
+
b58e57
 /*
b58e57
  * Returns the IP-address of the remote host as a string.  The returned
b58e57
  * string must not be freed.
b58e57
@@ -510,11 +510,12 @@
b58e57
 const char *
b58e57
 ssh_remote_ipaddr(struct ssh *ssh)
b58e57
 {
b58e57
-	const int sock = ssh->state->connection_in;
b58e57
+	int sock;
b58e57
 
b58e57
 	/* Check whether we have cached the ipaddr. */
b58e57
 	if (ssh->remote_ipaddr == NULL) {
b58e57
 		if (ssh_packet_connection_is_on_socket(ssh)) {
b58e57
+			sock = ssh->state->connection_in;
b58e57
 			ssh->remote_ipaddr = get_peer_ipaddr(sock);
b58e57
 			ssh->remote_port = get_peer_port(sock);
b58e57
 			ssh->local_ipaddr = get_local_ipaddr(sock);
b58e57
@@ -562,13 +570,6 @@ ssh_packet_close(struct ssh *ssh)
b58e57
 	if (!state->initialized)
b58e57
 		return;
b58e57
 	state->initialized = 0;
b58e57
-	if (state->connection_in == state->connection_out) {
b58e57
-		shutdown(state->connection_out, SHUT_RDWR);
b58e57
-		close(state->connection_out);
b58e57
-	} else {
b58e57
-		close(state->connection_in);
b58e57
-		close(state->connection_out);
b58e57
-	}
b58e57
 	sshbuf_free(state->input);
b58e57
 	sshbuf_free(state->output);
b58e57
 	sshbuf_free(state->outgoing_packet);
b58e57
@@ -600,11 +601,21 @@ ssh_packet_close(struct ssh *ssh)
b58e57
 				inflateEnd(stream);
b58e57
 		}
b58e57
 	}
b58e57
-	cipher_free(state->send_context);
b58e57
-	cipher_free(state->receive_context);
b58e57
+	if (packet_state_has_keys(state)) {
b58e57
+		cipher_free(state->send_context);
b58e57
+		cipher_free(state->receive_context);
b58e57
+		audit_session_key_free(MODE_MAX);
b58e57
+	}
b58e57
 	state->send_context = state->receive_context = NULL;
b58e57
 	free(ssh->remote_ipaddr);
b58e57
 	ssh->remote_ipaddr = NULL;
b58e57
+	if (state->connection_in == state->connection_out) {
b58e57
+		shutdown(state->connection_out, SHUT_RDWR);
b58e57
+		close(state->connection_out);
b58e57
+	} else {
b58e57
+		close(state->connection_in);
b58e57
+		close(state->connection_out);
b58e57
+	}
b58e57
 	free(ssh->state);
b58e57
 	ssh->state = NULL;
b58e57
 }
b58e57
@@ -950,6 +961,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
b58e57
 		    " (%llu bytes total)", __func__,
b58e57
 		    (unsigned long long)ps->blocks, dir,
b58e57
 		    (unsigned long long)ps->bytes);
b58e57
+		audit_session_key_free(mode);
b58e57
 		cipher_free(*ccp);
b58e57
 		*ccp = NULL;
b58e57
 		enc  = &state->newkeys[mode]->enc;
b58e57
@@ -2440,6 +2452,72 @@ ssh_packet_get_output(struct ssh *ssh)
b58e57
 	return (void *)ssh->state->output;
b58e57
 }
b58e57
 
b58e57
+static void
b58e57
+newkeys_destroy_and_free(struct newkeys *newkeys)
b58e57
+{
b58e57
+	if (newkeys == NULL)
b58e57
+		return;
b58e57
+
b58e57
+	free(newkeys->enc.name);
b58e57
+
b58e57
+	if (newkeys->mac.enabled) {
b58e57
+		mac_clear(&newkeys->mac);
b58e57
+		free(newkeys->mac.name);
b58e57
+	}
b58e57
+
b58e57
+	free(newkeys->comp.name);
b58e57
+
b58e57
+	newkeys_destroy(newkeys);
b58e57
+	free(newkeys);
b58e57
+}
b58e57
+
b58e57
+static void
b58e57
+packet_destroy_state(struct session_state *state)
b58e57
+{
b58e57
+	if (state == NULL)
b58e57
+		return;
b58e57
+
b58e57
+	cipher_free(state->receive_context);
b58e57
+	cipher_free(state->send_context);
b58e57
+
b58e57
+	buffer_free(state->input);
b58e57
+	state->input = NULL;
b58e57
+	buffer_free(state->output);
b58e57
+	state->output = NULL;
b58e57
+	buffer_free(state->outgoing_packet);
b58e57
+	state->outgoing_packet = NULL;
b58e57
+	buffer_free(state->incoming_packet);
b58e57
+	state->incoming_packet = NULL;
b58e57
+	if( state->compression_buffer ) {
b58e57
+		buffer_free(state->compression_buffer);
b58e57
+		state->compression_buffer = NULL;
b58e57
+	}
b58e57
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
b58e57
+	state->newkeys[MODE_IN] = NULL;
b58e57
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
b58e57
+	state->newkeys[MODE_OUT] = NULL;
b58e57
+	mac_destroy(state->packet_discard_mac);
b58e57
+//	TAILQ_HEAD(, packet) outgoing;
b58e57
+//	memset(state, 0, sizeof(state));
b58e57
+}
b58e57
+
b58e57
+void
b58e57
+packet_destroy_all(int audit_it, int privsep)
b58e57
+{
b58e57
+	if (audit_it)
b58e57
+		audit_it = (active_state != NULL && packet_state_has_keys(active_state->state));
b58e57
+	if (active_state != NULL)
b58e57
+		packet_destroy_state(active_state->state);
b58e57
+	if (audit_it) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+		if (privsep)
b58e57
+			audit_session_key_free(MODE_MAX);
b58e57
+		else
b58e57
+			audit_session_key_free_body(MODE_MAX, getpid(), getuid());
b58e57
+#endif
b58e57
+	}
b58e57
+}
b58e57
+
b58e57
 /* Reset after_authentication and reset compression in post-auth privsep */
b58e57
 static int
b58e57
 ssh_packet_set_postauth(struct ssh *ssh)
b58e57
diff -up openssh-7.4p1/packet.h.audit openssh-7.4p1/packet.h
b58e57
--- openssh-7.4p1/packet.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/packet.h	2016-12-23 18:54:54.438080420 +0100
b58e57
@@ -208,4 +208,5 @@ extern struct ssh *active_state;
b58e57
 # undef EC_POINT
b58e57
 #endif
b58e57
 
b58e57
+void	 packet_destroy_all(int, int);
b58e57
 #endif				/* PACKET_H */
b58e57
diff -up openssh-7.4p1/session.c.audit openssh-7.4p1/session.c
b58e57
--- openssh-7.4p1/session.c.audit	2016-12-23 18:54:54.430080418 +0100
b58e57
+++ openssh-7.4p1/session.c	2016-12-23 18:57:45.068115466 +0100
b58e57
@@ -142,7 +142,7 @@ extern int log_stderr;
b58e57
 extern int debug_flag;
b58e57
 extern u_int utmp_len;
b58e57
 extern int startup_pipe;
b58e57
-extern void destroy_sensitive_data(void);
b58e57
+extern void destroy_sensitive_data(int);
b58e57
 extern Buffer loginmsg;
b58e57
 
b58e57
 /* original command from peer. */
b58e57
@@ -576,6 +576,14 @@ do_exec_pty(Session *s, const char *comm
b58e57
 	/* Parent.  Close the slave side of the pseudo tty. */
b58e57
 	close(ttyfd);
b58e57
 
b58e57
+#if !defined(HAVE_OSF_SIA) && defined(SSH_AUDIT_EVENTS)
b58e57
+	/* do_login in the child did not affect state in this process,
b58e57
+	   compensate.  From an architectural standpoint, this is extremely
b58e57
+	   ugly. */
b58e57
+	if (command != NULL)
b58e57
+		audit_count_session_open();
b58e57
+#endif
b58e57
+
b58e57
 	/* Enter interactive session. */
b58e57
 	s->ptymaster = ptymaster;
b58e57
 	packet_set_interactive(1, 
b58e57
@@ -696,15 +704,19 @@ do_exec(Session *s, const char *command)
b58e57
 	    s->self);
b58e57
 
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
+	if (s->command != NULL || s->command_handle != -1)
b58e57
+		fatal("do_exec: command already set");
b58e57
 	if (command != NULL)
b58e57
-		PRIVSEP(audit_run_command(command));
b58e57
+		s->command = xstrdup(command);
b58e57
 	else if (s->ttyfd == -1) {
b58e57
 		char *shell = s->pw->pw_shell;
b58e57
 
b58e57
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
b58e57
 			shell =_PATH_BSHELL;
b58e57
-		PRIVSEP(audit_run_command(shell));
b58e57
+		s->command = xstrdup(shell);
b58e57
 	}
b58e57
+	if (s->command != NULL && s->ptyfd == -1)
b58e57
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
b58e57
 #endif
b58e57
 	if (s->ttyfd != -1)
b58e57
 		ret = do_exec_pty(s, command);
b58e57
@@ -1543,7 +1555,10 @@ do_child(Session *s, const char *command
b58e57
 	int r = 0;
b58e57
 
b58e57
 	/* remove hostkey from the child's memory */
b58e57
-	destroy_sensitive_data();
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
@@ -1757,6 +1772,9 @@ session_unused(int id)
b58e57
 	sessions[id].ttyfd = -1;
b58e57
 	sessions[id].ptymaster = -1;
b58e57
 	sessions[id].x11_chanids = NULL;
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	sessions[id].command_handle = -1;
b58e57
+#endif
b58e57
 	sessions[id].next_unused = sessions_first_unused;
b58e57
 	sessions_first_unused = id;
b58e57
 }
b58e57
@@ -1839,6 +1857,19 @@ session_open(Authctxt *authctxt, int cha
b58e57
 }
b58e57
 
b58e57
 Session *
b58e57
+session_by_id(int id)
b58e57
+{
b58e57
+	if (id >= 0 && id < sessions_nalloc) {
b58e57
+		Session *s = &sessions[id];
b58e57
+		if (s->used)
b58e57
+			return s;
b58e57
+	}
b58e57
+	debug("%s: unknown id %d", __func__, id);
b58e57
+	session_dump();
b58e57
+	return NULL;
b58e57
+}
b58e57
+
b58e57
+Session *
b58e57
 session_by_tty(char *tty)
b58e57
 {
b58e57
 	int i;
b58e57
@@ -2351,6 +2382,32 @@ session_exit_message(Session *s, int sta
b58e57
 		chan_write_failed(c);
b58e57
 }
b58e57
 
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+void
b58e57
+session_end_command2(Session *s)
b58e57
+{
b58e57
+	if (s->command != NULL) {
b58e57
+		if (s->command_handle != -1)
b58e57
+			audit_end_command(s->command_handle, s->command);
b58e57
+		free(s->command);
b58e57
+		s->command = NULL;
b58e57
+		s->command_handle = -1;
b58e57
+	}
b58e57
+}
b58e57
+
b58e57
+static void
b58e57
+session_end_command(Session *s)
b58e57
+{
b58e57
+	if (s->command != NULL) {
b58e57
+		if (s->command_handle != -1)
b58e57
+			PRIVSEP(audit_end_command(s->command_handle, s->command));
b58e57
+		free(s->command);
b58e57
+		s->command = NULL;
b58e57
+		s->command_handle = -1;
b58e57
+	}
b58e57
+}
b58e57
+#endif
b58e57
+
b58e57
 void
b58e57
 session_close(Session *s)
b58e57
 {
b58e57
@@ -2365,6 +2422,10 @@ session_close(Session *s)
b58e57
 
b58e57
 	if (s->ttyfd != -1)
b58e57
 		session_pty_cleanup(s);
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	if (s->command)
b58e57
+		session_end_command(s);
b58e57
+#endif
b58e57
 	free(s->term);
b58e57
 	free(s->display);
b58e57
 	free(s->x11_chanids);
b58e57
@@ -2575,6 +2636,15 @@ do_authenticated2(Authctxt *authctxt)
b58e57
 	server_loop2(authctxt);
b58e57
 }
b58e57
 
b58e57
+static void
b58e57
+do_cleanup_one_session(Session *s)
b58e57
+{
b58e57
+	session_pty_cleanup2(s);
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	session_end_command2(s);
b58e57
+#endif
b58e57
+}
b58e57
+
b58e57
 void
b58e57
 do_cleanup(Authctxt *authctxt)
b58e57
 {
b58e57
@@ -2626,7 +2696,7 @@ do_cleanup(Authctxt *authctxt)
b58e57
 	 * or if running in monitor.
b58e57
 	 */
b58e57
 	if (!use_privsep || mm_is_monitor())
b58e57
-		session_destroy_all(session_pty_cleanup2);
b58e57
+		session_destroy_all(do_cleanup_one_session);
b58e57
 }
b58e57
 
b58e57
 /* Return a name for the remote host that fits inside utmp_size */
b58e57
diff -up openssh-7.4p1/session.h.audit openssh-7.4p1/session.h
b58e57
--- openssh-7.4p1/session.h.audit	2016-12-19 05:59:41.000000000 +0100
b58e57
+++ openssh-7.4p1/session.h	2016-12-23 18:54:54.438080420 +0100
b58e57
@@ -60,6 +60,12 @@ struct Session {
b58e57
 		char	*name;
b58e57
 		char	*val;
b58e57
 	} *env;
b58e57
+
b58e57
+	/* exec */
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	int	command_handle;
b58e57
+	char	*command;
b58e57
+#endif
b58e57
 };
b58e57
 
b58e57
 void	 do_authenticated(Authctxt *);
b58e57
@@ -72,8 +78,10 @@ void	 session_close_by_pid(pid_t, int);
b58e57
 void	 session_close_by_channel(int, void *);
b58e57
 void	 session_destroy_all(void (*)(Session *));
b58e57
 void	 session_pty_cleanup2(Session *);
b58e57
+void	 session_end_command2(Session *);
b58e57
 
b58e57
 Session	*session_new(void);
b58e57
+Session *session_by_id(int);
b58e57
 Session	*session_by_tty(char *);
b58e57
 void	 session_close(Session *);
b58e57
 void	 do_setusercontext(struct passwd *);
b58e57
diff -up openssh-7.4p1/sshd.c.audit openssh-7.4p1/sshd.c
b58e57
--- openssh-7.4p1/sshd.c.audit	2016-12-23 18:54:54.403080411 +0100
b58e57
+++ openssh-7.4p1/sshd.c	2016-12-23 18:56:18.992101105 +0100
b58e57
@@ -119,6 +119,7 @@
b58e57
 #include "ssh-gss.h"
b58e57
 #endif
b58e57
 #include "monitor_wrap.h"
b58e57
+#include "audit.h"
b58e57
 #include "ssh-sandbox.h"
b58e57
 #include "version.h"
b58e57
 #include "ssherr.h"
b58e57
@@ -244,7 +245,7 @@ Buffer loginmsg;
b58e57
 struct passwd *privsep_pw = NULL;
b58e57
 
b58e57
 /* Prototypes for various functions defined later in this file. */
b58e57
-void destroy_sensitive_data(void);
b58e57
+void destroy_sensitive_data(int);
b58e57
 void demote_sensitive_data(void);
b58e57
 static void do_ssh2_kex(void);
b58e57
 
b58e57
@@ -261,6 +262,15 @@ close_listen_socks(void)
b58e57
 	num_listen_socks = -1;
b58e57
 }
b58e57
 
b58e57
+/*
b58e57
+ * Is this process listening for clients (i.e. not specific to any specific
b58e57
+ * client connection?)
b58e57
+ */
b58e57
+int listening_for_clients(void)
b58e57
+{
b58e57
+	return num_listen_socks >= 0;
b58e57
+}
b58e57
+
b58e57
 static void
b58e57
 close_startup_pipes(void)
b58e57
 {
b58e57
@@ -473,18 +483,45 @@ sshd_exchange_identification(struct ssh
b58e57
 	}
b58e57
 }
b58e57
 
b58e57
-/* Destroy the host and server keys.  They will no longer be needed. */
b58e57
+/*
b58e57
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
b58e57
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
b58e57
+ */
b58e57
 void
b58e57
-destroy_sensitive_data(void)
b58e57
+destroy_sensitive_data(int privsep)
b58e57
 {
b58e57
 	int i;
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	pid_t pid;
b58e57
+	uid_t uid;
b58e57
 
b58e57
+	pid = getpid();
b58e57
+	uid = getuid();
b58e57
+#endif
b58e57
 	for (i = 0; i < options.num_host_key_files; i++) {
b58e57
 		if (sensitive_data.host_keys[i]) {
b58e57
+			char *fp;
b58e57
+
b58e57
+			if (key_is_private(sensitive_data.host_keys[i]))
b58e57
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
b58e57
+			else
b58e57
+				fp = NULL;
b58e57
 			key_free(sensitive_data.host_keys[i]);
b58e57
 			sensitive_data.host_keys[i] = NULL;
b58e57
+			if (fp != NULL) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+				if (privsep)
b58e57
+					PRIVSEP(audit_destroy_sensitive_data(fp,
b58e57
+						pid, uid));
b58e57
+				else
b58e57
+					audit_destroy_sensitive_data(fp,
b58e57
+						pid, uid);
b58e57
+#endif
b58e57
+				free(fp);
b58e57
+			}
b58e57
 		}
b58e57
-		if (sensitive_data.host_certificates[i]) {
b58e57
+		if (sensitive_data.host_certificates
b58e57
+		    && sensitive_data.host_certificates[i]) {
b58e57
 			key_free(sensitive_data.host_certificates[i]);
b58e57
 			sensitive_data.host_certificates[i] = NULL;
b58e57
 		}
b58e57
@@ -497,12 +534,30 @@ demote_sensitive_data(void)
b58e57
 {
b58e57
 	Key *tmp;
b58e57
 	int i;
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+	pid_t pid;
b58e57
+	uid_t uid;
b58e57
 
b58e57
+	pid = getpid();
b58e57
+	uid = getuid();
b58e57
+#endif
b58e57
 	for (i = 0; i < options.num_host_key_files; i++) {
b58e57
 		if (sensitive_data.host_keys[i]) {
b58e57
+			char *fp;
b58e57
+
b58e57
+			if (key_is_private(sensitive_data.host_keys[i]))
b58e57
+				fp = sshkey_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
b58e57
+			else
b58e57
+				fp = NULL;
b58e57
 			tmp = key_demote(sensitive_data.host_keys[i]);
b58e57
 			key_free(sensitive_data.host_keys[i]);
b58e57
 			sensitive_data.host_keys[i] = tmp;
b58e57
+			if (fp != NULL) {
b58e57
+#ifdef SSH_AUDIT_EVENTS
b58e57
+				audit_destroy_sensitive_data(fp, pid, uid);
b58e57
+#endif
b58e57
+				free(fp);
b58e57
+			}
b58e57
 		}
b58e57
 		/* Certs do not need demotion */
b58e57
 	}
b58e57
@@ -585,7 +640,7 @@ privsep_preauth(Authctxt *authctxt)
b58e57
 
b58e57
 	if (use_privsep == PRIVSEP_ON)
b58e57
 		box = ssh_sandbox_init(pmonitor);
b58e57
-	pid = fork();
b58e57
+	pmonitor->m_pid = pid = fork();
b58e57
 	if (pid == -1) {
b58e57
 		fatal("fork of unprivileged child failed");
b58e57
 	} else if (pid != 0) {
b58e57
@@ -665,6 +720,12 @@ privsep_postauth(Authctxt *authctxt)
b58e57
 	else if (pmonitor->m_pid != 0) {
b58e57
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
b58e57
 		buffer_clear(&loginmsg);
b58e57
+		if (*pmonitor->m_pkex != NULL ){
b58e57
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_OUT]);
b58e57
+			newkeys_destroy((*pmonitor->m_pkex)->newkeys[MODE_IN]);
b58e57
+			audit_session_key_free_body(2, getpid(), getuid());
b58e57
+			packet_destroy_all(0, 0);
b58e57
+		}
b58e57
 		monitor_child_postauth(pmonitor);
b58e57
 
b58e57
 		/* NEVERREACHED */
b58e57
@@ -1154,6 +1215,7 @@ server_accept_loop(int *sock_in, int *so
b58e57
 		if (received_sigterm) {
b58e57
 			logit("Received signal %d; terminating.",
b58e57
 			    (int) received_sigterm);
b58e57
+			destroy_sensitive_data(0);
b58e57
 			close_listen_socks();
b58e57
 			if (options.pid_file != NULL)
b58e57
 				unlink(options.pid_file);
b58e57
@@ -2092,6 +2150,7 @@ main(int ac, char **av)
b58e57
 	 */
b58e57
 	if (use_privsep) {
b58e57
 		mm_send_keystate(pmonitor);
b58e57
+		packet_destroy_all(1, 1);
b58e57
 		exit(0);
b58e57
 	}
b58e57
 
b58e57
@@ -2148,6 +2207,9 @@ main(int ac, char **av)
b58e57
 	do_authenticated(authctxt);
b58e57
 
b58e57
 	/* The connection has been terminated. */
b58e57
+	packet_destroy_all(1, 1);
b58e57
+	destroy_sensitive_data(1);
b58e57
+
b58e57
 	packet_get_bytes(&ibytes, &obytes);
b58e57
 	verbose("Transferred: sent %llu, received %llu bytes",
b58e57
 	    (unsigned long long)obytes, (unsigned long long)ibytes);
b58e57
@@ -2321,6 +2383,16 @@ do_ssh2_kex(void)
b58e57
 void
b58e57
 cleanup_exit(int i)
b58e57
 {
b58e57
+	static int in_cleanup = 0;
b58e57
+	int is_privsep_child;
b58e57
+
b58e57
+	/* cleanup_exit can be called at the very least from the privsep
b58e57
+	   wrappers used for auditing.  Make sure we don't recurse
b58e57
+	   indefinitely. */
b58e57
+	if (in_cleanup)
b58e57
+		_exit(i);
b58e57
+	in_cleanup = 1;
b58e57
+
b58e57
 	if (the_authctxt) {
b58e57
 		do_cleanup(the_authctxt);
b58e57
 		if (use_privsep && privsep_is_preauth &&
b58e57
@@ -2332,9 +2404,14 @@ cleanup_exit(int i)
b58e57
 				    pmonitor->m_pid, strerror(errno));
b58e57
 		}
b58e57
 	}
b58e57
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
b58e57
+	if (sensitive_data.host_keys != NULL)
b58e57
+		destroy_sensitive_data(is_privsep_child);
b58e57
+	packet_destroy_all(1, is_privsep_child);
b58e57
 #ifdef SSH_AUDIT_EVENTS
b58e57
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
b58e57
-	if (!use_privsep || mm_is_monitor())
b58e57
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
b58e57
+	    (!use_privsep || mm_is_monitor()))
b58e57
 		audit_event(SSH_CONNECTION_ABANDON);
b58e57
 #endif
b58e57
 	_exit(i);
b58e57
diff -up openssh-7.4p1/sshkey.c.audit openssh-7.4p1/sshkey.c
b58e57
--- openssh-7.4p1/sshkey.c.audit	2016-12-23 18:54:54.425080417 +0100
b58e57
+++ openssh-7.4p1/sshkey.c	2016-12-23 18:54:54.439080420 +0100
b58e57
@@ -303,6 +303,33 @@ sshkey_type_is_valid_ca(int type)
b58e57
 }
b58e57
 
b58e57
 int
b58e57
+sshkey_is_private(const struct sshkey *k)
b58e57
+{
b58e57
+      switch (k->type) {
b58e57
+#ifdef WITH_OPENSSL
b58e57
+      case KEY_RSA_CERT:
b58e57
+      case KEY_RSA1:
b58e57
+      case KEY_RSA:
b58e57
+              return k->rsa->d != NULL;
b58e57
+      case KEY_DSA_CERT:
b58e57
+      case KEY_DSA:
b58e57
+              return k->dsa->priv_key != NULL;
b58e57
+#ifdef OPENSSL_HAS_ECC
b58e57
+      case KEY_ECDSA_CERT:
b58e57
+      case KEY_ECDSA:
b58e57
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
b58e57
+#endif /* OPENSSL_HAS_ECC */
b58e57
+#endif /* WITH_OPENSSL */
b58e57
+      case KEY_ED25519_CERT:
b58e57
+      case KEY_ED25519:
b58e57
+              return (k->ed25519_pk != NULL);
b58e57
+      default:
b58e57
+              /* fatal("key_is_private: bad key type %d", k->type); */
b58e57
+              return 0;
b58e57
+      }
b58e57
+}
b58e57
+
b58e57
+int
b58e57
 sshkey_is_cert(const struct sshkey *k)
b58e57
 {
b58e57
 	if (k == NULL)
b58e57
diff -up openssh-7.4p1/sshkey.h.audit openssh-7.4p1/sshkey.h
b58e57
--- openssh-7.4p1/sshkey.h.audit	2016-12-23 18:54:54.425080417 +0100
b58e57
+++ openssh-7.4p1/sshkey.h	2016-12-23 18:54:54.439080420 +0100
b58e57
@@ -134,6 +134,7 @@ u_int		 sshkey_size(const struct sshkey
b58e57
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
b58e57
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
b58e57
 int	 sshkey_type_from_name(const char *);
b58e57
+int	 sshkey_is_private(const struct sshkey *);
b58e57
 int	 sshkey_is_cert(const struct sshkey *);
b58e57
 int	 sshkey_type_is_cert(int);
b58e57
 int	 sshkey_type_plain(int);