def1de
diff -up openssh/audit-bsm.c.audit openssh/audit-bsm.c
def1de
--- openssh/audit-bsm.c.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/audit-bsm.c	2019-04-03 17:02:20.713886041 +0200
def1de
@@ -372,13 +372,26 @@ audit_connection_from(const char *host,
d3d59d
 #endif
d3d59d
 }
d3d59d
 
d3d59d
+int
def1de
+audit_run_command(struct ssh *ssh, const char *command)
d3d59d
+{
d3d59d
+	/* not implemented */
def1de
+	return 0;
d3d59d
+}
d3d59d
+
def1de
 void
def1de
-audit_run_command(const char *command)
def1de
+audit_end_command(struct ssh *ssh, int handle, const char *command)
def1de
 {
def1de
 	/* not implemented */
d3d59d
 }
d3d59d
 
d3d59d
 void
def1de
+audit_count_session_open(void)
def1de
+{
def1de
+	/* not necessary */
def1de
+}
def1de
+
def1de
+void
def1de
 audit_session_open(struct logininfo *li)
def1de
 {
def1de
 	/* not implemented */
def1de
@@ -390,6 +403,12 @@ audit_session_close(struct logininfo *li
d3d59d
 	/* not implemented */
d3d59d
 }
d3d59d
 
d3d59d
+int
def1de
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
d3d59d
+{
d3d59d
+	/* not implemented */
d3d59d
+}
d3d59d
+
d3d59d
 void
def1de
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
d3d59d
 {
def1de
@@ -451,4 +470,28 @@ audit_event(struct ssh *ssh, ssh_audit_e
d3d59d
 		debug("%s: unhandled event %d", __func__, event);
d3d59d
 	}
d3d59d
 }
d3d59d
+
d3d59d
+void
def1de
+audit_unsupported_body(struct ssh *ssh, int what)
d3d59d
+{
d3d59d
+	/* not implemented */
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
d3d59d
+{
d3d59d
+	/* not implemented */
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_session_key_free_body(struct ssh * ssh, int ctos, pid_t pid, uid_t uid)
d3d59d
+{
d3d59d
+	/* not implemented */
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
d3d59d
+{
d3d59d
+	/* not implemented */
d3d59d
+}
d3d59d
 #endif /* BSM */
def1de
diff -up openssh/audit.c.audit openssh/audit.c
def1de
--- openssh/audit.c.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/audit.c	2019-04-03 17:02:20.713886041 +0200
def1de
@@ -34,6 +34,12 @@
bbf61d
 #include "log.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"
b1ec43
+#include "ssherr.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
 
57349a
@@ -109,6 +113,35 @@ audit_event_lookup(ssh_audit_event_t ev)
535d34
 	return(event_lookup[i].name);
535d34
 }
535d34
 
535d34
+void
def1de
+audit_key(struct ssh *ssh, int host_user, int *rv, const struct sshkey *key)
535d34
+{
535d34
+	char *fp;
535d34
+
535d34
+	fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
def1de
+	if (audit_keyusage(ssh, host_user, fp, (*rv == 0)) == 0)
0f4b4c
+		*rv = -SSH_ERR_INTERNAL_ERROR;
535d34
+	free(fp);
535d34
+}
535d34
+
535d34
+void
def1de
+audit_unsupported(struct ssh *ssh, int what)
535d34
+{
def1de
+	PRIVSEP(audit_unsupported_body(ssh, what));
535d34
+}
535d34
+
535d34
+void
def1de
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
535d34
+{
def1de
+	PRIVSEP(audit_kex_body(ssh, ctos, enc, mac, comp, pfs, getpid(), getuid()));
535d34
+}
535d34
+
535d34
+void
def1de
+audit_session_key_free(struct ssh *ssh, int ctos)
535d34
+{
def1de
+	PRIVSEP(audit_session_key_free_body(ssh, ctos, getpid(), getuid()));
535d34
+}
535d34
+
535d34
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
535d34
 /*
535d34
  * Null implementations of audit functions.
def1de
@@ -138,6 +171,17 @@ audit_event(struct ssh *ssh, ssh_audit_e
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
  *
def1de
@@ -172,13 +216,82 @@ 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
def1de
-audit_run_command(const char *command)
535d34
+int
def1de
+audit_run_command(struct ssh *ssh, 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
def1de
+audit_end_command(struct ssh *ssh, 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
def1de
+audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
535d34
+{
57349a
+	debug("audit %s key usage euid %d user %s fingerprint %s, result %d",
57349a
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(),
28ce05
+		fp, rv);
535d34
+}
535d34
+
535d34
+/*
535d34
+ * This will be called when the protocol negotiation fails.
535d34
+ */
535d34
+void
def1de
+audit_unsupported_body(struct ssh *ssh, 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
def1de
+audit_kex_body(struct ssh *ssh, 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
def1de
+audit_session_key_free_body(struct ssh *, 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
def1de
+audit_destroy_sensitive_data(struct ssh *ssh, 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
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
535d34
 #endif /* SSH_AUDIT_EVENTS */
def1de
diff -up openssh/audit.h.audit openssh/audit.h
def1de
--- openssh/audit.h.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/audit.h	2019-04-03 17:02:20.713886041 +0200
6cf9b8
@@ -26,6 +26,7 @@
535d34
 # define _SSH_AUDIT_H
535d34
 
535d34
 #include "loginrec.h"
bbf61d
+#include "sshkey.h"
535d34
 
def1de
 struct ssh;
def1de
 
def1de
@@ -45,13 +46,32 @@ 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);
def1de
 void	audit_event(struct ssh *, 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 *);
def1de
+int	audit_run_command(struct ssh *, const char *);
def1de
+void 	audit_end_command(struct ssh *, int, const char *);
535d34
 ssh_audit_event_t audit_classify_auth(const char *);
def1de
+int	audit_keyusage(struct ssh *, int, char *, int);
def1de
+void	audit_key(struct ssh *, int, int *, const struct sshkey *);
def1de
+void	audit_unsupported(struct ssh *, int);
def1de
+void	audit_kex(struct ssh *, int, char *, char *, char *, char *);
def1de
+void	audit_unsupported_body(struct ssh *, int);
def1de
+void	audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
def1de
+void	audit_session_key_free(struct ssh *, int ctos);
def1de
+void	audit_session_key_free_body(struct ssh *, int ctos, pid_t, uid_t);
def1de
+void	audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
535d34
 
535d34
 #endif /* _SSH_AUDIT_H */
def1de
diff -up openssh/audit-linux.c.audit openssh/audit-linux.c
def1de
--- openssh/audit-linux.c.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/audit-linux.c	2019-04-03 17:02:20.713886041 +0200
f284c5
@@ -33,27 +33,40 @@
d3d59d
 
d3d59d
 #include "log.h"
d3d59d
 #include "audit.h"
bbf61d
+#include "sshkey.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
f284c5
+linux_audit_user_logxxx(int uid, const char *username,
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)",
f284c5
-	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
f284c5
+	    username == NULL ? uid : -1, NULL, ip, ttyn, success);
d3d59d
 	saved_errno = errno;
f284c5
 	close(audit_fd);
f284c5
 
57349a
@@ -65,9 +78,96 @@ 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,
f284c5
+    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)",
f284c5
+	    username == NULL ? uid : -1, NULL, 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
def1de
+audit_keyusage(struct ssh *ssh, int host_user, 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
+	}
9b05c6
+	snprintf(buf, sizeof(buf), "%s_auth grantors=auth-key", host_user ? "pubkey" : "hostbased");
d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
def1de
+		buf, audit_username(), -1, NULL, ssh_remote_ipaddr(ssh), NULL, rv);
d3d59d
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
d3d59d
+		goto out;
57349a
+	snprintf(buf, sizeof(buf), "op=negotiate kind=auth-key fp=%s", fp);
57349a
+	rc = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, buf, NULL,
def1de
+		ssh_remote_ipaddr(ssh), 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
def1de
@@ -76,49 +176,210 @@ audit_connection_from(const char *host,
d3d59d
 	/* not implemented */
b487a6
 }
d3d59d
 
d3d59d
+int
def1de
+audit_run_command(struct ssh *ssh, const char *command)
def1de
+{
28ce05
+	if (!user_login_count++)
f284c5
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1de
+		    ssh_remote_ipaddr(ssh),
f284c5
+		    "ssh", 1, AUDIT_USER_LOGIN);
f284c5
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1de
+	    ssh_remote_ipaddr(ssh),
f284c5
+	    "ssh", 1, AUDIT_USER_START);
d3d59d
+	return 0;
d3d59d
+}
d3d59d
+
def1de
 void
def1de
-audit_run_command(const char *command)
def1de
+audit_end_command(struct ssh *ssh, int handle, const char *command)
def1de
 {
def1de
-	/* not implemented */
f284c5
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1de
+	    ssh_remote_ipaddr(ssh),
f284c5
+	    "ssh", 1, AUDIT_USER_END);
28ce05
+	if (user_login_count && !--user_login_count)
f284c5
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
def1de
+		    ssh_remote_ipaddr(ssh),
f284c5
+		    "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,
f284c5
+		    li->line, 1, AUDIT_USER_LOGIN);
d3d59d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5
+	    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,
f284c5
+	    li->line, 1, AUDIT_USER_END);
28ce05
+	if (user_login_count && !--user_login_count)
d3d59d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
f284c5
+		    li->line, 1, AUDIT_USER_LOGOUT);
d3d59d
 }
d3d59d
 
d3d59d
 void
def1de
 audit_event(struct ssh *ssh, ssh_audit_event_t event)
def1de
 {
d3d59d
 	switch(event) {
667e6f
-	case SSH_AUTH_SUCCESS:
d3d59d
-	case SSH_CONNECTION_CLOSE:
d3d59d
 	case SSH_NOLOGIN:
d3d59d
-	case SSH_LOGIN_EXCEED_MAXTRIES:
d3d59d
 	case SSH_LOGIN_ROOT_DENIED:
f284c5
+		linux_audit_user_auth(-1, audit_username(),
b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, event);
f284c5
+		linux_audit_user_logxxx(-1, audit_username(),
b487a6
+			ssh_remote_ipaddr(ssh), "ssh", 0, AUDIT_USER_LOGIN);
d3d59d
 		break;
6996c6
-	case SSH_AUTH_FAIL_NONE:
d3d59d
 	case SSH_AUTH_FAIL_PASSWD:
6996c6
+		if (options.use_pam)
6996c6
+			break;
6996c6
+	case SSH_LOGIN_EXCEED_MAXTRIES:
d3d59d
 	case SSH_AUTH_FAIL_KBDINT:
d3d59d
 	case SSH_AUTH_FAIL_PUBKEY:
d3d59d
 	case SSH_AUTH_FAIL_HOSTBASED:
d3d59d
 	case SSH_AUTH_FAIL_GSSAPI:
f284c5
+		linux_audit_user_auth(-1, audit_username(),
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,
f284c5
+				    ssh_remote_ipaddr(ssh),
f284c5
+				    "ssh", 1, AUDIT_USER_END);
b487a6
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL,
f284c5
+			    ssh_remote_ipaddr(ssh),
f284c5
+			    "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);
f284c5
+		linux_audit_user_logxxx(-1, audit_username(),
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
def1de
+audit_unsupported_body(struct ssh *ssh, 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 ",
def1de
+		name[what], ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
def1de
+		ssh_local_port(ssh));
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,
def1de
+			buf, NULL, ssh_remote_ipaddr(ssh), NULL, 0);
d3d59d
+	audit_close(audit_fd);
d3d59d
+#endif
d3d59d
+}
d3d59d
+
d3d59d
+const static char *direction[] = { "from-server", "from-client", "both" };
d3d59d
+
d3d59d
+void
def1de
+audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress,
bbf61d
+    char *pfs, pid_t pid, 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,
def1de
+		ssh_remote_port(ssh), (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))), ssh_local_port(ssh));
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,
def1de
+			buf, NULL, ssh_remote_ipaddr(ssh), 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
def1de
+audit_session_key_free_body(struct ssh *ssh, 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,
def1de
+		 ssh_remote_port(ssh),
def1de
+		 (s = get_local_ipaddr(ssh_packet_get_connection_in(ssh))),
def1de
+		 ssh_local_port(ssh));
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,
def1de
+			buf, NULL, ssh_remote_ipaddr(ssh), 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
def1de
+audit_destroy_sensitive_data(struct ssh *ssh, 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,
def1de
+			listening_for_clients() ? NULL : ssh_remote_ipaddr(ssh),
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
+}
535d34
 #endif /* USE_LINUX_AUDIT */
def1de
diff -up openssh/auditstub.c.audit openssh/auditstub.c
def1de
--- openssh/auditstub.c.audit	2019-04-03 17:02:20.714886050 +0200
def1de
+++ openssh/auditstub.c	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -0,0 +1,52 @@
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
+
def1de
+struct ssh;
def1de
+
d3d59d
+void
def1de
+audit_unsupported(struct ssh *ssh, int n)
d3d59d
+{
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_kex(struct ssh *ssh, int ctos, char *enc, char *mac, char *comp, char *pfs)
d3d59d
+{
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_session_key_free(struct ssh *ssh, int ctos)
d3d59d
+{
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
d3d59d
+{
d3d59d
+}
def1de
diff -up openssh/auth2.c.audit openssh/auth2.c
def1de
--- openssh/auth2.c.audit	2019-04-03 17:02:20.651885453 +0200
def1de
+++ openssh/auth2.c	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -303,9 +303,6 @@ input_userauth_request(int type, u_int32
535d34
 		} else {
17b491
 			/* Invalid user, fake password information */
535d34
 			authctxt->pw = fakepw();
414bfa
-#ifdef SSH_AUDIT_EVENTS
def1de
-			PRIVSEP(audit_event(ssh, SSH_INVALID_USER));
535d34
-#endif
535d34
 		}
535d34
 #ifdef USE_PAM
535d34
 		if (options.use_pam)
def1de
diff -up openssh/auth2-hostbased.c.audit openssh/auth2-hostbased.c
def1de
--- openssh/auth2-hostbased.c.audit	2019-04-03 17:02:20.612885083 +0200
def1de
+++ openssh/auth2-hostbased.c	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -158,7 +158,7 @@ userauth_hostbased(struct ssh *ssh)
d3d59d
 	authenticated = 0;
def1de
 	if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
def1de
 	    chost, key)) &&
5b55d0
-	    PRIVSEP(sshkey_verify(key, sig, slen,
def1de
+	    PRIVSEP(hostbased_key_verify(ssh, key, sig, slen,
3cd489
 	    sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
d3d59d
 		authenticated = 1;
5b55d0
 
def1de
@@ -175,6 +175,19 @@ done:
d3d59d
 	return authenticated;
d3d59d
 }
d3d59d
 
d3d59d
+int
def1de
+hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
def1de
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat)
d3d59d
+{
d3d59d
+	int rv;
d3d59d
+
3cd489
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
d3d59d
+#ifdef SSH_AUDIT_EVENTS
def1de
+	audit_key(ssh, 0, &rv, key);
d3d59d
+#endif
d3d59d
+	return rv;
d3d59d
+}
d3d59d
+
d3d59d
 /* return 1 if given hostkey is allowed */
d3d59d
 int
def1de
 hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
def1de
diff -up openssh/auth2-pubkey.c.audit openssh/auth2-pubkey.c
def1de
--- openssh/auth2-pubkey.c.audit	2019-04-03 17:02:20.691885832 +0200
def1de
+++ openssh/auth2-pubkey.c	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -219,7 +219,7 @@ userauth_pubkey(struct ssh *ssh)
d3d59d
 		/* test for correct signature */
d3d59d
 		authenticated = 0;
3cd489
 		if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
bbf61d
-		    PRIVSEP(sshkey_verify(key, sig, slen,
def1de
+		    PRIVSEP(user_key_verify(ssh, key, sig, slen,
bbf61d
 		    sshbuf_ptr(b), sshbuf_len(b),
bbf61d
 		    (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
bbf61d
 		    ssh->compat)) == 0) {
def1de
@@ -278,6 +278,19 @@ done:
5b55d0
 	return authenticated;
d3d59d
 }
d3d59d
 
d3d59d
+int
def1de
+user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig,
def1de
+    size_t slen, const u_char *data, size_t datalen, const char *pkalg, u_int compat)
d3d59d
+{
d3d59d
+	int rv;
d3d59d
+
3cd489
+	rv = sshkey_verify(key, sig, slen, data, datalen, pkalg, compat);
d3d59d
+#ifdef SSH_AUDIT_EVENTS
def1de
+	audit_key(ssh, 1, &rv, key);
d3d59d
+#endif
d3d59d
+	return rv;
d3d59d
+}
d3d59d
+
5b55d0
 static int
5b55d0
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
5b55d0
 {
def1de
diff -up openssh/auth.c.audit openssh/auth.c
def1de
--- openssh/auth.c.audit	2019-04-03 17:02:20.691885832 +0200
def1de
+++ openssh/auth.c	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -366,7 +366,7 @@ auth_log(struct ssh *ssh, int authentica
c45ece
 # endif
c45ece
 #endif
c45ece
 #ifdef SSH_AUDIT_EVENTS
c45ece
-	if (authenticated == 0 && !authctxt->postponed)
c45ece
+	if (authenticated == 0 && !authctxt->postponed && !partial)
def1de
 		audit_event(ssh, audit_classify_auth(method));
c45ece
 #endif
c45ece
 }
def1de
@@ -592,9 +592,6 @@ getpwnamallow(struct ssh *ssh, const cha
def1de
 		record_failed_login(ssh, user,
5878eb
 		    auth_get_canonical_hostname(ssh, options.use_dns), "ssh");
535d34
 #endif
d3d59d
-#ifdef SSH_AUDIT_EVENTS
def1de
-		audit_event(ssh, SSH_INVALID_USER);
535d34
-#endif /* SSH_AUDIT_EVENTS */
535d34
 		return (NULL);
535d34
 	}
def1de
 	if (!allowed_user(ssh, pw))
def1de
diff -up openssh/auth.h.audit openssh/auth.h
def1de
--- openssh/auth.h.audit	2019-04-03 17:02:20.692885842 +0200
def1de
+++ openssh/auth.h	2019-04-03 17:02:20.714886050 +0200
def1de
@@ -195,6 +195,8 @@ struct passwd * getpwnamallow(struct ssh
535d34
 
535d34
 char	*expand_authorized_keys(const char *, struct passwd *pw);
535d34
 char	*authorized_principals_file(struct passwd *);
def1de
+int	 user_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3cd489
+    const u_char *, size_t, const char *, u_int);
535d34
 
535d34
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
535d34
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
def1de
@@ -214,6 +216,8 @@ struct sshkey	*get_hostkey_private_by_ty
5b55d0
 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
def1de
 int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
def1de
     u_char **, size_t *, const u_char *, size_t, const char *);
def1de
+int	 hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3cd489
+    const u_char *, size_t, const char *, u_int);
535d34
 
3cd489
 /* Key / cert options linkage to auth layer */
3cd489
 const struct sshauthopt *auth_options(struct ssh *);
def1de
diff -up openssh/cipher.c.audit openssh/cipher.c
def1de
--- openssh/cipher.c.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/cipher.c	2019-04-03 17:02:20.714886050 +0200
5b55d0
@@ -61,25 +61,6 @@ struct sshcipher_ctx {
6cf9b8
 	const struct sshcipher *cipher;
6cf9b8
 };
d3d59d
 
190035
-struct sshcipher {
9f526c
-	char	*name;
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	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)
5b55d0
-#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
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[] = {
5b55d0
 #ifdef WITH_OPENSSL
bbf61d
 #ifndef OPENSSL_NO_DES
def1de
@@ -410,7 +391,7 @@ cipher_get_length(struct sshcipher_ctx *
217da7
 void
217da7
 cipher_free(struct sshcipher_ctx *cc)
217da7
 {
217da7
-	if (cc == NULL)
217da7
+	if (cc == NULL || cc->cipher == NULL)
217da7
 		return;
217da7
 	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
217da7
 		explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
def1de
diff -up openssh/cipher.h.audit openssh/cipher.h
def1de
--- openssh/cipher.h.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/cipher.h	2019-04-03 17:02:20.714886050 +0200
5b55d0
@@ -45,7 +45,25 @@
190035
 #define CIPHER_ENCRYPT		1
190035
 #define CIPHER_DECRYPT		0
d3d59d
 
190035
-struct sshcipher;
5b55d0
+struct sshcipher {
d3d59d
+	char	*name;
d3d59d
+	u_int	block_size;
d3d59d
+	u_int	key_len;
d3d59d
+	u_int	iv_len;		/* defaults to block_size */
d3d59d
+	u_int	auth_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)
5b55d0
+#define CFLAG_INTERNAL		CFLAG_NONE /* Don't use "none" for packets */
190035
+#ifdef WITH_OPENSSL
d3d59d
+	const EVP_CIPHER	*(*evptype)(void);
190035
+#else
190035
+	void	*ignored;
190035
+#endif
d3d59d
+};
5b55d0
+
6cf9b8
 struct sshcipher_ctx;
6cf9b8
 
5b55d0
 const struct sshcipher *cipher_by_name(const char *);
def1de
diff -up openssh/kex.c.audit openssh/kex.c
def1de
--- openssh/kex.c.audit	2019-04-03 17:02:20.652885462 +0200
def1de
+++ openssh/kex.c	2019-04-03 17:02:20.715886060 +0200
def1de
@@ -60,6 +60,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"
def1de
@@ -758,12 +759,16 @@ kex_start_rekex(struct ssh *ssh)
def1de
 }
def1de
 
def1de
 static int
def1de
-choose_enc(struct sshenc *enc, char *client, char *server)
def1de
+choose_enc(struct ssh *ssh, struct sshenc *enc, char *client, char *server)
d3d59d
 {
d3d59d
 	char *name = match_list(client, server, NULL);
132f8f
 
d3d59d
-	if (name == NULL)
d3d59d
+	if (name == NULL) {
d3d59d
+#ifdef SSH_AUDIT_EVENTS
def1de
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_CIPHER);
d3d59d
+#endif
132f8f
 		return SSH_ERR_NO_CIPHER_ALG_MATCH;
d3d59d
+	}
17b491
 	if ((enc->cipher = cipher_by_name(name)) == NULL) {
17b491
 		free(name);
132f8f
 		return SSH_ERR_INTERNAL_ERROR;
def1de
@@ -783,8 +788,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
def1de
+		audit_unsupported(ssh, SSH_AUDIT_UNSUPPORTED_MAC);
d3d59d
+#endif
132f8f
 		return SSH_ERR_NO_MAC_ALG_MATCH;
d3d59d
+	}
17b491
 	if (mac_setup(mac, name) < 0) {
17b491
 		free(name);
132f8f
 		return SSH_ERR_INTERNAL_ERROR;
def1de
@@ -796,12 +805,16 @@ choose_mac(struct ssh *ssh, struct sshma
def1de
 }
def1de
 
def1de
 static int
def1de
-choose_comp(struct sshcomp *comp, char *client, char *server)
def1de
+choose_comp(struct ssh *ssh, struct sshcomp *comp, char *client, char *server)
d3d59d
 {
d3d59d
 	char *name = match_list(client, server, NULL);
132f8f
 
d3d59d
-	if (name == NULL)
d3d59d
+	if (name == NULL) {
d3d59d
+#ifdef SSH_AUDIT_EVENTS
def1de
+		audit_unsupported(ssh, 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) {
def1de
@@ -933,7 +946,7 @@ kex_choose_conf(struct ssh *ssh)
def1de
 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
def1de
 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
def1de
 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
def1de
-		if ((r = choose_enc(&newkeys->enc, cprop[nenc],
def1de
+		if ((r = choose_enc(ssh, &newkeys->enc, cprop[nenc],
def1de
 		    sprop[nenc])) != 0) {
def1de
 			kex->failed_choice = peer[nenc];
def1de
 			peer[nenc] = NULL;
def1de
@@ -948,7 +961,7 @@ kex_choose_conf(struct ssh *ssh)
def1de
 			peer[nmac] = NULL;
def1de
 			goto out;
def1de
 		}
def1de
-		if ((r = choose_comp(&newkeys->comp, cprop[ncomp],
def1de
+		if ((r = choose_comp(ssh, &newkeys->comp, cprop[ncomp],
def1de
 		    sprop[ncomp])) != 0) {
def1de
 			kex->failed_choice = peer[ncomp];
def1de
 			peer[ncomp] = NULL;
def1de
@@ -971,6 +984,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
def1de
+		audit_kex(ssh, mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
190035
+#endif
190035
 	}
190035
 	/* XXX need runden? */
190035
 	kex->we_need = need;
def1de
@@ -1129,6 +1146,36 @@ dump_digest(const char *msg, const u_cha
d3d59d
 }
d3d59d
 #endif
def1de
 
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
+}
def1de
+
def1de
 /*
def1de
  * Send a plaintext error message to the peer, suffixed by \r\n.
def1de
  * Only used during banner exchange, and there only for the server.
def1de
diff -up openssh/kex.h.audit openssh/kex.h
def1de
--- openssh/kex.h.audit	2019-04-03 17:02:20.652885462 +0200
def1de
+++ openssh/kex.h	2019-04-03 17:02:20.715886060 +0200
def1de
@@ -226,6 +226,8 @@ int	 kexgss_client(struct ssh *);
132f8f
 int	 kexgss_server(struct ssh *);
5160c9
 #endif
d3d59d
 
132f8f
+void	newkeys_destroy(struct newkeys *newkeys);
d3d59d
+
def1de
 int	 kex_dh_keypair(struct kex *);
def1de
 int	 kex_dh_enc(struct kex *, const struct sshbuf *, struct sshbuf **,
def1de
     struct sshbuf **);
def1de
diff -up openssh/mac.c.audit openssh/mac.c
def1de
--- openssh/mac.c.audit	2019-04-03 17:02:20.652885462 +0200
def1de
+++ openssh/mac.c	2019-04-03 17:02:20.715886060 +0200
def1de
@@ -243,6 +243,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
def1de
diff -up openssh/mac.h.audit openssh/mac.h
def1de
--- openssh/mac.h.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/mac.h	2019-04-03 17:02:20.715886060 +0200
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 */
def1de
diff -up openssh/Makefile.in.audit openssh/Makefile.in
def1de
--- openssh/Makefile.in.audit	2019-04-03 17:02:20.705885965 +0200
def1de
+++ openssh/Makefile.in	2019-04-03 17:02:20.715886060 +0200
def1de
@@ -109,7 +109,8 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
def1de
 	kexgexc.o kexgexs.o \
def1de
 	sntrup4591761.o kexsntrup4591761x25519.o kexgen.o \
def1de
 	kexgssc.o \
5b55d0
-	platform-pledge.o platform-tracing.o platform-misc.o
5b55d0
+	platform-pledge.o platform-tracing.o platform-misc.o \
5b55d0
+	auditstub.o
535d34
 
def1de
 
535d34
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
def1de
diff -up openssh/monitor.c.audit openssh/monitor.c
def1de
--- openssh/monitor.c.audit	2019-04-03 17:02:20.674885671 +0200
def1de
+++ openssh/monitor.c	2019-04-03 17:03:17.201421405 +0200
def1de
@@ -93,6 +93,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
 
def1de
@@ -107,6 +108,8 @@ extern u_char session_id[];
bbf61d
 extern struct sshbuf *loginmsg;
3cd489
 extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
d3d59d
 
def1de
+extern void destroy_sensitive_data(struct ssh *, int);
d3d59d
+
d3d59d
 /* State exported from the child */
132f8f
 static struct sshbuf *child_state;
d3d59d
 
def1de
@@ -157,6 +160,11 @@ int mm_answer_gss_updatecreds(struct ssh
d3d59d
 #ifdef SSH_AUDIT_EVENTS
def1de
 int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
def1de
 int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
def1de
+int mm_answer_audit_end_command(struct ssh *, int, struct sshbuf *);
def1de
+int mm_answer_audit_unsupported_body(struct ssh *, int, struct sshbuf *);
def1de
+int mm_answer_audit_kex_body(struct ssh *, int, struct sshbuf *);
def1de
+int mm_answer_audit_session_key_free_body(struct ssh *, int, struct sshbuf *);
def1de
+int mm_answer_audit_server_key_free(struct ssh *, int, struct sshbuf *);
d3d59d
 #endif
d3d59d
 
def1de
 static Authctxt *authctxt;
def1de
@@ -215,6 +223,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},
def1de
@@ -249,6 +261,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
 };
def1de
@@ -1445,8 +1462,10 @@ mm_answer_keyverify(struct ssh *ssh, int
3cd489
 	char *sigalg;
5b55d0
 	size_t signaturelen, datalen, bloblen;
5b55d0
 	int r, ret, valid_data = 0, encoded_ret;
d3d59d
+	int type = 0;
d3d59d
 
bbf61d
-	if ((r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
bbf61d
+	if ((r = sshbuf_get_u32(m, &type)) != 0 ||
bbf61d
+	    (r = sshbuf_get_string(m, &blob, &bloblen)) != 0 ||
5b55d0
 	    (r = sshbuf_get_string(m, &signature, &signaturelen)) != 0 ||
3cd489
 	    (r = sshbuf_get_string(m, &data, &datalen)) != 0 ||
bbf61d
 	    (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
def1de
@@ -1455,6 +1474,8 @@ mm_answer_keyverify(struct ssh *ssh, int
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
 
3cd489
 	/* Empty signature algorithm means NULL. */
3cd489
 	if (*sigalg == '\0') {
def1de
@@ -1470,21 +1491,24 @@ mm_answer_keyverify(struct ssh *ssh, int
5b55d0
 	case MM_USERKEY:
5b55d0
 		valid_data = monitor_valid_userblob(data, datalen);
5b55d0
 		auth_method = "publickey";
def1de
+		ret = user_key_verify(ssh, key, signature, signaturelen, data,
def1de
+		    datalen, sigalg, ssh->compat);
5b55d0
 		break;
5b55d0
 	case MM_HOSTKEY:
5b55d0
 		valid_data = monitor_valid_hostbasedblob(data, datalen,
5b55d0
 		    hostbased_cuser, hostbased_chost);
def1de
+		ret = hostbased_key_verify(ssh, key, signature, signaturelen, data,
def1de
+		    datalen, sigalg, ssh->compat);
5b55d0
 		auth_method = "hostbased";
5b55d0
 		break;
5b55d0
 	default:
5b55d0
 		valid_data = 0;
5b55d0
+		ret = 0;
5b55d0
 		break;
5b55d0
 	}
d3d59d
 	if (!valid_data)
d3d59d
 		fatal("%s: bad signature data blob", __func__);
d3d59d
 
5b55d0
-	ret = sshkey_verify(key, signature, signaturelen, data, datalen,
def1de
-	    sigalg, ssh->compat);
5b55d0
 	debug3("%s: %s %p signature %s", __func__, auth_method, key,
5b55d0
 	    (ret == 0) ? "verified" : "unverified");
5b55d0
 	auth2_record_key(authctxt, ret == 0, key);
def1de
@@ -1536,13 +1560,19 @@ mm_record_login(struct ssh *ssh, Session
def1de
 }
def1de
 
def1de
 static void
def1de
-mm_session_close(Session *s)
def1de
+mm_session_close(struct ssh *ssh, Session *s)
def1de
 {
def1de
 	debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
def1de
 	if (s->ttyfd != -1) {
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);
def1de
+		session_end_command2(ssh, s);
d3d59d
+	}
d3d59d
+#endif
d3d59d
 	session_unused(s->self);
d3d59d
 }
d3d59d
 
def1de
@@ -1609,7 +1639,7 @@ mm_answer_pty(struct ssh *ssh, int sock,
def1de
 
def1de
  error:
def1de
 	if (s != NULL)
def1de
-		mm_session_close(s);
def1de
+		mm_session_close(ssh, s);
def1de
 	if ((r = sshbuf_put_u32(m, 0)) != 0)
def1de
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
def1de
 	mm_request_send(sock, MONITOR_ANS_PTY, m);
def1de
@@ -1628,7 +1658,7 @@ mm_answer_pty_cleanup(struct ssh *ssh, i
def1de
 	if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
def1de
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
def1de
 	if ((s = session_by_tty(tty)) != NULL)
def1de
-		mm_session_close(s);
def1de
+		mm_session_close(ssh, s);
def1de
 	sshbuf_reset(m);
def1de
 	free(tty);
def1de
 	return (0);
def1de
@@ -1650,6 +1680,8 @@ mm_answer_term(struct ssh *ssh, int sock
d3d59d
 		sshpam_cleanup();
d3d59d
 #endif
d3d59d
 
def1de
+	destroy_sensitive_data(ssh, 0);
d3d59d
+
d3d59d
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
d3d59d
 		if (errno != EINTR)
d3d59d
 			exit(1);
def1de
@@ -1696,12 +1728,47 @@ mm_answer_audit_command(struct ssh *ssh,
d3d59d
 {
d3d59d
 	char *cmd;
bbf61d
 	int r;
d3d59d
+	Session *s;
d3d59d
 
d3d59d
 	debug3("%s entering", __func__);
bbf61d
 	if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
bbf61d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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
def1de
+	s->command_handle = audit_run_command(ssh, cmd);
739842
+#endif
d3d59d
+
bbf61d
+	sshbuf_reset(m);
bbf61d
+	sshbuf_put_u32(m, s->self);
d3d59d
+
d3d59d
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
d3d59d
+
d3d59d
+	return (0);
d3d59d
+}
d3d59d
+
d3d59d
+int
def1de
+mm_answer_audit_end_command(struct ssh *ssh, int socket, struct sshbuf *m)
d3d59d
+{
bbf61d
+	int handle, r;
bbf61d
+	size_t len;
bbf61d
+	u_char *cmd = NULL;
d3d59d
+	Session *s;
d3d59d
+
d3d59d
+	debug3("%s entering", __func__);
bbf61d
+	if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
bbf61d
+	    (r = sshbuf_get_string(m, &cmd, &len)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
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__);
def1de
+	mm_session_close(ssh, s);
84822b
 	free(cmd);
d3d59d
 	return (0);
d3d59d
 }
def1de
@@ -1767,6 +1834,7 @@ monitor_apply_keystate(struct ssh *ssh,
132f8f
 void
def1de
 mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
132f8f
 {
bbf61d
+	struct sshbuf *m;
132f8f
 	debug3("%s: Waiting for new keys", __func__);
d3d59d
 
132f8f
 	if ((child_state = sshbuf_new()) == NULL)
def1de
@@ -1774,6 +1842,19 @@ mm_get_keystate(struct ssh *ssh, struct
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
bbf61d
+	m = sshbuf_new();
5b55d0
+	mm_request_receive_expect(pmonitor->m_sendfd,
bbf61d
+				  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
def1de
+	mm_answer_audit_session_key_free_body(ssh, pmonitor->m_sendfd, m);
bbf61d
+	sshbuf_free(m);
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
 
def1de
@@ -2066,3 +2147,102 @@ mm_answer_gss_updatecreds(struct ssh *ss
5160c9
 
5160c9
 #endif /* GSSAPI */
d3d59d
 
d3d59d
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+int
def1de
+mm_answer_audit_unsupported_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59d
+{
bbf61d
+	int what, r;
d3d59d
+
bbf61d
+	if ((r = sshbuf_get_u32(m, &what)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59d
+
def1de
+	audit_unsupported_body(ssh, what);
d3d59d
+
bbf61d
+	sshbuf_reset(m);
d3d59d
+
d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
d3d59d
+	return 0;
d3d59d
+}
d3d59d
+
d3d59d
+int
def1de
+mm_answer_audit_kex_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59d
+{
bbf61d
+	int ctos, r;
190035
+	char *cipher, *mac, *compress, *pfs;
bbf61d
+	u_int64_t tmp;
d3d59d
+	pid_t pid;
d3d59d
+	uid_t uid;
d3d59d
+
bbf61d
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
bbf61d
+	    (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
bbf61d
+	    (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
bbf61d
+	    (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
bbf61d
+	    (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
bbf61d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	pid = (pid_t) tmp;
bbf61d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	uid = (pid_t) tmp;
d3d59d
+
def1de
+	audit_kex_body(ssh, ctos, cipher, mac, compress, pfs, pid, uid);
d3d59d
+
84822b
+	free(cipher);
84822b
+	free(mac);
84822b
+	free(compress);
190035
+	free(pfs);
bbf61d
+	sshbuf_reset(m);
d3d59d
+
d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
d3d59d
+	return 0;
d3d59d
+}
d3d59d
+
d3d59d
+int
def1de
+mm_answer_audit_session_key_free_body(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59d
+{
bbf61d
+	int ctos, r;
bbf61d
+	u_int64_t tmp;
d3d59d
+	pid_t pid;
d3d59d
+	uid_t uid;
d3d59d
+
bbf61d
+	if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
bbf61d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	pid = (pid_t) tmp;
bbf61d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	uid = (uid_t) tmp;
d3d59d
+
def1de
+	audit_session_key_free_body(ssh, ctos, pid, uid);
d3d59d
+
bbf61d
+	sshbuf_reset(m);
d3d59d
+
d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
d3d59d
+	return 0;
d3d59d
+}
d3d59d
+
d3d59d
+int
def1de
+mm_answer_audit_server_key_free(struct ssh *ssh, int sock, struct sshbuf *m)
d3d59d
+{
bbf61d
+	size_t len, r;
d3d59d
+	char *fp;
bbf61d
+	u_int64_t tmp;
d3d59d
+	pid_t pid;
d3d59d
+	uid_t uid;
d3d59d
+
bbf61d
+	if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
bbf61d
+	    (r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	pid = (pid_t) tmp;
bbf61d
+	if ((r = sshbuf_get_u64(m, &tmp)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	uid = (uid_t) tmp;
d3d59d
+
def1de
+	audit_destroy_sensitive_data(ssh, fp, pid, uid);
d3d59d
+
84822b
+	free(fp);
bbf61d
+	sshbuf_reset(m);
d3d59d
+
d3d59d
+	return 0;
d3d59d
+}
d3d59d
+#endif /* SSH_AUDIT_EVENTS */
def1de
diff -up openssh/monitor.h.audit openssh/monitor.h
def1de
--- openssh/monitor.h.audit	2019-04-03 17:02:20.674885671 +0200
def1de
+++ openssh/monitor.h	2019-04-03 17:02:20.715886060 +0200
def1de
@@ -65,7 +65,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,
def1de
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124,
def1de
 
def1de
 	MONITOR_REQ_GSSSIGN = 150, MONITOR_ANS_GSSSIGN = 151,
def1de
 	MONITOR_REQ_GSSUPCREDS = 152, MONITOR_ANS_GSSUPCREDS = 153,
def1de
diff -up openssh/monitor_wrap.c.audit openssh/monitor_wrap.c
def1de
--- openssh/monitor_wrap.c.audit	2019-04-03 17:02:20.653885472 +0200
def1de
+++ openssh/monitor_wrap.c	2019-04-03 17:02:20.716886069 +0200
def1de
@@ -513,7 +513,7 @@ mm_key_allowed(enum mm_keytype type, con
d3d59d
  */
d3d59d
 
d3d59d
 int
5b55d0
-mm_sshkey_verify(const struct sshkey *key, const u_char *sig, size_t siglen,
5b55d0
+mm_sshkey_verify(enum mm_keytype type, const struct sshkey *key, const u_char *sig, size_t siglen,
3cd489
     const u_char *data, size_t datalen, const char *sigalg, u_int compat)
d3d59d
 {
bbf61d
 	struct sshbuf *m;
def1de
@@ -525,7 +525,8 @@ mm_sshkey_verify(const struct sshkey *ke
bbf61d
 
bbf61d
 	if ((m = sshbuf_new()) == NULL)
bbf61d
 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
-	if ((r = sshkey_puts(key, m)) != 0 ||
bbf61d
+	if ((r = sshbuf_put_u32(m, type)) != 0 ||
bbf61d
+	    (r = sshkey_puts(key, m)) != 0 ||
bbf61d
 	    (r = sshbuf_put_string(m, sig, siglen)) != 0 ||
bbf61d
 	    (r = sshbuf_put_string(m, data, datalen)) != 0 ||
bbf61d
 	    (r = sshbuf_put_cstring(m, sigalg == NULL ? "" : sigalg)) != 0)
def1de
@@ -547,6 +548,20 @@ mm_sshkey_verify(const struct sshkey *ke
5b55d0
 	return 0;
d3d59d
 }
d3d59d
 
d3d59d
+int
def1de
+mm_hostbased_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
3cd489
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
d3d59d
+{
3cd489
+	return mm_sshkey_verify(MM_HOSTKEY, key, sig, siglen, data, datalen, pkalg, compat);
d3d59d
+}
d3d59d
+
d3d59d
+int
def1de
+mm_user_key_verify(struct ssh *ssh, const struct sshkey *key, const u_char *sig, size_t siglen,
3cd489
+    const u_char *data, size_t datalen, const char *pkalg, u_int compat)
d3d59d
+{
3cd489
+	return mm_sshkey_verify(MM_USERKEY, key, sig, siglen, data, datalen, pkalg, compat);
d3d59d
+}
d3d59d
+
132f8f
 void
def1de
 mm_send_keystate(struct ssh *ssh, struct monitor *monitor)
132f8f
 {
def1de
@@ -900,11 +915,12 @@ mm_audit_event(struct ssh *ssh, ssh_audi
bbf61d
 	sshbuf_free(m);
d3d59d
 }
d3d59d
 
d3d59d
-void
def1de
-mm_audit_run_command(const char *command)
d3d59d
+int
def1de
+mm_audit_run_command(struct ssh *ssh, const char *command)
d3d59d
 {
bbf61d
 	struct sshbuf *m;
bbf61d
 	int r;
d3d59d
+	int handle;
d3d59d
 
d3d59d
 	debug3("%s entering command %s", __func__, command);
d3d59d
 
def1de
@@ -914,6 +930,30 @@ mm_audit_run_command(const char *command
bbf61d
 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59d
 
bbf61d
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, m);
bbf61d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, m);
d3d59d
+
bbf61d
+	if ((r = sshbuf_get_u32(m, &handle)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+	sshbuf_free(m);
d3d59d
+
d3d59d
+	return (handle);
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+mm_audit_end_command(struct ssh *ssh, int handle, const char *command)
d3d59d
+{
bbf61d
+	int r;
bbf61d
+	struct sshbuf *m;
d3d59d
+
d3d59d
+	debug3("%s entering command %s", __func__, command);
d3d59d
+
bbf61d
+ 	if ((m = sshbuf_new()) == NULL)
bbf61d
+ 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((r = sshbuf_put_u32(m, handle)) != 0 ||
bbf61d
+	    (r = sshbuf_put_cstring(m, command)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59d
+
bbf61d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, m);
bbf61d
 	sshbuf_free(m);
d3d59d
 }
d3d59d
 #endif /* SSH_AUDIT_EVENTS */
def1de
@@ -1074,3 +1114,83 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_cc
bbf61d
 }
def1de
 
5160c9
 #endif /* GSSAPI */
d3d59d
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+void
def1de
+mm_audit_unsupported_body(struct ssh *ssh, int what)
d3d59d
+{
bbf61d
+	int r;
bbf61d
+	struct sshbuf *m;
d3d59d
+
bbf61d
+ 	if ((m = sshbuf_new()) == NULL)
bbf61d
+ 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((r = sshbuf_put_u32(m, what)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
d3d59d
+
bbf61d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, m);
d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
bbf61d
+				  m);
d3d59d
+
bbf61d
+	sshbuf_free(m);
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+mm_audit_kex_body(struct ssh *ssh, int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
d3d59d
+		  uid_t uid)
d3d59d
+{
bbf61d
+	int r;
bbf61d
+	struct sshbuf *m;
bbf61d
+
bbf61d
+ 	if ((m = sshbuf_new()) == NULL)
bbf61d
+ 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
bbf61d
+	    (r = sshbuf_put_cstring(m, cipher)) != 0 ||
bbf61d
+	    (r = sshbuf_put_cstring(m, (mac ? mac : "<implicit>"))) != 0 ||
bbf61d
+	    (r = sshbuf_put_cstring(m, compress)) != 0 ||
bbf61d
+	    (r = sshbuf_put_cstring(m, fps)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+
bbf61d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, m);
d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
bbf61d
+				  m);
d3d59d
+
bbf61d
+	sshbuf_free(m);
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+mm_audit_session_key_free_body(struct ssh *ssh, int ctos, pid_t pid, uid_t uid)
d3d59d
+{
bbf61d
+	int r;
bbf61d
+	struct sshbuf *m;
d3d59d
+
bbf61d
+ 	if ((m = sshbuf_new()) == NULL)
bbf61d
+ 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((r = sshbuf_put_u32(m, ctos)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+
bbf61d
+	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,
bbf61d
+				  m);
bbf61d
+	sshbuf_free(m);
d3d59d
+}
d3d59d
+
d3d59d
+void
def1de
+mm_audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
d3d59d
+{
bbf61d
+	int r;
bbf61d
+	struct sshbuf *m;
bbf61d
+
bbf61d
+ 	if ((m = sshbuf_new()) == NULL)
bbf61d
+ 		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((r = sshbuf_put_cstring(m, fp)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, pid)) != 0 ||
bbf61d
+	    (r = sshbuf_put_u64(m, uid)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(r));
bbf61d
+
bbf61d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, m);
bbf61d
+	sshbuf_free(m);
d3d59d
+}
d3d59d
+#endif /* SSH_AUDIT_EVENTS */
def1de
diff -up openssh/monitor_wrap.h.audit openssh/monitor_wrap.h
def1de
--- openssh/monitor_wrap.h.audit	2019-04-03 17:02:20.653885472 +0200
def1de
+++ openssh/monitor_wrap.h	2019-04-03 17:02:20.716886069 +0200
def1de
@@ -57,7 +57,9 @@ int mm_user_key_allowed(struct ssh *, st
3cd489
     struct sshauthopt **);
def1de
 int mm_hostbased_key_allowed(struct ssh *, struct passwd *, const char *,
5b55d0
     const char *, struct sshkey *);
5b55d0
-int mm_sshkey_verify(const struct sshkey *, const u_char *, size_t,
def1de
+int mm_hostbased_key_verify(struct ssh *, const struct sshkey *, const u_char *, size_t,
3cd489
+    const u_char *, size_t, const char *, u_int);
def1de
+int mm_user_key_verify(struct ssh*, const struct sshkey *, const u_char *, size_t,
3cd489
     const u_char *, size_t, const char *, u_int);
6cf9b8
 
6cf9b8
 #ifdef GSSAPI
def1de
@@ -82,7 +84,12 @@ void mm_sshpam_free_ctx(void *);
d3d59d
 #ifdef SSH_AUDIT_EVENTS
d3d59d
 #include "audit.h"
def1de
 void mm_audit_event(struct ssh *, ssh_audit_event_t);
d3d59d
-void mm_audit_run_command(const char *);
def1de
+int mm_audit_run_command(struct ssh *ssh, const char *);
def1de
+void mm_audit_end_command(struct ssh *ssh, int, const char *);
def1de
+void mm_audit_unsupported_body(struct ssh *, int);
def1de
+void mm_audit_kex_body(struct ssh *, int, char *, char *, char *, char *, pid_t, uid_t);
def1de
+void mm_audit_session_key_free_body(struct ssh *, int, pid_t, uid_t);
def1de
+void mm_audit_destroy_sensitive_data(struct ssh *, const char *, pid_t, uid_t);
d3d59d
 #endif
d3d59d
 
d3d59d
 struct Session;
def1de
diff -up openssh/packet.c.audit openssh/packet.c
def1de
--- openssh/packet.c.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/packet.c	2019-04-03 17:02:20.716886069 +0200
def1de
@@ -77,6 +77,7 @@
bbf61d
 #include <zlib.h>
d3d59d
 
d3d59d
 #include "xmalloc.h"
d3d59d
+#include "audit.h"
d3d59d
 #include "crc32.h"
132f8f
 #include "compat.h"
5b55d0
 #include "ssh2.h"
def1de
@@ -510,6 +511,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.
def1de
@@ -587,22 +595,19 @@ ssh_packet_close_internal(struct ssh *ss
5b55d0
 {
5b55d0
 	struct session_state *state = ssh->state;
5b55d0
 	u_int mode;
5b55d0
+	u_int had_keys = packet_state_has_keys(state);
5b55d0
 
132f8f
 	if (!state->initialized)
d3d59d
 		return;
132f8f
 	state->initialized = 0;
5b55d0
-	if (do_close) {
5b55d0
-		if (state->connection_in == state->connection_out) {
5b55d0
-			close(state->connection_out);
5b55d0
-		} else {
5b55d0
-			close(state->connection_in);
5b55d0
-			close(state->connection_out);
5b55d0
-		}
d3d59d
-	}
132f8f
 	sshbuf_free(state->input);
cdc735
+	state->input = NULL;
132f8f
 	sshbuf_free(state->output);
cdc735
+	state->output = NULL;
132f8f
 	sshbuf_free(state->outgoing_packet);
cdc735
+	state->outgoing_packet = NULL;
def1de
 	sshbuf_free(state->incoming_packet);
cdc735
+	state->incoming_packet = NULL;
cdc735
 	for (mode = 0; mode < MODE_MAX; mode++) {
cdc735
 		kex_free_newkeys(state->newkeys[mode]);	/* current keys */
cdc735
 		state->newkeys[mode] = NULL;
def1de
@@ -636,8 +641,18 @@ ssh_packet_close_internal(struct ssh *ss
d3d59d
 	}
5b55d0
 	cipher_free(state->send_context);
5b55d0
 	cipher_free(state->receive_context);
cdc735
+	if (had_keys && state->server_side) {
cdc735
+		/* Assuming this is called only from privsep child */
def1de
+		audit_session_key_free(ssh, MODE_MAX);
cdc735
+	}
6cf9b8
 	state->send_context = state->receive_context = NULL;
5b55d0
 	if (do_close) {
5b55d0
+		if (state->connection_in == state->connection_out) {
5b55d0
+			close(state->connection_out);
5b55d0
+		} else {
5b55d0
+			close(state->connection_in);
5b55d0
+			close(state->connection_out);
5b55d0
+		}
bbf61d
 		free(ssh->local_ipaddr);
bbf61d
 		ssh->local_ipaddr = NULL;
5b55d0
 		free(ssh->remote_ipaddr);
def1de
@@ -864,6 +879,7 @@ ssh_set_newkeys(struct ssh *ssh, int mod
5b55d0
 		   (unsigned long long)state->p_send.bytes,
5b55d0
 		   (unsigned long long)state->p_send.blocks);
5b55d0
 		kex_free_newkeys(state->newkeys[mode]);
def1de
+		audit_session_key_free(ssh, mode);
def1de
 		state->newkeys[mode] = NULL;
def1de
 	}
def1de
 	/* note that both bytes and the seqnr are not reset */
def1de
@@ -2167,6 +2183,71 @@ 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
+
bbf61d
+	sshbuf_free(state->input);
132f8f
+	state->input = NULL;
bbf61d
+	sshbuf_free(state->output);
132f8f
+	state->output = NULL;
bbf61d
+	sshbuf_free(state->outgoing_packet);
132f8f
+	state->outgoing_packet = NULL;
bbf61d
+	sshbuf_free(state->incoming_packet);
132f8f
+	state->incoming_packet = NULL;
bbf61d
+	if (state->compression_buffer) {
bbf61d
+		sshbuf_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
def1de
+packet_destroy_all(struct ssh *ssh, int audit_it, int privsep)
d3d59d
+{
d3d59d
+	if (audit_it)
def1de
+		audit_it = packet_state_has_keys(ssh->state);
def1de
+	packet_destroy_state(ssh->state);
d3d59d
+	if (audit_it) {
d3d59d
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+		if (privsep)
def1de
+			audit_session_key_free(ssh, MODE_MAX);
d3d59d
+		else
def1de
+			audit_session_key_free_body(ssh, 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)
def1de
diff -up openssh/packet.h.audit openssh/packet.h
def1de
--- openssh/packet.h.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/packet.h	2019-04-03 17:02:20.716886069 +0200
def1de
@@ -217,4 +217,5 @@ const u_char	*sshpkt_ptr(struct ssh *, s
132f8f
 # undef EC_POINT
132f8f
 #endif
d3d59d
 
def1de
+void	 packet_destroy_all(struct ssh *, int, int);
d3d59d
 #endif				/* PACKET_H */
def1de
diff -up openssh/session.c.audit openssh/session.c
def1de
--- openssh/session.c.audit	2019-04-03 17:02:20.712886031 +0200
def1de
+++ openssh/session.c	2019-04-03 17:02:20.716886069 +0200
def1de
@@ -136,7 +136,7 @@ extern char *__progname;
d3d59d
 extern int debug_flag;
d3d59d
 extern u_int utmp_len;
d3d59d
 extern int startup_pipe;
d3d59d
-extern void destroy_sensitive_data(void);
def1de
+extern void destroy_sensitive_data(struct ssh *, int);
bbf61d
 extern struct sshbuf *loginmsg;
3cd489
 extern struct sshauthopt *auth_opts;
def1de
 extern char *tun_fwd_ifnames; /* serverloop.c */
def1de
@@ -648,6 +648,14 @@ do_exec_pty(struct ssh *ssh, Session *s,
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;
def1de
 	ssh_packet_set_interactive(ssh, 1,
def1de
@@ -740,15 +748,19 @@ do_exec(struct ssh *ssh, Session *s, con
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)
def1de
+		s->command_handle = PRIVSEP(audit_run_command(ssh, s->command));
d3d59d
 #endif
d3d59d
 	if (s->ttyfd != -1)
5b55d0
 		ret = do_exec_pty(ssh, s, command);
def1de
@@ -1556,8 +1568,11 @@ do_child(struct ssh *ssh, Session *s, co
def1de
 	sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id));
d3d59d
 
d3d59d
 	/* remove hostkey from the child's memory */
d3d59d
-	destroy_sensitive_data();
def1de
+	destroy_sensitive_data(ssh, 1);
def1de
 	ssh_packet_clear_keys(ssh);
d3d59d
+	/* Don't audit this - both us and the parent would be talking to the
d3d59d
+	   monitor over a single socket, with no synchronization. */
def1de
+	packet_destroy_all(ssh, 0, 1);
d3d59d
 
d3d59d
 	/* Force a password change */
d3d59d
 	if (s->authctxt->force_pwchange) {
def1de
@@ -1769,6 +1784,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
 }
def1de
@@ -1851,6 +1869,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;
def1de
@@ -2461,6 +2492,32 @@ session_exit_message(struct ssh *ssh, Se
5b55d0
 		chan_write_failed(ssh, c);
d3d59d
 }
d3d59d
 
d3d59d
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+void
def1de
+session_end_command2(struct ssh *ssh, Session *s)
d3d59d
+{
acf988
+	if (s->command != NULL) {
f78ec0
+		if (s->command_handle != -1)
def1de
+			audit_end_command(ssh, s->command_handle, s->command);
84822b
+		free(s->command);
d3d59d
+		s->command = NULL;
d3d59d
+		s->command_handle = -1;
d3d59d
+	}
d3d59d
+}
d3d59d
+
d3d59d
+static void
def1de
+session_end_command(struct ssh *ssh, Session *s)
d3d59d
+{
d3d59d
+	if (s->command != NULL) {
f78ec0
+		if (s->command_handle != -1)
def1de
+			PRIVSEP(audit_end_command(ssh, 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
5b55d0
 session_close(struct ssh *ssh, Session *s)
d3d59d
 {
def1de
@@ -2474,6 +2531,10 @@ session_close(struct ssh *ssh, Session *
13073f
 
d3d59d
 	if (s->ttyfd != -1)
d3d59d
 		session_pty_cleanup(s);
d3d59d
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+	if (s->command)
def1de
+		session_end_command(ssh, s);
d3d59d
+#endif
84822b
 	free(s->term);
84822b
 	free(s->display);
84822b
 	free(s->x11_chanids);
def1de
@@ -2549,14 +2610,14 @@ session_close_by_channel(struct ssh *ssh
def1de
 }
def1de
 
def1de
 void
def1de
-session_destroy_all(struct ssh *ssh, void (*closefunc)(Session *))
def1de
+session_destroy_all(struct ssh *ssh, void (*closefunc)(struct ssh *ssh, Session *))
def1de
 {
def1de
 	int i;
def1de
 	for (i = 0; i < sessions_nalloc; i++) {
def1de
 		Session *s = &sessions[i];
def1de
 		if (s->used) {
def1de
 			if (closefunc != NULL)
def1de
-				closefunc(s);
def1de
+				closefunc(ssh, s);
def1de
 			else
def1de
 				session_close(ssh, s);
def1de
 		}
def1de
@@ -2683,6 +2744,15 @@ do_authenticated2(struct ssh *ssh, Authc
5b55d0
 	server_loop2(ssh, authctxt);
d3d59d
 }
d3d59d
 
d3d59d
+static void
def1de
+do_cleanup_one_session(struct ssh *ssh, Session *s)
d3d59d
+{
d3d59d
+	session_pty_cleanup2(s);
d3d59d
+#ifdef SSH_AUDIT_EVENTS
def1de
+	session_end_command2(ssh, s);
d3d59d
+#endif
d3d59d
+}
d3d59d
+
d3d59d
 void
5b55d0
 do_cleanup(struct ssh *ssh, Authctxt *authctxt)
d3d59d
 {
def1de
@@ -2746,7 +2816,7 @@ do_cleanup(struct ssh *ssh, Authctxt *au
d3d59d
 	 * or if running in monitor.
d3d59d
 	 */
d3d59d
 	if (!use_privsep || mm_is_monitor())
5b55d0
-		session_destroy_all(ssh, session_pty_cleanup2);
5b55d0
+		session_destroy_all(ssh, do_cleanup_one_session);
d3d59d
 }
5878eb
 
5878eb
 /* Return a name for the remote host that fits inside utmp_size */
def1de
diff -up openssh/session.h.audit openssh/session.h
def1de
--- openssh/session.h.audit	2019-03-27 23:26:14.000000000 +0100
def1de
+++ openssh/session.h	2019-04-03 17:02:20.717886079 +0200
def1de
@@ -61,6 +61,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
 
5b55d0
 void	 do_authenticated(struct ssh *, Authctxt *);
def1de
@@ -71,10 +77,12 @@ void	 session_unused(int);
def1de
 int	 session_input_channel_req(struct ssh *, Channel *, const char *);
def1de
 void	 session_close_by_pid(struct ssh *ssh, pid_t, int);
5b55d0
 void	 session_close_by_channel(struct ssh *, int, void *);
def1de
-void	 session_destroy_all(struct ssh *, void (*)(Session *));
def1de
+void	 session_destroy_all(struct ssh *, void (*)(struct ssh*, Session *));
d3d59d
 void	 session_pty_cleanup2(Session *);
def1de
+void	 session_end_command2(struct ssh *ssh, Session *);
d3d59d
 
d3d59d
 Session	*session_new(void);
d3d59d
+Session *session_by_id(int);
d3d59d
 Session	*session_by_tty(char *);
5b55d0
 void	 session_close(struct ssh *, Session *);
d3d59d
 void	 do_setusercontext(struct passwd *);
def1de
diff -up openssh/sshd.c.audit openssh/sshd.c
def1de
--- openssh/sshd.c.audit	2019-04-03 17:02:20.692885842 +0200
def1de
+++ openssh/sshd.c	2019-04-03 17:02:20.717886079 +0200
5b55d0
@@ -122,6 +122,7 @@
13073f
 #include "ssh-gss.h"
d3d59d
 #endif
d3d59d
 #include "monitor_wrap.h"
d3d59d
+#include "audit.h"
d3d59d
 #include "ssh-sandbox.h"
3cd489
 #include "auth-options.h"
d3d59d
 #include "version.h"
def1de
@@ -261,8 +262,8 @@ struct sshbuf *loginmsg;
d3d59d
 struct passwd *privsep_pw = NULL;
d3d59d
 
d3d59d
 /* Prototypes for various functions defined later in this file. */
d3d59d
-void destroy_sensitive_data(void);
def1de
-void demote_sensitive_data(void);
def1de
+void destroy_sensitive_data(struct ssh *, int);
def1de
+void demote_sensitive_data(struct ssh *);
def1de
 static void do_ssh2_kex(struct ssh *);
d3d59d
 
def1de
 /*
def1de
@@ -278,6 +279,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
 {
def1de
@@ -380,18 +390,45 @@ grace_alarm_handler(int sig)
def1de
 	    ssh_remote_port(the_active_state));
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)
def1de
+destroy_sensitive_data(struct ssh *ssh, int privsep)
d3d59d
 {
3cd489
 	u_int i;
28ce05
+#ifdef SSH_AUDIT_EVENTS
d3d59d
+	pid_t pid;
d3d59d
+	uid_t uid;
d3d59d
 
28ce05
+	pid = getpid();