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